Skip to contents

Rendered from docs/guides/prospicio-for-aggregate-users.qmd. The code runs each time the site is built, so the output below is current.

Use this guide if you work with Stephen Mildenhall’s package aggregate and want the same work in prospicio from R. It also teaches the collective risk model to readers new to both. It follows the “aggregate in Twenty Minutes” session (release 1.2.0; aggregate 2.1.0 prints the same numbers) step by step, with the published numbers beside prospicio’s. Where the two differ, the cause is a convention.

This is the R twin of the Python guide prospicio-for-aggregate-users.md, with the same sections, models and numbers.

Money is in the units of that session (think thousands). R/prospicio/tests/test-guides.R checks every printed number. To render the page, install the R package (remotes::install_github, see R/prospicio/bootstrap.R) and run quarto render docs/guides/prospicio-for-aggregate-users.qmd.

library(prospicio)

1. The idea, and what is different

Both libraries compute the distribution of the total loss of a year, S = X_1 + ... + X_N. They discretize the severity onto a grid, apply the probability generating function of the count with a fast Fourier transform (FFT), and read everything off the resulting probabilities. They differ in how you ask:

aggregate prospicio (R)
Model written as a DecL program (a string): agg Casualty 250 claims 1000 xs 0 sev lognorm 100 cv 1.5 poisson R objects: a count, a severity, a grid
Grid chosen for you (bs, log2), overridable with hints{} compound_auto() sizes it for an unlimited severity. With a limit, a layer or a portfolio you give the step and the number of points. audit_grid() re-runs the build around it as sharpen does
Result an Aggregate object holding DataFrames a grid_distribution (the distribution) whose report holds the checks
Reinsurance clauses in the program (occurrence net of ...) xol_layer() and reinsurance_tower() applied to the grid with tower_on_grid()

Two habits carry over unchanged. Validate the gross distribution before you reinsure it: the closed form you validate against disappears once you apply a cover. And an FFT answer is deterministic: the same inputs give the same digits every run.

The rest of this guide is the tour. Section 12 collects the DecL clauses in one table. Section 13 gives timings and the grid audit.

2. Exact and discrete: the dice

aggregate’s first example is agg Eg1 dfreq [1:5] dsev [1:3]: a count of 1 to 5 and a severity of 1 to 3, all equally likely. In prospicio an empirical count is empirical_count() (probabilities for 0, 1, 2, …) and an empirical severity is a grid_distribution() with step 1. compound_distribution() compounds them. Its default method is Panjer’s recursion, so ask for the FFT. R indexes from 1, so the probability of a total of k is probs[k + 1].

count <- empirical_count(c(0, rep(1 / 5, 5)))
severity <- grid_distribution(1, c(0, rep(1 / 3, 3)))
eg1 <- compound_distribution(count, severity, 16, method = "fft")
cat(sprintf("mean %.4f, sd %.4f\n", mean(eg1), sqrt(variance(eg1))))
## mean 6.0000, sd 3.1623
cat(sprintf("pmf(1) %.6f = 1/15, pmf(15) %.8f = 1/1215\n", eg1@probs[2], eg1@probs[16]))
## pmf(1) 0.066667 = 1/15, pmf(15) 0.00082305 = 1/1215
cat(sprintf("cdf(5) %.6f\n", cdf(eg1, 5)))
## cdf(5) 0.465021

Every bar is an exact probability.

library(ggplot2, exclude = "layer")   # keeps prospicio's layer()
ggplot(data.frame(total = 1:15, probability = eg1@probs[2:16]), aes(total, probability)) +
  geom_col() +
  labs(x = "total of the year (claim units)", y = "probability",
       title = "Eg1: the total of 1 to 5 claims of size 1 to 3")
0.0000.0250.0500.0750.100051015total of the year (claim units)probabilityEg1: the total of 1 to 5 claims of size 1 to 3

The smallest outcome is one claim of size one, 1/5 * 1/3. The largest is five claims of size three, 1/5 * (1/3)^5. The grid returns both exactly. The method is exact for a discrete severity on a grid, so there is nothing to validate.

3. The casualty book

The main example of the session is a book of 250 claims. The severity is lognormal with mean 100 and coefficient of variation (CV) 1.5, and each claim has a limit of 1,000:

