Advertisement
lewapkon

Probability

May 31st, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.30 KB | None | 0 0
  1. /**
  2.      * Calculates probability if the system of points is fortuitous.
  3.      * @param   x   Chi squared.
  4.      * @param   n   Degrees of freedom
  5.      * @return  Probability that the system of points is fortuitous.
  6.      */
  7.     private double calculateP(double x, int df) {
  8.  
  9.         double a = 0, y = 0, s = 0;
  10.         double e = 0, c = 0, z = 0;
  11.         Boolean even = false;                     /* True if df is an even number */
  12.  
  13.         final double LOG_SQRT_PI = 0.5723649429247000870717135; /* log(sqrt(pi)) */
  14.         final double I_SQRT_PI = 0.5641895835477562869480795;   /* 1 / sqrt(pi) */
  15.  
  16.         if (x <= 0.0 || df < 1) {
  17.             return 1.0;
  18.         }
  19.  
  20.         a = 0.5 * x;
  21.         if (x % 2 == 0) even = true;
  22.         if (df > 1) {
  23.             y = ex(-a);
  24.         }
  25.         s = (even ? y : (2.0 * poz(-Math.sqrt(x))));
  26.         if (df > 2) {
  27.             x = 0.5 * (df - 1.0);
  28.             z = (even ? 1.0 : 0.5);
  29.             if (a > 20.0) {
  30.                 e = (even ? 0.0 : LOG_SQRT_PI);
  31.                 c = Math.log(a);
  32.                 while (z <= x) {
  33.                     e = Math.log(z) + e;
  34.                     s += ex(c * z - a - e);
  35.                     z += 1.0;
  36.                 }
  37.                 return s;
  38.             } else {
  39.                 e = (even ? 1.0 : (I_SQRT_PI / Math.sqrt(a)));
  40.                 c = 0.0;
  41.                 while (z <= x) {
  42.                     e = e * (a / z);
  43.                     c = c + e;
  44.                     z += 1.0;
  45.                 }
  46.                 return c * y + s;
  47.             }
  48.         } else {
  49.             return s;
  50.         }
  51.     }
  52.  
  53.     private double ex(double x) {
  54.         return (x < -20.0) ? 0.0 : Math.exp(x);
  55.     }
  56.  
  57.     private double poz(double z) {
  58.         double y, x, w;
  59.         final double Z_MAX = 6.0;              /* Maximum meaningful z value */
  60.  
  61.         if (z == 0.0) {
  62.             x = 0.0;
  63.         } else {
  64.             y = 0.5 * Math.abs(z);
  65.             if (y >= (Z_MAX * 0.5)) {
  66.                 x = 1.0;
  67.             } else if (y < 1.0) {
  68.                 w = y * y;
  69.                 x = ((((((((0.000124818987 * w
  70.                         - 0.001075204047) * w + 0.005198775019) * w
  71.                         - 0.019198292004) * w + 0.059054035642) * w
  72.                         - 0.151968751364) * w + 0.319152932694) * w
  73.                         - 0.531923007300) * w + 0.797884560593) * y * 2.0;
  74.             } else {
  75.                 y -= 2.0;
  76.                 x = (((((((((((((-0.000045255659 * y
  77.                         + 0.000152529290) * y - 0.000019538132) * y
  78.                         - 0.000676904986) * y + 0.001390604284) * y
  79.                         - 0.000794620820) * y - 0.002034254874) * y
  80.                         + 0.006549791214) * y - 0.010557625006) * y
  81.                         + 0.011630447319) * y - 0.009279453341) * y
  82.                         + 0.005353579108) * y - 0.002141268741) * y
  83.                         + 0.000535310849) * y + 0.999936657524;
  84.             }
  85.         }
  86.         return (z > 0.0) ? ((x + 1.0) * 0.5) : ((1.0 - x) * 0.5);
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement