From 44e45ffac4a78deb65f438cd18444b2976ce8279 Mon Sep 17 00:00:00 2001 From: Yuan Chen Date: Wed, 2 Dec 2026 20:49:42 -0500 Subject: [PATCH 1/3] Add historical and parametric VaR and expected shortfall --- crates/RustQuant_math/src/lib.rs | 204 ++++++++++++++++--------------- crates/RustQuant_math/src/var.rs | 157 ++++++++++++++++++++++++ 2 files changed, 261 insertions(+), 100 deletions(-) create mode 100644 crates/RustQuant_math/src/var.rs diff --git a/crates/RustQuant_math/src/lib.rs b/crates/RustQuant_math/src/lib.rs index 018db858..22c7038a 100644 --- a/crates/RustQuant_math/src/lib.rs +++ b/crates/RustQuant_math/src/lib.rs @@ -1,100 +1,104 @@ -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -// MATHEMATICS MODULE -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -//! Mathematics related items. -//! -//! ### Optimization and Root Finding -//! -//! - [x] Gradient Descent -//! - [x] Newton-Raphson -//! -//! Note: the reason you need to specify the lifetimes and use the type `Variable` is because the gradient descent optimiser uses the `RustQuant::autodiff` module to compute the gradients. This is a slight inconvenience, but the speed-up is enormous when working with functions with many inputs (when compared with using finite-difference quotients). -//! -//! ```rust -//! use RustQuant::math::*; -//! use RustQuant::autodiff::*; -//! -//! // Define the objective function. -//! fn himmelblau<'v>(variables: &[Variable<'v>]) -> Variable<'v> { -//! let x = variables[0]; -//! let y = variables[1]; -//! -//! ((x.powf(2.0) + y - 11.0).powf(2.0) + (x + y.powf(2.0) - 7.0).powf(2.0)) -//! } -//! -//! // Create a new GradientDescent object with: -//! // - Step size: 0.005 -//! // - Iterations: 10000 -//! // - Tolerance: sqrt(machine epsilon) -//! let gd = GradientDescent::new(0.005, 10000, Some(std::f64::EPSILON.sqrt())); -//! -//! // Perform the optimisation with: -//! // - Initial guess (10.0, 10.0), -//! // - Verbose output. -//! let result = gd.optimize(&himmelblau, &vec![10.0, 10.0], true); -//! -//! // Print the result. -//! println!("{:?}", result.minimizer); -//! ``` -//! -//! ### Integration -//! -//! - Numerical Integration (needed for Heston model, for example): -//! - [x] Tanh-Sinh (double exponential) quadrature -//! -//! ```rust -//! use RustQuant::math::*; -//! -//! // Define a function to integrate: e^(sin(x)) -//! fn f(x: f64) -> f64 { -//! (x.sin()).exp() -//! } -//! -//! // Integrate from 0 to 5. -//! let integral = integrate(f, 0.0, 5.0); -//! -//! // ~ 7.18911925 -//! println!("Integral = {}", integral); -//! ``` -//! -//! ### Risk-Reward Metrics -//! -//! - [x] Risk-Reward Measures (Sharpe, Treynor, Sortino, etc) - -/// Statistical distributions. -pub mod distributions; -pub use distributions::*; - -/// Numerical integration routines. -/// The primary (useful) integrator is the Tanh-Sinh (double exponential) implementation. -pub mod integration; -pub use integration::*; - -/// Numerical optimization and root-finding routines. -pub mod optimization; -pub use optimization::*; - -/// Fast fourier transform. -pub mod fft; -pub use fft::*; - -/// Interpolation routines. -pub mod interpolation; -pub use interpolation::*; - -/// Simple risk/reward measures. -pub mod risk_reward; -pub use risk_reward::*; - -/// Root-finding routines. -pub mod rootfinding; -pub use rootfinding::*; - -/// Sequences of numbers and associated functions. -pub mod sequences; -pub use sequences::*; - -/// Statistic trait. -pub mod statistic; -pub use statistic::*; +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// MATHEMATICS MODULE +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +//! Mathematics related items. +//! +//! ### Optimization and Root Finding +//! +//! - [x] Gradient Descent +//! - [x] Newton-Raphson +//! +//! Note: the reason you need to specify the lifetimes and use the type `Variable` is because the gradient descent optimiser uses the `RustQuant::autodiff` module to compute the gradients. This is a slight inconvenience, but the speed-up is enormous when working with functions with many inputs (when compared with using finite-difference quotients). +//! +//! ```rust +//! use RustQuant::math::*; +//! use RustQuant::autodiff::*; +//! +//! // Define the objective function. +//! fn himmelblau<'v>(variables: &[Variable<'v>]) -> Variable<'v> { +//! let x = variables[0]; +//! let y = variables[1]; +//! +//! ((x.powf(2.0) + y - 11.0).powf(2.0) + (x + y.powf(2.0) - 7.0).powf(2.0)) +//! } +//! +//! // Create a new GradientDescent object with: +//! // - Step size: 0.005 +//! // - Iterations: 10000 +//! // - Tolerance: sqrt(machine epsilon) +//! let gd = GradientDescent::new(0.005, 10000, Some(std::f64::EPSILON.sqrt())); +//! +//! // Perform the optimisation with: +//! // - Initial guess (10.0, 10.0), +//! // - Verbose output. +//! let result = gd.optimize(&himmelblau, &vec![10.0, 10.0], true); +//! +//! // Print the result. +//! println!("{:?}", result.minimizer); +//! ``` +//! +//! ### Integration +//! +//! - Numerical Integration (needed for Heston model, for example): +//! - [x] Tanh-Sinh (double exponential) quadrature +//! +//! ```rust +//! use RustQuant::math::*; +//! +//! // Define a function to integrate: e^(sin(x)) +//! fn f(x: f64) -> f64 { +//! (x.sin()).exp() +//! } +//! +//! // Integrate from 0 to 5. +//! let integral = integrate(f, 0.0, 5.0); +//! +//! // ~ 7.18911925 +//! println!("Integral = {}", integral); +//! ``` +//! +//! ### Risk-Reward Metrics +//! +//! - [x] Risk-Reward Measures (Sharpe, Treynor, Sortino, etc) + +/// Statistical distributions. +pub mod distributions; +pub use distributions::*; + +/// Numerical integration routines. +/// The primary (useful) integrator is the Tanh-Sinh (double exponential) implementation. +pub mod integration; +pub use integration::*; + +/// Numerical optimization and root-finding routines. +pub mod optimization; +pub use optimization::*; + +/// Fast fourier transform. +pub mod fft; +pub use fft::*; + +/// Interpolation routines. +pub mod interpolation; +pub use interpolation::*; + +/// Simple risk/reward measures. +pub mod risk_reward; +pub use risk_reward::*; + +/// Root-finding routines. +pub mod rootfinding; +pub use rootfinding::*; + +/// Sequences of numbers and associated functions. +pub mod sequences; +pub use sequences::*; + +/// Statistic trait. +pub mod statistic; +pub use statistic::*; + +/// Value-at-Risk and Expected Shortfall computation. +pub mod var; +pub use var::*; diff --git a/crates/RustQuant_math/src/var.rs b/crates/RustQuant_math/src/var.rs new file mode 100644 index 00000000..80cc1f5d --- /dev/null +++ b/crates/RustQuant_math/src/var.rs @@ -0,0 +1,157 @@ +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// RustQuant: A Rust library for quantitative finance tools. +// Copyright (C) 2023 https://github.com/avhz +// Dual licensed under Apache 2.0 and MIT. +// See: +// - LICENSE-APACHE.md +// - LICENSE-MIT.md +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +//! Value-at-Risk (VaR) and Expected Shortfall (CVaR) computation. +//! +//! Sign convention: inputs are periodic simple returns expressed as decimals +//! (e.g. `-0.021` means a 2.1% loss). VaR and Expected Shortfall are returned +//! as signed losses (positive for losses, negative for gains) in decimal units, so a VaR of +//! `0.03` at 95% means "with 95% confidence we do not lose more than 3% over +//! one period". + +use crate::distributions::{Distribution, N}; +use crate::statistic::Statistic; + +/// Historical Value-at-Risk. +/// +/// The negative of the lower-tail empirical quantile of the return series, +/// using the same type-7 (linear interpolation) quantile as +/// [`Statistic::quantile`]. `alpha` is the left-tail probability +/// (`0.05` for 95% confidence, `0.01` for 99%). +/// +/// # Examples +/// ``` +/// use RustQuant::math::var::historical_var; +/// +/// let returns = vec![-0.05, -0.04, -0.03, -0.02, -0.01, 0.00, 0.01, 0.02, 0.03, 0.04]; +/// // type-7 quantile at 0.05 over 10 points -> -0.0455, negated -> 0.0455 +/// assert!((historical_var(returns, 0.05) - 0.0455).abs() < 1e-12); +/// ``` +#[must_use] +pub fn historical_var(returns: Vec, alpha: f64) -> f64 { + assert!(!returns.is_empty(), "Returns must have at least one element."); + assert!( + (0.0..=1.0).contains(&alpha), + "Alpha must be between 0 and 1." + ); + + -returns.quantile(alpha) +} + +/// Parametric (Gaussian) Value-at-Risk. +/// +/// Assumes returns are normally distributed and computes +/// `-(mu + Phi^{-1}(alpha) * sigma)` using the population standard deviation, +/// so the observed sample is treated as the entire distribution. `alpha` is +/// the left-tail probability. +/// +/// # Examples +/// ``` +/// use RustQuant::math::var::parametric_var; +/// +/// // Standard normal returns with alpha = 0.05: Phi^{-1}(0.05) = -1.64485... +/// let returns = vec![-0.02, -0.01, 0.00, 0.01, 0.02]; +/// let var = parametric_var(returns, 0.05); +/// assert!(var > 0.0); +/// ``` +#[must_use] +pub fn parametric_var(returns: Vec, alpha: f64) -> f64 { + assert!(returns.len() > 1, "Returns must have at least two elements."); + assert!( + alpha > 0.0 && alpha < 1.0, + "Alpha must be between 0 and 1." + ); + + let mu = returns.mean(); + let sigma = returns.population_standard_deviation(); + // Inverse standard-normal CDF (Phi^{-1}(alpha)); negative for alpha < 0.5. + let z = N.inv_cdf(alpha); + + -(mu + z * sigma) +} + +/// Historical Expected Shortfall / Conditional VaR. +/// +/// The negative of the mean of all returns at or below the `alpha` quantile. +/// By construction this is greater than or equal to +/// [`historical_var`](historical_var) for the same `alpha`. +/// +/// # Examples +/// ``` +/// use RustQuant::math::var::{historical_var, expected_shortfall}; +/// +/// let returns = vec![-0.05, -0.04, -0.03, -0.02, -0.01, 0.00, 0.01, 0.02, 0.03, 0.04]; +/// let es = expected_shortfall(returns.clone(), 0.05); +/// assert!(es >= historical_var(returns, 0.05)); +/// ``` +#[must_use] +pub fn expected_shortfall(returns: Vec, alpha: f64) -> f64 { + assert!(!returns.is_empty(), "Returns must have at least one element."); + assert!( + (0.0..=1.0).contains(&alpha), + "Alpha must be between 0 and 1." + ); + + let q = returns.quantile(alpha); + let tail: Vec = returns.iter().copied().filter(|r| *r <= q).collect(); + + if tail.is_empty() { + return q.abs(); + } + + -tail.mean() +} + +// ############################################################################ +// TESTS +// ############################################################################ + +#[cfg(test)] +mod tests_var { + use super::*; + use crate::distributions::{Distribution, N}; + use crate::statistic::Statistic; + use RustQuant_utils::{assert_approx_equal, RUSTQUANT_EPSILON as EPS}; + + #[test] + fn test_historical_var_known() { + // Sorted: -5%, ..., +4% (10 points). type-7 q(0.05): h = 9*0.05 = 0.45, + // interpolate -5% -> -4% gives -4.55%, so VaR = 0.0455. + let returns = vec![-0.05, -0.04, -0.03, -0.02, -0.01, 0.00, 0.01, 0.02, 0.03, 0.04]; + let var = historical_var(returns, 0.05); + assert_approx_equal!(var, 0.0455, EPS); + } + + #[test] + fn test_parametric_var_known() { + // Standard-normal sample, alpha = 0.05 -> z = -1.64485... + let returns = vec![-2.0, -1.0, 0.0, 1.0, 2.0]; + let mu = returns.mean(); // 0.0 + let sigma = returns.population_standard_deviation(); + let z = N.inv_cdf(0.05); + let var = parametric_var(returns, 0.05); + assert_approx_equal!(var, -(mu + z * sigma), EPS); + } + + #[test] + fn test_expected_shortfall_bounds_var() { + let returns = vec![-0.05, -0.04, -0.03, -0.02, -0.01, 0.00, 0.01, 0.02, 0.03, 0.04]; + let es = expected_shortfall(returns.clone(), 0.05); + let var = historical_var(returns, 0.05); + assert!(es >= var, "expected shortfall must bound VaR"); + } + + #[test] + fn test_expected_shortfall_known() { + // 5 returns, alpha = 0.2 -> tail is the single worst return -0.05. + let returns = vec![-0.05, -0.01, 0.00, 0.01, 0.02]; + let es = expected_shortfall(returns, 0.2); + assert_approx_equal!(es, 0.05, EPS); + } +} From df8115b7134e11a8053f221e1fefc87eeec92f33 Mon Sep 17 00:00:00 2001 From: Yuan Chen Date: Wed, 2 Dec 2026 20:50:27 -0500 Subject: [PATCH 2/3] Preserve source file line endings --- crates/RustQuant_math/src/lib.rs | 208 +++++++++++++++---------------- 1 file changed, 104 insertions(+), 104 deletions(-) diff --git a/crates/RustQuant_math/src/lib.rs b/crates/RustQuant_math/src/lib.rs index 22c7038a..8cee417d 100644 --- a/crates/RustQuant_math/src/lib.rs +++ b/crates/RustQuant_math/src/lib.rs @@ -1,104 +1,104 @@ -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -// MATHEMATICS MODULE -// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ - -//! Mathematics related items. -//! -//! ### Optimization and Root Finding -//! -//! - [x] Gradient Descent -//! - [x] Newton-Raphson -//! -//! Note: the reason you need to specify the lifetimes and use the type `Variable` is because the gradient descent optimiser uses the `RustQuant::autodiff` module to compute the gradients. This is a slight inconvenience, but the speed-up is enormous when working with functions with many inputs (when compared with using finite-difference quotients). -//! -//! ```rust -//! use RustQuant::math::*; -//! use RustQuant::autodiff::*; -//! -//! // Define the objective function. -//! fn himmelblau<'v>(variables: &[Variable<'v>]) -> Variable<'v> { -//! let x = variables[0]; -//! let y = variables[1]; -//! -//! ((x.powf(2.0) + y - 11.0).powf(2.0) + (x + y.powf(2.0) - 7.0).powf(2.0)) -//! } -//! -//! // Create a new GradientDescent object with: -//! // - Step size: 0.005 -//! // - Iterations: 10000 -//! // - Tolerance: sqrt(machine epsilon) -//! let gd = GradientDescent::new(0.005, 10000, Some(std::f64::EPSILON.sqrt())); -//! -//! // Perform the optimisation with: -//! // - Initial guess (10.0, 10.0), -//! // - Verbose output. -//! let result = gd.optimize(&himmelblau, &vec![10.0, 10.0], true); -//! -//! // Print the result. -//! println!("{:?}", result.minimizer); -//! ``` -//! -//! ### Integration -//! -//! - Numerical Integration (needed for Heston model, for example): -//! - [x] Tanh-Sinh (double exponential) quadrature -//! -//! ```rust -//! use RustQuant::math::*; -//! -//! // Define a function to integrate: e^(sin(x)) -//! fn f(x: f64) -> f64 { -//! (x.sin()).exp() -//! } -//! -//! // Integrate from 0 to 5. -//! let integral = integrate(f, 0.0, 5.0); -//! -//! // ~ 7.18911925 -//! println!("Integral = {}", integral); -//! ``` -//! -//! ### Risk-Reward Metrics -//! -//! - [x] Risk-Reward Measures (Sharpe, Treynor, Sortino, etc) - -/// Statistical distributions. -pub mod distributions; -pub use distributions::*; - -/// Numerical integration routines. -/// The primary (useful) integrator is the Tanh-Sinh (double exponential) implementation. -pub mod integration; -pub use integration::*; - -/// Numerical optimization and root-finding routines. -pub mod optimization; -pub use optimization::*; - -/// Fast fourier transform. -pub mod fft; -pub use fft::*; - -/// Interpolation routines. -pub mod interpolation; -pub use interpolation::*; - -/// Simple risk/reward measures. -pub mod risk_reward; -pub use risk_reward::*; - -/// Root-finding routines. -pub mod rootfinding; -pub use rootfinding::*; - -/// Sequences of numbers and associated functions. -pub mod sequences; -pub use sequences::*; - -/// Statistic trait. -pub mod statistic; -pub use statistic::*; - -/// Value-at-Risk and Expected Shortfall computation. -pub mod var; -pub use var::*; +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +// MATHEMATICS MODULE +// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +//! Mathematics related items. +//! +//! ### Optimization and Root Finding +//! +//! - [x] Gradient Descent +//! - [x] Newton-Raphson +//! +//! Note: the reason you need to specify the lifetimes and use the type `Variable` is because the gradient descent optimiser uses the `RustQuant::autodiff` module to compute the gradients. This is a slight inconvenience, but the speed-up is enormous when working with functions with many inputs (when compared with using finite-difference quotients). +//! +//! ```rust +//! use RustQuant::math::*; +//! use RustQuant::autodiff::*; +//! +//! // Define the objective function. +//! fn himmelblau<'v>(variables: &[Variable<'v>]) -> Variable<'v> { +//! let x = variables[0]; +//! let y = variables[1]; +//! +//! ((x.powf(2.0) + y - 11.0).powf(2.0) + (x + y.powf(2.0) - 7.0).powf(2.0)) +//! } +//! +//! // Create a new GradientDescent object with: +//! // - Step size: 0.005 +//! // - Iterations: 10000 +//! // - Tolerance: sqrt(machine epsilon) +//! let gd = GradientDescent::new(0.005, 10000, Some(std::f64::EPSILON.sqrt())); +//! +//! // Perform the optimisation with: +//! // - Initial guess (10.0, 10.0), +//! // - Verbose output. +//! let result = gd.optimize(&himmelblau, &vec![10.0, 10.0], true); +//! +//! // Print the result. +//! println!("{:?}", result.minimizer); +//! ``` +//! +//! ### Integration +//! +//! - Numerical Integration (needed for Heston model, for example): +//! - [x] Tanh-Sinh (double exponential) quadrature +//! +//! ```rust +//! use RustQuant::math::*; +//! +//! // Define a function to integrate: e^(sin(x)) +//! fn f(x: f64) -> f64 { +//! (x.sin()).exp() +//! } +//! +//! // Integrate from 0 to 5. +//! let integral = integrate(f, 0.0, 5.0); +//! +//! // ~ 7.18911925 +//! println!("Integral = {}", integral); +//! ``` +//! +//! ### Risk-Reward Metrics +//! +//! - [x] Risk-Reward Measures (Sharpe, Treynor, Sortino, etc) + +/// Statistical distributions. +pub mod distributions; +pub use distributions::*; + +/// Numerical integration routines. +/// The primary (useful) integrator is the Tanh-Sinh (double exponential) implementation. +pub mod integration; +pub use integration::*; + +/// Numerical optimization and root-finding routines. +pub mod optimization; +pub use optimization::*; + +/// Fast fourier transform. +pub mod fft; +pub use fft::*; + +/// Interpolation routines. +pub mod interpolation; +pub use interpolation::*; + +/// Simple risk/reward measures. +pub mod risk_reward; +pub use risk_reward::*; + +/// Root-finding routines. +pub mod rootfinding; +pub use rootfinding::*; + +/// Sequences of numbers and associated functions. +pub mod sequences; +pub use sequences::*; + +/// Statistic trait. +pub mod statistic; +pub use statistic::*; + +/// Value-at-Risk and Expected Shortfall computation. +pub mod var; +pub use var::*; From 5da18e514448e995b4d66d4d7f1126021e7cfdec Mon Sep 17 00:00:00 2001 From: Yuan Chen Date: Wed, 2 Dec 2026 21:05:49 -0500 Subject: [PATCH 3/3] Format VaR module with rustfmt --- crates/RustQuant_math/src/var.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/crates/RustQuant_math/src/var.rs b/crates/RustQuant_math/src/var.rs index 80cc1f5d..cb6a00a0 100644 --- a/crates/RustQuant_math/src/var.rs +++ b/crates/RustQuant_math/src/var.rs @@ -35,7 +35,10 @@ use crate::statistic::Statistic; /// ``` #[must_use] pub fn historical_var(returns: Vec, alpha: f64) -> f64 { - assert!(!returns.is_empty(), "Returns must have at least one element."); + assert!( + !returns.is_empty(), + "Returns must have at least one element." + ); assert!( (0.0..=1.0).contains(&alpha), "Alpha must be between 0 and 1." @@ -62,11 +65,11 @@ pub fn historical_var(returns: Vec, alpha: f64) -> f64 { /// ``` #[must_use] pub fn parametric_var(returns: Vec, alpha: f64) -> f64 { - assert!(returns.len() > 1, "Returns must have at least two elements."); assert!( - alpha > 0.0 && alpha < 1.0, - "Alpha must be between 0 and 1." + returns.len() > 1, + "Returns must have at least two elements." ); + assert!(alpha > 0.0 && alpha < 1.0, "Alpha must be between 0 and 1."); let mu = returns.mean(); let sigma = returns.population_standard_deviation(); @@ -92,7 +95,10 @@ pub fn parametric_var(returns: Vec, alpha: f64) -> f64 { /// ``` #[must_use] pub fn expected_shortfall(returns: Vec, alpha: f64) -> f64 { - assert!(!returns.is_empty(), "Returns must have at least one element."); + assert!( + !returns.is_empty(), + "Returns must have at least one element." + ); assert!( (0.0..=1.0).contains(&alpha), "Alpha must be between 0 and 1." @@ -123,7 +129,9 @@ mod tests_var { fn test_historical_var_known() { // Sorted: -5%, ..., +4% (10 points). type-7 q(0.05): h = 9*0.05 = 0.45, // interpolate -5% -> -4% gives -4.55%, so VaR = 0.0455. - let returns = vec![-0.05, -0.04, -0.03, -0.02, -0.01, 0.00, 0.01, 0.02, 0.03, 0.04]; + let returns = vec![ + -0.05, -0.04, -0.03, -0.02, -0.01, 0.00, 0.01, 0.02, 0.03, 0.04, + ]; let var = historical_var(returns, 0.05); assert_approx_equal!(var, 0.0455, EPS); } @@ -141,7 +149,9 @@ mod tests_var { #[test] fn test_expected_shortfall_bounds_var() { - let returns = vec![-0.05, -0.04, -0.03, -0.02, -0.01, 0.00, 0.01, 0.02, 0.03, 0.04]; + let returns = vec![ + -0.05, -0.04, -0.03, -0.02, -0.01, 0.00, 0.01, 0.02, 0.03, 0.04, + ]; let es = expected_shortfall(returns.clone(), 0.05); let var = historical_var(returns, 0.05); assert!(es >= var, "expected shortfall must bound VaR");