Skip to content

Repository files navigation

bayesian-pricing

PythonLicense: MITPyPITests

Hierarchical Bayesian models for insurance pricing thin-data segments.

The problem

UK personal lines rating operates on multi-dimensional grids. A typical motor model has driver age × NCD × vehicle group × postcode area × occupation. That is potentially 4.5 million rating cells. With 1 million policies, most cells are either empty or contain fewer than 30 observations.

Standard approaches all fail at thin cells:

  • Saturated GLM: one coefficient per cell. Overfits noise. A cell with 3 claims gets a relativity of 3/expected, which is meaningless.
  • Main-effects GLM: forces multiplicativity. A young driver in a sports car has a rate exactly equal to young-driver-relativity × sports-car-relativity. Reality is super-multiplicative and the model cannot detect it.
  • Ridge/LASSO GLM: uniform regularisation regardless of exposure. A cell with 5,000 policy-years gets the same shrinkage as one with 20 policy-years. Wrong.
  • GBM with min_data_in_leaf: refuses to split on thin cells. Cannot borrow strength from related cells. No calibrated uncertainty.

The correct answer is partial pooling: thin segments borrow strength from related segments via a shared population distribution. The degree of borrowing is data-driven - determined by the ratio of within-segment sampling noise to between-segment signal variance. This is the Bayesian posterior.

Under Normal-Normal conjugacy, partial pooling is exactly Bühlmann-Straub credibility. This library generalises it to Poisson (frequency) and Gamma (severity) likelihoods, with multiple crossed random effects.

Install

uv add "bayesian-pricing[pymc]"

PyMC 5.x is an optional dependency - it is not pulled in by default because it has C++ compiler requirements on some platforms. The [pymc] extra handles this. For GPU-accelerated inference on large portfolios:

uv add "bayesian-pricing[numpyro]"

Usage

Input is segment-level sufficient statistics - one row per rating cell, with exposure and claim count. This is the practical production design: aggregate your book to rating cells first, then run the model. A book with 500k policies typically has 5,000–20,000 non-empty rating cells. The model operates on those cells, making NUTS feasible on a standard machine.

importpolarsasplfrombayesian_pricingimportHierarchicalFrequency, BayesianRelativitiesfrombayesian_pricing.frequencyimportSamplerConfig# One row per rating celldf=pl.DataFrame({
"veh_group": ["Supermini", "Supermini", "Sports", "Sports", "Saloon"],
"age_band": ["17-21", "31-40", "17-21", "31-40", "31-40"],
"claims": [8, 120, 3, 45, 200],
"exposure": [60.0, 900.0, 25.0, 350.0, 2000.0],
})
# Fit hierarchical Poisson modelmodel=HierarchicalFrequency(
group_cols=["veh_group", "age_band"],
prior_mean_rate=0.09, # portfolio mean claim ratevariance_prior_sigma=0.3, # prior belief on between-segment variation
)
config=SamplerConfig(
method="nuts", # use "pathfinder" for fast iteration during model developmentdraws=1000,
tune=1000,
chains=4,
random_seed=42,
)
model.fit(df, claim_count_col="claims", exposure_col="exposure", sampler_config=config)
# Posterior predictive means for each segmentpreds=model.predict()
print(preds)
# veh_group age_band mean p5 p50 p95 credibility_factor# Supermini 17-21 0.1234 0.0812 0.1201 0.1731 0.38# Sports 17-21 0.1891 0.1102 0.1845 0.2881 0.21 <- thin# ...# Variance components: how much does each factor drive frequency?print(model.variance_components())

Relativities

rel=BayesianRelativities(model, hdi_prob=0.9)
# Full table for all factorstables=rel.relativities()
# Single factor in rate-table formatveh_table=rel.relativities(factor="veh_group")
print(veh_table.table)
# level relativity lower_90pct upper_90pct credibility_factor interval_width# Sports 1.524 1.234 1.891 0.71 0.657# Saloon 1.000 0.921 1.082 0.94 0.161# Supermini 0.819 0.764 0.881 0.89 0.117# Identify thin segments that need manual reviewthin=rel.thin_segments(credibility_threshold=0.3)
print(thin)
# factor level credibility_factor relativity# veh_group Sports-17-21 0.18 1.84 <- sparse cell, wide CI# Export for Excel / rate system importsummary_df=rel.summary() # long format: factor, level, relativity, CI, credibilitysummary_df.write_csv("bayesian_relativities.csv")

Severity model

The severity model has the same API but uses a Gamma likelihood:

frombayesian_pricingimportHierarchicalSeveritysev_model=HierarchicalSeverity(
group_cols=["veh_group"], # severity varies by vehicle, not driver ageprior_mean_severity=1800.0, # portfolio mean attritional claim costvariance_prior_sigma=0.2, # severity has less between-segment variation than frequency
)
sev_model.fit(
sev_df,
severity_col="avg_claim_cost",
weight_col="claim_count", # segments with more claims get more influencesampler_config=SamplerConfig(method="nuts", draws=1000, tune=1000, chains=4),
)
sev_preds=sev_model.predict()

