Skip to main content

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_light for 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

risk_backtest-0.1.0.tar.gz (35.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

risk_backtest-0.1.0-py3-none-any.whl (34.0 kB view details)

Uploaded Python 3

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

Hashes for risk_backtest-0.1.0.tar.gz
Algorithm Hash digest
SHA256 21c1f93d8764eca9ad9c32841830168492da14becd84dd15cba1cc87622f3ec0
MD5 9ab9ccf573b7a17e1893cdce68496f4d
BLAKE2b-256 42b96ec42efb959afd1166ce8a4ec1d86a1f4ecc038e327ede7b78e726f28fa5

See more details on using hashes here.

Provenance

The following attestation bundles were made for risk_backtest-0.1.0.tar.gz:

Publisher: publish.yml on william0424/risk-backtest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for risk_backtest-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f882b4b91937691b07a3dc0ad73145339cc8b08c9886d28238508455bea72651
MD5 c4a105ff62eee7b9043451a6306684be
BLAKE2b-256 d0202568fcd35affc037193250ec3b678cb5a150d2addbde4bcd5911a9f3df3c

See more details on using hashes here.

Provenance

The following attestation bundles were made for risk_backtest-0.1.0-py3-none-any.whl:

Publisher: publish.yml on william0424/risk-backtest

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page