Skip to main content

A minimal Python package for Value at Risk, Expected Shortfall

Project description

varlab

Python NumPy SciPy Value at Risk Expected Shortfall

Python package for Value at Risk and Expected Shortfall.

📦 Installation

Install from PyPI:

pip install varlab

🧰 What varlab Provides

varlab focuses on practical market risk workflows:

  • Value at Risk (var)
  • Expected Shortfall (es)
  • Rolling and expanding estimators (rolling_var, rolling_es, expanding_var, expanding_es)
  • VaR backtesting diagnostics with a concise ASCII report and structured outputs
  • PIT-based distribution diagnostics (including randomized PIT for empirical/discrete cases)

Both VaR and ES support:

  • method="empirical" (historical)
  • method="parametric" with distribution="normal" or distribution="t"
  • Optional exponential weighting in empirical mode via lamb
  • 1-day and multi-day horizons (n_days)
  • Optional sample mean component (mean="sample")

⚡ Quick Start

from varlab import rolling_var

var_estimates = rolling_var(
    returns,
    window=60,
    method="empirical",
    confidence=0.99,
    n_days=1,
    distribution="normal",
    mean="sample",
).shift(1).fillna(0)

Parametric Example (Student-t)

If df is not provided, degrees of freedom are estimated by MLE.

from varlab import var, es

var_t = var(
    returns,
    method="parametric",
    distribution="t",
    confidence=0.99,
    mean="sample",
)

es_t = es(
    returns,
    method="parametric",
    distribution="t",
    confidence=0.99,
    mean="sample",
)

Exponentially Weighted Historical Risk

For empirical VaR/ES you can apply exponential weighting by setting lamb:

from varlab import var, es

var_ew = var(returns, method="empirical", confidence=0.99, lamb=0.97)
es_ew = es(returns, method="empirical", confidence=0.99, lamb=0.97)

Backtesting Suite

Run a full backtesting diagnostic:

from varlab import backtesting

back_res = backtesting.diagnostic.run(
    returns=returns,
    exceedances=exceedances,
    confidence=0.99,
    window_type="rolling",
    pit_case="discrete",
    alpha=0.05,
    window=60,
)

Example report:

==================================================================
                  VALUE AT RISK BACKTEST REPORT
==================================================================
Confidence level: 99.00% | Sample size: 4445 | Date: 2026-03-08
Overall result: FAIL

COVERAGE (4/4)
------------------------------------------------------------------
Exact Binomial                                          PASS
Kupiec Pof                                              PASS
Christoffersen Conditional                              PASS
Basel Traffic Light                                     PASS

DISTRIBUTION (0/3)
------------------------------------------------------------------
Uniformity                                              FAIL
Independence                                            FAIL
Berkowitz                                               FAIL

INDEPENDENCE (2/2)
------------------------------------------------------------------
Christoffersen                                          PASS
Loss Quantile                                           PASS

The diagnostic object is typed and machine-friendly, so you can inspect details programmatically:

back_res.results['independence']["christoffersen"]

Test Families Included

  • Coverage: exact binomial, Kupiec POF, Christoffersen conditional coverage, Basel traffic light (when confidence is 99%)
  • Distribution: PIT-based diagnostics (uniformity, independence, Berkowitz)
  • Independence: Christoffersen exceedance independence, loss-quantile independence

Probability Integral Transform (PIT)

Distribution diagnostics rely on PIT values:

  • pit_case="continuous" for parametric settings
  • pit_case="discrete" for empirical settings (randomized PIT is used)

Window logic can be:

  • window_type="rolling"
  • window_type="expanding"

Public API

from varlab import (
    var,
    es,
    rolling_var,
    rolling_es,
    expanding_var,
    expanding_es,
    backtesting,
)

Notes

  • Inputs can be plain arrays/lists or pandas objects.
  • Risk values are returned as positive loss magnitudes.
  • For multi-asset parametric workflows, pass a return matrix and portfolio weights.
  • Shift rolling/expanding estimates by one step when using them for out-of-sample backtests.

License

MIT © 2025 — Developed with ❤️ by Lorenzo Santarsieri

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

varlab-0.1.1.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

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

varlab-0.1.1-py3-none-any.whl (30.4 kB view details)

Uploaded Python 3

File details

Details for the file varlab-0.1.1.tar.gz.

File metadata

  • Download URL: varlab-0.1.1.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for varlab-0.1.1.tar.gz
Algorithm Hash digest
SHA256 21cef08ec67c5d650366e142a2da3528daf6a0c2d140fd518099932639b7fe1c
MD5 3b34353158460aac9816cfccb5d31d4d
BLAKE2b-256 f1f7382f44edb01f7c8f88c7e2aa9560686738990329d8fbc36a5cc700945f41

See more details on using hashes here.

File details

Details for the file varlab-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: varlab-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 30.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.19

File hashes

Hashes for varlab-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 900ae421447d97d750351272741a7f2c07b5531a48d30f9b7deadf1ed96c0d7e
MD5 4caf3348e9740eb80e8a421f246c99f0
BLAKE2b-256 d9c15a3db3267b33b4357cbf68eb68531fd960db074dce6279614d04823283d4

See more details on using hashes here.

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