Skip to content

quxo/bayesian-credibility-glms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Bayesian Credibility

This page contains the R code used for generating the applied example of the article Bayesian Credibility. The published version can be found in this link and a preprint here It is first necessary to install the package rstan. If you have not installed it run first the command install.packages("rstan") before running what follows.


1. Downloading and preparing the data

The following code downloads the data and modifies the variables as indicated in the article. Note that the code above downloads the file car.csv to your working directory.

download.file("http://www.businessandeconomics.mq.edu.au/our_departments/Applied_Finance_and_Actuarial_Studies/acst_docs/glms_for_insurance_data/data/car.csv",destfile="car.csv")

cars.data <- read.csv("car.csv",header=TRUE)
cars.data$veh_body <- factor(cars.data$veh_body)
cars.data$veh_age <- factor(cars.data$veh_age)
cars.data$gender <- factor(cars.data$gender)
cars.data$area <- factor(cars.data$area)
cars.data$agecat <- factor(cars.data$agecat)

cars.data$veh_value <- cut(cars.data$veh_value,breaks=c(0,1.2,1.86,Inf),labels=c("P1","P2","P3"),include.lowest=T)

 ## Put together area A,B,C,D
cars.data$area <- factor(cars.data$area,levels=c(levels(cars.data$area),"ABCD") )
cars.data$area[cars.data$area %in% c("A","B","C","D")] <- "ABCD"
cars.data$area <- factor(cars.data$area)
cars.data$area <- relevel(cars.data$area,"ABCD")

2. Using Stan

In order to use Stan we first load the library and detect the cores of the computer.

library(rstan)

rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())

Now we create a string with the stan code of the model and store in a variable.

stan.severity.code="
data {
  int M; // Number of rows of the design matrix
  int P; // Number of columns of the design matrix
  matrix[M,P] X; // Design matrix
  vector[M] y; // response variable
  vector[M] ws; // durations
}

parameters {
  vector[P] beta;
  real phi;
}

transformed parameters{
  vector[M] mu;
  mu = exp(X * beta);
}

model{
  beta ~ uniform(-20,20);
  phi ~ uniform(0,1000);
  for (n in 1:M){
     y[n] ~ gamma(ws[n]/phi,ws[n]/(phi*mu[n]));
 }
}"

Now we aggregate the data, get the corresponding design matrix create a list with the data for Stan, and fit the stan model.

agg.data <- aggregate(cbind(claimcst0,numclaims) ~  agecat + gender + area + veh_value,
                        subset=clm==1,
                        data=cars.data,
                        FUN=sum)

X <- model.matrix( ~ agecat + gender + area + veh_value,
                  data=agg.data)


model.data <- list(M=dim(X)[1],
                       P=dim(X)[2],
                       y=agg.data$claimcst0/agg.data$numclaims,
                       X=X,
                       ws=agg.data$numclaims)

sev.stanfit <- stan(model_code=stan.severity.code,
                    data=model.data,
                    iter=30000,
                    warmup=2000,
                    chains=3)

3. Entropic Estimates

The following computes the posterior mean, entropic beta and entropic mean respectively.

## Posterior mean
post.mu <- colMeans(extract(sev.stanfit,pars="mu" )$mu)

## Entropic betas
entropic.beta <- coef(glm(post.mu ~ agecat + gender + area + veh_value,
                              family=Gamma(link="log"),
                              weights=numclaims,
                          data=agg.data ))

## Entropic credibility
		 entropic.mu <-exp( X %*% entropic.beta)

4. Non Credibility Estimates

Now we compute the premium using frequentist GLMs.

agg.data$obs.premium <- agg.data$claimcst0 / agg.data$numclaims

non.cred.model <- glm(obs.premium ~ agecat + gender + area + veh_value,
                      family=Gamma(link="log"),
                      weights=numclaims,
                      data=agg.data )

non.cred.premium <- predict(non.cred.model ,type="response")

4. Classes table

