@railpath/finance-toolkit
Version:
Production-ready finance library for portfolio construction, risk analytics, quantitative metrics, and ML-based regime detection
27 lines (26 loc) • 1.3 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateParametricExpectedShortfall = calculateParametricExpectedShortfall;
const calculateVolatility_1 = require("./calculateVolatility");
const inverseErf_1 = require("../utils/inverseErf");
/**
* Calculate Expected Shortfall using parametric (normal distribution) method
*
* @param returns Array of historical returns
* @param options Calculation options
* @returns Expected Shortfall value (negative = potential loss)
*/
function calculateParametricExpectedShortfall(returns, options) {
if (returns.length === 0) {
throw new Error('Returns array cannot be empty');
}
const mean = returns.reduce((sum, val) => sum + val, 0) / returns.length;
const volatilityResult = (0, calculateVolatility_1.calculateVolatility)(returns, { method: 'standard' });
const sigma = volatilityResult.value;
// Standard normal quantile function approximation
const z = Math.sqrt(2) * (0, inverseErf_1.inverseErf)(2 * options.confidenceLevel - 1);
// ES for normal distribution = μ - σ * φ(z) / (1 - α)
// where φ is the standard normal PDF
const phi = (1 / Math.sqrt(2 * Math.PI)) * Math.exp(-0.5 * z * z);
return mean - (sigma * phi) / (1 - options.confidenceLevel);
}