UNPKG

@railpath/finance-toolkit

Version:

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

36 lines (35 loc) 1.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.calculateInitialGuess = calculateInitialGuess; /** * Calculate improved initial guess for IRR calculation * * Uses a simple approximation based on total cash flows to provide * a better starting point than a fixed value, which can improve * convergence speed and stability. * * @param cashFlows - Array of cash flows (positive for inflows, negative for outflows) * @returns Initial guess for discount rate (as decimal) */ function calculateInitialGuess(cashFlows) { if (cashFlows.length === 0) { return 0.1; // Default fallback } // Calculate approximate return based on total cash flows const totalInflows = cashFlows.reduce((sum, cf) => sum + Math.max(0, cf), 0); const totalOutflows = Math.abs(cashFlows.reduce((sum, cf) => sum + Math.min(0, cf), 0)); const finalValue = cashFlows[cashFlows.length - 1]; // Simple approximation: (final - outflows) / (inflows + outflows) if (totalOutflows > 0 && totalInflows > 0) { const approxReturn = (finalValue - totalOutflows) / (totalInflows + totalOutflows); // Clamp to reasonable range if (approxReturn > -0.99 && approxReturn < 10.0) { return approxReturn; } } // Fallback: check if return is likely negative if (finalValue < totalOutflows) { return -0.1; // Start with negative return guess } return 0.1; // Default: 10% annual return }