This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
# Define parameters
r0 <- 0.05 # initial short rate
n <- 10 # number of tenors
T <- 1 # time horizon
dt <- 0.1 # time step
m <- 1000 # number of simulations
# Generate correlated random numbers
set.seed(123)
corr_matrix <- matrix(c(1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 1, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 1, 0.5, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 1, 0.5, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 0.5, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1, 0.5,
0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 1),
ncol=n, byrow=T)
e <- matrix(rnorm(n*m), ncol=n) %*% chol(corr_matrix)
# Define the LMM function
LIBOR_Market_Model <- function(r0, n, T, dt, e) {
r <- matrix(NA, nrow=m, ncol=n+1)
r[,1] <- r0
for (i in 1:n) {
r[,i+1] <- r[,i] + 0.1 * (0.05 - r[,i]) * dt + 0.1 * e[,i] * sqrt(dt)
}
return(r)
}
# Run the model
r <- LIBOR_Market_Model(r0, n, T, dt, e)
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.