agg Casualty 250 claims 1000 xs 0 sev lognorm 100 cv 1.5 poisson

In prospicio this is a few lines. discretize() puts the severity on a grid. The limit is a mapping on the grid (map_grid() keeps the mean exactly). The count is a poisson_count(), and compound_distribution() compounds them. The step of 1 and 2^16 points are what aggregate uses for this book.

library(gt)

STEP <- 1
POINTS <- 2^16

# The severity capped at limit, on a grid: `limit xs 0`.
limited_severity <- function(severity, limit, step = STEP, points = POINTS) {
  grid <- discretize(severity, step, points, method = "local_moment")
  map_grid(grid, function(x) min(x, limit))$grid
}

# A table of list(label, prospicio, book, decimals[, book decimals]) rows; a NULL book value stays blank.
compare <- function(rows, title, subtitle) {
  frame <- data.frame(
    measure = vapply(rows, function(r) r[[1]], ""),
    prospicio = vapply(rows, function(r) as.numeric(r[[2]]), 0),
    book = vapply(rows, function(r) if (is.null(r[[3]])) NA_real_ else as.numeric(r[[3]]), 0)
  )
  table <- gt(frame, rowname_col = "measure")
  for (i in seq_along(rows)) {
    r <- rows[[i]]
    table <- table |>
      fmt_number(columns = "prospicio", rows = i, decimals = r[[4]]) |>
      fmt_number(columns = "book", rows = i, decimals = if (length(r) > 4) r[[5]] else r[[4]])
  }
  table |> sub_missing(missing_text = "") |> tab_header(title, subtitle)
}

claim <- limited_severity(lognormal_from_mean_cv(100, 1.5), 1000)
casualty <- compound_distribution(poisson_count(250), claim, POINTS, method = "fft")
p <- c(0.01, 0.5, 0.99, 0.999)
q <- unname(quantile(casualty, p))
compare(
  list(
    list("claim mean", mean(claim), 98.137, 3),
    list("annual mean", mean(casualty), 24534, 1, 0),
    list("annual sd", sqrt(variance(casualty)), 2561.7, 1),
    list("1% quantile", q[1], 18954, 0),
    list("median", q[2], 24449, 0),
    list("99% quantile", q[3], 30862, 0),
    list("99.9% quantile", q[4], 33169, 0)
  ),
  "The casualty book against aggregate 1.2.0 and 2.1.0", "Claim units"
)
The casualty book against aggregate 1.2.0 and 2.1.0
Claim units
prospicio book
claim mean 98.137 98.137
annual mean 24,534.1 24,534
annual sd 2,561.7 2,561.7
1% quantile 18,954 18,954
median 24,449 24,449
99% quantile 30,862 30,862
99.9% quantile 33,170 33,169

The two agree to the digits shown, except the 99.9% quantile (section 9).

xs <- 15000:36000
top <- max(casualty@probs[xs + 1])
marks <- data.frame(q = quantile(casualty, p), label = sprintf("%.1f%%", 100 * p),
                    y = ifelse(p == 0.5, 0.05, 0.35) * top)
ggplot(data.frame(loss = xs, probability = casualty@probs[xs + 1]), aes(loss, probability)) +
  geom_line() +
  geom_vline(xintercept = marks$q, colour = "grey50", linetype = "dotted") +
  geom_text(aes(q, y, label = label), data = marks, hjust = -0.1, colour = "grey40",
            inherit.aes = FALSE) +
  scale_x_continuous(labels = scales::label_comma()) +
  labs(x = "annual loss", y = "probability per unit of loss",
       title = "The casualty book: distribution of the annual total")
1.0%50.0%99.0%99.9%0.000000.000050.000100.0001515,00020,00025,00030,00035,000annual lossprobability per unit of lossThe casualty book: distribution of the annual total

4. A vocabulary of severities

aggregate takes any scipy.stats family by mean and CV, and it takes mixtures by weights. The continuous families of prospicio have R constructors (lognormal(), gamma_distribution(), weibull_distribution(), pareto(), burr_distribution() and others, with lognormal_from_mean_cv() and gamma_from_mean_cv() by mean and CV). A mixture of limited severities is the weighted sum of their grids. The session’s three-component book,

