# MultiATSM package - General Guidelines

#### Rubens Moura

#### 2024-04-29

This document aims at providing general guidance on the use of the MultiATSM package available at the CRAN repository.

    library(MultiATSM)

The MultiATSM package provides several model outputs from various single and multi-country affine term structure of interest models (ATSMs). All the frameworks of this package are based on the unspanned economic risk framework from Joslin, Priebsch, and Singleton (2014) (JPS, 2014). In essence, these models assume the absence of arbitrage opportunities, consider a linear state space representation of the yield curve dynamics and offer a tractable approach to simultaneously combine the traditional yield curve factors (spanned factors) along with economic and financial variables (unspanned factors).

Due to the peculiar features of the JPS specification, an efficient estimation of the parameters governing the risk-neutral (\(Q\)-measure) and the physical (\(P\)-measure) probability measures can be carried out rather independently. The only exception is the variance-covariance matrix (sigma) term which is a common element to both the \(P\) and the \(Q\) density functions.

In its current version, the MultiATSM package can generate the outputs of 8 different classes of ATSMs. More specifically, the MultiATSM includes models in which the estimation is done on a country-by-country basis - as in JPS (2014) - or jointly for all the countries of the economic system - as in Jotikasthira, Le, and Lundblad (2015) (JLL, 2015) and Candelon and Moura (Forthcoming) (CM, 2021). In all cases, the risk factor dynamics under the \(P\)-measure follow a VAR(1) model of some sort. The table below summarizes the general features of each one of the models available at this package.

Table 0.1: Model Features
| P-dynamics |  | Q-dynamics |  | Sigma matrix estimation |  | Dominant Country
--- | --- | --- | --- | --- | --- | --- | ---
| Individual | Joint |  | Individual | Joint |  | P only | P and Q |
| Unrestricted | Restricted | Unrestricted | Restricted |
|  |  |  |  | JLL | GVAR |  |  |  |  |  |  |  |  |  |  |
Unrestricted VAR
JPS |  | x |  |  |  |  |  |  | x |  |  |  |  | x |  |  |
JPS jointP |  |  |  | x |  |  |  |  | x |  |  |  |  | x |  |  |
VAR jointQ |  |  |  | x |  |  |  |  |  | x |  |  |  | x |  |  |
Restricted VAR (GVAR)
GVAR sepQ |  |  |  |  |  | x |  |  | x |  |  |  |  | x |  |  |
GVAR jointQ |  |  |  |  |  | x |  |  |  | x |  |  |  | x |  |  |
Restricted VAR (JLL)
JLL original |  |  |  |  | x |  |  |  |  | x |  |  | x |  |  |  | x
JLL NoDomUnit |  |  |  |  | x |  |  |  |  | x |  |  | x |  |  |  |
JLL jointSigma |  |  |  |  | x |  |  |  |  | x |  |  |  | x |  |  | x

Some aspects of the multi-country frameworks are worth highlighting. As for the models based on the setup of JLL (2015), the version “JLL original” follows closely the seminal work of JLL (2015), i.e., it is assumed an economic cohort containing a worldwide dominant economy and a set of smaller countries, in addition to the estimation of the sigma matrix be performed exclusively under the \(P\)-measure. The two other alternative versions assume the absence of a dominant country (“JLL NoDomUnit”) and the estimation of sigma under both the \(P\) and \(Q\) measures (“JLL jointSigma”), as in the standard JPS (2014) model. As for the remaining multi-country ATSMs, it is considered that the dynamics of the risk factors under the \(P\)-measure evolves according to a GVAR model. The version labeled “GVAR jointQ” is the one presented in CM (2021).

In what follows, the focus will be devoted to describe the various pieces that are necessary to implement ATSMs. Specifically, Section 1 describes the data-set available at this package and the set of functions that are useful to retrieve data from Excel files. Section 2 exposes the necessary inputs that have to be specified by the user. In Section 3 the estimation procedure is detailed. Section 4 shows how to use the MultiATSM package to estimate ATSMs from scratch. To ease the exposition, throughout the next sections, the database used by CM (2021) is employed to illustrate the use of the various functions available in this package.

# 1 Data

## 1.1 Package data-set

The MultiATSM package contains the four data-sets used in CM (2021). The first set of data comprises several time series of zero-coupon bond yields from four emerging markets: China, Brazil, Mexico, and Uruguay. It is worth noting that this package requires (i) for estimation purposes, the maturities of the bond yields to be the same for all countries (although the function DataForEstimation handles different maturities across countries as inputs, the outputs generated by this same function are bond yields of the same maturities for all the economies); (ii) bond yields to be expressed in percentage terms (and not in basis points) per annum. The MultiATSM package does not support a routine to bootstrap zero-coupon yields from coupon bonds. This data manipulation procedure must be handled by the user herself, if necessary.

    data('CM_Yields')

