// RISK · MODULE 07/12
Quantitative risk: from VaR to the risk copilot
Exact VaR and Expected Shortfall, stress testing, and the LLM as an explanation layer, never a calculator.
Modules 4 through 6 built the understanding layer: data sources, document RAG, and prompts with structured output. With this module the course enters Block III —reason and decide— and changes register: exact mathematics takes the lead here, and the LLM is confined to the role the golden rule assigns it (§1.2.2): narrate, orchestrate, and alert, never compute.
7.1 Risk measurement with exact formulas
Quantitative risk management rests on a small set of formulas whose provenance is verifiable: the J.P. Morgan/Reuters RiskMetrics Technical Document (1996), the Basel Committee documents (d450, d457), and the canonical academic literature (Rockafellar–Uryasev, Kupiec, Christoffersen, Bollerslev). This module presents them exactly as they appear in the primary sources, with the positive-loss convention: denotes the log return on day ; is the tail significance level (0.01 for 99% VaR); is the standard normal quantile of the right loss tail; and are the standard normal density and distribution function.
7.1.1 VaR, Expected Shortfall, and validation
Parametric VaR (delta-normal). Under normal returns, VaR at confidence and horizon days for a portfolio of value with daily mean and daily volatility is (graphrag.com):
with (95%) and (99%). Time scaling applies the square-root-of-time rule, , valid only under i.i.d. returns. For a multi-asset portfolio with weights and covariance matrix (The Database for Multimodal AI):
Historical VaR (historical simulation). The historical factor moves of the last days (typically 250) are reapplied to the current portfolio and the hypothetical P&Ls are sorted ; VaR is the empirical quantile (arXiv.org):
With and , the 95% VaR is the 13th worst day (). It requires no distributional assumptions and captures the sample tails, but at 99% with 250 days there are only about 2.5 tail observations, which produces high sampling variation and slow response to regime changes (arXiv.org).
Monte Carlo VaR and FHS. factor paths are simulated under a calibrated model (GBM, Student-t, GARCH), the portfolio is revalued, and the quantile of the simulated distribution is taken (arXiv.org):
where is the empirical CDF of the simulated losses. The Filtered Historical Simulation variant (Barone-Adesi, Bourgoin, and Giannopoulos) fits a GARCH(1,1), extracts the standardized residuals , and simulates by resampling with replacement; comparative evidence shows that normal Monte Carlo underestimates the left tail relative to FHS (arXiv.org).
Expected Shortfall / CVaR. For a loss variable with continuous CDF, VaR and ES are defined as (🦜️🔗 LangChain):
Under normality a closed form exists:
At 99% the factor is ; at 97.5%, , the central figure in the FRTB design (§7.2). Unlike VaR, ES is a coherent measure in the sense of Artzner et al. (1999): it satisfies subadditivity, the property VaR violates (🦜️🔗 LangChain). The Rockafellar–Uryasev (2000) variational representation turns CVaR estimation and optimization into linear programming (🦜️🔗 LangChain):
with ; the minimizer is VaR itself. The empirical estimator in historical simulation is simply the average of the worst losses.
VaR backtesting. An unvalidated VaR model is an opinion. With the exception indicator , Kupiec's (1995) proportion-of-failures test contrasts the observed rate against the nominal (CurvedTrading):
7.1.2 Volatility: the raw material of every risk number
Realized volatility. With evenly spaced intraday observations of the log price and returns (QVeris AI):
Under a diffusion with jumps, is a consistent estimator of quadratic variation as (Andersen–Bollerslev); with microstructure noise, the practical optimal frequency is 5 minutes (QVeris AI).
EWMA / RiskMetrics. The recursion from the RiskMetrics Technical Document (4th ed., 1996) is (graphrag.com):
Expanded, : decaying geometric weights with effective memory days for . It is an IGARCH with no mean reversion (, ), and its multivariate version is always positive semidefinite (graphrag.com).
GARCH(1,1). The Engle (1982) and Bollerslev (1986) model introduces mean reversion (Bright Data):
The unconditional variance (long-run level) is , and the -step-ahead forecast reverts geometrically toward it:
The sum measures the persistence of volatility shocks; EWMA is the limiting case , (Bright Data). Numerical case. With , a volatility spike that doubles the long-run level () takes days to reduce the excess by half.
Maximum drawdown and recovery asymmetry. Let be the equity curve and the running maximum; the drawdown is and (Investopedia):
Recovery is asymmetric: a loss requires a gain to return to the peak — a 50% drop demands a 100% rise, and a 90% drop, a 900% one (Investopedia). It is the risk metric investors actually feel, and the one anchoring the Calmar and Sterling ratios.
7.1.3 Risk-adjusted performance ratios
The full catalog, with its conventions and documented pitfalls:
Sharpe. Introduced as the reward-to-variability ratio (Sharpe, 1966) (AlphaLog):
Annualization with or requires i.i.d. returns; with positive serial autocorrelation — typical of hedge fund strategies with smoothed valuations — the annualized Sharpe can be overstated by up to 65% (Lo, 2002) (Alpha Vantage). It is the most ignored methodological warning in the industry.
Sortino. It replaces total volatility with the downside deviation relative to the minimum acceptable return (databento.com):
Omega (Keating–Shadwick, 2002). It uses the entire distribution, with no moment assumptions (databento.com):
with empirical form (Macroption).
Calmar. , typically over 36-month windows; introduced by Terry W. Young (1991) (quantvps.com). Window bias: a track record that has not yet lived through its crisis exhibits an artificially high Calmar (quantvps.com).
Sterling. Deane Sterling Jones's (1981) original definition is (Alpha Vantage):
with the compound annual return and the average of the annual maximum drawdowns. The 10% reflected T-bill yields in 1981; variants with and modern versions with coexist (langfuse.com). Editorial rule: always cite the convention used.
Ulcer Index (Martin & McCann, 1987). Root mean square of percentage drawdowns from the last peak (TrueFoundry):
The square penalizes the depth and the duration of drawdowns jointly; it is the basis of the Ulcer Performance Index, (TrueFoundry).
Tracking error and information ratio. For an active portfolio against its benchmark (🦜️🔗 LangChain):
Grinold's (1989) fundamental law of active management decomposes IR into skill and breadth: , where is the information coefficient (correlation between forecast and realization) and the number of independent bets (NextFuture).
Higher-order moments and modified VaR. Equity returns exhibit negative skewness and excess kurtosis (kosmoy.com):
(normal: , ). The Cornish–Fisher expansion (Zangari, 1996) corrects the Gaussian quantile (AgenticWire):
and Boudt, Peterson, and Carl (2008) extend the correction to a coherent modified ES, (AgenticWire). The expansion stops being monotonic for or : verify the monotonicity of in before reporting it (AgenticWire).
The following table summarizes the module's main risk metrics.
Table 7.1 — Risk metrics: formula, horizon, assumptions, and main limitation
| Metric | Summary formula | Typical horizon | Key assumptions | Main limitation |
|---|---|---|---|---|
| Delta-normal VaR | 1–10 days | Normality, i.i.d., linearity | Underestimates fat tails and non-linearities (graphrag.com) | |
| Historical VaR | 1 day (250 obs.) | History represents the future | ~2.5 tail obs. at 99%; slow under a new regime (arXiv.org) | |
| Monte Carlo VaR / FHS | 1–10 days | Well-calibrated factor model | Specification risk; revaluation cost (arXiv.org) | |
| ES / CVaR | 1–10 days | Well-estimated tail | Statistically difficult backtesting (kosmoy.com) | |
| EWMA RiskMetrics | Daily | universal | No mean reversion (IGARCH) (graphrag.com) | |
| GARCH(1,1) | Daily–monthly | Stationarity, | Persistence misestimated in crises (Bright Data) | |
| MDD | Full history | Faithful equity curve | Window-dependent; backward-looking (Investopedia) |
7.2 Portfolio risk and stress testing
7.2.1 Euler decomposition, scenarios, and FRTB
Component and Marginal VaR. With parametric VaR and monetary positions , Garman's (1996) decomposition is (The Database for Multimodal AI):
equivalently, . The Euler property guarantees exact aggregation: (kosmoy.com). Marginal VaR identifies the "worst hedge": the position with the highest MVaR is the one whose reduction cuts the most risk per monetary unit. This decomposition is, moreover, the mathematical basis of narrative risk attribution with an LLM (§7.3): the tool computes the , the language model explains them (The Database for Multimodal AI). The underlying diversification is Markowitz's: with two assets, , and any reduces variance relative to the weighted average — with the critical caveat that in crises correlations converge to 1 (2008, March 2020) (confident-ai.com).
Historical scenarios. The shocks observed in real episodes are reapplied to the current portfolio: subprime 2007-08, COVID March 2020, Black Monday 1987 (−22.6% for the Dow in a single session), LTCM 1998, the 2022 rates shock. For each factor, the worst 1-day and 10-day shocks are determined. Their value is that they preserve the real stress correlations: in 2008 developed-market equities fell together, and in March 2020 equities and Treasury bonds were sold simultaneously in the dash for cash (openobserve.ai).
Hypothetical scenarios. Plausible extreme shocks not necessarily observed. A documented global bank's stress program combines historical scenarios ("subprime crisis", "Covid crisis") with a hypothetical worst case that moves each factor between ±3 and ±6 daily standard deviations, ignoring historical correlations (openobserve.ai).
Reverse stress testing. The BCBS principles invert the logic: "a stress testing programme must determine what scenarios could challenge the bank's viability (reverse stress tests) and thereby uncover hidden risks and interactions between risks", starting "from a known stress outcome — a breach of regulatory capital ratios, illiquidity or insolvency — and asking what events could lead to that outcome" (BCBS 2009, p. 14) (Laminar). PRA, EBA, and FCA incorporate it into their frameworks (ICAAP/SREP; FG 20/1), and the BCBS Range of Practices report (2017) documents that two-thirds of institutions practice it as a complement (xseek.io). The 2018 Stress Testing Principles (BCBS d450) replace the 2009 ones and require stress testing as a mandatory complement to internal models, precisely to cover what the model's assumptions do not capture (kosmoy.com).
FRTB: from 99% VaR to 97.5% ES. The Fundamental Review of the Trading Book (BCBS d352, 2016; revised in d457, 2019) replaces, in the internal models approach, 10-day 99% VaR with 97.5% ES calibrated over a stress period, with five liquidity horizons (10/20/40/60/120 days), a reduced set of modellable factors, and additive capital for non-modellable ones (NMRF) (kosmoy.com). The calibration is deliberately continuous under normality (Holiday Landmark):
— almost identical () when returns are normal, but with ES growing much faster than VaR as soon as fat tails appear: "same number in calm times, a larger number precisely on the fat-tail exposures that VaR historically undercapitalized" (Holiday Landmark). Backtesting is still done on 99% and 97.5% VaR with the traffic light over 250 days — because ES is not classically elicitable and its direct validation is statistically harder — complemented by the P&L Attribution Test (PLAT) at desk level; a desk that fails backtesting or PLAT is forcibly migrated from the internal to the standardized approach (kosmoy.com). Implementation calendar: EU (CRR3) January 2026, UK (PRA) January 2027, US pending (braintrust.dev).
Table 7.2 — FRTB (IMA) versus the previous internal approach (Basel 2.5)
| Dimension | Previous framework (Basel 2.5, IMA) | FRTB (IMA, d457) |
|---|---|---|
| Capital metric | 99% VaR, 10 days (+ stressed VaR) | 97.5% ES, calibrated to a stress period (braintrust.dev) |
| Horizon | Uniform 10 days | 10/20/40/60/120 days by factor liquidity (kosmoy.com) |
| Risk factors | All modellable by default | NMRF with additive capital; reduced set ≥ 75% of total ES |
| Diversification | Full implicit | IMCC recognizes only 50% of the benefit () |
| Validation | Traffic light on 99% VaR | Traffic light on 99% and 97.5% VaR + desk-level PLAT (kosmoy.com) |
| Consequence of failure | Capital multiplier | Forced migration of the desk to the standardized approach (kosmoy.com) |
| Entry into force | 2011 (Basel 2.5) | EU Jan-2026; UK Jan-2027; US pending (braintrust.dev) |
7.3 The risk copilot with LangChain
7.3.1 Canonical pattern: the tool computes, the LLM narrates, the human vetoes
The 2024-2026 literature documents a transition from "LLMs as text analysts" to LLM agents with quantitative tools: the LLM reasons and orchestrates; the numbers are computed by deterministic code (horizontrading.io). The 2026 trading-agent taxonomy classifies risk control as a native capability of the agent — "risk as a gating mechanism in the action architecture, not post-hoc" (horizontrading.io) — and the reference case is FinCon (NeurIPS 2024): its intra-episode risk alert is triggered by a sudden drop in CVaR, defined as the mean of the worst 1% of daily P&L, and when activated "the manager agent adopts a risk-averse stance that day, regardless of the prior risk state" (Github). In the open source ecosystem, MCP servers such as wraquant-mcp expose 218 tools (risk, volatility, and regime modules) to Claude/LangChain agents; the documented conversation pattern is literally "VaR(95%): −2.1%, CVaR: −3.4%" computed by the tool and narrated by the agent (quodfinancial.com). LangChain also maintains an official sample repository of a risk assessment agent (langchain-samples/risk-assessment-agent, LangGraph + LangSmith) with GRC scenarios and human-in-the-loop (TS Imagine), and projects such as DART architecturally separate risk_manager.py (Monte Carlo VaR, Kelly, sizing) from the LLM analyzer (sanj.dev).
Layer 1: the deterministic tool. Every risk figure is produced in Python, with controlled rounding and structured output:
import numpy as np
from langchain_core.tools import tool
@tool
def compute_var_es(returns: list[float], confidence: float = 0.99) -> dict:
"""VaR and Expected Shortfall via historical simulation (positive losses)."""
r = np.asarray(returns, dtype=float)
losses = np.sort(-r) # positive losses, sorted
alpha = 1.0 - confidence
var = float(np.quantile(losses, confidence))
tail = losses[losses >= var]
es = float(tail.mean()) if tail.size else var
# Controlled rounding: the LLM receives the final figure, never recomputes it
return {"var": round(var, 6), "es": round(es, 6),
"n_tail": int(tail.size), "alpha": alpha, "method": "historical"}Layer 2: backtesting as a validation tool. The same principle applies to Kupiec's test:
import numpy as np
from scipy.stats import chi2
from langchain_core.tools import tool
@tool
def kupiec_pof(exceptions: int, n_days: int, p: float = 0.01) -> dict:
"""Kupiec POF test: H0 = the exception rate equals the nominal p."""
N, T = exceptions, n_days
lr = -2.0 * (np.log((1 - p) ** (T - N) * p ** N)
- np.log((1 - N / T) ** (T - N) * (N / T) ** N))
return {"lr_pof": round(float(lr), 4),
"p_value": round(float(chi2.sf(lr, 1)), 4),
"reject_5pct": bool(lr > chi2.ppf(0.95, 1))}Layer 3: the copilot agent with human escalation. The LLM orchestrates the tools and drafts the attribution based on the Euler decomposition it receives as JSON; an interrupt halts the flow on any limit breach so the risk manager decides:
from langchain.agents import create_agent
from langchain.agents.middleware import HumanInTheLoopMiddleware
from langgraph.checkpoint.memory import InMemorySaver
SYSTEM = (
"Eres un copiloto de riesgo institucional. NUNCA calcules cifras de riesgo: "
"usa SIEMPRE las herramientas compute_var_es, kupiec_pof y euler_attribution. "
"Narra la atribución solo a partir del JSON que devuelvan las herramientas, "
"citando cada número exactamente como lo recibiste."
)
copilot = create_agent(
model="claude-sonnet-4-6",
tools=[compute_var_es, kupiec_pof, euler_attribution],
system_prompt=SYSTEM,
middleware=[
HumanInTheLoopMiddleware(
interrupt_on={
"escalate_limit_breach": True, # approve / edit / reject / respond
"compute_var_es": False, # safe read
},
),
],
checkpointer=InMemorySaver(), # in production: PostgresSaver (audit log)
)The full architecture separates three planes of responsibility:
interrupt escalates to the human on breaches.Limit monitoring with natural-language alerts. A scheduler runs the VaR/ES/MDD computation against limits; on a breach, the agent composes the alert —which limit, by how much it is exceeded, which positions contribute according to Component VaR, suggested action— and the interrupt holds it until the human decision (TS Imagine):
from langgraph.types import interrupt
def breach_node(state):
if state["var_99"] <= state["limit"]:
return {"status": "compliant"}
alert = llm.invoke(ALERT_PROMPT + json.dumps(state["euler_breakdown"]))
decision = interrupt({"alert": alert, "state": state}) # pause until the human
return {"status": decision["action"], "alert": alert}7.4 The limits of the model and of the LLM
7.4.1 Structural critique of VaR, numerical hallucination, and model risk
The anti-VaR literature. Nassim Taleb campaigned against VaR for more than a decade and called it, literally, "a fraud" (NYT Magazine, Jan-4-2009); David Einhorn described it as "relatively useless as a risk-management tool and potentially catastrophic when its use creates a false sense of security… like an airbag that works all the time, except when you have an accident" (quodfinancial.com). The quantification of the failure is brutal: under normality, a event should occur once every ~14,000 years; equity markets produce such events roughly once per decade, and in 2008 books with "moderate" 99% VaR suffered losses of several times VaR (🦜️🔗 LangChain). To the empirical critique is added the mathematical defect proven by Artzner et al. (1999): VaR is not subadditive, ignores severity beyond the quantile and, calibrated with low recent volatility, underestimates risk under stress — the three documented reasons for the regulatory change to ES (braintrust.dev). And a warning about endogenous risk: a massively adopted management technique becomes a source of risk ("portfolio insurance" in 1987; correlations at 1.0 in the 2008 liquidity crunch) (latitude.so).
Model risk and the London Whale. In 2012, JPMorgan's Chief Investment Office lost $6.2 billion: the internal investigation revealed that the VaR model had been modified to halve the reported risk and that the change evaded independent validation (paperswithbacktest.com). The historical reference framework was SR 11-7 (Fed/OCC, 2011), which defines "model" broadly and requires three pillars — conceptual soundness, effective validation with outcomes analysis and backtesting, and governance with an inventory — explicitly extended by supervisors to AI/ML models (paperswithbacktest.com). Regulatory update (as of Jul-29-2026): the Federal Reserve's SR 26-2 letter (Apr-17-2026) rescinded SR 11-7, explicitly left generative and agentic AI outside its perimeter, and declared itself non-binding; its principles — effective challenge, inventory, independent validation — persist as the standard of practice, but the governance of LLM agents is today in a regulatory vacuum that firms cover with observability (traces, checkpoints, audit logs) (United Fintech) (archived copy). The direct implication does not change: a LangChain risk copilot is, for practical purposes, a model (or a user of models), and requires inventory, independent validation, drift monitoring, and documented usage limits — with the human retaining the final veto over any consequential action (paperswithbacktest.com).
The module's synthesis is the governing insight of Block III: the LLM reasons, the math decides, the human vetoes. The formulas of sections 7.1 and 7.2 are the layer that decides; the copilot of section 7.3 is the layer that reasons and narrates; and the interrupt with audit log is the layer that vetoes. Weakening any of the three — letting the LLM compute, dispensing with backtesting, or removing human approval — reproduces exactly the failures documented in this section.
Exercises
- Multi-method VaR/ES tool. Extend the
compute_var_estool to support the three methods (delta-normal, historical, FHS with GARCH(1,1) and residual resampling). Verify on a synthetic series with Student-t tails that delta-normal underestimates 99% VaR relative to FHS, and document the size of the gap. - Full Kupiec and Christoffersen backtesting. Implement Kupiec's POF test and Christoffersen's conditional coverage test () as LangChain tools. Over 500 days of an EWMA model applied to the S&P 500, determine in which windows the model would have been rejected at 5% and whether exceptions cluster in streaks.
- Cornish–Fisher with guardrails. Implement the Cornish–Fisher modified VaR and Boudt's ES as a tool, with a prior validation that rejects the computation (and reports it to the agent) when or , also verifying the monotonicity of in .
- Narrative attribution copilot. Build the agent from section 7.3 with an
euler_attributiontool that returns Component VaR by position (EWMA covariance matrix). Require in the system prompt that every narrated figure match the tool's JSON exactly, and write a numerical exact-match evaluation over 20 generated attributions. - Limit monitor with interrupt. Implement Diagram 7.2 as a LangGraph
StateGraphwith a checkpointer: a deterministic computation node, a conditional breach edge, LLM alert drafting, andinterrupt()withapprove/rejectdecisions. Show that the paused graph resumes correctly withCommand(resume=...)and that the checkpoint records the decision for audit. - Miniature FRTB traffic light. Simulate 250 days of P&L against 99% and 97.5% VaR, classify the desk as green/amber/red according to the number of exceptions, and discuss why the regulator keeps the traffic light on VaR and not on ES (elicitability), contrasting your answer with the arithmetic .
Module 7 of 15 — LangChain for Quantitative Trading. Data and versions verified as of July 2026; prices and market figures subject to change. LangChain 1.3.14 / langchain-core 1.5.2.
// check
Check your understanding
Self-assessment with instant feedback. No score is stored.
Show answer
B. Kupiec's POF test contrasts the observed rate N/T against the nominal p under H0; with LR_POF = 5.50 > 3.84 the model is rejected at 5%. The N = 6 case illustrates how sharp the threshold is (3.56 < 3.84). Streaks are assessed by Christoffersen's independence term, which complements Kupiec but is not a requirement of the POF test.
Show answer
B. With α + β = 1 there is no finite unconditional variance: the recursion σ²_t = λσ²_{t−1} + (1−λ)r²_{t−1} treats each shock as permanent, with effective memory ≈ 1/(1−λ) ≈ 17 days. GARCH(1,1) with α + β < 1 does revert to σ̄² = ω/(1−α−β). Precisely the multivariate version of EWMA does guarantee positive semidefiniteness by construction.
Show answer
B. Annualization with √252 or √12 is only valid under i.i.d. Smooth marking of illiquid assets creates positive serial autocorrelation, which inflates the annualized Sharpe — Lo (2002) quantifies the overstatement at up to 65%. The module calls it the most ignored methodological warning in the industry, and the bias always plays in the manager's favor.
Show answer
C. The calibration is deliberately continuous under normality (2.338σ ≈ 2.326σ), but ES penalizes the fat tails that VaR undercapitalized in 2008. The regulator keeps backtesting on 99% and 97.5% VaR because ES is not elicitable in the classical sense: validation stays with the quantile, complemented by PLAT at desk level.
Show answer
B. MVaR_i = ∂VaR_p/∂x_i = z_α(Σx)_i/σ_p measures the sensitivity of total VaR to each monetary unit of position i: it is the 'worst hedge' to review. Notional weight is not enough — in the worked example, the position with 60% of the notional concentrates 77.4% of the risk — and the Euler property guarantees that Σ CVaR_i = VaR_p.
Show answer
B. FAITH (ICAIF 2025) measures intrinsic numerical hallucination: not even the best model exceeds 95.6% grounding, and small ones fall below 50%. In risk, precision is non-negotiable, so the module's golden rule is that the tool computes (with controlled rounding) and the LLM narrates the attribution quoting each number exactly as received.
Show answer
B. The interrupt halts execution at a persistent checkpoint: detection is quantitative, the LLM only drafts the alert with the Component VaR breakdown, and the human decides (approve / accept the exception with a waiver / reject and escalate to committee). Resuming with Command(resume=...) and recording the decision in the audit log is exercise 5 of the module.
Show answer
B. Artzner et al. (1999) proved that VaR is not a coherent measure: it violates subadditivity and is blind to what happens beyond the quantile. Added to the procyclicality of calibrating it with low recent volatility, these are the three documented reasons for the regulatory change to 97.5% ES, which is subadditive and looks at the tail mean.