Skip to main content

Model Risk Validator for Model Risk Governance

Project description

mrv-lib: Model Risk Validator

Regime model diagnostics for banks and financial institutions.

mrv tests whether your market regime model produces stable, reliable labels — or whether they silently depend on undisclosed modelling choices like feature selection, temporal resolution, or rolling window parameters. Built for SR 11-7 / Basel IV model risk governance.

What it does

Test Question Status
Representation Invariance Do regime labels change when you use different risk factors? Implemented
Resolution Invariance Do labels agree across 5m / 1h / 1d frequencies? Planned
Temporal Stability Do labels persist across rolling windows? Planned

Install

pip install mrv-lib

# Optional: IB data download
pip install mrv-lib[ib]

# Optional: regime models (GMM/HMM)
pip install mrv-lib[validator]

# Everything
pip install mrv-lib[all]

Quick start

# 1. Download data (requires IB Gateway running)
python run.py download config.yaml

# 2. Run representation invariance test + generate PDF
python run.py run config.yaml rep

# 3. Regenerate PDF from existing results
python run.py report

Or from Python:

from mrv.pipeline import run, download

download("config.yaml")             # fetch data from IB
run("config.yaml", "rep")           # validate + PDF

# Step by step (full control)
from mrv.pipeline import load_data, compute_factors, fit_labels, validate, report

cfg = load("config.yaml")
prices = load_data(cfg, "rep")
# ... user can replace any step

Configuration

All settings in one config.yaml:

download:
  data_dir: data
  symbols: [SPY, USDJPY, CL=F, IEF, GLD]
  freq: [5m, 15m, 1h, 1d]
  start: "2026-01-01"
  ib:
    host: 127.0.0.1
    port: 4002

validator:
  rep:
    assets:
      SPY: data/SPY_5m.csv
      CL:  data/CL_5m.csv
    model: gmm
    factors:
      - [vol, drawdown, maxdd, var, cvar]
      - [vol, drawdown, var, cvar]
      - [real_skew, vol_stab, var, cvar]

Custom factors and models

from mrv.data.factors import register_factor

def momentum(returns, price, windows):
    return price.pct_change(windows.get("mom_window", 20)).rename("momentum")

register_factor("momentum", momentum)
from mrv.models import register_model

def my_kmeans(features, n_states, **kwargs):
    from sklearn.cluster import KMeans
    return KMeans(n_clusters=n_states).fit_predict(features.values)

register_model("kmeans", my_kmeans)

Project layout

mrv-lib/
├── config.yaml              # Configuration
├── run.py                   # CLI entry point
├── templates/
│   └── template.tex         # LaTeX report template
├── src/mrv/
│   ├── pipeline.py          # data → factors → model → validate → report
│   ├── data/
│   │   ├── reader.py        # Load, validate, resample OHLCV
│   │   ├── factors.py       # Factor registry + built-in risk factors
│   │   └── normalize.py     # Rolling z-score, min-max
│   ├── models/
│   │   ├── __init__.py      # Model registry + fit()
│   │   ├── gmm.py           # Gaussian Mixture Model
│   │   └── hmm.py           # Hidden Markov Model
│   ├── validator/
│   │   ├── base.py          # BaseValidator (subclass for custom tests)
│   │   ├── rep.py           # Representation Invariance test
│   │   ├── metrics.py       # ARI, AMI, NMI, Spearman, VI
│   │   └── report.py        # JSON → LaTeX → PDF
│   └── utils/
│       ├── config.py        # YAML config loading
│       ├── download.py      # IB data download
│       └── log.py           # Logging setup
├── reports/                  # Output (gitignored)
│   └── mrv_report_YYYYMMDD_rep/
│       ├── result.json
│       ├── report.pdf
│       └── {asset}.png
└── tests/

Output

Each run creates a timestamped directory under reports/:

  • result.json — Complete data (reusable for report regeneration)
  • report.pdf — Professional report with cover page, dashboard, heatmaps, and remediation plan
  • summary.txt — Plain text quick view
  • {asset}.png — ARI heatmap per asset

Research

Based on the following PhD research:

  • Zheng, Low & Wang (2026). Regime Labels Are Not Representation-Invariant. Finance Research Letters.
  • Zheng, Low & Wang (2026). Regime Labels Are Not Resolution-Invariant. Working paper.

License

MIT. See LICENSE.

Maintainers

ModelGuard Lab — Author: Kai Zheng.

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

mrv_lib-0.1.0.tar.gz (34.0 kB view details)

Uploaded Source

Built Distribution

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

mrv_lib-0.1.0-py3-none-any.whl (31.5 kB view details)

Uploaded Python 3

File details

Details for the file mrv_lib-0.1.0.tar.gz.

File metadata

  • Download URL: mrv_lib-0.1.0.tar.gz
  • Upload date:
  • Size: 34.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mrv_lib-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e035fd2ebb64a7d5113daa99ddbc0f311d156ed148a0d8cd0d581b314bb6e640
MD5 1e9aaaab1e311a186ef7c03945273388
BLAKE2b-256 2ff302f5ef50a894982bf61211e47ed48f0a87cfac7286807edd5907b486452b

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on modelguard-lab/mrv-lib

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

File details

Details for the file mrv_lib-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: mrv_lib-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 31.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for mrv_lib-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 626a0ab2108a7c4056032d3a6ec446a1349b4ef4b0950d581421d2680f86025e
MD5 ac1f245e8271f2811627947610210d17
BLAKE2b-256 254643519ae0024c964ad4c529a887aef35bcd35d513f2e710c7cf6ef6a9d6fe

See more details on using hashes here.

Provenance

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

Publisher: publish.yml on modelguard-lab/mrv-lib

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