risk_metrics
Concept Overview
Section titled “Concept Overview”Downside risk measures over a return series or a return panel: value at risk (the quantile at the given confidence level), expected shortfall (the mean loss beyond it), conditional drawdown at risk, and portfolio variance from a covariance matrix and a weight vector. Expected shortfall and CDaR are subadditive where VaR is not, which is why a risk budget built on VaR alone can be gamed by splitting one position across two sleeves.
When to Use
Section titled “When to Use”Use it for portfolio-level guardrails and risk budgets, and as the input when hcaa should allocate on tail risk rather than on variance. Prefer expected shortfall to VaR whenever the number will be summed across books. These are non-parametric estimates, so they need enough tail observations to mean anything: at 95% confidence a 200-observation sample rests on ten points. All of them are &self methods on a unit struct, and the _from_matrix variants take return panels.
Mathematical Foundations
Section titled “Mathematical Foundations”
Expected Shortfall
Section titled “Expected Shortfall”
Usage Examples
Section titled “Usage Examples”Compute VaR and ES
Section titled “Compute VaR and ES”use openquant::risk_metrics::RiskMetrics;
let returns = vec![-0.02, 0.01, -0.005, 0.003, 0.004];
// These are &self methods on a unit struct, not associated functions: they need// a receiver. `confidence_level` is the tail probability (0.05 = 95% VaR).let metrics = RiskMetrics;let var_95 = metrics.calculate_value_at_risk(&returns, 0.05)?;let es_95 = metrics.calculate_expected_shortfall(&returns, 0.05)?;
println!("VaR(95%) = {var_95:.4}, ES(95%) = {es_95:.4}");API Reference
Section titled “API Reference”Python API
Section titled “Python API”risk.calculate_value_at_riskrisk.calculate_expected_shortfallrisk.calculate_conditional_drawdown_riskrisk.calculate_variancerisk.calculate_value_at_risk_from_matrixrisk.calculate_expected_shortfall_from_matrixrisk.calculate_conditional_drawdown_risk_from_matrix
Rust API
Section titled “Rust API”RiskMetrics::calculate_value_at_riskRiskMetrics::calculate_expected_shortfallRiskMetrics::calculate_conditional_drawdown_riskRiskMetrics::calculate_variance
Risk Notes and Caveats
Section titled “Risk Notes and Caveats”- Non-parametric estimates need enough tail observations.
- Use matrix variants for multi-asset return panels.