# How do I put the Geometric Brownian motion for continuously compounded stock market returns into a Monte Carlo Simulation?

I am writing a seminar paper on portfolio insurance strategies and how each of them performs in a Monte Carlo Simulation under the assumption of Cumulative Prospect Theory (CPT) investors.

For that, I am currently trying to program the simulation in R. The simulation is supposed to simulate 250 daily stock market returns and the continuously compounded stock market returns should be generated using the Geometric Brownian motion. There is also four different stock market scenarios that need to be considered. In the first, the risk premium is 4.5% and the volatility is 20%. In the second, the risk premium is 7% and the volatility is 20%. In the third, the risk premium is 4.5% and the volatility is 30%. In the fourth scenario, the risk premium is 7% and the volatility is 30%. I do not know how to incorporate all of that in the simulation. So my question is, how do I program the Monte Carlo Simulation so that it uses the Geometric Brownian motion to generate continuously compounded stock market returns and takes the different stock market returns into account. The simulation should be run for each stock market scenario, so a comparison of the performance of the portfolio insurance strategies in the different market scenarios is possible. The portfolio insurance strategies are Buy-and-Hold, Constant-Proportion-Portfolio-Insurance, Stop-Loss and Synthetic-Put.

Hello,

so I am not sure how to help you regarding the actual financial solutions (like CPPI etc), but I want to give you a starting point for your stock market simulations. Since there exists an analytical solution for the Geometric Brownian Motion (as you might have already read), you can use that to compute the end value and find everything in between:

# Calculate a GBM
gbm <- function(nsim = 25,t = 1,mu = 0,sigma = 0.25,S0 = 100,n = 250){
# number of time steps between time = 0 and time = t by time step length dt
time <- seq.default(from = 0, to = t, length.out = t*n + 1)
# define the Wiener process between time = 0 and time = t for nsim simulations
W <- matrix(
replicate(nsim, cumsum( c(0, sqrt(1/n) * rnorm( length(time) - 1) ) ) ),
nrow = length(time), ncol = nsim, byrow = FALSE)
# fill the paths via analytical solution
S <- S0 * exp( (mu - 0.5*sigma^2) * time + sigma * W )
S <- ts(S, start = 0, deltat = n)
return(S)
}

matplot(
gbm(nsim = 10, t = 1, mu = 0.07, sigma = 0.3, S0 = 100, n = 250),
type="l",
ylab="",
lty=1:5,
lwd=0.5
)


Created on 2022-11-17 by the reprex package (v2.0.1)

This code produces a GBM with 250 time steps between start and end points, specifying \mu with 0.07 and \sigma with 0.3.

Maybe this simple simulation of GBM in R will already help you to implement your desired financial solutions, to benchmark the solutions with your portfolio insurance strategies.

To make the results perfectly comparable, just integrate a seed argument in the above function.

Kind regards

1 Like

Thank you for your answer! It already helped me a lot.

Can I store the results, that are shown in the graph, as a matrix or data frame? So that I can calculate the returns?

The output of gbm() is a matrix. So it is already stored as a matrix

Oh yeah right But how can I access the data of the output matrix? Like so that I see it in the environment and so that I can refer to it to calculate returns of the stock prices? Like in what matrix (name of it) is it stored?

You have to assign it to an object. If you are not familiar with the basic structures in R, you might be better off starting with some lectures on the programming language R itself (e.g. the excellent R for Data Science (R4DS) book from Hadley Wickham).

To assign the function output to an object, just use the assignment operator <-:

my_obj <- gbm()


Kind regards

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.