内部收益率计算
作者:互联网
记录工具代码内部收益率计算
public class IrrUtils {
private static final double IRR_GUESS = 0.0001d;
public static double irr(double[] values, double guess) {
if (values == null || values.length <= 0) {
return 0.0d / 0.0;
}
int maxlterationCount = 20;
double absoluteAccuracy = 1.0E-007D;
double x0 = guess;
int i = 0;
while (i < maxlterationCount) {
double fValue = 0.0D;
double fDerivative = 0.0D;
for (int k = 0; k < values.length; k++) {
fValue += values[k] / Math.pow(1.0D + x0, k);
fDerivative += -k * values[k] / Math.pow(1.0D + x0, k + 1);
}
double x1 = x0 - fValue / fDerivative;
if (Math.abs(x1 - x0) <= absoluteAccuracy) {
return x1;
}
x0 = x1;
i++;
}
return 0.0d / 0.0;
}
// IRR特殊情况下会出现无穷值,导致无法计算出正确的IRR,需先计算估算值 guess
public static double calcGuess(double[] values,int deducTerm) {
if (values == null || values.length <= 0) {
return 0.0d / 0.0;
}
double guessValue = 0d;
double pemSum = 0d;
double svg = 0d;
for(int i=0; i < deducTerm; i++){
pemSum += values[i];
}
svg = Arrays.stream(values).max().getAsDouble();
guessValue = pemSum == 0 ? IRR_GUESS : (svg / pemSum);
return guessValue;
}
public static void main(String[] args) {
double irr1 = irr(dataArr1(), IRR_GUESS);
double irr2 = irr(dataArr2(), IRR_GUESS);
double irr3 = irr(dataArr3(), IRR_GUESS);
double irr4 = irr(dataArr4(), IRR_GUESS);
System.out.println(irr1); // 01830707136274641
System.out.println(irr2); // 0.029238953211348458
System.out.println(irr3); // 0.04255038879830903
System.out.println(irr4); // 0.016155046256780534
DecimalFormat df = new DecimalFormat("0.00");
String format = df.format(irr4); // 0.0426
System.out.println(format); // 0.0426
}
// 測試數據1
public static double[] dataArr1() {
double[] arr = {-213260, -213260, -213260, -213260, -213260, -213260, -213260, -213260, -213260, -213260, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2275697, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
return arr;
}
// 測試數據2
public static double[] dataArr2() {
double[] arr = {-213260, -213260, -213260, -213260, -213260, -213260, -213260, -213260, -213260, -213260, 129000, 29000, 29000, 29000, 29000, 179000, 29000, 29000, 29000, 29000, 229000, 29000, 29000, 29000, 29000, 279000, 29000, 29000, 29000, 29000, 279000, 29000, 29000, 29000, 29000, 279000, 29000, 29000, 29000, 29000, 2641067, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
return arr;
}
// 測試數據3
public static double[] dataArr3() {
double[] arr = {-213260, -213260, -213260, -213260, -213260, -213260, -213260, -213260, -213260, -213260, 170000, 70000, 70000, 70000, 70000, 220000, 70000, 70000, 70000, 70000, 270000, 70000, 70000, 70000, 70000, 320000, 70000, 70000, 70000, 70000, 320000, 70000, 70000, 70000, 70000, 320000, 70000, 70000, 70000, 70000, 2768437, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
return arr;
}
// 測試數據4
public static double[] dataArr4() {
double[] arr = {-15000 ,-15000 ,-13500 ,-13500 ,-13500,1500,1500,1500,1500,1500,72375,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
return arr;
}
}
标签:内部,double,IRR,values,计算,static,收益率,public 来源: https://blog.csdn.net/Zcat1024/article/details/120368059