onetimeprem=function(q,s,l_r,d) { # q is a vector of mortalities, s the pension, l_r retirement age # and d the dicount #The life table is set up l_e=length(q) kp=lifetab(q,l_e)$kp #The indicator matrix I=matrix(0,l_e+1,l_e+1) I[row(I)+col(I)>l_r+1]=1 #Tabulating the one-time premium ll=0:l_e M=s*(d**ll)*kp*I pi=apply(M,2,sum) list(pi=pi) } lifetab=function(q,K) { #Life table computed K periods ahead for every intial age l_e=length(q) p=c(1-q,rep(0,l_e+1)) kp=matrix(1,K,l_e+1) for(l in 0:l_e+1) kp[,l] = cumprod(p[1:K+(l-1)]) kp=rbind(rep(1,l_e+1),kp) #kp is the life table list(kp=kp) }