Tail-Risk-Toolkit
# Tail-Risk-Toolkit
An MCP server that gives an LLM agent a set of extreme value statistics tools for tail risk: peaks-over-threshold GPD fitting, Value-at-Risk and Expected Shortfall, return levels, threshold-stability diagnostics, and out-of-sample VaR backtesting. A small retrieval-augmented generation (RAG) layer lets the agent answer questions from your own documents, with citations.
## Why
Extreme value theory is the same machinery whether the tail is a flood or a portfolio loss. A 100-year return level and a 99% VaR on annual data are the same quantile of the same fitted distribution. This server exposes that machinery as agent-callable tools, so a model can be asked "how heavy is this tail, and does the risk model hold up out of sample?" and answer it with a real estimator rather than a plausible-sounding number.
The tools are deliberately opinionated about statistical practice:
- Threshold choice is the main judgement call in a POT analysis, so `tailrisk_threshold_stability` exists to make an agent check it rather than accept a single fit.
- `tailrisk_var_es` returns EVT, historical and Gaussian estimates side by side, because the gap between them is the argument for using a tail model at all.
- Backtests are strictly out of sample: every forecast is fitted on trailing data only.
- Errors explain what to do next ("lower the threshold", "the sample is too short at this confidence level") instead of failing silently or returning a fit that should not be trusted.
## Tools
| Tool | Purpose |
| --- | --- |
| `tailrisk_fit_gpd` | Fit a generalised Pareto distribution to threshold exceedances |
| `tailrisk_var_es` | VaR and Expected Shortfall by EVT, historical simulation and Gaussian baseline |
| `tailrisk_return_level` | Level exceeded once per return period, with the equivalent VaR confidence |
| `tailrisk_backtest_var` | Rolling-window backtest with Kupiec, Christoffersen and conditional coverage tests |
| `tailrisk_threshold_stability` | Refit across candidate thresholds to check the fit is in the asymptotic regime |
| `tailrisk_load_csv_series` | Read a numeric column from a local CSV so a series can be analysed |
| `tailrisk_add_document` | Add a local `.txt`, `.md` or `.pdf` file to the knowledge base |
| `tailrisk_search_knowledge` | Retrieve the passages that answer a question, with source and chunk IDs for citation |
| `tailrisk_list_documents` | List indexed documents and the search mode in use |
The analytics tools are read-only and side-effect free. The knowledge-base tools only change the in-memory index; nothing is written to disk.
## Install
```bash
git clone https://github.com/John-Amal/Tail-Risk-Toolkit.git
cd Tail-Risk-Toolkit
python -m venv .venv && source .venv/bin/activate
pip install -e ".[dev]"
pytest
```
Requires Python 3.10+ and MCP SDK 2.x.
## Connect to an MCP client
Add to your client's server configuration (for Claude Desktop, `claude_desktop_config.json`):
```json
{
"mcpServers": {
"tailrisk": {
"command": "/absolute/path/to/Tail-Risk-Toolkit/.venv/bin/python",
"args": ["-m", "tailrisk_mcp.server"]
}
}
}
```
The server speaks stdio. Use `mcp.run(transport="streamable-http")` in `server.py` for a remote deployment.
## Worked example
On 5,000 draws from a Student-t with 4 degrees of freedom, asking for the 99.9% loss quantile:
| Method | VaR (99.9%) | Expected Shortfall |
| --- | --- | --- |
| EVT (POT, threshold at the 95th percentile) | 7.52 | 9.97 |
| Historical simulation | 8.51 | 10.02 |
| Gaussian | 4.41 | not defined here |
The true value is 7.17. The Gaussian figure understates it by 39%, which is the practical point: at the 99.9% level a normality assumption discards most of the risk. Historical simulation happens to land close here but cannot go beyond the largest observed loss at all, so it stops working exactly where the question gets interesting.
Reproduce with:
```bash
python examples/demo.py
```
## Knowledge base (RAG)
The agent can ground its answers in your own material: methodology notes, model documentation, validation reports, desk procedures. Drop `.txt`, `.md` or `.pdf` files into `knowledge/` (or set `TAILRISK_KNOWLEDGE_DIR`), or add a file during a conversation with `tailrisk_add_document`. The repository ships four short notes describing this toolkit's own methods, so it works from a clean clone.
How it works:
1. **Ingest and chunk.** Each document is split into passages of about 900 characters, keeping paragraphs intact, with a short overlap so a statement that straddles a boundary is still found.
2. **Retrieve.** Keyword search uses BM25 (implemented with numpy, no extra dependency). Set `TAILRISK_EMBEDDING_MODEL=all-MiniLM-L6-v2` (after `pip install -e ".[rag]"`) to add semantic search with sentence embeddings; the two rankings are then combined by reciprocal rank fusion (hybrid search).
3. **Guardrail.** A passage is returned only if it covers at least a quarter of the query's information (IDF-weighted terms) or, in hybrid mode, passes an embedding-similarity threshold. When nothing qualifies, the tool returns no passages and tells the agent not to answer from general knowledge as if it came from the documents.
4. **Generate.** The MCP client's LLM writes the answer from the returned passages and cites them by `source` and `chunk_id`.
Retrieval is evaluated on a labelled set of 16 questions, each worded differently from the document that answers it (`tests/test_knowledge.py`, run in `pytest`):
| Mode | Hit rate @1 | Hit rate @3 | MRR |
| --- | --- | --- | --- |
| Keyword (BM25) | 0.94 | 1.00 | 0.97 |
Off-topic queries such as "capital of France" or "mortgage interest rates" return no passages. The keyword guardrail can still be fooled by a single shared word ("football results" matches the backtesting note on "results"), which is the case semantic search is there to handle.
```bash
python examples/rag_demo.py
```
## Methods
Exceedances above a threshold `u` are modelled with a GPD by maximum likelihood, giving shape `xi` and scale `sigma`. The tail is
```
P(X > z) = zeta_u * [1 + xi (z - u) / sigma] ** (-1 / xi)
```
where `zeta_u` is the empirical exceedance rate. Inverting it gives both VaR (set the exceedance probability to `1 - q`) and return levels (set it to `1 / T`). Expected Shortfall follows in closed form as `(VaR + sigma - xi*u) / (1 - xi)` and is reported as undefined when `xi >= 1`, since the tail then has no finite mean.
Backtesting uses the Kupiec proportion-of-failures test for breach frequency and the Christoffersen test for breach independence, combined into a conditional coverage statistic.
References: Coles (2001); McNeil and Frey (2000); Kupiec (1995); Christoffersen (1998).
## Testing
`pytest` runs 28 tests: 17 for the estimators and 11 for the knowledge base. The statistical ones simulate from distributions with known tail behaviour and check the estimators recover it: a Pareto(3) sample should return a shape near 1/3, and EVT VaR on Student-t data should match the analytic quantile within 10%. The calibration test evaluates coverage across several random seeds, because with around 20 breaches per run the independence statistic is noisy enough that a single seed will occasionally reject correctly specified data.
## Limitations
- Observations are assumed independent and identically distributed. Real financial returns are volatility-clustered, so a production implementation would filter with a GARCH model first and fit the GPD to the standardised residuals, following McNeil and Frey. This server fits the raw series.
- Parameter uncertainty is not propagated: no confidence intervals on VaR, ES or return levels. Profile likelihood or a bootstrap would be the next addition.
- No multivariate or dependence modelling, so nothing here addresses portfolio aggregation across risk factors.
- The knowledge base is held in memory and rebuilt at start-up, which suits tens of documents rather than thousands; a larger corpus would need a persistent vector store. Retrieval quality is measured on a small hand-labelled set, so the figures above are indicative rather than a benchmark.
## Licence
MIT.
TDQS
Scored across 6 tools
Each tool targets a distinct stage of tail-risk analysis: data loading, threshold diagnostics, GPD fitting, VaR/ES estimation, return levels, and backtesting. Even the related VaR/ES and return-level tools are clearly separated by phrasing (confidence level vs. return period).
All tools share a consistent tailrisk_ prefix and use snake_case, making the family instantly recognizable. However, the second part mixes verb-object forms like fit_gpd and backtest_var with noun phrases like var_es and threshold_stability, so the naming is not perfectly uniform.
Six tools is well-scoped for a specialized tail-risk toolkit. Each tool contributes a necessary part of the workflow without redundancy or bloat.
The toolkit covers the full practical workflow: loading data, assessing threshold stability, fitting the GPD, computing VaR/ES and return levels, and backtesting the VaR model. There are no obvious dead ends or missing core operations for the stated domain.