agg Mix 100 claims 2000 xs 0 sev lognorm [50 100 200] cv [1 1.5 2] wts [.5 .3 .2] poisson

is a weighted average of three limited, discretized lognormals:

# A mixture of limited severities, from weights and severities.
mixed_severity <- function(weights, severities, limit, step = STEP, points = POINTS) {
  probs <- numeric(points)
  for (i in seq_along(weights)) {
    p <- limited_severity(severities[[i]], limit, step, points)@probs # ends at the limit
    probs[seq_along(p)] <- probs[seq_along(p)] + weights[i] * p
  }
  grid_distribution(step, probs)
}

mix_claim <- mixed_severity(
  c(0.5, 0.3, 0.2),
  list(
    lognormal_from_mean_cv(50, 1.0),
    lognormal_from_mean_cv(100, 1.5),
    lognormal_from_mean_cv(200, 2.0)
  ),
  2000
)
mix <- compound_distribution(poisson_count(100), mix_claim, POINTS, method = "fft")
mq <- unname(quantile(mix, c(0.01, 0.5, 0.99)))
compare(
  list(
    list("claim mean", mean(mix_claim), 92.996, 3),
    list("annual mean", mean(mix), 9299.6, 1),
    list("annual sd", sqrt(variance(mix)), 1894.5, 1),
    list("1% quantile", mq[1], 5638, 0),
    list("median", mq[2], 9130, 0),
    list("99% quantile", mq[3], 14423, 0)
  ),
  "The three-component book against aggregate 1.2.0 and 2.1.0", "Claim units"
)
The three-component book against aggregate 1.2.0 and 2.1.0
Claim units
prospicio book
claim mean 92.996 92.996
annual mean 9,299.6 9,299.6
annual sd 1,894.5 1,894.5
1% quantile 5,638 5,638
median 9,130 9,130
99% quantile 14,423 14,423

Tail shape has a visible price. Two books with the same mean and CV differ at the 99.9% level:

families <- list(
  lognormal = lognormal_from_mean_cv(100, 2),
  gamma = gamma_from_mean_cv(100, 2)
)
published <- c(lognormal = 13980, gamma = 11166)
tails <- list()
for (name in names(families)) {
  tail_book <- compound_distribution(
    poisson_count(50), limited_severity(families[[name]], 20000), POINTS, method = "fft"
  )
  tails <- c(tails, list(
    list(paste0(name, ", mean"), mean(tail_book), NULL, 0),
    list(paste0(name, ", 99.9%"), quantile(tail_book, 0.999), published[[name]], 0)
  ))
}
compare(tails, "Same mean and CV, different tail", "50 claims, CV 2; claim units")
Same mean and CV, different tail
50 claims, CV 2; claim units
prospicio book
lognormal, mean 5,000
lognormal, 99.9% 13,979 13,980
gamma, mean 5,000
gamma, 99.9% 11,166 11,166

The digits can differ by the cap, but the ordering is the point: the lognormal’s 99.9% sits well above the gamma’s.

5. Frequency: Poisson is an opinion

A Poisson count says that years differ only by chance. Years can also differ for reasons that all claims share (weather, inflation, the legal climate). Then the count is over-dispersed. mixed gamma 0.4 in DecL multiplies the Poisson rate by a gamma factor of mean 1 and CV 0.4. In prospicio it is mixed_poisson_count():

book <- limited_severity(lognormal_from_mean_cv(100, 1.5), 1000)
poisson <- compound_distribution(poisson_count(100), book, POINTS, method = "fft")
mixed <- compound_distribution(mixed_poisson_count(100, 0.4), book, POINTS, method = "fft")
agg_cv <- function(g) sqrt(variance(g)) / mean(g)
frame <- data.frame(
  measure = c("agg CV", "agg CV, book", "99.5% VaR", "99.5% VaR, book"),
  Poisson = c(agg_cv(poisson), 0.165, quantile(poisson, 0.995), 14456),
  `mixed gamma` = c(agg_cv(mixed), 0.433, quantile(mixed, 0.995), 23914),
  check.names = FALSE
)
gt(frame, rowname_col = "measure") |>
  fmt_number(columns = c("Poisson", "mixed gamma"), rows = 1:2, decimals = 3) |>
  fmt_number(columns = c("Poisson", "mixed gamma"), rows = 3:4, decimals = 0) |>
  tab_header("Poisson against mixed gamma count", "Annual loss in claim units")
