Class VarEngine
java.lang.Object
com.quantfinlib.risk.VarEngine
Portfolio Value-at-Risk, all four classic flavors over one input
shape: factor EXPOSURES (currency P&L per unit factor return — a
delta vector) against a factor covariance matrix or a factor-return
history.
RiskMetrics answers the single-series question; this
engine answers the PORTFOLIO one, where the methods genuinely
disagree and the disagreement is the point:
- Delta-normal (variance-covariance) — σ_P = √(δ'Σδ), VaR = z·σ_P. Instant, and exactly wrong for optionality;
- Monte Carlo — Cholesky-correlated Gaussian factor draws through the linear map; converges to delta-normal for a linear book (the tests pin that agreement), and exists so the same harness can price non-linear books by full revaluation;
- Delta-gamma (Cornish-Fisher) — second-order P&L
δ'Δx + ½Δx'ΓΔxwhose skew tilts the quantile via the Cornish-Fisher expansion: a short-gamma book's VaR is WORSE than delta-normal says, a long-gamma book's better — the asymmetry delta-normal cannot see; - Historical — replay actual factor-return rows through the exposures; no distributional assumption, no correlation matrix, exactly as fat-tailed as the sample was.
Conventions: VaR and ES are returned as POSITIVE losses in currency units; confidence is the one-sided level (0.99 = 99%); factor returns and Σ are per-horizon (scale √t outside). Expected shortfall accompanies each method — post-FRTB, ES is the primary number and VaR the diagnostic. Research lane, deterministic (MC per seed).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceRevalues the book under one scenario's factor moves.static final recordVaR and ES from Gaussian Monte Carlo factor scenarios. -
Method Summary
Modifier and TypeMethodDescriptionstatic doubledeltaGammaEs(double[] exposures, double[][] gamma, double[][] covariance, double confidence) Second-order ES: the tail mean of the Cornish-Fisher loss quantile, integrated in CLOSED FORM.static doubledeltaGammaVar(double[] exposures, double[][] gamma, double[][] covariance, double confidence) Second-order VaR via the Cornish-Fisher quantile of the delta-gamma P&L.static doubledeltaNormalEs(double[] exposures, double[][] covariance, double confidence) Delta-normal ES: the Gaussian tail mean, σ·φ(z)/(1−c).static doubledeltaNormalVar(double[] exposures, double[][] covariance, double confidence) Delta-normal VaR: z-quantile of the Gaussian portfolio P&L.static VarEngine.VarResultfullRevaluationVar(double[][] scenarios, VarEngine.ScenarioReval pricer, double confidence) Full-revaluation VaR: every scenario repriced through the CALLER'S pricer — the method that sees what every sensitivity shortcut misses (a knocked-out barrier, a pinned short gamma, an autocall triggered by the scenario itself).static VarEngine.VarResulthistoricalVar(double[] exposures, double[][] factorReturns, double confidence) Historical simulation: each row offactorReturnsis one scenario replayed through the exposures.static VarEngine.VarResultmonteCarloVar(double[] exposures, double[][] covariance, double confidence, int scenarios, long seed) static doubleportfolioStdev(double[] exposures, double[][] covariance) Portfolio stdev √(δ'Σδ) in currency units.
-
Method Details
-
portfolioStdev
public static double portfolioStdev(double[] exposures, double[][] covariance) Portfolio stdev √(δ'Σδ) in currency units. -
deltaNormalVar
public static double deltaNormalVar(double[] exposures, double[][] covariance, double confidence) Delta-normal VaR: z-quantile of the Gaussian portfolio P&L. -
deltaNormalEs
public static double deltaNormalEs(double[] exposures, double[][] covariance, double confidence) Delta-normal ES: the Gaussian tail mean, σ·φ(z)/(1−c). -
monteCarloVar
public static VarEngine.VarResult monteCarloVar(double[] exposures, double[][] covariance, double confidence, int scenarios, long seed) -
deltaGammaVar
public static double deltaGammaVar(double[] exposures, double[][] gamma, double[][] covariance, double confidence) Second-order VaR via the Cornish-Fisher quantile of the delta-gamma P&L. Moments ofδ'Δx + ½Δx'ΓΔxunder Gaussian factors: mean½tr(ΓΣ), varianceδ'Σδ + ½tr((ΓΣ)²), and the skew that moves the quantile. Accurate for MODERATE gamma — the expansion degrades when the quadratic term dominates (skew beyond ~1), which is when full revaluation Monte Carlo earns its cost; that boundary is the documented limit, not a hidden one. -
deltaGammaEs
public static double deltaGammaEs(double[] exposures, double[][] gamma, double[][] covariance, double confidence) Second-order ES: the tail mean of the Cornish-Fisher loss quantile, integrated in CLOSED FORM. With loss quantileq(p) = −μ + σ·(z_p + (z_p²−1)·s/6)(s = loss skew), the identitiesE[Z·1{Z>z}] = φ(z)andE[(Z²−1)·1{Z>z}] = z·φ(z)giveES = −μ + σ·φ(z)/(1−c)·(1 + z·s/6)— no numerical integration, and it reduces EXACTLY todeltaNormalEs(double[], double[][], double)when Γ = 0. Same moderate-gamma validity bound asdeltaGammaVar(double[], double[][], double[][], double). -
historicalVar
public static VarEngine.VarResult historicalVar(double[] exposures, double[][] factorReturns, double confidence) Historical simulation: each row offactorReturnsis one scenario replayed through the exposures. No assumptions — and no more tail than the window actually contained. -
fullRevaluationVar
public static VarEngine.VarResult fullRevaluationVar(double[][] scenarios, VarEngine.ScenarioReval pricer, double confidence) Full-revaluation VaR: every scenario repriced through the CALLER'S pricer — the method that sees what every sensitivity shortcut misses (a knocked-out barrier, a pinned short gamma, an autocall triggered by the scenario itself). Scenarios are rows of factor moves — historical rows for historical full-reval, Cholesky-generated rows for Monte Carlo full-reval. A pricer returning NaN/Infinity throws: a scenario your pricer cannot price is a modelling problem, not a quantile.
-