Home > Mobile >  Monte Carlo Sim in R plots STRAIGHTS
Monte Carlo Sim in R plots STRAIGHTS

Time:10-21

So I am getting started with Monte Carlo Sims, and went with this basic code to simulate Returns for a given portfolio. Well somehow a portion of the simulated returns always results in straight linear lines which are easy to see on the plotted graph. First I decreased the number of sims so you can see it clearer and I also played around with some other factors but they keep showing up. The rest of the output looks promising and "random". Added the link to the image as my account is new and also the code, appreciate any help!:

library(quantmod)
library(ggplot2)
maxDate<- "2000-01-01"
tickers<-c("MSFT", "AAPL", "BRK-B")
getSymbols(tickers, from=maxDate)
Port.p<-na.omit(merge(Cl(AAPL),Cl(MSFT),Cl(`BRK-B`)))
Port.r<-ROC(Port.p, type = "discrete")[-1,]
stock_Price<- as.matrix(Port.p[,1:3])
stock_Returns <- as.matrix(Port.r[,1:3])

mc_rep = 50 # Number of Sims
training_days = 200 
portfolio_Weights = c(0.5,0.3,0.2)

coVarMat = cov(stock_Returns)
miu = colMeans(stock_Returns)
Miu = matrix(rep(miu, training_days), nrow = 3)
portfolio_Returns_m = matrix(0, training_days, mc_rep)

set.seed(2000)
for (i in 1:mc_rep) {
  Z = matrix ( rnorm( dim(stock_Returns)[2] * training_days ), ncol = training_days )
  L = t( chol(coVarMat) )
  daily_Returns = Miu   L %*% Z  
  portfolio_Returns_200 = cumprod( portfolio_Weights %*% daily_Returns   1 )
  portfolio_Returns_m[,i] = portfolio_Returns_200;
}
x_axis = rep(1:training_days, mc_rep)
y_axis = as.vector(portfolio_Returns_m-1)
plot_data = data.frame(x_axis, y_axis)
ggplot(data = plot_data, aes(x = x_axis, y = y_axis))   geom_path(col = 'red', size = 0.1)  
  xlab('Days')   ylab('Portfolio Returns')   
  ggtitle('Simulated Portfolio Returns in 200 days') 
  theme_bw()  
  theme(plot.title = element_text(hjust = 0.5))

enter image description here

CodePudding user response:

The lines are the 'return' from the end of each series to the beginning of the next. You can keep the lines separate by adding a grouping variable to your plotting data and using the group aesthetic to tell ggplot about it:

g <- rep(1:training_days, each = mc_rep)
plot_data = data.frame(x_axis, y_axis, g)
ggplot(data = plot_data, aes(x = x_axis, y = y_axis, group = g))   ...
  • Related