Java-积分正态分布的数值方法?

时间:2013-05-16 09:08:31

标签: java helper normal-distribution

代码: (是的,我知道它效率低,而且通常非常糟糕。只是想知道为什么它不起作用。)数学可能是错误的,但很可能是编码。任何帮助都会很好。

public class NormalDistribution {

static void normalDistributionDecode(double u, double sd, double x) {
    double z = ((x-u)/sd);
    System.out.println("The point "+x+" on a normal distribution "
     +"with standard deviation "+sd+" and mean "+u+" translates"
     +" to point "+z+" on a normal distribution with mean 0 and"
     +" standard distribution 1");
}

static void standardNDAlgorithm(double x) {

    double pi    = 3.1415926535;
    double e     = 2.7182818284;
    double fx    = (Math.pow(e, (-0.5*Math.pow(x , 2))))/(2*pi);
    long end     = -10;
    double fend  = (Math.pow(e, (-0.5*Math.pow(end , 2))))/(2*pi);
    double a = x-0.25;
    double b = x-0.75;
    double c = x-1.0;
    double d = x-1.25;
    double E = x-1.5;
    double f = x-1.75;
    double g = x-2.2;
    double h = x-2.25;
    double i = x-2.5;
    double j = x-2.75;
    double k = x-3.0;
    double l = x-3.25;
    double m = x-3.5;
    double n = x-3.75;
    double o = x-4.0;
    double p = x-4.25;
    double q = x-4.5;
    double r = x-4.75;
    double s = x-5.0;
    double t = x-5.25;
    double u = x-5.5;
    double v = x-5.75;
    double w = x-6.0;
    double y = x-6.25;
    double z = x-6.5;
    double cw= 0.25;
    double f1 = (Math.pow(e, (-0.5*Math.pow(a , 2))))/(2*pi);
    double f2 = (Math.pow(e, (-0.5*Math.pow(b , 2))))/(2*pi);
    double f3 = (Math.pow(e, (-0.5*Math.pow(c , 2))))/(2*pi);
    double f4 = (Math.pow(e, (-0.5*Math.pow(d , 2))))/(2*pi);
    double f5 = (Math.pow(e, (-0.5*Math.pow(E , 2))))/(2*pi);
    double f6 = (Math.pow(e, (-0.5*Math.pow(f , 2))))/(2*pi);
    double f7 = (Math.pow(e, (-0.5*Math.pow(g , 2))))/(2*pi);       
    double f8 = (Math.pow(e, (-0.5*Math.pow(h , 2))))/(2*pi);
    double f9 = (Math.pow(e, (-0.5*Math.pow(i , 2))))/(2*pi);
    double f10= (Math.pow(e, (-0.5*Math.pow(j , 2))))/(2*pi);
    double f11= (Math.pow(e, (-0.5*Math.pow(k , 2))))/(2*pi);
    double f12= (Math.pow(e, (-0.5*Math.pow(l , 2))))/(2*pi);
    double f13= (Math.pow(e, (-0.5*Math.pow(m , 2))))/(2*pi);
    double f14= (Math.pow(e, (-0.5*Math.pow(n , 2))))/(2*pi);
    double f15= (Math.pow(e, (-0.5*Math.pow(o , 2))))/(2*pi);
    double f16= (Math.pow(e, (-0.5*Math.pow(p , 2))))/(2*pi);
    double f17= (Math.pow(e, (-0.5*Math.pow(q , 2))))/(2*pi);
    double f18= (Math.pow(e, (-0.5*Math.pow(r , 2))))/(2*pi);
    double f19= (Math.pow(e, (-0.5*Math.pow(s , 2))))/(2*pi);
    double f20= (Math.pow(e, (-0.5*Math.pow(t , 2))))/(2*pi);
    double f21= (Math.pow(e, (-0.5*Math.pow(u , 2))))/(2*pi);
    double f22= (Math.pow(e, (-0.5*Math.pow(v , 2))))/(2*pi);
    double f23= (Math.pow(e, (-0.5*Math.pow(w , 2))))/(2*pi);
    double f24= (Math.pow(e, (-0.5*Math.pow(y , 2))))/(2*pi);
    double f25= (Math.pow(e, (-0.5*Math.pow(z , 2))))/(2*pi);       
    double integfx= 0.5*cw*(fend+fx+2*(f1+f2+f3+f4+f5+f6+f7+f8+f9+f10
              +f11+f12+f13+f14+f15+f16+f17+f18+f19+f20+f21+f22+f23+f24+f25));
    System.out.println(integfx);
    System.out.println(fx);
    System.out.println(f1);
}
public static void main(String[] args) {
normalDistributionDecode(0, 1, 10);
standardNDAlgorithm(10);
}
}

忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略忽略ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore ignore

1 个答案:

答案 0 :(得分:0)

看来你正试图通过辛普森的规则来计算概率密度函数的积分近似值。有不同的方法,但这是一种可接受的方法。

我发现您使用的公式存在一些问题,无法计算每个点的密度。

(1)分母应为sd * sqrt(2 * pi)

(2)每个点a,b,c等应该是(n.nn - u)/sd,其中n.nn是0.25,0.75等。

可能还有其他问题。