Metadata-Version: 2.5
Name: fhs-market-risk
Version: 0.1.0
Summary: Zero-network Filtered Historical Simulation (EWMA FHS), exact additive Expected Shortfall, causal walk-forward Kupiec/Christoffersen VaR backtesting, and base-currency FX translation sensitivity
Project-URL: Homepage, https://github.com/benjamin-di/fhs-market-risk
Project-URL: Issues, https://github.com/benjamin-di/fhs-market-risk/issues
Author: benjamin-di
License-Expression: MIT
License-File: LICENSE
Keywords: backtest,christoffersen,ewma,expected-shortfall,fhs,finance,kupiec,market-risk,var
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Financial and Insurance Industry
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Office/Business :: Financial :: Investment
Requires-Python: >=3.11
Requires-Dist: eod-market-data<1,>=0.1.0
Requires-Dist: numpy<3,>=1.26
Requires-Dist: pandas<3.1,>=2.2
Description-Content-Type: text/markdown

# fhs-market-risk

Zero-network **Filtered Historical Simulation (EWMA FHS)**, exact fractional-probability **Expected Shortfall (ES)** with additive component attribution, strictly causal **walk-forward Kupiec/Christoffersen VaR backtesting**, and **base-currency FX translation sensitivity**.

Built to pair cleanly with [`eod-market-data`](https://pypi.org/project/eod-market-data/) (or used directly on `numpy` return matrices).

## Features

- **Empirical VaR & Exact Additive Expected Shortfall** (`tail_statistics`): Inverse-CDF VaR and exact fractional-probability tail mean so component ES values sum identically to portfolio ES (preserving negative/hedging contributions).
- **Causal EWMA Filtered Historical Simulation** (`filtered_scenarios`): Zero-mean log-return EWMA with a strictly causal expanding second-moment variance floor (zero look-ahead) that preserves joint cross-asset shock dates.
- **Walk-Forward VaR Backtesting** (`backtest_var`, `kupiec_pof_test`, `christoffersen_test`): Strictly causal out-of-sample VaR exceedances with exact Kupiec POF ($\chi^2_1$), Christoffersen independence ($\chi^2_1$), and conditional coverage ($\chi^2_2$) tests — zero `scipy` runtime dependency.
- **Calendar- & Split-Aware Book Engine** (`build_market_risk`): Replays dated joint shocks on current exposures using `eod-market-data` history, respecting exchange calendars, split divisors, gross dividends, futures notional, and explicit exclusion diagnostics (missing exposures never become zero risk).
- **Base-Currency FX Translation Sensitivity** (`sensitivity`, `implied_fx_rates`, `product_currency`): One-factor balance-sheet FX shocks with NAV reconciliation for any base currency (`EUR`, `USD`, `GBP`, etc.).

## Installation

```bash
pip install fhs-market-risk
```

## Quick Start

```python
import numpy as np
from fhs_market_risk import backtest_var, filtered_scenarios, tail_statistics

rng = np.random.default_rng(42)
returns = rng.normal(0.0, 0.012, size=(250, 2))
exposure = np.array([60_000.0, 40_000.0])

# 1D Filtered Historical Simulation scenarios
filtered_log, forecast_vol = filtered_scenarios(np.log1p(returns))
scenario_pnl = np.expm1(filtered_log) * exposure
stats = tail_statistics(scenario_pnl, confidence=0.975)
print(stats["var"], stats["es"], stats["components"])

# Strictly causal walk-forward VaR backtest
bt = backtest_var(returns, exposure, confidence=0.95)
print(bt["status"], bt["exceedances"], bt["observations"], bt["p_value_pof"], bt["p_value_cc"])
```

## CLI

```bash
fhs-risk backtest returns.csv --confidence 0.95 --weights 60000,40000
```

## License

MIT — see [LICENSE](LICENSE).
