0% found this document useful (0 votes)
7 views

Initializing The Dataframe For Storing Return Forecasts

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Initializing The Dataframe For Storing Return Forecasts

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Initializing the Dataframe for Storing Return Forecasts

AR_10_Return_Forecast <- data.frame(Start.Date =


Test_Return_Ten_df$Start.Date, Forecast = 0)
n <- nrow(AR_10_Return_Forecast)

1. Creating the Dataframe:


o AR_10_Return_Forecast is initialized as a dataframe.
o It has two columns: Start.Date and Forecast.
o Start.Date is filled with the dates from Test_Return_Ten_df$Start.Date.
o Forecast is initialized with zeros.
2. Determining the Number of Rows:
o n <- nrow(AR_10_Return_Forecast) gets the number of rows in the dataframe,
which is the number of test observations we have.

Generating Return Forecasts


for (i in 1:n) {
forecast_value <- Coefficients[1]
for (j in 1:10) {
if (i + 10 - j > n) break
forecast_value <- forecast_value + Coefficients[j + 1] *
Test_Return_New_df[i + 10 - j, 2]
}
AR_10_Return_Forecast[i, 2] <- forecast_value
}

1. Outer Loop:
o for (i in 1:n): This loop iterates over each test observation from 1 to n.
2. Initialize Forecast Value:
o forecast_value <- Coefficients[1]: Start with the intercept (constant term)
from the AR(10) model.
3. Inner Loop:
o for (j in 1:10): This loop iterates over the 10 lags.
4. Boundary Check:
o if (i + 10 - j > n) break: This ensures we do not access out-of-bounds
indices in the test data. If the index i + 10 - j exceeds n, the loop breaks.
5. Update Forecast Value:
o forecast_value <- forecast_value + Coefficients[j + 1] *
Test_Return_New_df[i + 10 - j, 2]: Update the forecast value by adding the
product of the j-th coefficient and the corresponding lagged return. Here's how it
works:
▪ Coefficients[j + 1]: Get the coefficient for the j-th lag (plus 1 because
Coefficients[1] is the intercept).
▪ Test_Return_New_df[i + 10 - j, 2]: Access the return value at the
appropriate lag for the i-th forecast. i + 10 - j calculates the correct
index in the Test_Return_New_df dataframe.
6. Assign Forecast Value:
o AR_10_Return_Forecast[i, 2] <- forecast_value: Assign the calculated
forecast value to the Forecast column of AR_10_Return_Forecast for the i-th
observation.

Explanation of Forecast Calculation

• For each observation i in the test set:


o Start with the intercept from the AR(10) model.
o Add contributions from the last 10 lagged return values, each multiplied by their
respective coefficients from the AR(10) model.
o Ensure the index does not go out of bounds while accessing lagged values.

This process generates the forecasted return for each test observation using the fitted AR(10)
model. Once all forecasts are generated, you can transform these return forecasts into price
forecasts, as described in the next part of the code.

You might also like