FORMULA DE PANJER

pgamsum <- function(x, dfreq, argfreq, shape, rate, Nmax=10){
tol <- 1e-10; maxit <- 10; nbclaim <- 0:Nmax
dnbclaim <- do.call(dfreq, c(list(x=nbclaim), argfreq))
psumfornbclaim <- sapply(nbclaim, function(n)
pgamma(x, shape=shape*n, rate=rate))
psumtot <- psumfornbclaim %*% dnbclaim
dnbclaimtot <- dnbclaim
iter <- 0
while( abs(sum(dnbclaimtot)-1) > tol && iter < maxit){
nbclaim <- nbclaim+Nmax
dnbclaim <- do.call(dfreq, c(list(x=nbclaim), argfreq))
psumfornbclaim <- sapply(nbclaim, function(n)
pgamma(x, shape=shape*n, rate=rate))
psumtot <- psumtot + psumfornbclaim %*% dnbclaim
dnbclaimtot <- c(dnbclaimtot, dnbclaim)
iter <- iter+1}
as.numeric(psumtot)}

GAMMA

pgamsum <- function(x, dfreq, argfreq, shape, rate, Nmax=10){
tol <- 1e-10; maxit <- 10; nbclaim <- 0:Nmax
dnbclaim <- do.call(dfreq, c(list(x=nbclaim), argfreq))
psumfornbclaim <- sapply(nbclaim, function(n)
pgamma(x, shape=shape*n, rate=rate))
psumtot <- psumfornbclaim %*% dnbclaim
dnbclaimtot <- dnbclaim
iter <- 0
while( abs(sum(dnbclaimtot)-1) > tol && iter < maxit){
nbclaim <- nbclaim+Nmax
dnbclaim <- do.call(dfreq, c(list(x=nbclaim), argfreq))
psumfornbclaim <- sapply(nbclaim, function(n)
pgamma(x, shape=shape*n, rate=rate))
psumtot <- psumtot + psumfornbclaim %*% dnbclaim
dnbclaimtot <- c(dnbclaimtot, dnbclaim)
iter <- iter+1}
as.numeric(psumtot)}
I <- 5 # Montos de reclamaciones
J <- 3 # \’Indice m\’aximo para tasas de mortalidad
R <- 20 # Valor m\’aximo para r en g(r) #
n <- array(1:15, dim=c(5,3))
n[1,1]<-1
n[2,1]<-3
n[3,1]<-5
n[4,1]<-2
n[5,1]<-2
n[1,2]<-3
n[2,2]<-5
n[3,2]<-3
n[4,2]<-2
n[5,2]<-3
n[1,3]<-1
n[2,3]<-4
n[3,3]<-4
n[4,3]<-6
n[5,3]<-4
# Probabilidades de reclamación
q <- array(1:3, dim=c(3)) 
q[1]<-0.03
q[2]<-0.04
q[3]<-0.05 
# Función h(i,k) #...............................
h <- function(i,k) {
  aux <- 0
  for (j in 1:J) {
    aux <- aux+n[i,j]*(q[j]/(1-q[j]))^k 
    }
  aux <- i*((-1)^(k-1))*aux 
  return(aux)
}
gc <- array(1:R, dim=c(R))
gc0 <-0.145180
g <- function(r) {
  if (r==0) {
    aux <- 1
    for (i in 1:I) {
      for (j in 1:J) {
        aux <- aux*((1-q[j])^n[i,j]) 
        }
    }
    return(aux)
  }
  else
  {
    aux <- 0
    for (i in 1:min(r,I)) {
      for (k in 1:floor(r/i)) {
        if (r-i*k==0) 
          { 
          aux <- aux + gc0*h(i,k) 
        } 
        else {aux <- aux + gc[r-i*k]*h(i,k)}
      } 
      }
    aux <- aux/r 
    gc[r] <- aux 
    return(aux) 
    }
}
# Asignaci\’on en el arreglo "gc" y graficaci\’on. #...............................
for (i in 1:R) {
  gc[0]<-gc0
  gc[i] <- g(i)
}

barplot(gc,main="Función de densidad de S",xlab="r", ylab="g(r)")

barplot(cumsum(gc),main="Función de densidad de S",xlab="r", ylab="g(r)")

data.frame(q=1:length(gc),d=gc,p=cumsum(gc))
##     q           d          p
## 1   1 0.030278656 0.03027866
## 2   2 0.076789215 0.10706787
## 3   3 0.086757318 0.19382519
## 4   4 0.100411928 0.29423712
## 5   5 0.112962524 0.40719964
## 6   6 0.073737040 0.48093668
## 7   7 0.083489097 0.56442578
## 8   8 0.074232227 0.63865800
## 9   9 0.067083986 0.70574199
## 10 10 0.054313186 0.76005518
## 11 11 0.042558868 0.80261404
## 12 12 0.036879372 0.83949341
## 13 13 0.028950759 0.86844417
## 14 14 0.022678084 0.89112226
## 15 15 0.016817527 0.90793978
## 16 16 0.012760692 0.92070048
## 17 17 0.009607107 0.93030758
## 18 18 0.006947536 0.93725512
## 19 19 0.004976997 0.94223212
## 20 20 0.003493552 0.94572567