The second group of data concerns the time series of the risk factors. Specifically, along the terminology defined in JPS (2014), this data-set contains (i) country-specific spanned factors (denoted by level, slope, and curvature - see Section 3.1 for a more detailed description) and (ii) an array of country-specific and global unspanned factors (namely, some measure of economic growth and inflation). Similarly to the case of the bond yield data, the measures of economic and financial variables must be constructed by the user.

    data('CM_Factors')

The two last blocks of data are necessary for the estimation of the GVAR-based models. The trade flows database presents the sum of the value of all goods imports and exports between any two countries of the sample on a yearly basis since 1948. All the values are free on board and are expressed in U.S. dollars. These data are used to construct the transition matrix of the GVARs models.

    data('CM_Trade')

The GVAR factors database casts country-specific lists of all the factors that are used in the estimation of each country’s VARX(1,1,1). The function titled DatabasePrep provides assistance to structure the data in a similar fashion to this form. Moreover, a specific function for computing the star risk factors is detailed in the Section 3.2.2.

    data('CM_Factors_GVAR')

## 1.2 Import data from Excel files

This package also offers an automatized procedure to extract data from Excel files and to, subsequentially, prepare the risk factor databases that are directly used in the model estimation. The use of the package functions requires that the databases (i) are constructed in separate Excel files for the unspanned factors, the term structure data, and the measures of interdependence (for the GVAR-based models); (ii) contain, in each Excel file, one separate tab per country (in addition to the global variables, in the case of the unspanned factors’ database); and (iii) have identical variable labels across all the tabs within each file. For illustration, see the Excel file available at the package. One example of list of inputs to be provided is, for instance,

    Initial_Date <- "2006-09-01" # Format "yyyy-mm-dd"
    Final_Date <- "2019-01-01" # Format "yyyy-mm-dd"
    DataFrequency <- "Monthly"
    GlobalVar <- c("GBC", "VIX") # Global Variables
    DomVar <- c("Eco_Act", "Inflation", "Com_Prices", "Exc_Rates") #  Domestic variables
    N <-  3 # Number of spanned factors per country
    Economies <- c("China", "Mexico", "Uruguay", "Brazil", "Russia")
    ModelType <- "JPS"

Based on these inputs, one can construct the variable ZZfull which contains the complete set of the model risk factors.

    FactorLabels <- LabFac(N, DomVar, GlobalVar, Economies, ModelType)
    ZZfull <-DataForEstimation(Initial_Date, Final_Date, Economies, N, FactorLabels, ModelType, DataFrequency)

# 2 Required user inputs

## 2.1 Basic inputs used in the model estimation

In order to estimate any ATSM of this package, the user needs to specify several general model inputs, namely:

- Model type: a string-vector containing the label of the model to be estimated (as per described in Table 0.1);
- Model parameters corrected for bias: following the bias correction method described in Bauer, Rudebusch, and Wu (2012) (BRW, 2012), this variable assumes value equal to “1” if the user wishes to implement the framework of BRW (2012) and “0”, otherwise;
- Frequency of the data: a string-vector that specifies the frequency of the time series data. The available options are: “Annually”, “Quarterly”, “Monthly”, “Weekly”, “Daily Business Days”, and “Daily All Days”;
- Economies: a string-vector containing the names of the economies which are part of the economic system;
- Number of spanned factors (N): number of country-specific spanned factors. In this package, N is assumed to be equal across countries;
- Global variables: a string-vector containing the labels of the global unspanned factors used in the model estimation;
- Domestic variables: a string-vector containing the labels of the domestic unspanned factors used in the model estimation;
- Bond yield set of maturities: a numerical-vector containing the maturities of the yields used in the model estimation. The units of this vector must be expressed in years. Alternatively, the user can rely on the Maturities routine to extract the bond maturities from the time series of bond yields (see the documentation of the Maturities function). In this case, the user must specify the unit of time in which the label of the bond yields are expressed, e.g. if the label of the 10-years bond is selected as Y120M (Y10y), then the option Month (Year) must be selected;
- Stationarity constraint under the \(Q\)-dynamics: a binary variable which takes value equal to “1” if the user wishes the largest eigenvalue under the \(Q\)-measure to be strictly smaller than 1. Otherwise, this same variable must be set to “0”;
- Computation of forward premia: this is another binary variable which takes the value equal to “1” if the user wishes the computation of the forward premia or “0”, otherwise. If the this variable is set to “1”, then the user must specify a two-element numerical vector containing the maturties associated with the starting and the ending date of the loan;
- Output label: a single element string-vector which contains the of the name that appears in the file name that stores the model outputs.

