UNPKG

@railpath/finance-toolkit

Version:

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

29 lines (28 loc) 1.02 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.calculateNPV = calculateNPV; /** * Calculate Net Present Value (NPV) of cash flows * * NPV = Σ(CF_i / (1 + r)^t_i) * * @param cashFlows - Array of cash flows (positive for inflows, negative for outflows) * @param timePeriods - Array of time periods in years corresponding to each cash flow * @param rate - Discount rate (as decimal, e.g., 0.1 for 10%) * @returns Net Present Value */ function calculateNPV(cashFlows, timePeriods, rate) { if (cashFlows.length !== timePeriods.length) { throw new Error('Cash flows and time periods must have same length'); } if (rate <= -1) { throw new Error('Discount rate must be greater than -1'); } return cashFlows.reduce((sum, cf, index) => { const time = timePeriods[index]; if (time < 0) { throw new Error('Time periods must be non-negative'); } return sum + cf / Math.pow(1 + rate, time); }, 0); }