knitr::opts_chunk$set(echo = TRUE)

Contents:

0 - Install ahead

ahead is released under the BSD Clear license. Here's how to install the R version of the package:

Using ahead:

library(ahead)
library(ahead)
library(fpp)

1 - Get and transform data

data(EuStockMarkets)

EuStocks <- ts(EuStockMarkets[1:100, ], 
               start = start(EuStockMarkets),
               frequency = frequency(EuStockMarkets))

EuStocksLogReturns <- ahead::getreturns(EuStocks, type = "log")

print(head(EuStocksLogReturns))

2 - Risk-neutralize simulations

2 - 1 Yield to maturities (fake risk-free rates)

ym <- c(0.03013425, 0.03026776, 0.03040053, 0.03053258, 0.03066390, 0.03079450, 0.03092437)

freq <- frequency(EuStocksLogReturns)
(start_preds <- tsp(EuStocksLogReturns)[2] + 1 / freq)
(ym <- stats::ts(ym,
                 start = start_preds,
                 frequency = frequency(EuStocksLogReturns)))

2 - 2 Risk-neutralized simulations

obj <- ahead::ridge2f(EuStocksLogReturns, h = 7L,
                      type_pi = 'bootstrap',
                      B = 10L, ym = ym)
rowMeans(obj$neutralized_sims$CAC)
print(ym)
rowMeans(obj$neutralized_sims$DAX)
print(ym)

3 - Visualization

#par(mfrow = c(2, 2))

matplot(EuStocksLogReturns, type = 'l', 
     main = "Historical log-Returns", xlab = "time")

plot(ym, main = "fake spot curve", 
     xlab = "time to maturity",
     ylab = "yield", 
     ylim = c(0.02, 0.04))

matplot(obj$neutralized_sims$DAX, type = 'l', 
     main = "simulations of \n predicted DAX log-returns ('risk-neutral')", 
     ylim = c(0.02, 0.04), 
     ylab = "log-returns")

ci <- apply(obj$neutralized_sims$DAX, 1, function(x) t.test(x)$conf.int)
plot(rowMeans(obj$neutralized_sims$DAX), type = 'l', main = "average predicted \n DAX log-returns ('risk-neutral')", col = "blue", 
     ylim = c(0.02, 0.04), 
     ylab = "log-returns")
lines(ci[1, ], col = "red")
lines(ci[2, ], col = "red")


Techtonique/ahead documentation built on Nov. 24, 2024, 10:33 a.m.