public static double xnormi(double p){
  double arg, t, t2, t3, xnum, xden, qinvp, x, pc;
  final double c[]={2.515517,.802853,.010328};
  final double d[]={1.432788,.189269,.001308};
  if (p <= .5) {
    arg=-2.0 * Math.log(p);
    t=Math.sqrt(arg);
    t2=t * t;
    t3=t2 * t;
    xnum=c[0] + c[1] * t + c[2] * t2;
    xden=1.0 + d[0] * t + d[1] * t2 + d[2] * t3;
    qinvp=t - xnum / xden;
    x=-qinvp;
    return x;
  }
 else {
    pc=1.0 - p;
    arg=-2.0 * Math.log(pc);
    t=Math.sqrt(arg);
    t2=t * t;
    t3=t2 * t;
    xnum=c[0] + c[1] * t + c[2] * t2;
    xden=1.0 + d[0] * t + d[1] * t2 + d[2] * t3;
    x=t - xnum / xden;
    return x;
  }
}

public static double xnormi ( double p ) { double arg , t , t2 , t3 , xnum , xden , qinvp , x , pc ; final double c [ ] = { 2.515517 , .802853 , .010328 } ; final double d [ ] = { 1.432788 , .189269 , .001308 } ; if ( p <= .5 ) { arg = - 2.0 * Math . log ( p ) ; t = Math . sqrt ( arg ) ; t2 = t * t ; t3 = t2 * t ; xnum = c [ 0 ] + c [ 1 ] * t + c [ 2 ] * t2 ; xden = 1.0 + d [ 0 ] * t + d [ 1 ] * t2 + d [ 2 ] * t3 ; qinvp = t - xnum / xden ; x = - qinvp ; return x ; } else { pc = 1.0 - p ; arg = - 2.0 * Math . log ( pc ) ; t = Math . sqrt ( arg ) ; t2 = t * t ; t3 = t2 * t ; xnum = c [ 0 ] + c [ 1 ] * t + c [ 2 ] * t2 ; xden = 1.0 + d [ 0 ] * t + d [ 1 ] * t2 + d [ 2 ] * t3 ; x = t - xnum / xden ; return x ; } }
this method calculates the normal cdf inverse function . < p > let phi ( x ) be the normal cdf . suppose that q calculates 1 . 0 - phi ( x ) , and that qinv calculates qinv ( p ) for p in ( 0 . 0 , . 5 ] . then for p . le . . 5 , x = phiinv ( p ) = - qinv ( p ) . for p . gt . . 5 , x = phiinv ( p ) = qinv ( 1 . 0 - p ) . the formula for approximating qinv is taken from abramowitz and stegun , handbook of mathematical functions , dover , 9th printing , formula 26 . 2 . 23 , page 933 . the error in x is claimed to be less than 4 . 5e - 4 in absolute value .
