如何在蒙特卡洛模拟中添加均值和标准差线?

Posted

技术标签:

【中文标题】如何在蒙特卡洛模拟中添加均值和标准差线?【英文标题】:How to add mean and sd lines with fill in a Monte Carlo simulation? 【发布时间】:2019-12-16 03:10:12 【问题描述】:

我对养老金计划问题进行了一些蒙特卡罗模拟。我让所有模拟出现在 ggplot 中,但我还想显示均值和 sd,填充 sd 之间的间隙,只是为了美观。

我尝试在其他数据集上获取手段和 sds 和/或使用 stat_summary、geom_ribbon 等函数,但不起作用。

# Parameters
N     <- 22 # Number of Years Simulations
M     <- 100  # Number of Monte Carlo Simulations   
mu    <- 0.0588
sigma <- 0.115
year <- 2019:(N+2019)
price_init <-  6387786 
 #initial price
premium_payout <- c(2944227,    3417635,    3285270,    2799238,    2718016,    2579202,    2674255,    2990758,    3167386,    2773270,    2478602,    2569300,    2611695,    1933911,    1831078,    1459534,    1626927,    1728564,    1268749,    1383170,    1183889,    1095443,    1027536,    893171, 736958, 376525, 327245, 413949, 413974, 214892, 283205, 0,0,0,0,0)
pension_disimbursement<-c(-37190,   -1873850,   -4252838,   -1289580,   -2259324,   -2306284,   -992270,    -1066318,   -4424768,   -3365870,   -2119356,   -2469627,   -6943775,   -807525,    -4210481,   -1569241,   -372050,    -4909025,   -762436,    -1992166,   -2223456,   -1051695,   -2978799,   -2083058,   -2755359,   -1499381,   -0, -0, -2216731,   -0, -2335107,-0,-0,-0,-0,-0)




# Simulate prices
set.seed(123)
monte_carlo_mat <- matrix(nrow = N, ncol = M)
for (j in 1:M) 
  monte_carlo_mat[[1, j]] <- price_init
  for(i in 2:N) 
    monte_carlo_mat[[i, j]] <- (monte_carlo_mat[[i - 1, j]] + pension_disimbursement[i - 1])   * (1 + rnorm(1, mu, sigma)) + premium_payout[i-1]
  

# Format and organize data frame
price_sim <- cbind(year, monte_carlo_mat) %>%
  as_tibble() 
nm <- str_c("Sim.", seq(1, M))
nm <- c("Year", nm)
names(price_sim) <- nm
price_sim <- price_sim %>%
  gather(key = "Simulation", value = "Balance", -(Year))




#plot with all lines of simulations
price_sim %>%
  ggplot(aes(x = Year, y = Balance, Group = Simulation))  + #define axis by simulation group and color them
  geom_line(alpha = 0.1) + #transparency of the line
  ggtitle(str_c("HR Yara:", M, 
                " Monte Carlo Simulations for Balance Over ", N, 
                " Years"))+ #title definition
  theme(axis.text.x = element_text(angle = 60 ,hjust = 1))+ #x axis in 60 degrees agle and out of the graph 
  scale_y_continuous(labels = dollar)+ #money sign
  labs(y = "Balance (EUR)")+ # y axis label
  theme(legend.position = "none", #no legend for the colors of each simulation
        panel.background = element_rect(fill = 'white'),
        plot.title = element_text(color = "darkblue", size = 12, face = "bold.italic"),
        axis.title = element_text(color = "darkblue", size = 10, face= "italic"),
        axis.text.x = element_text(color="black", size = 8),
        axis.text.y = element_text(color="black", size = 7),
        panel.grid.major.y = element_line(color = "gray"), 
        panel.grid.major.x = element_line(color = "transparent")) +
  coord_cartesian(xlim = c(2019, 2040), ylim = c(0, 60000000))

我只想要一个带有平均值和 sd 的相同图表,我已经把我的头烧坏了,不知道还能做什么。

【问题讨论】:

【参考方案1】:

您只需要在美学规范中使用如下填充选项指定它:

ggplot(aes(fill = my_means))

ggplot(aes(fill = my_sds))

【讨论】:

以上是关于如何在蒙特卡洛模拟中添加均值和标准差线?的主要内容,如果未能解决你的问题,请参考以下文章

基于二叉树蒙特卡洛模拟BS方程的期权定价模型

蛋白质结构和网格的蒙特卡罗模拟

蒙特卡洛模拟(Monte Carlo Simulation)浅析

蒙特卡洛与 Python 的集成

如何在 Ocaml 中使用多核进行蒙特卡罗模拟?

蒙特卡洛方法原理