在 R 中绘制废墟

发布于 2025-01-19 20:10:48 字数 967 浏览 3 评论 0原文

我正在尝试使用 R 重新创建类似于现代精算风险理论中的图像的内容: https://www .academia.edu/37238799/Modern_Actuarial_Risk_Theory(第89页)

单击此处查看图像

在我的例子中,水滴的大小基于参数为 1/ 的指数分布2000 并且它们以泊松到达时间间隔开,这意味着它们以 0.25 的速率参数呈指数分布(在我的模型中)

U 的值由初始盈余加上每单位时间的保费收入 (c) 给出(对于一个由到达间隔分布确定的时间量)减去索赔金额,索赔金额是从上述指数分布中随机产生的。

我有一种感觉需要使用一个循环,这就是我到目前为止所拥有的:

lambda <- 0.25
EX <- 2000
theta <- 0.5

c <- lambda*EX*(1+theta)

x <- rexp(1, 1/2000)
s <- function(t1){for(t1 in 1:10){v <- c(rep(rexp(t1,1/2000)))
print(sum(v))}}


u <- function(t){10000+c*t}
plot(u, xlab = "t", xlim = c(-1,10), ylim = c(0,20000))
abline(v=0)


for(t1 in 1:10){v <- c(rep(rexp(t1,1/2000))) 
print(sum(v))}

最终目标是在 10 年的时间内运行此模拟 10,000 次,并将其用作可视化表示,作为一个项目的破产率。保险公司。

任何帮助表示赞赏。

I'm trying to recreate something similar to an image in modern actuarial risk theory using R: https://www.academia.edu/37238799/Modern_Actuarial_Risk_Theory (page 89)

Click here for image

In my case, the drops are of size based on an exponential distribution with parameter 1/2000 and they are spaced apart with Poisson inter arrival times which means they are distributed exponentially with a rate parameter of 0.25 (in my model)

The value of U is given by an initial surplus plus a premium income (c) per unit time (for an amount of time determined by the inter arrival distribution) minus a claim amount which would be random from the exponential distribution mentioned above.

I have a feeling a loop will need to be used and this is what I have so far:

lambda <- 0.25
EX <- 2000
theta <- 0.5

c <- lambda*EX*(1+theta)

x <- rexp(1, 1/2000)
s <- function(t1){for(t1 in 1:10){v <- c(rep(rexp(t1,1/2000)))
print(sum(v))}}


u <- function(t){10000+c*t}
plot(u, xlab = "t", xlim = c(-1,10), ylim = c(0,20000))
abline(v=0)


for(t1 in 1:10){v <- c(rep(rexp(t1,1/2000))) 
print(sum(v))}

The end goal is to run this simulation say 10,000 times over a 10 year span and use it as a visible representation as the rate of ruin for an insurance company.

Any help appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像你 2025-01-26 20:10:48

我认为您正在寻找这样的东西,所有这些都包裹在整洁的功能中,默认情况下绘制了剧情,但是如果想要的话,请简单地返回“ ruin”或“安全”,以便您可以在模拟中运行它:

simulate_ruin <- function(lambda = 0.25, EX = 2000,
                          theta  = 0.5, initial_amount = 10000,
                          max_time = 10, draw = TRUE) {

  income_per_year <- lambda * EX * (1 + theta)
  
  # Simulate a Poisson process. Include the initial time 0,
  # and replicate every other time point so we have values "before" and
  # "after" each drop
  times <- c(0, rep(cumsum(rexp(1000, lambda)), each = 2))
  times <- c(times[times < max_time], max_time)
  
  # This would be our income if there were no drops (a straight line)
  total_without_drops <- initial_amount + (income_per_year * times)
    
  # Now simulate some drops. 
  drop_size <- rexp((length(times) - 1) / 2, 1/2000)
  
  # Starting from times[3], we apply our cumulative drops every second index:
  payout_total <- rep(c(0, cumsum(drop_size)), each = 2)
  
  total <- total_without_drops - payout_total
  
  if(draw) {
    plot(times, total, type = "l", ylim = c(-1000, 20000))
    abline(h = 0, lty = 2)
  } else {
    if(any(total < 0)) 
      return("ruin") 
    else 
      return("safe")
  }
}

因此我们可以称呼它一次进行模拟:

simulate_ruin()

“在此处输入图像描述”

,并且再次进行其他模拟

simulate_ruin()

https://i.sstatic.net/uydjb.png“ alt =“在此处输入图像说明”>

和表10,000个模拟的结果以找到毁灭的速率,事实证明约为3%

table(sapply(1:10000, function(x) simulate_ruin(draw = FALSE)))
#> 
#> ruin safe 
#>  305 9695

< sup>由

I think you're looking for something like this, all wrapped up in a neat function which by default draws the plot, but if wanted simply returns "ruin" or "safe" so you can run it in simulation:

simulate_ruin <- function(lambda = 0.25, EX = 2000,
                          theta  = 0.5, initial_amount = 10000,
                          max_time = 10, draw = TRUE) {

  income_per_year <- lambda * EX * (1 + theta)
  
  # Simulate a Poisson process. Include the initial time 0,
  # and replicate every other time point so we have values "before" and
  # "after" each drop
  times <- c(0, rep(cumsum(rexp(1000, lambda)), each = 2))
  times <- c(times[times < max_time], max_time)
  
  # This would be our income if there were no drops (a straight line)
  total_without_drops <- initial_amount + (income_per_year * times)
    
  # Now simulate some drops. 
  drop_size <- rexp((length(times) - 1) / 2, 1/2000)
  
  # Starting from times[3], we apply our cumulative drops every second index:
  payout_total <- rep(c(0, cumsum(drop_size)), each = 2)
  
  total <- total_without_drops - payout_total
  
  if(draw) {
    plot(times, total, type = "l", ylim = c(-1000, 20000))
    abline(h = 0, lty = 2)
  } else {
    if(any(total < 0)) 
      return("ruin") 
    else 
      return("safe")
  }
}

So we can call it once for a simulation:

simulate_ruin()

enter image description here

And again for a different simulation

simulate_ruin()

enter image description here

And table the results of 10,000 simulations to find the rate of ruin, which turns out to be around 3%

table(sapply(1:10000, function(x) simulate_ruin(draw = FALSE)))
#> 
#> ruin safe 
#>  305 9695

Created on 2022-04-06 by the reprex package (v2.0.1)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文