A data-agnostic backtesting toolbox for VaR and other risk measures
Project description
risk-backtest
A Python library for Value at Risk (VaR) model backtesting with statistical tests, cluster detection, and parallel batch processing.
Installation
pip install risk-backtest
For parallel processing support:
pip install risk-backtest[parallel]
Quick Start
import numpy as np
from risk_backtest import run_backtest, BacktestConfig
# Single fund
returns = np.random.normal(0, 0.01, 500)
var_series = np.full(500, 0.02)
result = run_backtest(returns, var_series, window_sizes=[250])
print(result.summary)
print(result.pass_rates)
Batch Mode
# Multiple funds in one call
returns_dict = {"Fund_A": returns_a, "Fund_B": returns_b}
var_dict = {"Fund_A": var_a, "Fund_B": var_b}
result = run_backtest(returns_dict, var_dict, window_sizes=[250, 500], n_jobs=4)
Custom Risk Measures
from risk_backtest import BacktestConfig
# Annual volatility backtesting (auto-scales to daily)
config = BacktestConfig(
risk_measure="volatility",
confidence_level=0.8413, # 1-sigma
horizon="annual",
)
result = run_backtest(returns, annual_vol, config=config, window_sizes=[252])
Statistical Tests
Six tests are applied per window:
| Test | What it measures |
|---|---|
| Binomial | Exact probability of observed breaches |
| Z-test | Normal approximation of breach frequency |
| Kupiec (LR-UC) | Unconditional coverage |
| Christoffersen (LR-IND) | Independence of breaches |
| Joint (LR-CC) | Combined coverage + independence |
| Martingale | Predictability in breach sequence |
Each test runs twice: once on raw breaches and once cluster-adjusted (avoiding double-counting correlated breaches).
Features
- Cluster detection: Identifies breach clusters to avoid inflating test statistics
- Parallelization: Process hundreds of funds efficiently with joblib or concurrent.futures
- Flexible horizons: Daily, weekly, monthly, annual with sqrt-T scaling
- Multiple risk measures: VaR, volatility, Expected Shortfall
- Expected Shortfall:
historical_es,normal_es,es_from_var_series - Regulatory helpers:
basel_traffic_lightfor Basel green/yellow/red zone classification
Regulatory Backtesting
from risk_backtest import basel_traffic_light
result = basel_traffic_light(breaches=6, n_obs=250)
print(result.zone) # 'yellow'
print(result.multiplier_addon) # 0.50
print(result.multiplier) # 3.50
License
MIT
Project details
Release history Release notifications | RSS feed
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 risk_backtest-0.1.0.tar.gz.
File metadata
- Download URL: risk_backtest-0.1.0.tar.gz
- Upload date:
- Size: 35.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21c1f93d8764eca9ad9c32841830168492da14becd84dd15cba1cc87622f3ec0
|
|
| MD5 |
9ab9ccf573b7a17e1893cdce68496f4d
|
|
| BLAKE2b-256 |
42b96ec42efb959afd1166ce8a4ec1d86a1f4ecc038e327ede7b78e726f28fa5
|
Provenance
The following attestation bundles were made for risk_backtest-0.1.0.tar.gz:
Publisher:
publish.yml on william0424/risk-backtest
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
risk_backtest-0.1.0.tar.gz -
Subject digest:
21c1f93d8764eca9ad9c32841830168492da14becd84dd15cba1cc87622f3ec0 - Sigstore transparency entry: 1710168628
- Sigstore integration time:
-
Permalink:
william0424/risk-backtest@3af29b361a048d914ab94ae84acd1c0d5abe18b6 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/william0424
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3af29b361a048d914ab94ae84acd1c0d5abe18b6 -
Trigger Event:
release
-
Statement type:
File details
Details for the file risk_backtest-0.1.0-py3-none-any.whl.
File metadata
- Download URL: risk_backtest-0.1.0-py3-none-any.whl
- Upload date:
- Size: 34.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f882b4b91937691b07a3dc0ad73145339cc8b08c9886d28238508455bea72651
|
|
| MD5 |
c4a105ff62eee7b9043451a6306684be
|
|
| BLAKE2b-256 |
d0202568fcd35affc037193250ec3b678cb5a150d2addbde4bcd5911a9f3df3c
|
Provenance
The following attestation bundles were made for risk_backtest-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on william0424/risk-backtest
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
risk_backtest-0.1.0-py3-none-any.whl -
Subject digest:
f882b4b91937691b07a3dc0ad73145339cc8b08c9886d28238508455bea72651 - Sigstore transparency entry: 1710168650
- Sigstore integration time:
-
Permalink:
william0424/risk-backtest@3af29b361a048d914ab94ae84acd1c0d5abe18b6 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/william0424
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3af29b361a048d914ab94ae84acd1c0d5abe18b6 -
Trigger Event:
release
-
Statement type: