Javascript IRR (Internal rate of return) Formula Accuracy

后端 未结 7 1599
名媛妹妹
名媛妹妹 2021-02-04 20:00

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\

7条回答
  •  悲哀的现实
    2021-02-04 20:48

    I would like to build on from @Zohaib Answer, but what I would like to do is show undefined where appropriate. The best I can do is get it to equal to zero. I am using this simple dataset irr_arr=[-100, 100, 100, 100, 100,100]. I would appreciate some advice.

    //IRRCALC funtion that handles irr going to infinity 
    
    function IRRCalc_test(CArray) {
    
      min = 0.0;
      max = 1.0;
      c=0;
      do {
        guest = (min + max) / 2;
        NPV = 0;
        for (var j=0; j 0) {
          min = guest;
          c++; 
        }
        else {
          max = guest;
          c++;
        }
    
        if(c>=15){ return guest*100; }
      } while(Math.abs(NPV) > 0.000001);
      return guest*100;
    }
    
    
    // some testing 
    
    irr_arr=[-100, 100, 100, 100, 100,100]
    irr_res_arr_expected=[0,0,61.8,83.93,92.76,96.6]
    for(i=1;i<=irr_arr.length;i++){
    console.log("irr_arr - ",irr_arr.slice(0,i));
    console.log("IRRCalc - ",IRRCalc(irr_arr.slice(0,i)))
    //console.log("irr_expected - ", irr_res_arr_expected[i-1])
    //if(IRRCalc(irr_arr.slice(0,i))===parseFloat(irr_res_arr_expected[i-1]).toFixed(2)){console.log(i,"- TRUE")} else {console.log(i,"- FALSE")}
    }
    

    this is the output

    irr_arr -  [-100]
    IRRCalc -  0.00      <<<<<<<<<<<<<<<<<<<------- this should be 'undefined' and not 'zero'
    irr_arr -  [-100, 100]
    IRRCalc -  0.00
    irr_arr -  [-100, 100, 100]
    IRRCalc -  61.80
    irr_arr -  [-100, 100, 100, 100]
    IRRCalc -  83.93
    irr_arr -  [-100, 100, 100, 100, 100]
    IRRCalc -  92.76
    irr_arr -  [-100, 100, 100, 100, 100, 100]
    IRRCalc -  96.60
    

    here is the excel of what I am trying to produce

提交回复
热议问题