Skip to contents

Rendered from docs/guides/cat-2-risk-metrics.qmd. The code runs each time the site is built, so the output below is current.

Page 1 changed the files of Larkspur Re into simulated years. Before Larkspur quotes the layers of Alder Mutual, its underwriter wants to know about the bad years of Alder:

  • How bad are the worst 1% of years?
  • Is a bad year one very large storm or many smaller events?
  • Does the simulation agree with the event table of the vendor?
  • How often does a loss reach the first layer of Alder?

The story, the files and the notation are on the overview. Money is in millions of Varland crowns.

This is the R twin of the Python guide cat-2-risk-metrics.md. It has the same sections, files and numbers. Under each R chunk is the text that it prints, and R/prospicio/tests/test-guides.R requires each chunk to print exactly that text. Where a line names a call, it names the R call.

1. Alder’s simulated years

This page builds the simulated years of Alder Mutual again from the year event loss table (YELT), with the helper of page 1. The helper keeps the time of each loss, which the hours clause in section 5 needs. The tests run the guides from the repository root, and Quarto renders them from docs/guides/. So find_data() looks for examples/data/ in the working directory and in its parent directories.

library(prospicio)
find_data <- function(name, dir = getwd()) {
  repeat {
    path <- file.path(dir, "examples", "data", name)
    if (file.exists(path)) return(path)
    parent <- dirname(dir)
    if (identical(parent, dir)) stop("examples/data/", name, " not found", call. = FALSE)
    dir <- parent
  }
}
read_book <- function(name) {
  utils::read.csv(find_data(name), colClasses = "character", na.strings = character())
}

yelt <- utils::read.csv(find_data("cat_book_yelt.csv"), stringsAsFactors = FALSE)
contracts <- read_book("cat_book_contracts.csv")
N_YEARS <- 2000
cedent_events <- function(cedent, perils = "all") {
  keep <- yelt$cedent == cedent
  if (perils != "all") keep <- keep & yelt$peril %in% strsplit(perils, "+", fixed = TRUE)[[1]]
  rows <- yelt[keep, ]
  year <- factor(rows$year, levels = seq_len(N_YEARS))
  times <- ((rows$day - 1) * 24 + rows$hour) / 8760
  ev <- events_from_years(unname(split(rows$loss, year)), times = unname(split(times, year)))
  with_labels(ev, list(peril = rows$peril))
}

alder <- cedent_events("alder")
elt_rows <- read_book("cat_book_elt_alder.csv")
col <- function(k) as.numeric(elt_rows[[k]])
elt <- event_loss_table(col("rate"), col("mean"), col("sd_independent"),
                        col("sd_correlated"), col("exposure"))
cat(sprintf("years %d, losses %d, mean annual loss %.3f\n", as.integer(alder@n_sims),
            as.integer(sum(event_counts(alder))), mean(total(alder))))
## years 2000, losses 5391, mean annual loss 16.778
cat(sprintf("ELT: %d events, %.4f a year, AAL %.3f\n", length(elt$columns$rate), elt$rate,
            elt$expected_annual_loss))
## ELT: 299 events, 2.3365 a year, AAL 16.056

2. The worst years: VaR and TVaR

The expected loss (EL, here the AAL) is the mean of the annual totals. VaR at level p is the p-quantile of the annual total. TVaR at p is the mean of the total over the worst 1 - p of years.

On 2,000 simulated years, a quantile is an order statistic, and the definition sets which one. Hyndman and Fan (1996) list nine definitions. prospicio gives two, through the quantile = option of statistics() and the method of VaR():

  • "inverted_cdf" (the default, their type 1, R’s quantile(type = 1)): the smallest draw whose empirical cdf reaches p. At N = 2000 and p = 0.99 that is the 1,980th smallest draw, so 20 years lie strictly above it.
  • "linear" (their type 7, R’s and numpy’s default): interpolation between neighbouring draws.

Some reports use a third rule: the k-th largest year, with k = floor((1 - p) N), so only k - 1 years are above it (#582).

total_loss <- total(total(alder))@draws
d <- sort(total_loss)
N <- length(d)
k <- floor((1 - 0.99) * N)
cat(sprintf("k = %d\n", as.integer(k)))
## k = 20
cat(sprintf("k-th largest year       %.3f\n", d[N - k + 1]))
## k-th largest year       177.979
cat(sprintf("VaR (inverted cdf)      %.3f\n", VaR(sampled(d), 0.99)))
## VaR (inverted cdf)      176.824
cat(sprintf("quantile, type 7        %.3f\n", unname(stats::quantile(d, 0.99, type = 7))))
## quantile, type 7        176.836
cat(sprintf("statistics, linear      %.3f\n",
            statistics(d, 99, quantile = "linear")[["Percentile 99"]]))
## statistics, linear      176.836

TVaR has the same choice. The exact TVaR() of a sampled gives each year a mass of 1 / N (as does the default tvar = "exact" of statistics()). It splits one year when (1 - p) N is not a whole number. At N = 2000, every round level (99%, 99.5%, 99.6%, 99.8%) gives a whole number of years, so the exact TVaR is the mean of the worst k years (the two differ only at a level such as 99.33%). tvar = "linear" integrates the interpolated quantile and gives a lower value.

cat(sprintf("TVaR (exact)              %.3f\n", TVaR(sampled(d), 0.99)))
## TVaR (exact)              255.969
cat(sprintf("mean of the worst %d years %.3f\n", as.integer(k), sum(d[(N - k + 1):N]) / k))
## mean of the worst 20 years 255.969
cat(sprintf("statistics, tvar linear   %.3f\n",
            statistics(d, 99, tvar_levels = 99, tvar = "linear")[["TVaR 99"]]))
## statistics, tvar linear   246.942
table <- statistics(d, c(90, 99), tvar_levels = 99)
for (name in c("Mean", "SD", "Percentile 90", "Percentile 99", "TVaR 99")) {
  cat(sprintf("%-14s %8.3f\n", name, table[[name]]))
}
## Mean             16.778
## SD               35.158
## Percentile 90    42.244
## Percentile 99   176.824
## TVaR 99         255.969

Name the rule with each VaR that you report: here the rules differ by about 1.2, or 0.7%.

3. A band of bad years

Capital and retro work sometimes needs the mean loss between two levels, for example the 50-year and 100-year points. This window TVaR comes from two ordinary TVaRs (#583). The worst 1 - p0 of years are the window plus the worst 1 - p1, so:

window(p0, p1) = ((1 - p0) TVaR(p0) - (1 - p1) TVaR(p1)) / (p1 - p0)

For whole numbers of years, this is the mean of the years ranked between the two levels.

p0 <- 0.98
p1 <- 0.99
window <- ((1 - p0) * TVaR(sampled(d), p0) - (1 - p1) * TVaR(sampled(d), p1)) / (p1 - p0)
cat(sprintf("window TVaR 98%% to 99%%: %.3f\n", window))
## window TVaR 98% to 99%: 151.818
cat(sprintf("mean of years ranked 21 to 40 from the top: %.3f\n", sum(d[(N - 39):(N - 20)]) / 20))
## mean of years ranked 21 to 40 from the top: 151.818

4. One large event or many: two exceedance curves

A year can be bad in two ways: one very large event, or many medium events.

  • The aggregate exceedance probability (AEP) of an amount x is the chance that the year’s total loss is above x.
  • The occurrence exceedance probability (OEP) of x is the chance that the year’s largest single loss is above x.

On simulated years, both are shares of years. The return period of x is 1 / p, so a loss with a 1% annual probability is “the 100-year loss”. The AEP is at least the OEP, because a year’s total is at least its largest loss.

An occurrence layer, for example a 40 xs 30 cat XoL, pays per event, so read it from the OEP. An aggregate cover, for example a stop loss, uses the AEP. total() of an event set gives the total of each year, and annual_maxima() gives the largest loss of each year. total() of either result gives the draws as a sampled.

library(gt)
largest <- total(annual_maxima(alder))@draws
levels <- c(30, 70, 110)
curve <- data.frame(
  x = levels,
  AEP = sapply(levels, function(x) mean(total_loss > x)),
  OEP = sapply(levels, function(x) mean(largest > x))
)
curve[["OEP return period"]] <- 1 / curve$OEP
gt(curve, rowname_col = "x") |>
  tab_header("Alder: exceedance probabilities", "x in millions of crowns; return period in years") |>
  fmt_number(columns = c(AEP, OEP), decimals = 4) |>
  fmt_number(columns = `OEP return period`, decimals = 1)
Alder: exceedance probabilities
x in millions of crowns; return period in years
AEP OEP OEP return period
30 0.1465 0.1070 9.3
70 0.0535 0.0345 29.0
110 0.0235 0.0190 52.6

The figure draws both curves for all amounts. The gap is the part of the tail that many events make and one event does not.

library(ggplot2, exclude = "layer")   # keeps prospicio's layer()
xs <- seq(5, 150, length.out = 60)
curves <- rbind(
  data.frame(x = xs, p = sapply(xs, function(x) mean(total_loss > x)), curve = "AEP (year total)"),
  data.frame(x = xs, p = sapply(xs, function(x) mean(largest > x)), curve = "OEP (largest event)")
)
ggplot(curves, aes(x, p, colour = curve)) +
  geom_line(linewidth = 0.8) +
  scale_y_log10() +
  labs(x = "loss x (millions of crowns)", y = "probability of a loss above x in a year",
       colour = NULL, title = "Alder: aggregate and occurrence exceedance, 2,000 years") +
  theme(legend.position = "top")
0.030.100.30050100150loss x (millions of crowns)probability of a loss above x in a yearAEP (year total)OEP (largest event)Alder: aggregate and occurrence exceedance, 2,000 years

5. Does the simulation agree with the event table?

The event loss table (ELT) of Alder gives each catalog event a rate and a loss distribution. With a Poisson count of events, the OEP has a closed form:

OEP(x) = 1 - exp(-sum_i rate_i * P(X_i > x))

elt_oep() calculates it exactly, and elt_occurrence_pml() inverts it. The simulated shares should agree with it up to a standard error of sqrt(p (1 - p) / 2000).

The YELT gives some windstorms and convective storms in pieces a few hours apart, but the ELT counts each event once. An occurrence is what a contract counts as one loss: here, all pieces in the 72 hours from the first piece (page 3 explains the hours clause). Raw maxima see single pieces and understate the OEP. Grouping by 72 hours (hours_clause()) joins the pieces again, but it can also join two different catalog events in the same window, which raises the grouped OEP.

grouped <- hours_clause(alder, 72)
largest_grouped <- total(annual_maxima(grouped))@draws
elt_p <- sapply(levels, function(x) elt_oep(elt, x)$oep)
check <- data.frame(
  x = levels,
  ELT = elt_p,
  raw = sapply(levels, function(x) mean(largest > x)),
  grouped = sapply(levels, function(x) mean(largest_grouped > x)),
  `s.e.` = sqrt(elt_p * (1 - elt_p) / N_YEARS),
  check.names = FALSE
)
gt(check, rowname_col = "x") |>
  tab_header("Alder: occurrence exceedance, simulated and from the ELT", "x in millions of crowns") |>
  fmt_number(columns = c(ELT, raw, grouped, `s.e.`), decimals = 4)
Alder: occurrence exceedance, simulated and from the ELT
x in millions of crowns
ELT raw grouped s.e.
30 0.1136 0.1070 0.1150 0.0071
70 0.0374 0.0345 0.0375 0.0042
110 0.0180 0.0190 0.0215 0.0030
cat(sprintf("ELT 100-year occurrence loss: %.2f\n", elt_occurrence_pml(elt, 0.01)))
## ELT 100-year occurrence loss: 147.94
al <- yelt[yelt$cedent == "alder", ]
al$hour <- (al$day - 1) * 24 + al$hour
windows <- 0
joined <- 0
joined_above_30 <- 0
for (pieces in split(al, al$year)) {
  pieces <- pieces[order(pieces$hour), ]
  i <- 1
  while (i <= nrow(pieces)) {
    j <- i
    while (j <= nrow(pieces) && pieces$hour[j] - pieces$hour[i] < 72) j <- j + 1
    w <- pieces[i:(j - 1), ]
    windows <- windows + 1
    if (length(unique(w$event_id)) > 1) {
      joined <- joined + 1
      joined_above_30 <- joined_above_30 + (sum(w$loss) > 30)
    }
    i <- j
  }
}
cat(sprintf("occurrences %d, joining two or more catalog events %d, of which above 30: %d\n",
            as.integer(windows), as.integer(joined), as.integer(joined_above_30)))
## occurrences 4238, joining two or more catalog events 87, of which above 30: 9

The figure shows the same check for all amounts. Up to about 90, the raw maxima (grey) are below the ELT line and the grouped line follows it. Above 90, both simulated lines are above it, and few years lie behind each point (about 40 at 110): joined occurrences and sampling error.

xs <- seq(20, 130, length.out = 45)
oep_curves <- rbind(
  data.frame(x = xs, p = sapply(xs, function(x) mean(largest > x)), curve = "simulated, raw maxima"),
  data.frame(x = xs, p = sapply(xs, function(x) mean(largest_grouped > x)),
             curve = "simulated, grouped by 72 hours"),
  data.frame(x = xs, p = sapply(xs, function(x) elt_oep(elt, x)$oep), curve = "ELT, closed form")
)
ggplot(oep_curves, aes(x, p, colour = curve)) +
  geom_line(linewidth = 0.8) +
  scale_colour_manual(values = c("simulated, raw maxima" = "grey75",
                                 "simulated, grouped by 72 hours" = "#F8766D",
                                 "ELT, closed form" = "#00BA38")) +
  scale_y_log10() +
  labs(x = "loss x (millions of crowns)", y = "OEP(x)", colour = NULL,
       title = "Alder: occurrence exceedance from the years and from the ELT") +
  theme(legend.position = "top")
0.030.050.1050100loss x (millions of crowns)OEP(x)ELT, closed formsimulated, grouped by 72 hourssimulated, raw maximaAlder: occurrence exceedance from the years and from the ELT

At 30 and 70, the grouped shares are well within one standard error of the ELT. At 110, the difference of 0.0035 is a little more than one standard error. The nine joined occurrences above 30 push the share the same way. The hours clause pays on the joined occurrence, so price a contract on the grouped curve, although it is not the curve of the ELT.

6. Losses at a return period

The curves also give the loss at a return period. The aggregate probable maximum loss (PML) comes from the totals, and the occurrence PML from the maxima. Underwriters call both “PML”, so say which one. Each is a VaR of section 2, here with the inverted-cdf rule.

rps <- c(10, 50, 100, 250)
pml <- data.frame(
  `return period` = rps,
  `AEP loss` = sapply(rps, function(rp) VaR(sampled(total_loss), 1 - 1 / rp)),
  `OEP loss` = sapply(rps, function(rp) VaR(sampled(largest_grouped), 1 - 1 / rp)),
  `ELT OEP loss` = sapply(rps, function(rp) elt_occurrence_pml(elt, 1 / rp)),
  check.names = FALSE
)
gt(pml, rowname_col = "return period") |>
  tab_header("Alder: loss at a return period",
             "Millions of crowns; simulated losses use the inverted-cdf rule") |>
  fmt_number(columns = c(`AEP loss`, `OEP loss`, `ELT OEP loss`), decimals = 3)
Alder: loss at a return period
Millions of crowns; simulated losses use the inverted-cdf rule
AEP loss OEP loss ELT OEP loss
10 42.244 33.971 33.648
50 125.246 120.464 103.432
100 176.824 163.418 147.938
250 232.741 223.450 199.470

In the far tail, the simulated OEP losses are above those of the ELT, for the reasons of section 5. At 250 years, only eight of the 2,000 years are beyond the estimate, so the sampling error is large. The hours clause also joins occurrences, and more years do not remove that increase.

7. How often is Alder’s first layer touched, and used up?

For a layer l xs a, an underwriter asks two questions:

  • Attachment probability: what is the probability that a loss reaches the layer? Per occurrence, it is P(X > a), strictly above the attachment.
  • Exhaustion probability: what is the probability that one loss uses all of the limit? Per occurrence, it is P(X >= a + l).

Each has a per-occurrence and an annual version. The strict and inclusive comparisons matter because a simulated loss can sit exactly on a boundary. With a Poisson count of lambda occurrences a year, the annual probability of a loss above a is 1 - exp(-lambda P(X > a)), the OEP at a.

On a severity grid, occurrence_layer_statistics() gives all of these values exactly (aggregate-distributions.md shows how to make grids). Here the grid is the empirical distribution of Alder’s grouped occurrences, each rounded up to the next half crown, with a Poisson count at their simulated rate. The terms come from cat_book_contracts.csv.

c1 <- contracts[contracts$contract == "alder_cat_1", ]
a <- as.numeric(c1$attachment)
l <- as.numeric(c1$limit)
share <- as.numeric(c1$share)
occ <- unlist(lapply(seq_len(N_YEARS), function(i) events(grouped, i)))
lam <- length(occ) / N_YEARS
step <- 0.5
cells <- ceiling(occ / step)
probs <- tabulate(cells + 1, nbins = ceiling(max(occ) / step) + 2) / length(occ)
stats <- occurrence_layer_statistics(poisson_count(lam), grid_distribution(step, probs),
                                     limit = l, attachment = a, share = share)
cat(sprintf("alder_cat_1: %.0f xs %.0f, %.3f occurrences a year\n", l, a, lam))
## alder_cat_1: 40 xs 30, 2.119 occurrences a year
cat(sprintf("grid:   P(attach) %.4f  P(exhaust) %.4f  P(layer hit in a year) %.4f\n",
            stats$pr_attach, stats$pr_detach, stats$pr_loss))
## grid:   P(attach) 0.0569  P(exhaust) 0.0184  P(layer hit in a year) 0.1135

On the simulated years (#581), count the grouped losses per occurrence and read the grouped maxima per year. The ELT gives per-occurrence values from its inverted OEP formula: P(X > x) = -log(1 - OEP(x)) / rate.

oep_elt <- function(x) elt_oep(elt, x)$oep
elt_attach <- -log(1 - oep_elt(a)) / elt$rate
elt_exhaust <- -log(1 - oep_elt(a + l)) / elt$rate
rescale <- elt$rate / lam
odds <- data.frame(
  basis = c("trials, per occurrence", "trials, per year", "ELT, per occurrence",
            sprintf("ELT, per %.3f occurrences", lam), "ELT, per year"),
  `P(attach)` = c(mean(occ > a), mean(largest_grouped > a), elt_attach,
                  elt_attach * rescale, oep_elt(a)),
  `P(exhaust)` = c(mean(occ >= a + l), mean(largest_grouped >= a + l), elt_exhaust,
                   elt_exhaust * rescale, oep_elt(a + l)),
  check.names = FALSE
)
gt(odds, rowname_col = "basis") |>
  tab_header("Alder cat 1: attachment and exhaustion probabilities") |>
  fmt_number(columns = c(`P(attach)`, `P(exhaust)`), decimals = 4)
Alder cat 1: attachment and exhaustion probabilities
P(attach) P(exhaust)
trials, per occurrence 0.0569 0.0179
trials, per year 0.1150 0.0375
ELT, per occurrence 0.0516 0.0163
ELT, per 2.119 occurrences 0.0569 0.0180
ELT, per year 0.1136 0.0374

The per-occurrence rows differ, but they agree at the same count. The ELT counts 2.3365 events a year. The grouped trials count 2.119 occurrences (the hours clause joins pieces, and the YELT has no pieces below its 0.05 floor). The annual rows need no rescale, which is one reason to compare annual values.

The exhaustion probability of the grid is a little higher than that of the trials, because rounding up to the next half crown moves a few losses just below 70 to 70. “Exhausted” here means that one occurrence used all of the limit. The annual limit is the limit plus its one reinstatement, 80 at 100%. Using all of it needs layer losses of 80 in one year, from any number of occurrences, which is rarer. Page 3 runs the layer itself.

8. From one cedent to the book

Larkspur writes all three cedents, so its tail is the tail of their sum, and each cedent’s share of that tail is less than its stand-alone TVaR. Page 7 measures this on the contracts of Larkspur and allocates capital from it.

In Python

The Python guide, cat-2-risk-metrics.md, makes the same calls: EventSet.maxima, EventSet.grouped, EventLossTable.oep, EventLossTable.occurrence_pml, Sampled.var, Sampled.tvar, statistics and occurrence_layer_statistics.

Further reading

  • Hyndman, R. J. and Fan, Y. (1996), “Sample quantiles in statistical packages”, The American Statistician 50(4), 361-365.
  • Risk measures for VaR, TVaR and their properties.
  • reinsurance.dev by Oliver Baltzer, another introduction to property catastrophe reinsurance.