-
-
Save Tacombel/67ea9dbcf43a2b6eabf7408433d157ae to your computer and use it in GitHub Desktop.
XIRR Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2012 Sutoiku, Inc. (MIT License) | |
// Some algorithms have been ported from Apache OpenOffice: | |
/************************************************************** | |
* | |
* Licensed to the Apache Software Foundation (ASF) under one | |
* or more contributor license agreements. See the NOTICE file | |
* distributed with this work for additional information | |
* regarding copyright ownership. The ASF licenses this file | |
* to you under the Apache License, Version 2.0 (the | |
* "License"); you may not use this file except in compliance | |
* with the License. You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, | |
* software distributed under the License is distributed on an | |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
* KIND, either express or implied. See the License for the | |
* specific language governing permissions and limitations | |
* under the License. | |
* | |
*************************************************************/ | |
function XIRR(values, dates, guess) { | |
// Credits: algorithm inspired by Apache OpenOffice | |
// Calculates the resulting amount | |
var irrResult = function(values, dates, rate) { | |
var r = rate + 1; | |
var result = values[0]; | |
for (var i = 1; i < values.length; i++) { | |
result += values[i] / Math.pow(r, moment(dates[i]).diff(moment(dates[0]), 'days') / 365); | |
} | |
return result; | |
} | |
// Calculates the first derivation | |
var irrResultDeriv = function(values, dates, rate) { | |
var r = rate + 1; | |
var result = 0; | |
for (var i = 1; i < values.length; i++) { | |
var frac = moment(dates[i]).diff(moment(dates[0]), 'days') / 365; | |
result -= frac * values[i] / Math.pow(r, frac + 1); | |
} | |
return result; | |
} | |
// Check that values contains at least one positive value and one negative value | |
var positive = false; | |
var negative = false; | |
for (var i = 0; i < values.length; i++) { | |
if (values[i] > 0) positive = true; | |
if (values[i] < 0) negative = true; | |
} | |
// Return error if values does not contain at least one positive value and one negative value | |
if (!positive || !negative) return '#NUM!'; | |
// Initialize guess and resultRate | |
var guess = (typeof guess === 'undefined') ? 0.1 : guess; | |
var resultRate = guess; | |
// Set maximum epsilon for end of iteration | |
var epsMax = 1e-10; | |
// Set maximum number of iterations | |
var iterMax = 50; | |
// Implement Newton's method | |
var newRate, epsRate, resultValue; | |
var iteration = 0; | |
var contLoop = true; | |
do { | |
resultValue = irrResult(values, dates, resultRate); | |
newRate = resultRate - resultValue / irrResultDeriv(values, dates, resultRate); | |
epsRate = Math.abs(newRate - resultRate); | |
resultRate = newRate; | |
if (resultRate < -1) { | |
resultRate = -0.999999999 | |
}; | |
contLoop = (epsRate > epsMax) && (Math.abs(resultValue) > epsMax); | |
} while(contLoop && (++iteration < iterMax)); | |
if(contLoop) return '#NUM!'; | |
// Return internal rate of return | |
return resultRate; | |
} |
Thanks, was hunting the same bug....
You are welcome 😄
El lun., 16 nov. 2020 23:12, Todd Horst <[email protected]> escribió:
… ***@***.**** commented on this gist.
------------------------------
Thanks, was hunting the same bug....
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/67ea9dbcf43a2b6eabf7408433d157ae#gistcomment-3529709>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AEJDPC55DTIZZTMOWU4NEJDSQGPT3ANCNFSM4TXYNCGQ>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is a problem when the result are negative values. If resultRate < -1 then irrResult and irrResultDeriv fail because r is a negative number, so, as it is impossible to have results that are inferior to -1, I correct those values to be able to carry on with the calculation.