Using a for loop to reconstruct exchange rate returns using a factor model formula

12 views Asked by At

I have a dataset (nrow = 10,000 ncol = 29) called random_draws for a factor model. the first 27 columns are exchange rate returns, 28th and 29th columns are the factors observations.

I want to calculate exchange rate returns with the following factor model formula:

FX_returns = mean(i) + std_returns(i) * (rho1 * w1 + sqrt(1 - rho1^2) * w(i))

  • std_returns(i) * (rho2 * w2 + sqrt(1 - rho2^2) * w(i))

w: factor values w(i): idiosyncratic values

The code I have right now is the following:

set.seed(123) # Set a seed for reproducibility
num_draws <- 10000
num_pairs <- 27
num_variables <- num_pairs + 2

for (i in 1:num_pairs) {
    rho1 <- factor1_loadings[i]
    rho2 <- factor2_loadings[i]
    factor1_shocks <- random_draws[, num_pairs + 1]
    factor2_shocks <- random_draws[, num_pairs + 2]
    independent_shocks <- random_draws[, i]
    FX_returns[, i] <- mean_returns[i] + std_returns[i] * (rho1 * factor1_shocks + sqrt(1 - rho1^2) * (independent_shocks)+ std_returns[i] * (rho2 * factor2_shocks + sqrt(1 - rho2^2) * independent_shocks)
}

factor1_loadings is a vector of factor loadings for the first principal component I got from a PCA and factor2_loadings is the same but for the second principal component factor1_shocks are observations from the 28th column in the dataset factor2_shocks are the observations from the 29th column in the dataset independent _shocks are the observations per column from 1-27

I have calculated the mean and standard deviation of each column 1-27 which are the mean and standard deviations for each exchange rate

I want to place the FX returns outputs in a dataset with nrow = 10000 and ncol = 27 (same number of exchange rates)

The error code I keep getting is this one: Error in FX_returns[, i] <- mean_returns[i] + std_returns[i] * (rho1 * : incorrect number of subscripts on matrix

Can anyone help with how to fix this code so that it works?

0

There are 0 answers