One possible example of the basic model inputs is

    ModelType <- "JPS"
    BiasCorrection <- 1 # 1 = estimate model with bias correction; 0 = estimate model without bias correction
    DataFrequency <- "Monthly"
    UnitMatYields <- "Month" # Available options are: "Month" or "Year".
    Economies <- c("China", "Brazil", "Mexico", "Uruguay")
    N <- 3 # number of spanned factors per country
    GlobalVar <- c("GBC", "CPI_OECD") # Global variables
    DomVar <- c("Eco_Act", "Inflation") # Domestic variables
    mat <- c(0.25, 0.5, 1, 3, 5, 10) # vector of maturities
    StationarityUnderQ <- 0 # 1 = set stationarity condition under  the Q; 0 = no stationarity condition
    WishForwardPremia <- 1 # Wish to estimate the forward premia: 1= Yes; 0= No
    FPmatLim <- c(60,120) # maturty of the loan starts in 60 months and ends in 120 months in the future
    OutputLabel <- "Model_demo" # output label

### 2.1.1 GVARinputs and JLLinputs

Some additional inputs are required if the user intends to estimate the GVAR-ATSM or JLL-ATSM related setups. The extra inputs should be stored in separate lists for each model for convenience. To assist the user in executing this task, one can use the ListModelInputs function. Otherwise, the GVARinputs and JLLinputs constructs can be formed manually as specified below.

To GVAR models, the set of supplementary inputs include:

- Economies: a string-vector containing the names of the economies as described above;
- GVAR list of risk factors: a list of risk factors sorted by country in addition to the global variables. See function DatabasePrep;
- VARX type: a string-vector containing the desired estimated form of the VARX(1,1,1). Two possibilities are available. The “unconstrained” form estimates the model without any constrains, using standard OLS regressions for each model equation. The “constrained: Spanned Factors” form prevents foreign-spanned-factors to impact any domestic factor in the feedback matrix, whereas “constrained: ’ followed by the name of the risk factor restricts this same factor to be influenced only by its own lagged values and the lagged values of its own star variables. In the last two cases, the VARX(1,1,1) is estimated by restricted least squares.

    GVARinputs <- list()
    GVARinputs$Economies <- Economies
    GVARinputs$GVARFactors <- FactorsGVAR
    GVARinputs$VARXtype <- "constrained: Inflation"

Furthermore, the estimation of the GVAR requires the specification of the necessary inputs to build the transition matrix (see Section 3.2.2 for more details). One example of this list of inputs is

    t_First <- "2000" # First year of the sample
    t_Last <-  "2015" # Last year of the sample
    W_type <- 'Sample Mean'

Concerning the JLL-ATSM frameworks, the list of inputs includes:

- JLL type: a string-vector containing the label of the JLL model to be estimated (as described in Table 0.1);
- Economies: a string-vector containing the names of the economies as described above;
- Dominant Country: a string-vector containing the name of the economy which is assigned as the dominant country (applicable for the JLL original and JLL jointSigma models) or “None” (applicable for the JLL NoDomUnit).
- Wish the estimation of the sigmas matrices: this is a binary variable which assumes value equal to 1 if the user wishes the estimation of all JLL sigma matrices (i.e. variance-covariance and the Cholesky factorization matrices) and, 0 otherwise. The estimation of the sigma matrices can take several minutes;
- Sigma of the non-orthogonalized variance-covariance matrix: to save time, the user may provide the variance-covariance matrix from the non-orthogonalized dynamics. Otherwise, this input should be set as NULL.

One possible list of JLL inputs is as follows:

      JLLinputs <- list()
      ModelType <- "JLL original"
      JLLinputs$Economies <- Economies
      JLLinputs$DomUnit <- "China"
      JLLinputs$WishSigmas <- 1
      JLLinputs$SigmaNonOrtho <- NULL
      JLLinputs$JLLModelType <- ModelType

### 2.1.2 BRWinputs

In an influential paper, BRW (2012) have shown that the estimates from traditional ATSMs can be severely biased due to the typical small-sample size used in these studies. As result, such models may produce unreasonably stable expected future short-term interest, therefore distorting the term premia estimates for long-maturity bonds. To circumvent this problem, BRW (2012) propose a bootstrap-based method that relies on indirect inference estimation that corrects for the bias present in previous works.

From its version \(0.3.1\), the MultiATSM package accomodates the framework proposed by BRW (2012) to each one of its ATSM types. Some additional inputs must be specified, if the user intends to perform the model estimation along the lines of BRW (2012). They are:

- Mean or median of OLS estimates: the user must decide whether to compute the mean or median of the OLS estimates after each bootstrap iteration. To compute the mean (median), the user must set TRUE (FALSE);
- Number of iteration: total amount of iterations used in the stochatic approximation algorithm after burn-in;
- Number of burn-in iteration: quantity of the iterations to be discarded in the first stage of the bias-correction estimation process. The recommended number is \(15\%\) of the total number of iterations;
- Number of bootstrap samples: quantity of simulated samples used in each burn-in or actual iteration
- Adjustment parameter: this parameter controls the degree of shrinkage between the difference in the OLS estimates and the BRW (2012)’s bootstrap-based one after each iteration. The value of this parameter is fixed and must lie in the interval \(]0;1[\);
- Perform closeness check: flag whether the user wishes to check the root mean square distance between the model estimates produced by the bias-correction method and those generated by OLS. Default is set to TRUE;
- Number of bootstrap samples used in the closeness check: default is set to 100,000;

    flag_mean <- TRUE # TRUE = compute the mean; FALSE = compute the median
    gamma <- 0.2 # Adjustment parameter
    N_iter <- 500 # Number of iteration to be conserved
    N_burn <- N_iter*0.15  # Number of iteration to be discarded
    B <- 50 # Number of bootstrap samples
    checkBRW <- 1 # Closeness check
    B_check <- 1000 # Number of bootstrap samples used in the closeness check

## 2.2 Additional inputs for numerical and graphical outputs

Once the model parameters from the ATSM have been estimated, the MultiATSM package enables the numerical and graphical compilation of the following additional outputs:

- Model fit of the bond yields;
- Impulse response functions (IRFs);
- Forecast error variance decompositions (FEVDs);
- Generalized impulse response functions (GIRFs);
- Generalized forecast error variance decompositions (GFEVDs);
- Bond yields decomposition into their expected component and term premia.

For the IRFs, GIRFs, FEVDs or GFEVDs, a horizon of analysis has to be specified, e.g.:

    Horiz <- 100

For the graphical outputs, the user must indicate the desired types of graphs in a string-based vector:

    DesiredGraphs <- c("Fit", "GIRF", "GFEVD") # Available options are: "Fit", "IRF", "FEVD", "GIRF",
                                              # "GFEVD", "TermPremia".

Moreover, the user must select the types of variables of interest (yields, risk factors or both) and, for the JLL type of models, whether the orthogonalized version should be additionally included

    WishGraphRiskFac <- 0 #   YES: 1; No = 0.
    WishGraphYields <- 1 #    YES: 1; No = 0.
    WishOrthoJLLgraphs <- 0 # YES: 1; No = 0.

The function InputsForOutputs can provide some guidance for customizing the features of the wished outputs. Conditional to these settings, individual folders are created at your temporary directory to store the different types of the desired graphical outputs.

### 2.2.1 Bootstrap settings

If the user intends to generate confidence bands through some bootstrap procedure, an additional list of inputs is required. Specifically:

- methodBS: the desired bootstrap procedure. Available options are (i) standard residual bootstrap (‘bs’); (ii) wild bootstrap (‘wild’), and block bootstrap (‘block’). If the latest is selected, then the block length must be indicated;
- ndraws: the number of bootstrap draws;
- pctg: the confidence level expressed in percentage points.

    Bootlist <- list()
    Bootlist$methodBS <- 'block'
    Bootlist$BlockLength <- 4
    Bootlist$ndraws <-  3
    Bootlist$pctg   <-  95

### 2.2.2 Out-of-sample forecast settings

To perform out-of-sample forecasts of bond yields, the following list-based features have to be detailed:

- ForHoriz: number of periods-ahead that the forecasts are to be generated;
- t0Sample: time-dimension index of the first observations belonging to the information set;
- t0Forecast: time-dimension index of the last observation of the information set used to perform the first forecast set;
- ForType: rolling window forecasting to be done by setting this parameter as Rolling. In such a case, the forecast length used in the forecasts is the one specified as in t0Sample. Should the user wishes the forecasts to be performed on a expanding window basis, then this input should be left empty.

    ForecastList <- list()
    ForecastList$ForHoriz <- 12 # forecast horizon
    ForecastList$t0Sample <- 1 # initial sample date
    ForecastList$t0Forecast <- 70 # last sample date for the first forecast
    ForecastList$ForType <- "Rolling" # forecasting will be performed using rolling windows

# 3 Model Estimation

Having gathered bond yields and other economic time series data, the ATSM estimation is carried out in three steps. Firstly, one needs to provide the number of country-specific spanned factors to be included in the global ATSM. Secondly, it is decided on the form of the risk factor dynamics under the \(P\)-measure. Finally, the user needs to select the general features for the model optimization.

## 3.1 Spanned Factors

The spanned factors are yield-related variables that are used to fit the cross-section dimensions of the term structures. Typically, the spanned factors of country \(i\), \(P_{i,t}\), are computed as the N first principal components (PCs) of the set of observed bond yields. Formally, \(P_{i,t}\) is constructed as \(P_{i,t} = w_i Y_{i,t}\) where \(w_i\) is the PC weight matrix and \(Y_{i,t}\) is a country-specific column-vector of yields with increasing maturities.

For N=3, the spanned factors are traditionally interpreted as level, slope, and curvature. The nature of such interpretability results from the features of the PC weight matrix as illustrated below:

    w <- pca_weights_one_country(Yields, Economy = "Uruguay")

In matrix w, each row stores the weights used for constructing each spanned factor. The entries of the first row are linked to the composition of the level factor in that they load rather equally on all yields. Accordingly, high (low) values of the level factor indicate an overall high (low) value of yields across all maturities. In the second row, the weights monotonically increase with the maturities and, therefore, they capture the degree of steepness (slope) of the term structure. High slope factor values imply a steep yield curve, whereas low ones entail flat (or, possibly, downward) curves. In the third row, the weights of the curvature factor are presented. The name of this factor follows from the fact that the weights have a more pronounced effect on the middle range maturities of the curve. These concepts are graphically illustrated below.

    ## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
    ## ℹ Please use `linewidth` instead.
    ## This warning is displayed once every 8 hours.
    ## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
    ## generated.

Figure 3.1: Yield loadings on the spanned factors

To directly obtain the time series of the country-specific spanned factors, the user can simply use the Spanned_Factors function as follows:

    data('CM_Yields')
    Economies <- c("China", "Brazil", "Mexico", "Uruguay")
    N <- 2
    SpaFact <- Spanned_Factors(Yields, Economies, N)

## 3.2 The P-dynamics estimation

As presented in Table 0.1, the dynamics of the risk factors under the \(P\)-measure evolve according to a VAR(1) model, which may be fully unrestricted (as in the case of the JPS-related models) or somewhat restricted (as in the GVAR and JLL frameworks). Below, the usage of each of these dimensions are illustrated.

### 3.2.1 VAR

Using the VAR function of this package requires simply selecting the appropriated set of risk factors for the desired estimated model. For instance, for the models JPS jointP and VAR jointQ, the estimation of the \(P\)-dynamics parameters is obtained as

    data("CM_Factors")
    PdynPara <- VAR(RiskFactors, VARtype= "unconstrained")

whereas the estimation of a JPS model for China is

    FactorsChina <- RiskFactors[1:7,]
    PdynPara <- VAR(FactorsChina, VARtype= "unconstrained")

In both cases, the outputs generated are the vector of intercepts and both the feedback and variance-covariance matrices.

### 3.2.2 GVAR

The estimation of a GVAR model requires defining a measure of interdependence among the countries of the economic system. This information is reported in the transition matrix, the entries of which reflect the degree of interconnection of two entities of this same economic system. In this package, the illustration of the transition matrix is based on the average of the cross-border trade flow weights for the period spanning the years from 2006 to 2019. Note that each row sums up to 1.

    data("CM_Trade")
    t_First <- "2006"
    t_Last <-  "2019"
    Economies <- c("China", "Brazil", "Mexico", "Uruguay")
    type <- "Sample Mean"
    W_gvar <- Transition_Matrix(t_First, t_Last, Economies, type, DataPath = NULL, TradeFlows)
    print(W_gvar)

    ##             China    Brazil     Mexico     Uruguay
    ## China   0.0000000 0.6549066 0.31550095 0.029592464
    ## Brazil  0.8268799 0.0000000 0.12340661 0.049713473
    ## Mexico  0.8596078 0.1326284 0.00000000 0.007763753
    ## Uruguay 0.3811195 0.5497525 0.06912795 0.000000000

Starting from package version \(0.3.3\), the GVAR setups can be estimated incorporating time-varying interdependence weights. This entails that the star factors are constructed by applying trade flow weights from the specific year to adjust the risk factors of the corresponding year. To employ this feature, users need to specify the transition matrix type as Full Sample and select the same specific date (year) for both the initial and final years within the transition matrix. This implies that the trade weights of this year are used for solving (link matrices) the GVAR system. Taking reference from the variable names in the previous example, an illustration could be set as follows:

    t_First <- "2019"
    t_Last <-  "2019"
    type <- "Full Sample"

Having defined the form of the transition matrix, one can complete the GVARinputs variable along the lines discussed in Section 2.1.1.

    data("CM_Factors_GVAR")

    GVARinputs <- list()
    GVARinputs$Economies <- Economies
    GVARinputs$GVARFactors <- FactorsGVAR
    GVARinputs$VARXtype <- "unconstrained"
    GVARinputs$Wgvar <- W_gvar

    N <- 3

    GVARpara <- GVAR(GVARinputs, N)

A separate routine is provided for computing the foreign-specific factors (also commonly referred to as the star variables) used in the estimation of the VARX models.

    data('CM_Factors')
    StaFac <- StarFactors(RiskFactors, Economies, W_gvar)

### 3.2.3 JLL

Calculating the \(P\)-dynamics parameters in the form proposed by JLL (2015) requires the following inputs: (i) the time series of the risk factors in non-orthogonalized form; (ii) the number of country-specific spanned factors and (iii) the specification of the JLLinputs as presented in Section 2.1.1. See, for instance:

    data("CM_Factors")
    N <- 3
    JLLinputs <- list()
    ModelType <- "JLL original"
    JLLinputs$Economies <- Economies
    JLLinputs$DomUnit <- "China"
    JLLinputs$WishSigmas <- 1
    JLLinputs$SigmaNonOrtho <- NULL
    JLLinputs$JLLModelType <- ModelType
    JLLpara <- JLL(RiskFactors, N, JLLinputs)

