0

下面的代码可以工作,以便每年评估折扣级别之间的变动。例如,在 1 年内没有提出索赔,上调折扣水平,在下一年提出索赔,降低折扣水平,一年中提出 2 次或更多索赔,降回 0% 的折扣水平,这是根据索赔率 0.1,...,0.8。如果我们现在将时间段更改为两年。例如,如果在第 1 年或第 2 年没有提出索赔,则提高折扣水平,现在如果在第 1 年或第 2 年提出 1 次索赔,则降低折扣水平,或者如果在这两者上提出 2 次或更多次索赔年期间,降至 0% 的折扣水平。如何编辑此代码以更改时间段?

a <- array(0:0,dim=c(21,5000)) # over time period t=21, 5000 policy holders
d<-array(1:5) 
e<-array(1:5) # five discount levels used
p<-array(1:8) # premium charged for 8 separate claim rates
z=0
e[1]=1 # discount 0%
e[2]=.8 # discount 20%
e[3]=.7 # discount 30%
e[4]=.6 # discount 40%
e[5]=.5 # discount 50%

for (l in seq(0.1,0.8,.1)){ # claim rates 0.1,0.2,0.3...0.8
  for (j in 1:20){
    for (i in 1:5000) {
      b<-min(2,rpois(1,l))
      if (b==2) {a[j+1,i]=0}     # b is the number of claims made, if 2 or more, drop down to 0%discount
      if (b==1) {a[j+1,i]=max(0,a[j,i]-1)} # if 1 claim made, drop back one discount level
      if (b==0) {a[j+1,i]=min(4,a[j,i]+1)} # if 0 claims made, go to next level of discount
    }
  }    
  for (k in 1:5){
    d[k]=1000*e[k]*(length(subset(a[21,],a[21,]==(k-1)))/5000)
  }
  z=z+1;p[z]=sum(d)
}
p
4

1 回答 1

1

你真的只需要在内存中保存以前的值并将它添加到你的'if'语句中并翻转你的'i'和'j'循环。这可能看起来像这样:

for (i in 1:5000) {
  b_prev <- 0
  for (j in 1:20){    
    b<-min(2,rpois(1,l))
    if (b + b_prev >=2) {a[j+1,i]=0}     # b is the number of claims made, if 2 or more, drop down to 0%discount
    if (b + b_prev ==1) {a[j+1,i]=max(0,a[j,i]-1)} # if 1 claim made, drop back one discount level
    if (b + b_prev ==0) {a[j+1,i]=min(4,a[j,i]+1)} # if 0 claims made, go to next level of discount
    b_prev <- b
  }
} 

我们在这里所做的是计算单个保单持有人的所有 20 年,而不是计算所有 5,000 名保单持有人的一年。您的数学应该完全相同,因为您使用的是显式引用。但是,循环的这种重新排列允许我们使用滞后变量“b_prev”来保存一年索赔的最后一个值,并在决定如何降低折扣水平时将其添加到当前年份的索赔中。但是请注意,2 不再是您的最大值,因为您可以有两年的 2 次索赔,最多 4 次。我添加了一个>=用于将折扣降回零的计算。

于 2013-01-31T13:05:19.613 回答