Javascript IRR (Internal rate of return) Formula Accuracy

后端 未结 7 1673
名媛妹妹
名媛妹妹 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:40

    Try this.

    function NPV(discountRate, cashFlow){
        var npv = 0;
        for(var t = 0; t < cashFlow.length; t++) {
            npv += cashFlow[t] / Math.pow((1+ discountRate),t);
        }
        return npv;
    }
    
    
    function IRR(cashFlow,guess){
        guess = guess ? guess : 0.1;
        var npv;
        var cnt = 0;
        do
        {
            npv = NPV(guess,cashFlow);
            guess+= 0.001;
    
            cnt++;
        }
        while(npv > 0)
    
        return guess;
    }
    

提交回复
热议问题