UNPKG

@railpath/finance-toolkit

Version:

Production-ready finance library for portfolio construction, risk analytics, quantitative metrics, and ML-based regime detection

21 lines (20 loc) 745 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.calculateHistoricalVaR = calculateHistoricalVaR; /** * Historical VaR - Uses empirical distribution of returns */ function calculateHistoricalVaR(returns, confidenceLevel) { const sorted = [...returns].sort((a, b) => a - b); const index = Math.floor((1 - confidenceLevel) * sorted.length); const varValue = Math.abs(sorted[index]); // Calculate CVaR (average of losses beyond VaR) const tailLosses = sorted.slice(0, index + 1); const cvar = Math.abs(tailLosses.reduce((sum, val) => sum + val, 0) / tailLosses.length); return { value: varValue, confidenceLevel, method: 'historical', cvar, }; }