Poisson against mixed gamma count
Annual loss in claim units
Poisson mixed gamma
agg CV 0.165 0.433
agg CV, book 0.165 0.433
99.5% VaR 14,456 23,914
99.5% VaR, book 14,456 23,914

The mixed count has the same mean, and its capital is about two-thirds higher.

xs <- 0:40000
both <- data.frame(
  loss = rep(xs, 2),
  probability = c(poisson@probs[xs + 1], mixed@probs[xs + 1]),
  count = factor(rep(c("Poisson", "mixed gamma"), each = length(xs)),
                 levels = c("Poisson", "mixed gamma"))
)
ggplot(both, aes(loss, probability, colour = count)) +
  geom_line() +
  scale_x_continuous(labels = scales::label_comma()) +
  labs(x = "annual loss", y = "probability per unit of loss", colour = NULL,
       title = "Poisson against mixed gamma count: same mean, longer tail") +
  theme(legend.position = "top")
0.000000.000050.000100.000150.000200.00025010,00020,00030,00040,000annual lossprobability per unit of lossPoissonmixed gammaPoisson against mixed gamma count: same mean, longer tail

Other mixing families, zero-modified and zero-truncated counts and the logarithmic are count constructors too (zero_truncated_count(), zero_modified_count(), logarithmic_count(), and mixed_poisson_count() with mixing = "inverse_gaussian"). The Python guide Severity and frequency explains when to use which.

6. Trust, but verify

aggregate validates (the V in Mildenhall’s SUVA-use cycle) by comparing moments computed exactly from the specification with moments estimated from the FFT output. The grid of prospicio carries the same audit in its report property. The closed form:

n <- 250
exact_mean <- n * mean(claim)
exact_var <- n * (variance(claim) + mean(claim)^2) # Poisson: n * E[X^2]
report <- casualty@report
cat(sprintf("mean: exact %.4f, grid %.4f\n", exact_mean, mean(casualty)))
## mean: exact 24534.1289, grid 24534.1289
cat(sprintf("variance: exact %.1f, grid %.1f\n", exact_var, variance(casualty)))
## variance: exact 6562423.0, grid 6562423.0
cat(sprintf("aliasing error below 1e-15: %s, tail mass %.1e\n",
            report$aliasing_error < 1e-15, report$tail_mass))
## aliasing error below 1e-15: TRUE, tail mass 0.0e+00

For a continuous severity, the gap is discretization error, which the step controls. The aliasing error shows whether the FFT buffer was long enough. The tail mass is the probability that fell off the end of the grid. Read both every time, as the Python guide Aggregate distributions explains. Here the aliasing error is rounding noise, below 1e-18. Its digits change with the platform, so the check prints only that it is small.

7. Limits, attachments and increased limits factors

Policy terms are a mapping on the grid, and increased limits factors are a loop over the limit (the session’s 100 claims lim xs 0 sev lognorm 100 cv 1.5):

base_grid <- discretize(lognormal_from_mean_cv(100, 1.5), STEP, POINTS, method = "local_moment")
limits <- c(500, 1000, 2000, 5000)
means <- vapply(limits, function(limit) {
  capped <- map_grid(base_grid, function(x) min(x, limit))$grid
  mean(compound_distribution(poisson_count(100), capped, POINTS, method = "fft"))
}, 0)
ilf <- data.frame(
  limit = formatC(limits, format = "d", big.mark = ","),
  `E[S]` = means,
  `E[S], book` = c(9334, 9814, 9963, 9997),
  ILF = means / means[1],
  `ILF, book` = c(1.000, 1.051, 1.067, 1.071),
  check.names = FALSE
)
gt(ilf, rowname_col = "limit") |>
  fmt_number(columns = c("E[S]", "E[S], book"), decimals = 0) |>
  fmt_number(columns = c("ILF", "ILF, book"), decimals = 3) |>
  tab_header("Increased limits factors", "100 claims, lognormal severity, mean 100 and CV 1.5")
Increased limits factors
100 claims, lognormal severity, mean 100 and CV 1.5
E[S] E[S], book ILF ILF, book
500 9,334 9,334 1.000 1.000
1,000 9,814 9,814 1.051 1.051
2,000 9,963 9,963 1.067 1.067
5,000 9,997 9,997 1.071 1.071

