prospicio
prospicio is a library for actuarial and risk models. The core is in Rust. The Python and R packages call the same Rust code, so all three languages give the same numbers.
What prospicio does
- Reserving. Make loss triangles. Estimate reserves with Chain Ladder, Mack, Bornhuetter–Ferguson, Cape Cod, Clark, the ODP bootstrap and the Bayesian chain ladder. Calculate the one-year view for capital.
- Aggregate losses and reinsurance. Combine a claim count and a claim size into an aggregate loss distribution. Apply layers, towers, reinstatements and other treaty terms.
- Pricing. Price a layer or a tower with a risk load. Calculate credibility and the overall rate indication.
- Distributions. Use parametric distributions, copulas and extreme value methods. Save a distribution as JSON and load it again.
- Models. Fit GLMs, GAMs, elastic nets, neural networks and Bayesian models through one model interface.
The tests in validation/ compare the results with reference
implementations, for example R ChainLadder, chainladder-python, actuar,
SciPy and statsmodels.
How the crates work together
An arrow means "uses". All arrows point down, so the crates have no dependency cycles.
Foundation
prospicio-coreholds the error type, the random-number streams and the calendar periods. All crates use it.prospicio-mathis the numerical engine: special functions, linear algebra, splines and root finding.prospicio-probholds the distributions andPredictiveDistribution. The method crates read and write these types, so the result of one crate can go into another crate.
Actuarial methods
prospicio-aggregatebuilds aggregate loss distributions and reinsurance towers.prospicio-pricingprices treaties from theaggregatemodels. It also holds credibility and ratemaking.prospicio-modelsdefines the interface that every model engine uses.prospicio-glmfits GLMs, GAMs and elastic nets through that interface.prospicio-bayessamples Bayesian models with NUTS and checks the draws.prospicio-nntrains neural networks with the Burn library.prospicio-reservingholds theTriangleand the reserving methods. It usesglmfor the ODP GLM. It usesbayesonly when you turn on thebayesfeature.
Packages
prospiciois the Rust umbrella crate. It re-exports the other crates. Thebayes,nnandarrowfeatures are optional.prospicio-python(withpython/) andprospicio-r(withR/prospicio) wrap the Rust code for Python and R. Neither package includesnnat this time.
Example: the price of a reinsurance layer
probsupplies a claim size distribution and a claim count distribution.aggregatecombines them into the aggregate loss distribution of the layer.pricingadds the risk load and gives the price.- The Python or R package returns the result to you.
Where to read more
| Path | What |
|---|---|
| docs/guides/ | Guides for actuaries, for example how a reinsurer arrives at a price |
| examples/ | Notebooks that use the Python package from start to end |
| docs/architecture.md | The plan for the library |
| docs/design/ | Design notes for the core abstractions |
| validation/ | Reference datasets and the parity tests |
| knowledge/ | Facts collected while building the library |
Build and test
Prerequisites
| macOS / Linux | Windows (PowerShell) | |
|---|---|---|
| Rust ≥ 1.85 | curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh |
winget install Microsoft.VisualStudio.2022.BuildTools --override "--quiet --wait --add Microsoft.VisualStudio.Workload.VCTools --includeRecommended" then winget install Rustlang.Rustup |
| uv (Python) | curl -LsSf https://astral.sh/uv/install.sh | sh |
winget install astral-sh.uv |
| R ≥ 4.2 | from CRAN | R from CRAN, Rtools matching your R version, then rustup target add x86_64-pc-windows-gnu |
| Quarto (docs only) | from quarto.org | winget install Posit.Quarto |
Open a new terminal after installing so cargo, uv and R are on PATH.
Rust
All pure-Rust crates and the parity suite:
Python (uv)
uv run rebuilds the extension automatically when Rust sources change.
R
Without a checkout, with Rust (cargo, rustc 1.85 or later) installed:
remotes::
remotes builds a source tarball of R/prospicio; the package's
bootstrap.R first copies the Rust crates it needs into src/rust/, so the
tarball compiles on its own (third-party crates still come from crates.io).
From a checkout, at the repository root (a terminal where cargo --version works). The
package's R API is built on S7, so
install that first:
for; do ; done
Windows PowerShell: R is PowerShell's alias for Invoke-History, so
call R.exe CMD INSTALL R\prospicio instead. To put R on PATH for your
user (newest installed R; rerun after upgrading R), then open a new window:
$rbin = (Get-ChildItem "C:\Program Files\R" -Directory | Sort-Object Name | Select-Object -Last 1).FullName + "\bin"
[Environment]::SetEnvironmentVariable("Path", [Environment]::GetEnvironmentVariable("Path", "User") + ";$rbin", "User")
On Windows, R builds packages with Rtools' MinGW compiler, so the Rust code
is compiled for the x86_64-pc-windows-gnu target (see
R/prospicio/src/Makevars.win); the build stops with a message if that
target is not installed.
Docs
Building a binding regenerates its docs (see "Documentation" in docs/architecture.md). Use these instead of the plain builds above whenever you change a doc comment or a public API:
The Python docs need Python 3.11+ (great-docs); cargo xtask python asks uv
for one. Commit the regenerated python/prospicio/prospicio_native.pyi,
R/prospicio/man/ and R/prospicio/NAMESPACE; never edit them by hand.
cargo xtask docs --check fails if they are stale.
The same distribution, driven by the same Rust code, from all three:
=
,
d <-
;
d@meanlog; d@sdlog # read-only S7 properties
Errors raised in Rust surface as Python ValueErrors and ordinary R errors.
Reserving
prospicio-reserving holds the four-axis, masked Triangle
(docs/design/triangle.md), development factors,
ChainLadder, Mack and the ODP bootstrap (OdpBootstrap), whose reserve
distribution by origin is a PredictiveDistribution.
use ;
let origin = .map;
let tri = from_long?;
let cl = default.fit?;
let mack = default.fit?;
assert!;
println!;
Python and R expose the same objects:
=
.
=
# a PredictiveDistribution keyed by origin
tri <-
@total_standard_error
boot <-
# a predictive_distribution keyed by origin
Segments are named by key columns. keys (Rust Long::keys, Python
keys=, R keys =) lists them, and the long table comes back with them
by name:
=
, # ['lob', 'state'], [('Auto', 'CA'), ...]
# columns lob, state, origin, development, paid, incurred
tri <-
tri@keys; tri@index # a data.frame with columns lob and state
# columns lob, state, origin, development, paid, incurred
Select segments and columns by key name, and sum segments over keys:
let auto = tri.select?;
let paid = tri.select_columns?;
let by_lob = tri.group_by?; // sums states; group_by(&[]) is the total
R uses base generics (subset(), aggregate() with keep, as for a
predictive_distribution) so it does not mask dplyr's filter(),
select() or group_by().
Methods fit every segment at once, each on its own, and give long
results with the key columns by name: one row per segment and origin,
per segment (totals), or per segment and age (development factors). The
bootstrap gives one joint PredictiveDistribution with the keys and
origin as dimensions, so aggregating over origins keeps the dependence
between segments:
let fits = default.fit_segments?;
let by_origin = fits.to_long; // keys, origin, latest, ..., standard_error
let totals = fits.totals; // one row per segment
let factors = fits.development_table; // keys, age, ldf, cdf, sigma, std_err
let auto_ca = fits.segment?;
let boot = default.fit_segments?;
let by_lob = boot.reserves.aggregate?;
= # any number of segments
; ;
.
fit <-
; ;
@ldf
Per-origin fields (reserve, standard_error, ...) follow the rows of
the long table, so a single-segment fit reads as before. Per-age fields
(ldf, cdf, sigma, std_err), Mack's total standard errors and the
bootstrap's scale, fitted and residuals need one segment; with
several they raise an error that points to the frames or segment().
To read a triangle, view one segment and measure as an origin × development table, or summarise every segment and measure. A triangle with one segment and one measure prints as that table; otherwise it prints the summary, one row per segment and measure with its origins, valuation and latest diagonal total. The text comes from Rust, so both bindings print the same thing:
let grid = tri.view?;
let rows = tri.summary.rows; // n_origins, first/last origin, valuation, latest
println!; // the grid or the summary table
# DataFrame: origins x ages
# one row per segment and measure
# prints the grid or the summary
# origins x ages
# a data.frame
tri # same printout
R uses as.matrix() rather than a view() verb, which would mask
tibble::view().
Every Chain Ladder and Mack value is checked against R ChainLadder and
chainladder-python on RAA, GenIns and ABC in validation/; the ODP
bootstrap is checked against R BootChainLadder.
Licence
Licensed under either of Apache License, Version 2.0 or MIT licence, at your option. Copyright (c) 2026 Ethan Kang.