This is the R twin of the Python guide distributions-for-actuaries-claim-size.md.
It recomputes the examples and problems of chapters 2, 5 and 6 of CAS
Monograph 2, Distributions for Actuaries by David Bahnemann
(2015), with the same sections and numbers. Read the Python guide for
the discussion of each result. The companion R guide, distributions-for-actuaries-counts-aggregates.qmd,
covers chapters 3 and 4.
R/prospicio/tests/test-guides.R requires each chunk to
print exactly the text under it (a table prints as its labels and
formatted cells). book is the monograph’s printed value, a
literal in the code. In tables with one row per grid point, each row
marked “, book” is the monograph’s value under prospicio’s. This guide
minimizes with R’s optim(), the Python guide with SciPy’s
Nelder–Mead; both find the same minima to the printed digits.
1. Translating the parameters
Monograph
Its formula
prospicio (R)
gamma (α, β), β a scale
mean αβ
gamma_distribution(α, β)
exponential β
mean β
gamma_distribution(1, β)
lognormal (μ, σ)
log X normal (μ, σ)
lognormal(μ, σ)
Weibull (β, δ)
F = 1 − exp(−(x/β)^δ)
weibull_distribution(shape = δ, scale = β)
Pareto (α, β), the “shifted” one used in all examples
F = 1 − (β/(x+β))^α, from 0
generalized_pareto(1/α, β/α), or
burr_distribution(α, 1, β)
classical Pareto (γ, α)
F = 1 − (γ/x)^α, from γ
pareto(γ, α)
The monograph’s Pareto starts at zero (a Lomax). prospicio’s
pareto() starts at a threshold.
limited_second_moment builds E[X²; l] as
layer_variance + layer^2 from a
truncated_distribution() with no bounds.
surv() is 1 - cdf(). book_table()
and grid_table() are the guide’s table helpers.
library(prospicio)library(gt)shifted_pareto<-function(alpha, beta)generalized_pareto(1/alpha, beta/alpha)surv<-function(sev, x)1-cdf(sev, x)limited_second_moment<-function(sev, limit){t<-truncated_distribution(sev)layer_variance(t, limit, 0)+layer(t, limit, 0)^2}layer_second_moment<-function(sev, limit, attachment){t<-truncated_distribution(sev)layer_variance(t, limit, attachment)+layer(t, limit, attachment)^2}num<-function(x, digits=0)formatC(x, format ="f", digits =digits, big.mark =",")row<-function(label, book, ours, digits=0)list(label, book, ours, digits)book_table<-function(rows, title, subtitle=NULL){frame<-data.frame(item =vapply(rows, function(r)r[[1]], ""), book =vapply(rows, function(r)r[[2]], 0), prospicio =vapply(rows, function(r)r[[3]], 0))digits<-vapply(rows, function(r)r[[4]], 0)table<-gt(frame, rowname_col ="item")for(placesinsort(unique(digits))){table<-fmt_number(table, columns =c(book, prospicio), rows =which(digits==places), decimals =places)}table|>sub_missing(missing_text ="")|>tab_header(title, subtitle)}# One measure: book and prospicio columns. Several: rows alternate prospicio and ", book".# Each measure is list(name, book values, prospicio values, decimals).grid_table<-function(stub, index, measures, title, subtitle=NULL){at<-if(is.numeric(index))formatC(index, format ="f", digits =0, big.mark =",")elseindexif(length(measures)==1){m<-measures[[1]]table<-gt(data.frame(at =at, book =m[[2]], prospicio =m[[3]]), rowname_col ="at")|>tab_spanner(m[[1]], columns =c(book, prospicio))|>fmt_number(columns =c(book, prospicio), decimals =m[[4]])}else{frame<-data.frame(at =as.vector(rbind(at, paste0(at, ", book"))))for(minmeasures)frame[[m[[1]]]]<-as.vector(rbind(m[[3]], m[[2]]))table<-gt(frame, rowname_col ="at")for(minmeasures)table<-fmt_number(table, columns =all_of(m[[1]]), decimals =m[[4]])}table|>tab_stubhead(stub)|>sub_missing(missing_text ="")|>tab_header(title, subtitle)}p<-shifted_pareto(2, 3000)closed_form<-3000/(2-1)*(1-3000/(8000+3000))cat(sprintf("E[X; 8,000] of Pareto (2; 3,000): book 2,182, closed form %s, prospicio %s, Burr %s\n",num(closed_form, 2), num(lev(p, 8000), 2), num(lev(burr_distribution(2, 1, 3000), 8000), 2)))
## E[X; 8,000] of Pareto (2; 3,000): book 2,182, closed form 2,181.82, prospicio 2,181.82, Burr 2,181.82
P2.25: mean 1,000, standard deviation 2,000. How many of 5,000 claims
exceed 1,000 under each family?
fits<-list(gamma =gamma_from_mean_cv(1000, 2), lognormal =lognormal_from_mean_cv(1000, 2), Pareto =shifted_pareto(8/3, 1000*(8/3-1)))book<-c(gamma =1282, lognormal =1315, Pareto =1428)book_table(lapply(names(fits), function(name)row(name, book[[name]], 5000*surv(fits[[name]], 1000))),"P2.25: claims above 1,000 out of 5,000", "Mean 1,000 and standard deviation 2,000, by family")
P2.25: claims above 1,000 out of 5,000
Mean 1,000 and standard deviation 2,000, by family
book
prospicio
gamma
1,282
1,282
lognormal
1,315
1,315
Pareto
1,428
1,428
3. Fitting grouped data
Ex 2.7 matches a gamma to the sample moments M1 = 1,555 and M2 =
3,036,875 of Ex 2.6’s 200 claims. The printed scale 397.931 is a slip
for 397.974. The monograph’s tables use it, and so do the next
chunks:
Lognormals fitted by minimum chi-square to truncated samples
Ex 2.11 (deductible 1,000, limit 200,000) and P2.43 (up to 50,000)
book
prospicio
Ex 2.11 mu
6.6916
6.6916
Ex 2.11 sigma
2.6965
2.6965
Ex 2.11 chi-square
4.691
4.691
P2.43 mu
9.4951
9.4951
P2.43 sigma
1.0842
1.0842
Ex 2.11 band by band, the observed claims beside the expected
counts:
ex_2_11<-data.frame( band =c("1k to 5k", "5k to 10k", "10k to 25k", "25k to 50k", "50k to 75k", "75k to 100k","100k to 125k", "125k to 150k", "150k to 175k", "175k to 200k", "at the 200k limit"), observed =observed, book =c(360, 122, 121, 63, 27, 16, 10, 7, 5, 4, 34), prospicio =770*cell_probs(fitted, cuts))gt(ex_2_11, rowname_col ="band")|>tab_header("Ex 2.11: 770 claims above a 1,000 deductible, censored at 200,000","Expected counts of the lognormal fitted by minimum chi-square")|>tab_spanner("expected", columns =c(book, prospicio))|>fmt_integer(columns =c(observed, book, prospicio))
Ex 2.11: 770 claims above a 1,000 deductible, censored at 200,000
Expected counts of the lognormal fitted by minimum chi-square
observed
expected
book
prospicio
1k to 5k
367
360
360
5k to 10k
112
122
122
10k to 25k
118
121
121
25k to 50k
65
63
63
50k to 75k
36
27
27
75k to 100k
13
16
16
100k to 125k
10
10
10
125k to 150k
8
7
7
150k to 175k
6
5
5
175k to 200k
4
4
4
at the 200k limit
31
34
34
severity_fit() fits individual claims by maximum
likelihood: the deductible is a reporting threshold and the limit is
censoring. The 20,000 draws equal the Python guide’s:
truth<-lognormal(6.6916, 2.6965)ground_up<-draws(truth, 20000, seed =2)kept<-ground_up[ground_up>1000]recorded<-pmin(kept, 200000)at_limit<-kept>=200000cat(sprintf("%s of 20,000 claims recorded, %d at the limit\n", num(length(kept)), sum(at_limit)))
## 9,551 of 20,000 claims recorded, 415 at the limit
fits<-list("ignoring both"=severity_fit("lognormal", recorded),"censoring only"=severity_fit("lognormal", recorded, censored =at_limit),"truncation and censoring"=severity_fit("lognormal", recorded, censored =at_limit, reporting_thresholds =rep(1000, length(kept))))frame<-data.frame( fit =names(fits), mu =unname(sapply(fits, function(f)f$parameters[["meanlog"]])), se =unname(sapply(fits, function(f)f$standard_errors[["meanlog"]])), sigma =unname(sapply(fits, function(f)f$parameters[["sdlog"]])))gt(frame, rowname_col ="fit")|>tab_header("Ex 2.11 refitted from the 20,000 simulated claims", "The population has mu 6.69 and sigma 2.70")|>cols_label(se ="se of mu")|>fmt_number(columns =c(mu, se, sigma), decimals =2)
Ex 2.11 refitted from the 20,000 simulated claims
The population has mu 6.69 and sigma 2.70
mu
se of mu
sigma
ignoring both
8.94
0.01
1.46
censoring only
8.96
0.02
1.52
truncation and censoring
6.76
0.15
2.67
On a log scale: the 20,000 ground-up claims (never seen), the
recorded ones, and each fit’s expected count per bin:
edges<-10^((0:70)/10)# 1 to 10,000,000, ten bins a decadeshown<-list("fit ignoring both"=fits[["ignoring both"]],"fit with both"=fits[["truncation and censoring"]])steps<-do.call(rbind, lapply(names(shown), function(label){p<-shown[[label]]$parameterscounts<-20000*diff(cdf(lognormal(p[["meanlog"]], p[["sdlog"]]), edges))data.frame(fit =label, size =edges, claims =c(counts, counts[length(counts)]))}))ggplot()+geom_histogram(aes(size, fill ="ground-up"), data =data.frame(size =ground_up), breaks =edges)+geom_histogram(aes(size, fill ="recorded"), data =data.frame(size =recorded), breaks =edges)+geom_step(aes(size, claims, colour =fit), data =steps, linewidth =0.8)+geom_vline(xintercept =c(1000, 200000), colour ="grey40", linetype ="dotted")+scale_x_log10(labels =scales::label_comma())+scale_fill_manual(values =c("ground-up"="grey82", "recorded"="grey55"))+labs(x ="claim size (dotted: the deductible and the limit)", y ="claims in the bin", fill =NULL, colour =NULL, title ="Ex 2.11: only the corrected fit follows the population")+theme(legend.position ="top")
Ex 5.7’s layer of 4,000 above 5,000, Pareto (2; 3,000):
p<-shifted_pareto(2, 3000)book_table(list(row("E[X; 5,000]", 1875, lev(p, 5000)),row("E[X; 9,000]", 2250, lev(p, 9000)),row("layer mean per excess claim", 2667, layer(p, 4000, 5000)/survival(p, 5000)),row("as Pareto (2; 8,000) at 4,000", 2667, lev(shifted_pareto(2, 8000), 4000))),"Ex 5.7: the layer 4,000 xs 5,000 of a Pareto (2; 3,000)")
Ex 5.7: the layer 4,000 xs 5,000 of a Pareto (2; 3,000)
book
prospicio
E[X; 5,000]
1,875
1,875
E[X; 9,000]
2,250
2,250
layer mean per excess claim
2,667
2,667
as Pareto (2; 8,000) at 4,000
2,667
2,667
Ex 5.8 and P5.10: CVs of the lognormal (5.9809; 1.8) and its layers.
The monograph’s differences of rounded limited means cause the
differences in the third and fourth figures; the rounded-inputs row
reproduces its layer CV:
ln<-lognormal(5.9809, 1.8)s3<-surv(ln, 3000)layer_mean<-layer(ln, 5000, 3000)/s3layer_m2<-layer_second_moment(ln, 5000, 3000)/s3cv_limited<-function(l)sqrt(limited_second_moment(ln, l)-lev(ln, l)^2)/lev(ln, l)m<-(1276-891)/0.130239m2<-(5774970-1853050-2*3000*(1276-891))/0.130239book_table(list(row("Ex 5.8 F(3,000)", 0.869761, cdf(ln, 3000), 6),row("E[X; 3,000]", 891, lev(ln, 3000)),row("E[X^2; 3,000]", 1853050, limited_second_moment(ln, 3000)),row("E[X^2; 8,000]", 5774970, limited_second_moment(ln, 8000)),row("CV[X]", 4.9531, sqrt(variance(ln))/mean(ln), 4),row("E[X_3K], the excess mean", 8515, stop_loss(ln, 3000)/s3),row("CV of the layer 5K xs 3K", 0.6452, sqrt(layer_m2-layer_mean^2)/layer_mean, 4),row("same, from the book's rounded inputs", 0.6452, sqrt(m2-m*m)/m, 4),row("P5.10 CV[X; 3,000]", 1.1551, cv_limited(3000), 4),row("P5.10 CV[X; 8,000]", 1.5959, cv_limited(8000), 4)), "Moments and CVs of the lognormal (5.9809; 1.8)")
Moments and CVs of the lognormal (5.9809; 1.8)
book
prospicio
Ex 5.8 F(3,000)
0.869761
0.869761
E[X; 3,000]
891
891
E[X^2; 3,000]
1,853,050
1,853,050
E[X^2; 8,000]
5,774,970
5,774,970
CV[X]
4.9531
4.9532
E[X_3K], the excess mean
8,515
8,518
CV of the layer 5K xs 3K
0.6452
0.6385
same, from the book's rounded inputs
0.6452
0.6452
P5.10 CV[X; 3,000]
1.1551
1.1561
P5.10 CV[X; 8,000]
1.5959
1.5955
P5.12: a policy with a 100 deductible and a 3,000 limit, Pareto (3;
5,000), 5 ground-up claims:
p<-shifted_pareto(3, 5000)shares<-severities<-losses<-numeric()for(Linlist(c(0, 100), c(100, 2900), c(3000, Inf))){reach<-survival(p, L[1])loss<-if(is.infinite(L[2]))stop_loss(p, L[1])elselayer(p, L[2], L[1])shares<-c(shares, reach)severities<-c(severities, loss/reach)losses<-c(losses, 5*loss)}grid_table("layer", c("[0; 100]", "(100; 3,000]", "(3,000; inf)", "total, 5 x mean"), list(list("share reaching", c(1.0000, 0.9423, 0.2441, NA), c(shares, NA), 4),list("severity", c(97.08, 1513.66, 4000.00, NA), c(severities, NA), 2),list("expected loss", c(485, 7132, 4883, 12500), c(losses, sum(losses)), 0)), "P5.12: three layers of a policy with a 100 deductible and a 3,000 limit")
P5.12: three layers of a policy with a 100 deductible and a 3,000 limit
layer
share reaching
severity
expected loss
[0; 100]
1.0000
97.08
485
[0; 100], book
1.0000
97.08
485
(100; 3,000]
0.9423
1,513.66
7,132
(100; 3,000], book
0.9423
1,513.66
7,132
(3,000; inf)
0.2441
4,000.00
4,883
(3,000; inf), book
0.2441
4,000.00
4,883
total, 5 x mean
12,500
total, 5 x mean, book
12,500
for(caseinlist(list("Ex 5.3", 43, c(8.67593, 1.18109), 300, 1500),list("P5.23", 125, c(7.960294, 1.428801), 1000, 500))){f<-cdf(lognormal(case[[3]][1], case[[3]][2]), case[[5]])cat(sprintf("%s claims hidden by the deductible: book %d, prospicio %s\n", case[[1]], case[[2]],num(case[[4]]*f/(1-f))))}
## Ex 5.3 claims hidden by the deductible: book 43, prospicio 43## P5.23 claims hidden by the deductible: book 125, prospicio 125
7. Inflation above an attachment: the leveraging effect
Ex 6.10, inflation on a deductible of 500, without a limit and with a
5,000 limit:
pure_premium<-function(sev, top, d, e, phi=0.25){capped<-if(is.null(top))mean(sev)elselev(sev, top)phi*(capped-lev(sev, d)+surv(sev, d)*e)}now<-shifted_pareto(2, 8000); later<-shifted_pareto(2, 8400)rows<-list()for(caseinlist(list(NULL, c(1893, 1994, 5.3), 0, "no limit"),list(5000, c(662.7, 677.3, 2.2), 1, "5,000 limit"))){p0<-pure_premium(now, case[[1]], 500, 50)p1<-pure_premium(later, case[[1]], 500, 52.5)rows<-c(rows, list(row(paste0("Ex 6.10 ", case[[4]], ": pure premium"), case[[2]][1], p0, case[[3]]),row("after 5% inflation", case[[2]][2], p1, case[[3]]),row("effective trend, %", case[[2]][3], 100*(p1/p0-1), 1)))}book_table(rows, "Ex 6.10: inflation on a deductible of 500")
Ex 6.10: inflation on a deductible of 500
book
prospicio
Ex 6.10 no limit: pure premium
1,893
1,893
after 5% inflation
1,994
1,994
effective trend, %
5.3
5.3
Ex 6.10 5,000 limit: pure premium
662.7
662.7
after 5% inflation
677.3
677.3
effective trend, %
2.2
2.2
11. What agreed and what did not
The comparisons and their reasons (rounding carried into differences,
misprints, spreadsheet solver results) are those of section 11 of the
Python guide.