A layer that attaches above zero, 300 xs 200, has two readings in DecL. The default is the severity conditional on exceeding the attachment, with the claim count thinned by the probability of exceeding it. A trailing ! keeps every claim, most of them paying nothing. For a Poisson count the two give the same annual distribution, because thinning a Poisson count gives a Poisson count with a smaller mean. prospicio takes the second route and keeps the full count. The check builds the first route by hand:

full <- discretize(lognormal_from_mean_cv(100, 1.5), STEP, POINTS, method = "local_moment")
layered <- map_grid(full, function(x) min(max(x - 200, 0), 300))$grid # 300 xs 200
p0 <- layered@probs[1]
conditional <- grid_distribution(STEP, c(0, layered@probs[-1] / (1 - p0)))
unconditional <- compound_distribution(poisson_count(40), layered, POINTS, method = "fft")
thinned <- compound_distribution(poisson_count(40 * (1 - p0)), conditional, POINTS, method = "fft")
cat(sprintf("claims reaching the layer: %.3f of 40\n", 40 * (1 - p0)))
## claims reaching the layer: 4.732 of 40
cat(sprintf("mean %.4f vs %.4f\n", mean(unconditional), mean(thinned)))
## mean 631.2392 vs 631.2392
cat(sprintf("99%% %.0f vs %.0f\n", quantile(unconditional, 0.99), quantile(thinned, 0.99)))
## 99% 1663 vs 1663

A binomial or fixed count is not closed under thinning. With such a count the two readings differ, and the conditional one needs the thinned count.

8. Reinsurance, gross to net

The Book of the session has ten claims limited to 1,000, with lognormal severity of mean 100 and CV 2. A reinsurer takes half of 300 xs 200 and all of 500 xs 500 per occurrence:

agg Book 10 claims 1000 xs 0 sev lognorm 100 cv 2
    occurrence net of 50% po 300 xs 200 and 100% po 500 xs 500 poisson

An xol_layer() is a limit, an attachment and a placed share. A reinsurance_tower() holds the layers. tower_on_grid() applies them to the grid. It returns the ceded loss of each layer and, when there are no annual terms, the net.

book_claim <- limited_severity(lognormal_from_mean_cv(100, 2), 1000, STEP, 2^14)
programme <- reinsurance_tower(list(
  xol_layer("half of 300 xs 200", 300, 200, share = 0.5),
  xol_layer("500 xs 500", 500, 500)
))
cover <- tower_on_grid(programme, poisson_count(10), book_claim, 2^14)
gross <- compound_distribution(poisson_count(10), book_claim, 2^14, method = "fft")
ceded_mean <- sum(vapply(cover$ceded, mean, numeric(1)))
layer_mean <- vapply(cover$ceded, mean, 0)
layer_sd <- vapply(cover$ceded, function(g) sqrt(variance(g)), 0)
tower <- data.frame(
  view = c("gross", names(cover$ceded), "ceded, both layers", "net"),
  mean = c(mean(gross), layer_mean, ceded_mean, mean(cover$net)),
  `mean, book` = c(952.7, NA, NA, 161.52, 791.18),
  sd = c(NA, layer_sd, NA, sqrt(variance(cover$net))),
  `sd, book` = c(NA, NA, NA, NA, 369.93),
  `99%` = c(NA, NA, NA, NA, quantile(cover$net, 0.99)),
  check.names = FALSE
)
gt(tower, rowname_col = "view") |>
  fmt_number(columns = "mean", rows = c(1, 4, 5), decimals = 2) |>
  fmt_number(columns = "mean", rows = 2:3, decimals = 3) |>
  fmt_number(columns = "mean, book", decimals = 2, drop_trailing_zeros = TRUE) |>
  fmt_number(columns = "sd", rows = 2:3, decimals = 3) |>
  fmt_number(columns = "sd", rows = 5, decimals = 2) |>
  fmt_number(columns = "sd, book", decimals = 2) |>
  fmt_number(columns = "99%", decimals = 1) |>
  sub_missing(missing_text = "") |>
  tab_header("The Book, gross to net", "Per occurrence; claim units")