agecat gender area veh_value claimcst0 numclaims Credibility Premium Premium
5 F ABCD P3 143286.00 103.00 1373.24 1362.23
5 F ABCD P2 190286.65 99.00 1428.23 1416.31
6 F ABCD P3 49604.06 33.00 1475.97 1458.01
3 F ABCD P3 342153.16 210.00 1517.42 1508.93
4 F ABCD P3 175800.70 157.00 1520.57 1512.69
6 F ABCD P2 90709.05 74.00 1535.08 1515.90
3 F ABCD P2 300949.94 202.00 1578.18 1568.84
4 F ABCD P2 427309.04 226.00 1581.46 1572.75
5 F E P3 14694.00 16.00 1598.67 1572.53
5 F ABCD P1 166165.91 95.00 1604.94 1590.78
5 M ABCD P3 107384.24 112.00 1649.01 1635.28
5 F E P2 5494.55 9.00 1662.68 1634.97
2 F ABCD P3 260872.70 151.00 1670.48 1660.47
5 M ABCD P2 93941.60 76.00 1715.05 1700.21
6 F E P3 1985.14 3.00 1718.27 1683.11
6 F ABCD P1 91259.62 67.00 1725.01 1702.64
2 F ABCD P2 243282.20 164.00 1737.38 1726.39
3 F E P3 47338.40 25.00 1766.52 1741.88
4 F E P3 28446.99 20.00 1770.19 1746.22
6 M ABCD P3 150576.26 71.00 1772.38 1750.27
3 F ABCD P1 416263.74 204.00 1773.45 1762.09
4 F ABCD P1 382979.87 221.00 1777.14 1766.48
6 F E P2 33129.19 7.00 1787.07 1749.93
3 M ABCD P3 271928.40 162.00 1822.15 1811.38
4 M ABCD P3 268648.71 165.00 1825.94 1815.90
3 F E P2 43056.17 25.00 1837.26 1811.04
4 F E P2 28805.81 18.00 1841.08 1815.55
6 M ABCD P2 108135.99 63.00 1843.35 1819.76
5 F E P1 14554.23 8.00 1868.41 1836.37
3 M ABCD P2 198017.50 112.00 1895.11 1883.30
4 M ABCD P2 260961.26 131.00 1899.06 1888.00
5 M E P3 39418.93 15.00 1919.71 1887.74
5 M ABCD P1 184712.55 75.00 1927.25 1909.65
2 F E P3 18717.61 13.00 1944.71 1916.81
2 F ABCD P1 341669.44 166.00 1952.34 1939.06
5 M E P2 8078.37 7.00 1996.59 1962.69
5 F F P3 16626.50 7.00 2002.05 1962.81
2 M ABCD P3 415682.61 142.00 2005.95 1993.30
6 F E P1 9685.72 6.00 2008.19 1965.50
2 F E P2 27660.32 17.00 2022.59 1992.92
1 F ABCD P3 127635.32 55.00 2054.96 2033.95
6 M E P3 988.43 2.00 2063.33 2020.48
3 F E P1 31886.61 16.00 2064.58 2034.13
4 F E P1 46814.15 19.00 2068.87 2039.20
6 M ABCD P1 117751.87 48.00 2071.43 2043.93
5 F F P2 1210.95 1.00 2082.22 2040.74
2 M ABCD P2 216822.03 114.00 2086.28 2072.44
3 M E P3 56767.03 24.00 2121.27 2091.03
4 M E P3 49861.02 14.00 2125.68 2096.24
3 M ABCD P1 204610.22 108.00 2129.60 2115.30
4 M ABCD P1 273757.03 144.00 2134.03 2120.57
1 F ABCD P2 229982.48 122.00 2137.25 2114.71
6 M E P2 20723.49 4.00 2145.96 2100.70
6 F F P3 4993.37 3.00 2151.82 2100.83
3 M E P2 5918.78 5.00 2206.22 2174.05
4 M E P2 52690.12 11.00 2210.80 2179.47
3 F F P3 46057.12 34.00 2212.25 2174.19
4 F F P3 34487.43 20.00 2216.85 2179.61
5 M E P1 7157.38 7.00 2243.62 2204.46
2 F E P1 13886.76 9.00 2272.84 2238.42
3 F F P2 56496.29 19.00 2300.84 2260.52
4 F F P2 32559.16 7.00 2305.62 2266.15
2 M E P3 44224.71 32.00 2335.25 2301.03
5 F F P1 2783.49 2.00 2339.85 2292.13
2 M ABCD P1 168426.88 80.00 2344.42 2327.74
1 F E P3 8017.28 5.00 2392.30 2347.96
1 F ABCD P1 163074.98 76.00 2401.69 2375.21
5 M F P3 46239.91 9.00 2404.10 2356.25
6 M E P1 4026.32 9.00 2411.47 2359.48
2 M E P2 7568.42 11.00 2428.76 2392.39
2 F F P3 101384.81 43.00 2435.40 2392.54
1 M ABCD P3 213348.43 78.00 2467.64 2441.65
3 M E P1 2177.30 2.00 2479.19 2441.86
4 M E P1 18164.05 9.00 2484.35 2447.95
1 F E P2 5835.85 5.00 2488.10 2441.19
5 M F P2 12066.20 5.00 2500.37 2449.80
2 F F P2 35546.70 13.00 2532.93 2487.54
1 M ABCD P2 151417.25 62.00 2566.46 2538.60
3 F F P1 1792.19 3.00 2585.52 2538.98
3 M F P3 36945.01 26.00 2656.51 2610.00
4 M F P3 55052.36 15.00 2662.04 2616.50
2 M E P1 38062.92 6.00 2729.27 2687.10
3 M F P2 21796.48 7.00 2762.89 2713.63
4 M F P2 8965.32 8.00 2768.64 2720.39
1 F E P1 16903.95 9.00 2795.95 2741.90
5 M F P1 7310.71 2.00 2809.74 2751.58
2 F F P1 1401.77 3.00 2846.33 2793.96
1 M E P3 87116.26 17.00 2872.73 2818.60
1 M ABCD P1 94418.41 51.00 2884.00 2851.31
2 M F P3 24323.16 26.00 2924.49 2872.12
1 M E P2 28476.67 7.00 2987.76 2930.51
1 F F P3 8258.59 9.00 2995.93 2930.70
2 M F P2 18357.16 6.00 3041.60 2986.15
3 M F P1 47952.73 5.00 3104.75 3047.90
1 F F P2 10839.49 7.00 3115.90 3047.06
1 M E P1 490.00 1.00 3357.44 3291.51
2 M F P1 6950.54 4.00 3417.93 3354.00
1 F F P1 14113.60 6.00 3501.43 3422.41
1 M F P3 127855.12 13.00 3597.58 3518.14
1 M F P2 11469.09 1.00 3741.64 3657.83
1 M F P1 8120.13 1.00 4204.60 4108.42

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published