javascript数租
2015-07-07 20:48
603 查看
function IRR(cashFlows, estimatedResult) { var result = "isNAN"; if (cashFlows != null && cashFlows.length > 0) { // check if business startup costs is not zero: if (cashFlows[0] != 0) { var noOfCashFlows = cashFlows.length; var sumCashFlows = 0; // check if at least 1 positive and 1 negative cash flow exists: var noOfNegativeCashFlows = 0; var noOfPositiveCashFlows = 0; for (var i = 0; i < noOfCashFlows; i++) { sumCashFlows += cashFlows[i]; if (cashFlows[i] > 0) { noOfPositiveCashFlows++; } else { if (cashFlows[i] < 0) { noOfNegativeCashFlows++; } } } // at least 1 negative and 1 positive cash flow available? if (noOfNegativeCashFlows > 0 && noOfPositiveCashFlows > 0) { // set estimated result: var irrGuess = 0.1; // default: 10% if (!isNaN(estimatedResult)) { irrGuess = estimatedResult; if (irrGuess <= 0) { irrGuess = 0.5; } } // initialize first IRR with estimated result: var irr = 0; if (sumCashFlows < 0) { // sum of cash flows negative? irr = -irrGuess; } else { // sum of cash flows not negative irr = irrGuess; } // iteration: // the smaller the distance, the smaller the interpolation // error var minDistance = 1e-15; // business startup costs var cashFlowStart = cashFlows[0]; var maxIteration = 100; var wasHi = false; var cashValue = 0; for (var i = 0; i <= maxIteration; i++) { // calculate cash value with current irr: cashValue = cashFlowStart; // init with startup costs // for each cash flow for (var j = 1; j < noOfCashFlows; j++) { cashValue += cashFlows[j] / Math.pow(1 + irr, j); } // cash value is nearly zero if (Math.abs(cashValue) < 0.01) { result = irr; break; } // adjust irr for next iteration: // cash value > 0 => next irr > current irr if (cashValue > 0) { if (wasHi) { irrGuess /= 2; } irr += irrGuess; if (wasHi) { irrGuess -= minDistance; wasHi = false; } } else {// cash value < 0 => next irr < current irr irrGuess /= 2; irr -= irrGuess; wasHi = true; } // estimated result too small to continue => end // calculation if (irrGuess <= minDistance) { result = irr; break; } } } } } return result; }
相关文章推荐
- js中的各种难题(1)
- javascript对文档对象的内容、属性、样式的操作
- JSP整理总结--9大内置对象、4个作用域
- JSon 简单例子
- JAWR【一个java项目的javascript和CSS集成和压缩工具】
- JSP整理总结--JSP指令、动作标签
- javascript笔记02:严格模式的特定要求
- JavaScript学习笔记
- 完美的js URLEncode函数
- js 增删改查
- 高性能javascript小结
- 枚举做JSP下拉选框的数据源
- 【转载】JavaScript 经典实例收集整理
- JavaScript之this用法详解
- js window对象
- 【翻译】使用Sencha Ext JS 6打造通用应用程序
- 【转载】理解 Javascript 中变量的作用域
- jsp:useBean用法
- 【翻译】使用Sencha Ext JS 6打造通用应用程序
- 【翻译】使用Sencha Ext JS 6打造通用应用程序