The Book, gross to net
Per occurrence; claim units
mean mean, book sd sd, book 99%
gross 952.70 952.7


half of 300 xs 200 88.497
100.878

500 xs 500 73.019
168.066

ceded, both layers 161.52 161.52


net 791.18 791.18 369.93 369.93 1,816.0

The means are exact on the grid. cover$on_points says whether every recovery landed on a grid point. When it is FALSE, the shapes can smear by up to one step while the means stay exact.

An annual cover is an aggregate_stop_loss(), applied to the total of the year. The stop loss of the session attaches at 30,000 on a book that averages 9,814, so it never pays. An attachment inside the distribution makes the point:

year <- compound_distribution(poisson_count(100), book, POINTS, method = "fft")
stop <- tower_on_grid(
  reinsurance_tower(list(aggregate_stop_loss("5,000 xs 10,000", 5000, 10000))),
  poisson_count(100), book, POINTS
)
cat(sprintf("gross mean %.1f, 99%% %.0f\n", mean(year), quantile(year, 0.99)))
## gross mean 9813.7, 99% 13949
cat(sprintf("stop loss pays %.2f on average\n", mean(stop$ceded[[1]])))
## stop loss pays 558.87 on average
cat(sprintf("net mean %.1f, net 99%% %.0f\n", mean(stop$net), quantile(stop$net, 0.99)))
## net mean 9254.8, net 99% 10000

Per-layer statistics (how often a layer is hit, loss on line, ceded and net moments by view) and the contract terms are in the Python guides What a reinsurance price is and Experience and exposure rating. The R package has them as occurrence_layer_statistics(), aggregate_layer_statistics() and the with_*() contract terms.

9. The head to head: FFT against Monte Carlo

Simulate the casualty book five times at 20,000 years each and read the 99.9% quantile. simulate_events() draws each year from its own stream of the generator, so the same seed gives the same years in R, Python and Rust:

exact <- quantile(casualty, 0.999)
seeds <- c(4817, 2531, 9026, 311, 7742)
estimates <- vapply(seeds, function(s) {
  quantile(total(simulate_events(poisson_count(250), claim, 20000, s)), 0.999)
}, 0)
runs <- data.frame(
  run = c("FFT", "FFT, book", paste("simulation, seed", seeds)),
  `99.9%` = c(exact, 33169, estimates),
  `against FFT` = c(NA, NA, estimates / exact - 1),
  check.names = FALSE
)
gt(runs, rowname_col = "run") |>
  fmt_number(columns = "99.9%", decimals = 0) |>
  fmt_percent(columns = "against FFT", decimals = 2, force_sign = TRUE) |>
  sub_missing(missing_text = "") |>
  tab_header("The casualty book's 99.9% quantile", "FFT against 20,000-year simulations")
The casualty book's 99.9% quantile
FFT against 20,000-year simulations
99.9% against FFT
FFT 33,170
FFT, book 33,169
simulation, seed 4817 33,145 −0.08%
simulation, seed 2531 33,146 −0.07%
simulation, seed 9026 33,214 +0.13%
simulation, seed 311 32,909 −0.79%
simulation, seed 7742 32,882 −0.87%

The 1-unit gap to aggregate’s FFT is the quantile convention where the distribution function crosses 0.999 inside a bucket. At a step of 1 it is the resolution of the answer. Each simulated estimate rests on the twenty largest of 20,000 years, so it moves by a percent or so from run to run. That matters when it sets capital. The FFT value is the same every run. Simulation is still the tool when events need an order, a date or a sum insured, or when dependence cannot be put on a grid (see the Python guide Aggregate distributions).

10. Portfolios, capital and a first price

A port in DecL convolves independent units on one grid. In prospicio that is capital_portfolio(): a named list of grids of the same step, convolved by FFT as independent units (Python: Portfolio.from_independent). The book of the session has a property unit and a casualty unit. A gamma of CV 0.4 mixes the casualty count:

port Book
    agg Property 80 claims 500 xs 0 sev lognorm 50 cv 1.2 poisson
    agg Casualty 20 claims 2000 xs 0 sev lognorm 250 cv 2.0 mixed gamma 0.4

portfolio_assets(port, p) gives the lower p quantile of the total (Python: Portfolio.assets(p)). natural_price() with p prices at that level:

property_unit <- compound_distribution(
  poisson_count(80), limited_severity(lognormal_from_mean_cv(50, 1.2), 500), POINTS,
  method = "fft"
)
casualty_unit <- compound_distribution(
  mixed_poisson_count(20, 0.4), limited_severity(lognormal_from_mean_cv(250, 2.0), 2000), POINTS,
  method = "fft"
)
book_port <- capital_portfolio(list(Property = property_unit, Casualty = casualty_unit))
priced <- natural_price(book_port, distortion("ccoc", 0.10), p = 0.996)
total_row <- priced[priced$unit == "total", ]
stand_alone <- quantile(property_unit, 0.996) + quantile(casualty_unit, 0.996)
cat(sprintf("99.6%% VaR, units alone summed %s (aggregate: 19,989)\n",
            formatC(stand_alone, format = "f", digits = 0, big.mark = ",")))
## 99.6% VaR, units alone summed 19,989 (aggregate: 19,989)
cat(sprintf("99.6%% VaR, portfolio         %s (aggregate: 18,137)\n",
            formatC(portfolio_assets(book_port, 0.996), format = "f", digits = 0, big.mark = ",")))
## 99.6% VaR, portfolio         18,137 (aggregate: 18,137)

The gap is the diversification benefit. The session then asks for a price that backs capital with the 99.6% VaR and targets a 10% return on it (price_pentagon). The pentagon is loss L, margin M, premium P = L + M, capital Q and assets a = P + Q, with the ratios between them. A constant cost of capital of 10% on assets at the 99.6% quantile prices the book (distortion("ccoc", 0.10) above). pentagon() completes the identity from three of its quantities:

pg <- pentagon(loss = total_row$loss, assets = total_row$assets, return_on_capital = 0.10)
compare(
  list(
    list("loss L", pg[["loss"]], 8639.7, 1),
    list("margin M", pg[["margin"]], 863.39, 2),
    list("premium P", pg[["premium"]], 9503.1, 1),
    list("capital Q", pg[["capital"]], 8633.9, 1),
    list("assets a", pg[["assets"]], 18137, 0),
    list("loss ratio", pg[["loss_ratio"]], 0.90915, 5),
    list("premium to capital", pg[["premium_to_capital"]], 1.1007, 4)
  ),
  "The pentagon of the book against aggregate 1.2.0 and 2.1.0",
  "CCoC 10% at the 99.6% quantile; claim units"
)
The pentagon of the book against aggregate 1.2.0 and 2.1.0
CCoC 10% at the 99.6% quantile; claim units
prospicio book
loss L 8,639.7 8,639.7
margin M 863.39 863.39
premium P 9,503.1 9,503.1
capital Q 8,633.9 8,633.9
assets a 18,137 18,137
loss ratio 0.90915 0.90915
premium to capital 1.1007 1.1007

L is the expected loss capped at the assets (limited liability), a little under the uncapped mean:

cat(sprintf("uncapped mean %.1f, capped at assets %.1f\n",
            mean(property_unit) + mean(casualty_unit), total_row$loss))
## uncapped mean 8646.1, capped at assets 8639.7
units <- priced[priced$unit != "total", ]
for (i in seq_len(nrow(units))) {
  cat(sprintf("%-9s loss %8.1f  premium %8.1f  capital %8.1f\n",
              units$unit[i], units$loss[i], units$premium[i], units$capital[i]))
}
## Property  loss   3972.3  premium   3969.3  capital    -30.3
## Casualty  loss   4667.4  premium   5533.8  capital   8664.2

The natural allocation splits premium and capital between the units. The Python guide Risk loads and distortions explains which distortion to choose and how to allocate. In R, calibrate_portfolio() calibrates a distortion to a quote.

11. Everything is an object you can read

The probabilities of a grid are the numeric vector @probs, and natural_price() returns a data.frame, so a table or a ggplot2 chart is one line away:

print(round(casualty@probs[20001:20003], 6)) # losses of 20,000 to 20,002
## [1] 3.2e-05 3.2e-05 3.2e-05
cat(sprintf("F(20,000) %.6f\n", cdf(casualty, 20000)))
## F(20,000) 0.031989

aggregate prints the same: 0.000032 per unit near 20,000 and F(20,000) of 0.031989.

