Join predictive distributions into a portfolio
portfolio.Rdjoin_predictive() puts distributions of different models side by side
(a reserve bootstrap by origin, premium risk by line, a tower's net),
with a new leading key column dim holding each part's name, followed by
the union of the parts' key columns ("" where a part lacks one).
Simulation i of the result is simulation i of every part.
reorder_groups() then sets the dependence between the groups of dim
by Iman-Conover on the group totals, moving each group's simulations as
whole rows: every group keeps its distribution and internal joint
structure. Use aggregate(), VaR(), TVaR() and capital_allocation()
on the result. group_spearman() is Spearman's rho between the group
totals (ties at their average rank, as cor(method = "spearman")), to
check what reorder_groups() achieved.
reorder_groups_by_copula() sets the dependence by a copula instead:
n draws of uniforms (draw i on stream i - 1 of seed, as
copula_sample()), and each group's simulations, whole rows sorted by the
group's total, take the ranks of its column. The group totals then have
the copula's rank pattern, so a t, Gumbel or Clayton copula gives its
joint tails to results already simulated.
Usage
join_predictive(parts, dim, same_simulations = FALSE)
reorder_groups(x, dim, correlation, seed, kind = "pearson", index = FALSE)
reorder_groups_by_copula(x, dim, copula, seed, index = FALSE)
group_spearman(x, dim)Arguments
- parts
A named list of predictive_distributions with the same number of simulations.
- dim
Name of the new key column.
- same_simulations
FALSE: the parts were simulated separately, and two with the same seed (which would share random numbers) are refused.TRUE: the parts come from the same scenarios (a cover applied to a reserve) and keep their pairing.- x
- correlation
Correlation matrix, one row and column per group in order of first appearance (
unique(x@keys[[dim]])), or a long table: a data frame whose first two columns name a pair of groups and whose third holds its value, each pair of distinct groups once.- seed
Seed, a whole number.
- kind
"pearson"(the default: the normal scores' correlation, so Spearman's rho comes out near(6 / pi) asin(r / 2)) or"spearman"(a Spearman target, converted to2 sin(pi rho / 6)first).- index
TRUEto return where each simulation came from too.- copula
A copula with one dimension per group, in order of first appearance.
Value
A predictive_distribution; group_spearman() a matrix with the
groups as dimnames. With index = TRUE, reorder_groups() and
reorder_groups_by_copula() return a list of result, the
distribution, and index, a named list with one integer vector per
group: simulation i of the result holds the group's row of input
simulation index[[group]][i]. permute_events() with that vector
keeps a class's events with its reordered totals, with no matching of
values (which tied draws defeat).
Examples
a <- predictive_distribution(cbind(c(10, 12), c(20, 25)), data.frame(origin = c(2023, 2024)))
b <- predictive_distribution(matrix(c(50, 40), ncol = 1), data.frame(lob = "motor"))
p <- join_predictive(list(reserve = a, premium = b), "risk")
p@keys
#> risk origin lob
#> 1 reserve 2023
#> 2 reserve 2024
#> 3 premium motor
n <- 4000
pd <- predictive_distribution(cbind(0:(n - 1), ((0:(n - 1)) * 7919) %% n),
data.frame(seg = c("a", "b")))
rho <- data.frame(from = "a", to = "b", rho = 0.6)
group_spearman(reorder_groups(pd, "seg", rho, seed = 5, kind = "spearman"), "seg")
#> a b
#> a 1.0000000 0.5988615
#> b 0.5988615 1.0000000
# Clayton with theta = 2: Spearman's rho near 0.68, more joint low totals.
group_spearman(reorder_groups_by_copula(pd, "seg", archimedean_copula("clayton", 2), 5), "seg")
#> a b
#> a 1.000000 0.673868
#> b 0.673868 1.000000
r <- reorder_groups(pd, "seg", rho, seed = 5, index = TRUE)
all(draw_matrix(r$result)[, 1] == draw_matrix(pd)[r$index$a, 1])
#> [1] TRUE