## 3.3 ATSM estimation

The estimation of ATSM involves defining the model inputs from the log-likelihood function and, subsequently, the parameters used in its optimization process. The structure proposed in this part of the code is in a great extent based on the term structure package by Le and Singleton (2018).

### 3.3.1 The log-likelihood function

For the sake of simplicity, one illustrates the construction of a log-likelihood function based on a VAR jointQ model. For both the JLL and GVAR-based models, one must further specify the JLLinputs or GVARinputs along the lines described in the Section 2.1.1.

    # Inputs to be specified by the user
    data("CM_Yields")
    data("CM_Factors")
    ModelType <- "VAR jointQ"
    Yields <- Yields
    ZZ <- RiskFactors
    Economies <- c("China", "Brazil", "Mexico", "Uruguay")
    mat <-  Maturities(Yields, Economies, UnitYields = "Month")
    DataFrequency <- "Monthly"
    GlobalVar <- c("GBC", "CPI_OECD") # Global variables
    DomVar <- c("Eco_Act", "Inflation") # Domestic variables
    N <- 3 # Number of country-specific spanned factors

    # Generate the "Factor Labels" list (necessary preliminarily step)
    FactorLabels <- LabFac(N, DomVar, GlobalVar, Economies, ModelType)

    # Prepare the inputs for log-Likelihood function
    ATSMInputs <- InputsForMLEdensity(ModelType, Yields, ZZ, FactorLabels, mat, Economies, DataFrequency)

    # Log-Likelihood function
    f <- Functionf(ATSMInputs, Economies, mat, DataFrequency, FactorLabels, ModelType)

### 3.3.2 Optimization parameters

JPS (2014) requires the estimation of a set of parameters containing 6 elements, namely: the risk-neutral long-run mean of the short rate (r0), the risk-neutral feedback matrix (K1XQ), the variance-covariance matrix (SSZ) from the VAR processes, the standard deviation of the errors from the portfolios of yields observed with error (se), in addition to the intercept (K0Z) and the feedback (K1Z) matrices of the physical dynamics. Each of these parameters must be cast in an individual list that should contain the (i) the starting value of the parameter (if any); (ii) the variable label followed by a ‘:’ and a type of constraint; (iii) a lower bound (if any), and (iv) an upper bound (if any). The variable labels of r0, se, K0Z and K1Z should be preceded by the symbol @ as a manner to account for the fact that the solution of these parameters are known in closed-form. For the remaining ones (K1XQ and SSZ), Le and Singleton (2018) provide standardized routines (already incorporated in this package) to set good initial values so that the optimization process runs fast.

The type of constraint to be set for the parameters r0, se, K0Z and K1Z is typically bounded and are used for bounded matrices. For K1XQ and SSZ, the MultiATSM package currently provides the following options:

- K1XQ: ‘Jordan’ (or Jordan MultiCountry’) for a matrix of Jordan type for single (multi) country specifications. These labels can be extended by ‘; stationary’, if one wishes to impose the largest eigenvalue of the risk-neutral feedback matrix to be strictly smaller than 1;
- SSZ: ‘psd’ for a positive semi-definite matrix; ‘diag’ for a diagonal matrix; ‘BlockDiag’ for a block diagonal matrix (typical from the GVAR-based models) and ‘JLLstructure’ for the models containing the restrictions along the lines of JLL (2015).

One example for the list of parameters specification is

    K1XQinputs <- list(NULL, "K1XQ: Jordan" , NULL , NULL)
    SSZinputs <- list(NULL, "SSZ: psd", NULL, NULL)
    r0inputs <- list(NULL, "@r0: bounded", NULL, NULL)
    seinputs <- list(NULL, "@se: bounded", 1e-6, NULL)
    K0Zinputs<- list(NULL, "@K0Z: bounded", NULL, NULL)
    K1Zinputs<- list(NULL, "@K1Z: bounded", NULL, NULL)

To complete the optimization setting, the user needs to specify the level of convergence tolerance (usually, 1e-4 is a reasonable value) and the vector OptRun. The first element of this vector must be filled by the word ‘iter off’, if the user wishes to switch off the printouts of the numerical optimization routines, or simply by ‘iter’ otherwise. The second element of the vector concerns the algorithm that is used in the optimization: the option ‘fminunc only’ only uses fminunc, whereas ‘fminsearch only’ only applies fminsearch. If OptRun is formed exclusively by ‘iter off’, then both fminunc and fminsearch are employed in the optimization.

# 4 Examples of full implementation of ATSMs

This section presents some examples on how to use the MultiATSM package to fully implement ATSMs. Further on, the functions of this package are used to replicate some of the results presented in the original papers of JPS (2014) and CM (2021). See the Paper Replication Vignette of this package for a detailed reproduction.