12. DecL clauses and their prospicio equivalents

DecL What it says prospicio (R)
250 claims expected claim count the count’s mean: poisson_count(250)
500 loss expected loss, count derived poisson_count(500 / mean(claim)) on the limited severity’s mean
1000 prem at .65 lr premium and loss ratio expected loss 1000 * 0.65, then as 500 loss
1000 xs 0 per-claim limit map_grid(grid, function(x) min(x, 1000))$grid
130 xs 20 a layer, claims thinned map_grid(grid, function(x) min(max(x - 20, 0), 130))$grid with the full count (section 7)
[10 20 30] claims [100 200 75] xs [0 50 75] a limit profile one limited grid per limit, then capital_portfolio() on a list of grids or a weighted grid_distribution()
sev lognorm 100 cv 1.5 severity by mean and CV lognormal_from_mean_cv(100, 1.5), gamma_from_mean_cv()
sev lognorm [50 100] cv [1 2] wts [.5 .5] a mixture a weighted average of grids (section 4), or mixture_distribution() for a continuous severity
dsev [1 2 10] [.4 .4 .2], dfreq [1:5] discrete outcomes grid_distribution(step, probs), discrete_distribution(), empirical_count()
poisson, negbin, binomial, geometric, fixed count families poisson_count(), negative_binomial_count(), binomial_count(), geometric_count(), fixed_count()
mixed gamma 0.4 mixed Poisson mixed_poisson_count(mean, 0.4), with mixing = "inverse_gaussian" as an option
zt, zm .5 zero-truncated and zero-modified counts zero_truncated_count(base), zero_modified_count(base, 0.5)
occurrence net of 50% po 300 xs 200 per-claim cover, net xol_layer(name, 300, 200, share = 0.5) in a reinsurance_tower(), tower_on_grid(), $net
aggregate net of 5000 xs 10000 annual cover aggregate_stop_loss(name, 5000, 10000)
occurrence ceded to tower [10:50:10] a tower of layers a reinsurance_tower() of xol_layer()s
reinstatements 1 free and 1 at 50% reinstatements xol_layer(..., reinstatements = 1), reinstatement_rates = c(...)
swing, slide, pc, corridor, deposit, rol, rate, cede contract terms with_swing_rating(), with_sliding_scale(), with_profit_commission(), with_loss_corridor(), with_deposit_premium(), with_rate_on_line(), with_premium_rate(), with_ceding_commission()
approximate sgamma moment-matched aggregate moment_approximation(frequency, severity, "shifted_gamma")
tweedie p mean dispersion a Tweedie aggregate tweedie()
port ... agg ... agg ... independent units capital_portfolio(list(name = grid, ...))
bivariate, clash two lines, one count fft_bivariate(), clash_model() with clash_aggregate()
pnl, xpnl premium less loss pnl_ledger(), pnl_book(), pnl_program()
distortion D ph 0.9 a distortion distortion("proportional_hazard", 0.9), and "wang", "ccoc", "tvar" and others
port.price(p, ROE), price_pentagon capital-loaded price natural_price(port, distortion, p = ...), pentagon()
hints{bs=1/64; log2=16} the grid the step and points arguments, and compound_auto(..., log2 = 16)

13. Speed and the grid audit

The R package runs the same Rust code as the Python package, so the Python guide’s timings against aggregate apply to R too: a release build ran 1.1 to 2.1 times faster end to end on the workloads of this tour. They were not measured from R, and CI does not check them.

compound_auto() sizes the grid for an unlimited severity. With a limit or a portfolio, you pick the step and read aliasing_error and tail_mass (section 6). audit_grid() (and audit_portfolio_grid()) does the work of sharpen. It builds the compound at your step and at doubled and halved steps, scores each build by its moment errors against the exact moments, and returns the best sound cell. It audits the grid that you give it.

aggregate (1.2.0 to 2.1.0) shares a mixing variable across the severity components of one Aggregate (one mixed count on the claim-weighted mixture) and across the two units of a bivariate. It never shares it across the units of a portfolio, whose total is the product of their transforms. prospicio supports both: a mixed Poisson count compounded on a mixture, and fft_bivariate() with a mixed count. capital_portfolio() on a list of grids treats units as independent, as the portfolios of aggregate do.

The Python guides below apply to R too.