java 实现Excel irr计算(改进版)

import java.util.ArrayList;
import java.util.List;

public class IrrUtil {
    /**迭代次数*/
    public static int LOOPNUM=1000;
    /**最小差异*/
    public static final double MINDIF=0.00000001;
    
    
    /**
     * @desc 使用方法参考main方法
     * @param cashFlow  资金流
     * @return 收益率
     */
    public static double getIrr(List<Double> cashFlow){
        double flowOut=cashFlow.get(0);
        double minValue=0d;
        double maxValue=1d;
        double testValue=0d;
        while(LOOPNUM>0){
            testValue=(minValue+maxValue)/2;
            double npv=NPV(cashFlow,testValue);
            if(Math.abs(flowOut+npv)<MINDIF){
                break;
            }else if(Math.abs(flowOut)>npv){
                maxValue=testValue;
            }else{
                minValue=testValue;
            }
            LOOPNUM--;
        }
        return testValue;
    }
    
    public static double NPV(List<Double> flowInArr,double rate){
        double npv=0;
        for(int i=1;i<flowInArr.size();i++){
            npv+=flowInArr.get(i)/Math.pow(1+rate, i);
        }
        return npv;
    }
    
    public static void main(String[] args) {
        double flowOut=-237000d;
        List<Double> flowInArr=new ArrayList<Double>();
        flowInArr.add(flowOut);
        flowInArr.add(22643.999991d);
        flowInArr.add(22643.999991d);
        flowInArr.add(22643.999991d);
        flowInArr.add(22643.999991d);
        flowInArr.add(22643.999991d);
        flowInArr.add(22643.999991d);
        flowInArr.add(22643.999991d);
        flowInArr.add(22643.999991d);
        flowInArr.add(22643.999991d);
        flowInArr.add(22643.999991d);
        flowInArr.add(22643.999991d);
        flowInArr.add(22643.999991d);
        
        
        System.out.println(IrrUtil.getIrr(flowInArr)*12);
    }
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值