Convergence diagnostics

MCMC results are only valid if the sampler converged. Check before using output:

frombayesian_pricing.diagnosticsimportconvergence_summary, posterior_predictive_check# R-hat, ESS, divergence countsdiag=convergence_summary(model)
# Prints warnings if R-hat > 1.01 or ESS < 400# Check model describes the datappc=posterior_predictive_check(model, claim_count_col="claims")
# Returns: mean, variance, p90, p95 checks

Inference options

MethodWhen to useSpeedAccuracy
SamplerConfig(method="pathfinder")Model development, prior sensitivityMinutesGood approximation
SamplerConfig(method="nuts")Final production estimates20–60 minExact (asymptotically)
SamplerConfig(nuts_sampler="numpyro")Large portfolios, GPU availableFast on GPUExact

For portfolios with more than 50k rating cells, consider the two-stage approach: fit a GBM on the full book, extract segment-level residuals, then run the Bayesian model on the residuals. The GBM captures dense-cell signal; the Bayesian model handles thin-cell pooling.

Relationship to Bühlmann-Straub credibility

The Bühlmann-Straub credibility premium is the exact posterior mean of a hierarchical model under Normal-Normal conjugacy. This library generalises that result:

FeatureBühlmann-Straubbayesian-pricing
LikelihoodNormal (symmetric loss)Poisson, Gamma, NB
Number of grouping factorsOneMultiple crossed
Credible intervalsNo (point estimates)Yes (full posterior)
Hyperparameter uncertaintyPlugged inIntegrated out
Groups> 20 needed for stable KWorks with 5+

For single-factor pricing with many groups (e.g., scheme pricing), Bühlmann-Straub is computationally trivial and entirely adequate. Use this library when you need multiple crossed random effects, non-Normal likelihoods, or full posterior uncertainty.

Design decisions

Non-centered parameterization throughout. The centered version (u_i ~ Normal(0, sigma)) creates funnel geometry in the posterior when sigma is small - which is exactly the case for well-regularised insurance models. HMC cannot traverse the funnel efficiently. The non-centered version decouples the raw offsets from the scale and eliminates this problem. See Twiecki (2017) for the clearest exposition.

Segment-level input, not policy-level. This is the practical production design. NUTS does not scale linearly with observation count. A model with 10,000 rating cells runs in minutes; a model with 1 million policy rows takes hours. Aggregate first.

HalfNormal variance hyperpriors, not HalfCauchy. HalfCauchy has heavy tails that allow unrealistically large random effects for thin cells - the opposite of the regularisation we want. HalfNormal (Gelman et al., 2013) produces appropriate shrinkage for insurance factors.

Frequency-severity split, not Tweedie. The split allows different pooling structures for frequency and severity. Young drivers have high frequency but similar severity to older drivers. A Tweedie cannot capture this. The Gamma likelihood handles attritional severity; model large claims separately with Pareto or log-normal.

PyMC optional. The library parses and validates data without PyMC. Tests for the data layer run in CI without it. This makes the library usable in environments where PyMC is hard to install.

References

  1. Bühlmann, H. (1967). Experience rating and credibility. ASTIN Bulletin, 4(3), 199–207.
  2. Gelman et al. (2013). Bayesian Data Analysis, 3rd ed. Chapter 5.
  3. Ohlsson, E. (2008). Combining generalised linear models and credibility models. Scandinavian Actuarial Journal.
  4. Krapu et al. (2023). Flexible hierarchical risk modeling for large insurance data via NumPyro. arXiv:2312.07432.
  5. Twiecki, T. (2017). Why hierarchical models are awesome, tricky, and Bayesian. twiecki.io.

Other Burning Cost libraries

Model building

LibraryDescription
shap-relativitiesExtract rating relativities from GBMs using SHAP
insurance-interactionsAutomated GLM interaction detection via CANN and NID scores
insurance-cvWalk-forward cross-validation respecting IBNR structure

Uncertainty quantification

LibraryDescription
insurance-conformalDistribution-free prediction intervals for Tweedie models
credibilityBühlmann-Straub credibility weighting

Deployment and optimisation

LibraryDescription
rate-optimiserConstrained rate change optimisation with FCA PS21/5 compliance
insurance-demandConversion, retention, and price elasticity modelling

Governance

LibraryDescription
insurance-fairnessProxy discrimination auditing for UK insurance models
insurance-causalDouble Machine Learning for causal pricing inference
insurance-monitoringModel monitoring: PSI, A/E ratios, Gini drift test

Spatial

LibraryDescription
insurance-spatialBYM2 spatial territory ratemaking for UK personal lines

All libraries →

About

Hierarchical Bayesian models for thin-data insurance pricing segments. PyMC 5, non-centered parameterization, credibility factors.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages