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);
    }
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个JavaIRR计算工具类的示例代码: ```java import org.apache.commons.math3.analysis.UnivariateFunction; import org.apache.commons.math3.analysis.solvers.BrentSolver; import org.apache.commons.math3.exception.TooManyIterationsException; public class IRRCalculator { /** * 计算IRR * @param cashFlows 现金流数组,第一个元素是负值,表示投资的现金流出;其余元素是正值,表示现金流入。 * @return IRR(内部收益率) */ public static double calculateIRR(double[] cashFlows) { UnivariateFunction f = new UnivariateFunction() { @Override public double value(double x) { double npv = 0.0; for (int i = 0; i < cashFlows.length; i++) { npv += cashFlows[i] / Math.pow(1 + x, i); } return npv; } }; BrentSolver solver = new BrentSolver(); solver.setAbsoluteAccuracy(1e-10); solver.setMaximalIterationCount(1000); try { return solver.solve(f, -1.0, 1.0); } catch (TooManyIterationsException e) { return Double.NaN; } } /** * 测试 */ public static void main(String[] args) { double[] cashFlows = {-1000.0, 200.0, 250.0, 300.0, 350.0, 400.0, 450.0}; double irr = calculateIRR(cashFlows) * 100; System.out.println("IRR: " + irr + "%"); } } ``` 在这个示例代码中,我们定义了一个静态方法`calculateIRR`,它接受一个现金流数组作为参数,并返回IRR。我们使用了Apache Commons Math库中的`UnivariateFunction`和`BrentSolver`来计算IRR,其中`UnivariateFunction`表示一元函数,`BrentSolver`表示一个非线性方程求解器。在`calculateIRR`方法中,我们首先定义了一个一元函数`f`,它根据输入的IRR计算现金流的净现值(NPV)。然后,我们使用`BrentSolver`来求解方程`f(x) = 0`,其中`x`就是IRR。最后,我们在`main`方法中使用示例数据来测试`calculateIRR`方法,计算IRR并打印出来。 需要注意的是,在现实中,现金流一般是以年为单位计算的。如果现金流是以月、季度或其他时间间隔计算的,我们需要根据实际情况将其转换为年。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值