Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript IRR (Internal rate of return) Formula Accuracy

I'm using a IRR function in javascript to create calculation a that is done in excel using its own IRR function. The problem is mine is little off and I have no idea why. Here's the code below.

var IRRval = [];

IRRval.push(-financed);
for (i = 0; i < period; i++) {
    IRRval.push(rental);
}

var IRR = IRRCalc(IRRval, 0.001) * 0.01;

function IRRCalc(CArray, guest) {
    inc = 0.000001;
    do {
        guest += inc;
        NPV = 0;
        for (var j=0; j < CArray.length; j++) {
            NPV += CArray[j] / Math.pow((1 + guest), j);
        }
    } while (NPV > 0);
    return guest * 100;
}

Now if you use these figures:


Period 24

Financed 22000

Rental 1017.5000

My Result is: 0.008523000000000175

Excel Result is: 0.008522918


OR


Period 42

Financed 218000

Rental 5917.1429

My Result is: 0.006247000000000489

Excel Result is: 0.00624616


The Excel function is called: =IRR(T12:T73,0.01) T12-T73 is the same figures I'm using.

Any help would be much appreciated, Thanks Jason

UPDATE

I've solved it by changing the values below. But now the performance is too slow. Any ideas on how to improve this?

IRRCalc(IRRval, 0.001)

//to

IRRCalc(IRRval, 0.0001)

inc = 0.000001;

//to

inc = 0.00000001;
like image 772
dciso Avatar asked Feb 26 '13 12:02

dciso


People also ask

What is the internal rate of return (IRR)?

Internal rate of return is a capital budgeting calculation for deciding which projects or investments under consideration are investment-worthy and ranking them. IRR is the discount rate for which the net present value (NPV) equals zero (when time-adjusted future cash flows equal the initial investment).

What is IRR formula in Excel?

It is defined as the discount rate which makes the net present value of the cash flows from the investment equal to zero. In Excel, we have IRR formula to compute the discount rate based on the cashflows for an investment/project.

What is internal rate of return in project management?

Internal rate of return is an important method for analyzing and selecting business projects or other investments. The advantage of internal rate of return is that businesses will not choose projects estimated to generate a return below their hurdle rate (required rate of return) which includes a risk premium.

What is the license for the function computing IRR?

Following is the code snippet of the function computing IRR: This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL) This member has not yet provided a Biography.


2 Answers

We modified the code to achieve performance and accuracy. Try this:

function IRRCalc(CArray) {

  min = 0.0;
  max = 1.0;
  do {
    guest = (min + max) / 2;
    NPV = 0;
    for (var j=0; j<CArray.length; j++) {
          NPV += CArray[j]/Math.pow((1+guest),j);
    }
    if (NPV > 0) {
      min = guest;
    }
    else {
      max = guest;
    }
  } while(Math.abs(NPV) > 0.000001);
  return guest * 100;
}
like image 60
Zohaib Avatar answered Oct 20 '22 19:10

Zohaib


After a quick skim read through your code, the error seems to be related to floating point precision error. More information can be found here: http://ajaxian.com/archives/crock-on-floating-points-in-javascript

In older javascript engines, if you did 0.3 + 0.3 you get something like 0.600000000001

Though most javascript engines today return 0.6, under the hood, the problem remains. Adding floats together causes unexpected results. So in your case the

inc = 0.000001;
guest += inc;

seems to me, to be the problem.

A way to solve this would be to use whole numbers. So instead of 0.000001 you would use 1 and instead of 0.001 you would use 1000. Then divide your return result by 100000

like image 33
whitneyit Avatar answered Oct 20 '22 20:10

whitneyit