Extreme-loss estimation and tail risk analytics in Python
Project description
extremeloss
extremeloss is a Python library for extreme-loss estimation, extreme value modeling, and tail-risk diagnostics.
It is designed to sit alongside:
lossmodelsfor actuarial loss distributions and aggregate modelingrisksimfor portfolio and contract-level simulation
The package focuses on the part of the loss distribution that is hardest to estimate well: the far tail.
Scope
extremeloss currently covers five main areas.
1. Rare-event and tail estimation
- empirical exceedance probability estimation
- empirical VaR and TVaR estimation
- conditional Monte Carlo summaries from precomputed conditional probabilities or tail expectations
- importance-sampling estimators for means, tail probabilities, exceedance curves, VaR, and TVaR
- effective sample size and weight diagnostics
2. Extreme value theory workflows
- peaks-over-threshold (POT) workflows
- generalized Pareto distribution (GPD) fitting
- block-maxima / generalized extreme value (GEV) workflows
- Hill and Pickands tail-index estimators
- threshold diagnostics and mean-excess analysis
3. Tail-risk analytics
- return periods and return levels
- summary tables for tail quantities
- exceedance-frequency views
4. Uncertainty estimation
- bootstrap uncertainty estimation for tail probabilities, VaR, and TVaR
- reusable bootstrap wrapper for scalar statistics
5. Ecosystem integration helpers
- duck-typed helpers for
lossmodels-style objects exposingsample(size) - duck-typed helpers for
risksim-style result objects exposinglosses,gross_losses,retained_losses, orceded_losses - component and layer tail summaries for simulation outputs
Why this library exists
Naive Monte Carlo works well in the center of a distribution but becomes inefficient when estimating very small tail probabilities or very high quantiles. extremeloss gives your ecosystem a clear third layer:
lossmodelsdefines or samples loss modelsrisksimsimulates portfolios and contract structuresextremelosshandles the far tail, EVT extrapolation, and tail diagnostics
That keeps the package distinct rather than turning it into another generic risk-measures library.
Installation
Editable install:
pip install -e .
Development install:
pip install -e .[dev]
Requirements
- Python 3.10+
- NumPy
- SciPy
- Matplotlib
Quick start
Empirical tail probability
import numpy as np
from extremeloss import estimate_tail_probability
rng = np.random.default_rng(123)
losses = rng.lognormal(mean=2.0, sigma=0.9, size=50_000)
result = estimate_tail_probability(losses, threshold=50.0)
print(result.summary())
POT / GPD workflow
import numpy as np
from extremeloss import fit_pot
rng = np.random.default_rng(123)
losses = rng.lognormal(mean=2.0, sigma=1.0, size=100_000)
fit = fit_pot(losses, threshold=40.0)
print(fit.summary())
print("Tail probability above 100:", fit.tail_probability(100.0))
print("VaR(0.995):", fit.var(0.995))
print("Return level for period 250:", fit.return_level(250.0))
Block-maxima / GEV workflow
import numpy as np
from extremeloss import fit_block_maxima
rng = np.random.default_rng(123)
losses = rng.pareto(a=3.0, size=36_500) * 20.0
fit = fit_block_maxima(losses, block_size=365)
print(fit.summary())
print("Return level for period 20:", fit.return_level(20.0))
Bootstrap uncertainty estimation
import numpy as np
from extremeloss import bootstrap_tail_probability
rng = np.random.default_rng(123)
losses = rng.lognormal(mean=2.0, sigma=0.9, size=8_000)
boot = bootstrap_tail_probability(losses, threshold=80.0, n_resamples=250, random_state=1)
print(boot.summary())
Risksim-style integration
from dataclasses import dataclass
import numpy as np
from extremeloss import tail_summary_from_risksim
@dataclass
class SimpleResult:
losses: np.ndarray
gross_losses: np.ndarray
retained_losses: np.ndarray
ceded_losses: np.ndarray
rng = np.random.default_rng(123)
losses = rng.gamma(shape=2.0, scale=30.0, size=25_000)
result = SimpleResult(
losses=losses,
gross_losses=losses,
retained_losses=0.9 * losses,
ceded_losses=0.1 * losses,
)
print(tail_summary_from_risksim(result, view="retained", thresholds=[20, 40, 60]))
Main API
Estimation
estimate_tail_probabilityestimate_varestimate_tvarestimate_var_tvarestimate_tail_probability_cmcestimate_tvar_cmcestimate_mean_isestimate_tail_probability_isestimate_exceedance_curve_isestimate_var_isestimate_tvar_isestimate_var_tvar_iseffective_sample_sizeimportance_sampling_diagnosticslog_importance_weightsstabilize_weights
EVT
extract_exceedancesfit_gpdfit_potgpd_tail_probabilitygpd_vargpd_tvarmake_blocksfit_gevfit_block_maximablock_return_levelhill_estimatorpickands_estimatorhill_curvemean_excessthreshold_diagnostic_table
Analytics
return_periodreturn_levelexceedance_frequencyextreme_loss_summaryvar_tvar_diagnostic_table
Bootstrap utilities
bootstrap_statisticbootstrap_tail_probabilitybootstrap_varbootstrap_tvar
Integration helpers
sample_lossmodelfit_pot_from_lossmodellosses_from_risksimtail_summary_from_risksimcomponent_tail_metricslayer_tail_metrics
Result objects
TailEstimateResultGPDFitGEVFitBootstrapResultThresholdScan
Package layout
extremeloss/
├── README.md
├── pyproject.toml
├── docs/
├── examples/
├── tests/
└── src/extremeloss/
├── __init__.py
├── analytics/
│ ├── __init__.py
│ ├── diagnostics.py
│ └── return_periods.py
├── estimation/
│ ├── __init__.py
│ ├── conditional_mc.py
│ ├── importance_sampling.py
│ ├── metrics.py
│ └── rare_event.py
├── evt/
│ ├── __init__.py
│ ├── block_maxima.py
│ ├── gpd.py
│ ├── pot.py
│ ├── tail_index.py
│ └── thresholds.py
├── integration.py
├── plotting.py
├── protocols.py
├── results.py
└── utils/
├── __init__.py
├── bootstrap.py
└── validation.py
Design principles
Array-first API
Most functions work directly on one-dimensional arrays of losses.
Duck-typed interoperability
The package stays standalone, but convenience helpers accept objects that behave like your other packages:
sample(size)forlossmodels-style modelslosses/gross_losses/retained_losses/ceded_lossesforrisksim-style results
Lightweight result containers
Estimators and fitted models return structured objects rather than raw scalars only.
Focus on the far tail
extremeloss is meant to specialize in extreme-region estimation and diagnostics, not replace general-purpose simulation or loss-distribution libraries.
Documentation
Repository markdown docs are included under docs/.
Suggested reading order:
docs/guides/getting-started.mddocs/guides/package-overview.mddocs/guides/design.mddocs/guides/integration.mddocs/examples/README.mddocs/api/reference.md
Main entry point:
docs/index.md
Examples
Runnable scripts are included in examples/.
Core workflows
empirical_tail_analysis.pypot_gpd_workflow.pyimportance_sampling_demo.pydiagnostic_plots.py
Extended workflows
conditional_mc_demo.pyblock_maxima_gev_workflow.pybootstrap_uncertainty_demo.pyintegration_helpers_demo.py
Run them from the repository root:
python examples/empirical_tail_analysis.py
python examples/pot_gpd_workflow.py
python examples/importance_sampling_demo.py
python examples/conditional_mc_demo.py
python examples/block_maxima_gev_workflow.py
python examples/bootstrap_uncertainty_demo.py
python examples/integration_helpers_demo.py
python examples/diagnostic_plots.py
The scripts add src/ to sys.path, so they can run directly from the repo without requiring a prior editable install.
Generated plot images are written to examples/output/.
Testing
Run the full suite with:
pytest -q
Install development dependencies first if needed:
pip install -e .[dev]
Troubleshooting editable installs
If you add new modules or result classes and Python still imports an older installed copy, clear caches and reinstall:
find . -type d -name __pycache__ -prune -exec rm -rf {} +
find . -type f -name '*.pyc' -delete
pip uninstall -y extremeloss
pip install -e .
To verify the active import path:
python -c "import extremeloss; import extremeloss.results as r; print(extremeloss.__file__); print(r.__file__)"
Roadmap ideas
Natural next areas to add include:
- more specialized conditional Monte Carlo methods for aggregate-loss models
- more advanced importance-sampling strategies for compound losses
- richer EVT diagnostics and threshold-selection tools
- multivariate extremes and tail dependence
- direct optional adapters for installed
lossmodelsandrisksim
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file extremeloss-0.1.1.tar.gz.
File metadata
- Download URL: extremeloss-0.1.1.tar.gz
- Upload date:
- Size: 20.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
90774cde338aa6650a9c4c3928e40b6d5ca34c66a173ab3ac36435a9f5a793b8
|
|
| MD5 |
8654fa5ac072ce1743e0a1bf38555b56
|
|
| BLAKE2b-256 |
5802e8b95274dbdb072db91a625a592bbfd2860e90bbac2b3c729f3e48df9f13
|
File details
Details for the file extremeloss-0.1.1-py3-none-any.whl.
File metadata
- Download URL: extremeloss-0.1.1-py3-none-any.whl
- Upload date:
- Size: 22.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e36fa060a717156b40830331d3e1cf9d1d62e4a8f5f52df52d51ad6de97d69f
|
|
| MD5 |
f075d4d5a2b19607061c7fee5f242474
|
|
| BLAKE2b-256 |
9d51252786e3f2efabaa501c73b4fa8f365984e610fdd92d6819250407ab313d
|