R中的简单模拟:随机模拟均值与截断的正常期望不匹配

时间:2019-02-05 02:10:06

标签: r math simulation probability montecarlo

我正在为学校制定一项财务预测项目。我通过以下方法对假设的保险公司的“股权”进行了随机模拟:

采用期初准备金,增加利息和收入,然后减去年度亏损以获取下一年的权益。重复此步骤,但使用上一年的权益而不是准备金。如果资产净值曾经达到0或更低,它将永远停留在0(破产是一种吸收状态)。

损失按正态分布进行了舍弃,左舍位为@0。有人知道为什么某些模拟的预期路径如此不准确吗?我正在使用this wikipedia page.

中的条件期望公式

我试图为仿真创建一条预期的路径,但是看来我无法获得与仿真的平均值相匹配的路径!到目前为止,我的策略是(我认为)类似于E(X)= E(X | x> 0)* Pr(x> 0)。抱歉,我不认识乳胶。

关于哪里出错的任何想法?

这是我的代码:

# This is just for looking at the exp_path vs. mean path

library(crch)
library(tidyr)
library(ggplot2)
library(matrixStats)

num_sim = 100000
rev = 100
reserve = 10
exp_loss = 100
st_dv_loss = 20
real_return = 0.03
plan_horzn = 30

# Simulated forecast
sim_data = matrix(data = NA, nrow = plan_horzn, ncol = num_sim)
sim_data[1,1:num_sim] = as.vector(rep(reserve, num_sim))

for (i in 2:plan_horzn){
  equity_vec <- sim_data[i-1,]*(1+real_return) + rev - rtnorm(num_sim, mean = exp_loss, sd = st_dv_loss, left = 0, right = Inf)
  equity_vec[equity_vec < 0] <- 0
  equity_vec[sim_data[i-1,]==0] <- 0
  sim_data[i,] <- equity_vec
}

# My expected forecast path
exp_path = vector(length = plan_horzn)
exp_path[1] = reserve

for (i in 2:plan_horzn){
  cash = exp_path[i-1]*(1+real_return) + rev
  prob = ptnorm(cash, mean = exp_loss, sd = st_dv_loss, left = 0)
  cond_loss = exp_loss + st_dv_loss*(dnorm((0 - exp_loss)/st_dv_loss, mean = 0, sd = 1)-dnorm((cash - exp_loss)/st_dv_loss, mean = 0, sd = 1))/(pnorm((cash - exp_loss)/st_dv_loss, mean = 0, sd = 1) - pnorm((0 - exp_loss)/st_dv_loss, mean = exp_loss, sd = st_dv_loss))
  exp_path[i] = (cash - cond_loss)*prob
}

#Plotting a comparison between the mean and expected path
mean <- rowMeans(sim_data)
sim_summary<- data.frame(mean)
sim_summary$med_pc = rowQuantiles(sim_data, probs = .50)
sim_summary$yr <- seq(1,plan_horzn)
sim_summary$expected <- exp_path
sim_summary_adj <- reshape2::melt(sim_summary, id.var = "yr")

g1 <- ggplot(data = sim_summary_adj, aes(x = yr, y = value, colour = variable)) + 
  geom_line() +
  labs( x = "Time (Yrs)",
        y = "Equity ($) ",
        title = "Company Equity Over Time",
        color = "Legend") +
  scale_color_manual(labels = c("Mean", "Median", "Exp"), values = c("blue", "purple", "green"))+
  theme_minimal()

plot(g1)

0 个答案:

没有答案