## 4.1 General template

    ########################################################################################################
    #################################### USER INPUTS #######################################################
    ########################################################################################################
    # A) Load database data
    data("CM_Factors")
    data('CM_Factors_GVAR')
    data('CM_Trade')
    data('CM_Yields')

    # B) Decide on general model inputs
    ModelType <- "VAR jointQ" # available options are "JPS", "JPS jointP", "GVAR sepQ", "VAR jointQ",
                              #"GVAR jointQ", "JLL original", "JLL NoDomUnit", "JLL jointSigma".

    StationarityUnderQ <- 0 # Wish to impose stationary condition for the eigenvalues of each country:
                             #YES: 1,NO:0
    BiasCorrection <- 1 # Wish to estimate the model with the bias correction method of BRW (2012):
                        #YES: 1, NO:0

    WishForwardPremia <- 1 # Wish to estimate the forward premia: YES: 1, NO:0
    FPmatLim <- c(60,120) #  If the forward premia is desired, then choose the Min and max maturities of the
                            # forward premia. Otherwise set NA

    Economies <- c("China", "Brazil", "Mexico", "Uruguay") # Names of the economies from the economic system
    GlobalVar <- c("GBC", "CPI_OECD") # Global Variables
    DomVar <- c("Eco_Act", "Inflation") # Country-specific variables

    N <- 3 # Number of spanned factors per country

    OutputLabel <- "Test" # label of the model for saving the file
    DataFrequency <- "Monthly" # Frequency of the data
    UnitMatYields <- "Month" # bond yields time-unit. Available options are "Month" or "Year"

    # B.1) Decide on specific model inputs
    #################################### GVAR-based models ##################################################
    t_First_Wgvar <- "2004" # First year of the sample (transition matrix)
    t_Last_Wgvar <-  "2019" # Last year of the sample (transition matrix)
    W_type <- 'Sample Mean' # Method to compute the transition matrix
    VARXtype <- "unconstrained" # (i) "unconstrained" or (ii) ""constrained" (VARX)
    #################################### JLL-based models ###################################################
    DomUnit <- "China"
    WishSigmas <- 1 # Sigma matrix is estimated within the "InputsForMLEdensity" function
    SigmaNonOrtho <- NULL
    JLLModelType <- ModelType
    ###################################### BRW inputs  ######################################################
    flag_mean <- TRUE # TRUE = compute the mean; FALSE = compute the median
    gamma <- 0.2 # Adjustment parameter
    N_iter <- 500 # Number of iteration to be conserved
    N_burn <- N_iter*0.15  # Number of iteration to be discarded
    B <- 50 # Number of bootstrap samples
    checkBRW <- 1
    B_check <- 1000 #
    #########################################################################################################

    # C) Decide on Settings for numerical outputs
    Horiz <- 50
    DesiredGraphs <- c("Fit", "IRF", "FEVD") # "Fit", "IRF", "FEVD", "GIRF", "GFEVD", "TermPremia"
    WishGraphRiskFac <- 0
    WishGraphYields <- 1
    WishOrthoJLLgraphs <- 0

    # D) Bootstrao settings
    WishBootstrap <- 1 #  YES: 1; No = 0.
    Bootlist <- list()
    Bootlist$methodBS <- 'bs' # (i) 'bs' ; (ii) 'wild'; (iii) 'block'
    Bootlist$BlockLength <- 4 # necessary input if one chooses the block bootstrap
    Bootlist$ndraws <-  5
    Bootlist$pctg   <-  95 # confidence level

    # E) Out-of-sample forecast
    WishForecast <- 1 #  YES: 1; No = 0.
    ForecastList <- list()
    ForecastList$ForHoriz <- 12 # forecast horizon
    ForecastList$t0Sample <- 1 # initial sample date
    ForecastList$t0Forecast <- 145 # last sample date for the first forecast
    ForecastList$ForType <- c()

    #########################################################################################################
    ############################### NO NEED TO MAKE CHANGES FROM HERE #######################################
    #########################################################################################################

    # 2) Minor preliminary work
    C <- length(Economies)
    FactorLabels <- LabFac(N, DomVar,GlobalVar, Economies, ModelType) # Generate the set of labels
    ZZ <- RiskFactors
    if(any(ModelType == c("GVAR sepQ", "GVAR jointQ"))){
    Data <- list()
    Data$GVARFactors <- FactorsGVAR}
    mat <- Maturities(Yields, Economies, UnitYields = UnitMatYields)

    # 2.1) Generate GVARinputs, JLLinputs and BRWinputs
    ModInputs <- ListModelInputs(ModelType, Data, Economies, VARXtype, t_First_Wgvar, t_Last_Wgvar, W_type,
                                 DomUnit, WishSigmas, SigmaNonOrtho, BiasCorrection, flag_mean, gamma, N_iter,
                                 N_burn, B, checkBRW, B_check)

    GVARinputs <- ModInputs$GVARinputs
    JLLinputs <- ModInputs$JLLinputs
    BRWinputs <- ModInputs$BRWinputs

    # 3) Prepare the inputs of the likelihood function
    ModelParaList <- list()
    for (i in 1:C){
      if (( any(ModelType ==c("GVAR jointQ", "VAR jointQ","JLL original", "JLL NoDomUnit","JLL jointSigma")))
          & i >1 ){break}

      # 3.1) Compute the inputs that go directly into the log-likelihood function
      ATSMInputs <- InputsForMLEdensity(ModelType, Yields, ZZ, FactorLabels, mat, Economies, DataFrequency,
                                        JLLinputs, GVARinputs, BRWinputs)

      # 3.2) Initial guesses for Variables that will be concentrared out of from the log-likelihood function
      K1XQ <- ATSMInputs$K1XQ
      if (any(ModelType == c("JLL original", "JLL NoDomUnit"))){ SSZ <- NULL} else{SSZ <- ATSMInputs$SSZ}

      # 4) Build the objective function
      f <- Functionf(ATSMInputs, Economies, mat, DataFrequency, FactorLabels, ModelType)

      # 5) Set the optimization settings
      VarLab <- ParaLabels(ModelType, StationarityUnderQ)

      varargin <- list()
      varargin$K1XQ <-list(K1XQ, VarLab[[ModelType]][["K1XQ"]] , NULL , NULL)
      varargin$SSZ <- list(SSZ, VarLab[[ModelType]][["SSZ"]], NULL, NULL)
      varargin$r0 <- list(NULL, VarLab[[ModelType]][["r0"]], NULL, NULL)
      varargin$se <- list(NULL, VarLab[[ModelType]][["se"]], 1e-6, NULL)
      varargin$K0Z <- list(NULL, VarLab[[ModelType]][["K0Z"]], NULL, NULL)
      varargin$K1Z <- list(NULL, VarLab[[ModelType]][["K1Z"]], NULL, NULL)
      varargin$OptRun <- c("iter off")

      LabelVar<- c('Value', 'Label', 'LB', 'UB') # Elements of each parameter
      for (d in 1:(length(varargin)-1)){ names(varargin[[d]]) <-  LabelVar}

      tol <- 1e-4

      # 6) Optimization of the model
      if (ModelType == 'JPS' || ModelType == 'JPS jointP' || ModelType == "GVAR sepQ"){
    ModelParaList[[ModelType]][[Economies[i]]] <- Optimization(f, tol, varargin, FactorLabels,
                                                               Economies, ModelType)$Summary
      }else{
    ModelParaList[[ModelType]] <- Optimization(f, tol, varargin, FactorLabels, Economies, ModelType,
                                               JLLinputs, GVARinputs)$Summary}
    }

    # 7) Numerical and graphical outputs
    InputsForOutputs <- InputsForOutputs(ModelType, Horiz, DesiredGraphs, OutputLabel, StationarityUnderQ,
                                         UnitMatYields, WishGraphYields, WishGraphRiskFac, WishOrthoJLLgraphs,
                                         WishForwardPremia, FPmatLim, WishBootstrap, Bootlist, WishForecast,
                                         ForecastList)
    # A) Fit, IRF, FEVD, GIRF, GFEVD, and Term Premia
    NumericalOutputs <- NumOutputs(ModelType, ModelParaList, InputsForOutputs, FactorLabels, Economies)

    # B) Bootstrap
    Bootstrap <- Bootstrap(ModelType, ModelParaList, NumericalOutputs, mat, Economies, InputsForOutputs,
                           FactorLabels, DataFrequency, varargin, JLLinputs, GVARinputs, BRWinputs)

    # C) Out-of-sample forecasting
    Forecasts <- ForecastYields(ModelType, ModelParaList, InputsForOutputs, FactorLabels, Economies,
                                DataFrequency, JLLinputs, GVARinputs, BRWinputs)

# References

Bauer, Michael D, Glenn D Rudebusch, and Jing Cynthia Wu. 2012. “Correcting Estimation Bias in Dynamic Term Structure Models.” Journal of Business & Economic Statistics 30 (3): 454–67.
Candelon, Bertrand, and Rubens Moura. Forthcoming. “A Multicountry Model of the Term Structures of Interest Rates with a GVAR.” Journal of Financial Econometrics, Forthcoming.
Joslin, Scott, Marcel Priebsch, and Kenneth J. Singleton. 2014. “Risk Premiums in Dynamic Term Structure Models with Unspanned Macro Risks.” Journal of Finance 69 (3): 1197–1233.
Jotikasthira, Chotibhak, Anh Le, and Christian Lundblad. 2015. “Why Do Term Structures in Different Currencies Co-Move?” Journal of Financial Economics 115: 58–83.
Le, Anh, and Ken Singleton. 2018. “A Small Package of Matlab Routines for the Estimation of Some Term Structure Models.” Euro Area Business Cycle Network Training School - Term Structure Modelling.