import java.math.BigDecimal;
public class ExcelIRRUtil {
public static double irr(double[] income) {
return irr(income, 0.1D);
}
public static double irr(double[] values, double guess) {
int maxIterationCount = 20;
double absoluteAccuracy = 1.0E-007D;
double x0 = guess;
int i = 0;
while (i < maxIterationCount) {
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.0D);
}
public static double irr(double ybf,int jft,int kct,double zsy) {
double[] income = new double[100];
if(jft <= kct){
for (int i = 0; i < jft; i++) {
income[i] = ybf;
}
zsy = -zsy;
for (int i = jft; i < income.length; i++) {
if(i == kct){
income[i] = zsy;
}else{
income[i] = 0;
}
}
}
if(jft > kct){
for (int i = 0; i < kct; i++) {
income[i] = ybf;
}
zsy = -zsy;
for (int i = kct; i < income.length; i++) {
if(i == kct){
income[i] = zsy;
}else{
income[i] = 0;
}
}
}
return irr(income, 0.00001d);
}
public static String irr_gz(int year){
String tb[] = new String[]{"2.3002","2.3905","2.5344","2.6247","2.7018","2.8297","2.8953","2.8968","2.8974","2.8975","2.9228","2.9859",",0705","3.1604","3.2394","3.2934","3.3255","3.345","3.3614","3.3841","3.4095","3.4287","3.4426","3.4525","3.4594","3.4645","3.4688","3.4735","3.4797","3.4885","3.4984","3.5074","3.5156","3.5233","3.5305","3.5374","3.5443","3.5511","3.5581","3.5656","3.5728","3.5789","3.5843","3.589","3.5933","3.5972","3.6011","3.605","3.6092","3.6137"};
if(year >= 50){
year = 50;
}
return tb[year - 1];
}
//考察期13年
public static void main(String[] args) {
double i1 = irr(5000.5, 10, 13, 60000.88);
double i2 = irr(5000, 10, 5, 600000);
double i4 = irr(5000, 10, 9, 600000);
double i3 = irr(5000, 10, 10, 600000);
System.out.println(i1);
double[] income = {5000,
5000,
5000,
5000,
5000,
-50000,
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, 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,
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, 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 };
double ret = irr(income, 0.00001d);
System.out.println((new BigDecimal(ret)));
}
}