test_class.file: jtstoolkit/src/test/java/ec/tstoolkit/dstats/GammaTest.java
test_class.identifier: GammaTest
test_case.body: @Test
      public void testRandom() {
          double s = 0;
          double ssq=0;
          int N = 1000000;
          long t0 = System.currentTimeMillis();
          Gamma dist = new Gamma(7.48, 0.295);
          MersenneTwister rng = new MersenneTwister(0);
          for (int i = 0; i < N; ++i) {
              double r=dist.random(rng);
              s += r;
              ssq+=r*r;
          }
          long t1 = System.currentTimeMillis();
          assertEquals(dist.getExpectation(), s / N, .01);
          assertEquals(dist.getVariance(), ssq / N-s/N*s/N, .01);
  //        System.out.println("New");
  //        System.out.println(t1 - t0);
  //        System.out.println(s / N);
  //        System.out.println(ssq / N-s/N*s/N);
  //        System.out.println(dist.getExpectation());
  //        System.out.println(dist.getVariance());
      }
test_case.class_method_signature: GammaTest.testRandom()
test_case.full_signature: @Test public void testRandom()
test_case.identifier: testRandom
test_case.invocations: currentTimeMillis random currentTimeMillis assertEquals getExpectation assertEquals getVariance
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testRandom()
focal_class.declarator: MAXGAM = 171.624376956302725
focal_class.modifier: public static final
focal_class.original_string: public static final double MAXGAM = 171.624376956302725;
focal_class.type: double
focal_class.var_name: MAXGAM
focal_class.declarator: alpha
focal_class.modifier: private final
focal_class.original_string: private final double alpha, beta;
focal_class.type: double
focal_class.var_name: alpha
focal_class.declarator: MAXSTIR = 143.01608
focal_class.modifier: private static final
focal_class.original_string: private static final double MAXSTIR = 143.01608;
focal_class.type: double
focal_class.var_name: MAXSTIR
focal_class.file: jtstoolkit/src/main/java/ec/tstoolkit/dstats/Gamma.java
focal_class.identifier: Gamma
focal_class.interfaces: implements IContinuousDistribution
focal_class.class_method_signature: Gamma.Gamma(double alpha, double beta)
focal_class.full_signature: public  Gamma(double alpha, double beta)
focal_class.identifier: Gamma
focal_class.modifiers: public
focal_class.parameters: (double alpha, double beta)
focal_class.signature:  Gamma(double alpha, double beta)
focal_class.class_method_signature: Gamma.getDensity(double x)
focal_class.full_signature: @Override public double getDensity(double x)
focal_class.identifier: getDensity
focal_class.modifiers: @Override public
focal_class.parameters: (double x)
focal_class.return: double
focal_class.signature: double getDensity(double x)
focal_class.class_method_signature: Gamma.getShape()
focal_class.full_signature: public double getShape()
focal_class.identifier: getShape
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: double
focal_class.signature: double getShape()
focal_class.class_method_signature: Gamma.getScale()
focal_class.full_signature: public double getScale()
focal_class.identifier: getScale
focal_class.modifiers: public
focal_class.parameters: ()
focal_class.return: double
focal_class.signature: double getScale()
focal_class.class_method_signature: Gamma.getLeftBound()
focal_class.full_signature: @Override public double getLeftBound()
focal_class.identifier: getLeftBound
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: double
focal_class.signature: double getLeftBound()
focal_class.class_method_signature: Gamma.getRightBound()
focal_class.full_signature: @Override public double getRightBound()
focal_class.identifier: getRightBound
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: double
focal_class.signature: double getRightBound()
focal_class.class_method_signature: Gamma.getDescription()
focal_class.full_signature: @Override public String getDescription()
focal_class.identifier: getDescription
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: String
focal_class.signature: String getDescription()
focal_class.class_method_signature: Gamma.getExpectation()
focal_class.full_signature: @Override public double getExpectation()
focal_class.identifier: getExpectation
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: double
focal_class.signature: double getExpectation()
focal_class.class_method_signature: Gamma.getProbability(double x, ProbabilityType pt)
focal_class.full_signature: @Override public double getProbability(double x, ProbabilityType pt)
focal_class.identifier: getProbability
focal_class.modifiers: @Override public
focal_class.parameters: (double x, ProbabilityType pt)
focal_class.return: double
focal_class.signature: double getProbability(double x, ProbabilityType pt)
focal_class.class_method_signature: Gamma.getProbabilityInverse(double p, ProbabilityType pt)
focal_class.full_signature: @Override public double getProbabilityInverse(double p, ProbabilityType pt)
focal_class.identifier: getProbabilityInverse
focal_class.modifiers: @Override public
focal_class.parameters: (double p, ProbabilityType pt)
focal_class.return: double
focal_class.signature: double getProbabilityInverse(double p, ProbabilityType pt)
focal_class.class_method_signature: Gamma.getVariance()
focal_class.full_signature: @Override public double getVariance()
focal_class.identifier: getVariance
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: double
focal_class.signature: double getVariance()
focal_class.class_method_signature: Gamma.hasLeftBound()
focal_class.full_signature: @Override public BoundaryType hasLeftBound()
focal_class.identifier: hasLeftBound
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: BoundaryType
focal_class.signature: BoundaryType hasLeftBound()
focal_class.class_method_signature: Gamma.hasRightBound()
focal_class.full_signature: @Override public BoundaryType hasRightBound()
focal_class.identifier: hasRightBound
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: BoundaryType
focal_class.signature: BoundaryType hasRightBound()
focal_class.class_method_signature: Gamma.isSymmetrical()
focal_class.full_signature: @Override public boolean isSymmetrical()
focal_class.identifier: isSymmetrical
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: boolean
focal_class.signature: boolean isSymmetrical()
focal_class.class_method_signature: Gamma.random(IRandomNumberGenerator rng)
focal_class.full_signature: @Override public double random(IRandomNumberGenerator rng)
focal_class.identifier: random
focal_class.modifiers: @Override public
focal_class.parameters: (IRandomNumberGenerator rng)
focal_class.return: double
focal_class.signature: double random(IRandomNumberGenerator rng)
focal_class.class_method_signature: Gamma.beta(double a, double b)
focal_class.full_signature: public static double beta(double a, double b)
focal_class.identifier: beta
focal_class.modifiers: public static
focal_class.parameters: (double a, double b)
focal_class.return: double
focal_class.signature: double beta(double a, double b)
focal_class.class_method_signature: Gamma.gamma(double x)
focal_class.full_signature: public static double gamma(double x)
focal_class.identifier: gamma
focal_class.modifiers: public static
focal_class.parameters: (double x)
focal_class.return: double
focal_class.signature: double gamma(double x)
focal_class.class_method_signature: Gamma.incompleteBeta(double aa, double bb, double xx)
focal_class.full_signature: public static double incompleteBeta(double aa, double bb, double xx)
focal_class.identifier: incompleteBeta
focal_class.modifiers: public static
focal_class.parameters: (double aa, double bb, double xx)
focal_class.return: double
focal_class.signature: double incompleteBeta(double aa, double bb, double xx)
focal_class.class_method_signature: Gamma.incompleteBetaFraction1(double a, double b, double x)
focal_class.full_signature: public static double incompleteBetaFraction1(double a, double b, double x)
focal_class.identifier: incompleteBetaFraction1
focal_class.modifiers: public static
focal_class.parameters: (double a, double b, double x)
focal_class.return: double
focal_class.signature: double incompleteBetaFraction1(double a, double b, double x)
focal_class.class_method_signature: Gamma.incompleteBetaFraction2(double a, double b, double x)
focal_class.full_signature: static double incompleteBetaFraction2(double a, double b, double x)
focal_class.identifier: incompleteBetaFraction2
focal_class.modifiers: static
focal_class.parameters: (double a, double b, double x)
focal_class.return: double
focal_class.signature: double incompleteBetaFraction2(double a, double b, double x)
focal_class.class_method_signature: Gamma.incompleteGamma(double a, double x)
focal_class.full_signature: public static double incompleteGamma(double a, double x)
focal_class.identifier: incompleteGamma
focal_class.modifiers: public static
focal_class.parameters: (double a, double x)
focal_class.return: double
focal_class.signature: double incompleteGamma(double a, double x)
focal_class.class_method_signature: Gamma.incompleteGammaComplement(double a, double x)
focal_class.full_signature: public static double incompleteGammaComplement(double a, double x)
focal_class.identifier: incompleteGammaComplement
focal_class.modifiers: public static
focal_class.parameters: (double a, double x)
focal_class.return: double
focal_class.signature: double incompleteGammaComplement(double a, double x)
focal_class.class_method_signature: Gamma.logGamma(double x)
focal_class.full_signature: public static double logGamma(double x)
focal_class.identifier: logGamma
focal_class.modifiers: public static
focal_class.parameters: (double x)
focal_class.return: double
focal_class.signature: double logGamma(double x)
focal_class.class_method_signature: Gamma.powerSeries(double a, double b, double x)
focal_class.full_signature: static double powerSeries(double a, double b, double x)
focal_class.identifier: powerSeries
focal_class.modifiers: static
focal_class.parameters: (double a, double b, double x)
focal_class.return: double
focal_class.signature: double powerSeries(double a, double b, double x)
focal_class.class_method_signature: Gamma.stirlingFormula(double x)
focal_class.full_signature: static double stirlingFormula(double x)
focal_class.identifier: stirlingFormula
focal_class.modifiers: static
focal_class.parameters: (double x)
focal_class.return: double
focal_class.signature: double stirlingFormula(double x)
focal_method.body: @Override
      public double random(IRandomNumberGenerator rng) throws DStatException {
          double a = alpha;
          double aa = -1.0, aaa = -1.0,
                  b = 0.0, c = 0.0, d = 0.0, e, r, s = 0.0, si = 0.0, ss = 0.0, q0 = 0.0,
                  q1 = 0.0416666664, q2 = 0.0208333723, q3 = 0.0079849875,
                  q4 = 0.0015746717, q5 = -0.0003349403, q6 = 0.0003340332,
                  q7 = 0.0006053049, q8 = -0.0004701849, q9 = 0.0001710320,
                  a1 = 0.333333333, a2 = -0.249999949, a3 = 0.199999867,
                  a4 = -0.166677482, a5 = 0.142873973, a6 = -0.124385581,
                  a7 = 0.110368310, a8 = -0.112750886, a9 = 0.104089866,
                  e1 = 1.000000000, e2 = 0.499999994, e3 = 0.166666848,
                  e4 = 0.041664508, e5 = 0.008345522, e6 = 0.001353826,
                  e7 = 0.000247453;
  
          double gds, p, q, t, sign_u, u, v, w, x;
          double v1, v2, v12;
  
          if (a < 1.0) { // CASE A: Acceptance rejection algorithm gs
              b = 1.0 + 0.36788794412 * a;              // Step 1
              while (true) {
                  p = b * rng.nextDouble();
                  if (p <= 1.0) {                       // Step 2. Case gds <= 1
                      gds = Math.exp(Math.log(p) / a);
                      if (Math.log(rng.nextDouble()) <= -gds) {
                          return (gds * beta);
                      }
                  } else {                                // Step 3. Case gds > 1
                      gds = -Math.log((b - p) / a);
                      if (Math.log(rng.nextDouble()) <= ((a - 1.0) * Math.log(gds))) {
                          return (gds * beta);
                      }
                  }
              }
          } else {        // CASE B: Acceptance complement algorithm gd (gaussian distribution, box muller transformation)
              if (a != aa) {                        // Step 1. Preparations
                  ss = a - 0.5;
                  s = Math.sqrt(ss);
                  d = 5.656854249 - 12.0 * s;
              }
              // Step 2. Normal deviate
              do {
                  v1 = 2.0 * rng.nextDouble() - 1.0;
                  v2 = 2.0 * rng.nextDouble() - 1.0;
                  v12 = v1 * v1 + v2 * v2;
              } while (v12 > 1.0);
              t = v1 * Math.sqrt(-2.0 * Math.log(v12) / v12);
              x = s + 0.5 * t;
              gds = x * x;
              if (t >= 0.0) {
                  return (gds * beta);         // Immediate acceptance
              }
              u = rng.nextDouble();                // Step 3. Uniform random number
              if (d * u <= t * t * t) {
                  return (gds * beta); // Squeeze acceptance
              }
              if (a != aaa) {                           // Step 4. Set-up for hat case
                  r = 1.0 / a;
                  q0 = ((((((((q9 * r + q8) * r + q7) * r + q6) * r + q5) * r + q4)
                          * r + q3) * r + q2) * r + q1) * r;
                  if (a > 3.686) {
                      if (a > 13.022) {
                          b = 1.77;
                          si = 0.75;
                          c = 0.1515 / s;
                      } else {
                          b = 1.654 + 0.0076 * ss;
                          si = 1.68 / s + 0.275;
                          c = 0.062 / s + 0.024;
                      }
                  } else {
                      b = 0.463 + s - 0.178 * ss;
                      si = 1.235;
                      c = 0.195 / s - 0.079 + 0.016 * s;
                  }
              }
              if (x > 0.0) {                        // Step 5. Calculation of q
                  v = t / (s + s);                  // Step 6.
                  if (Math.abs(v) > 0.25) {
                      q = q0 - s * t + 0.25 * t * t + (ss + ss) * Math.log(1.0 + v);
                  } else {
                      q = q0 + 0.5 * t * t * ((((((((a9 * v + a8) * v + a7) * v + a6)
                              * v + a5) * v + a4) * v + a3) * v + a2) * v + a1) * v;
                  }								  // Step 7. Quotient acceptance
                  if (Math.log(1.0 - u) <= q) {
                      return (gds * beta);
                  }
              }
  
              while (true) {              			      // Step 8. Double exponential deviate t
                  do {
                      e = -Math.log(rng.nextDouble());
                      u = rng.nextDouble();
                      u = u + u - 1.0;
                      sign_u = (u > 0) ? 1.0 : -1.0;
                      t = b + (e * si) * sign_u;
                  } while (t <= -0.71874483771719); // Step 9. Rejection of t
                  v = t / (s + s);                  // Step 10. New q(t)
                  if (Math.abs(v) > 0.25) {
                      q = q0 - s * t + 0.25 * t * t + (ss + ss) * Math.log(1.0 + v);
                  } else {
                      q = q0 + 0.5 * t * t * ((((((((a9 * v + a8) * v + a7) * v + a6)
                              * v + a5) * v + a4) * v + a3) * v + a2) * v + a1) * v;
                  }
                  if (q <= 0.0) {
                      continue;           // Step 11.
                  }
                  if (q > 0.5) {
                      w = Math.exp(q) - 1.0;
                  } else {
                      w = ((((((e7 * q + e6) * q + e5) * q + e4) * q + e3) * q + e2)
                              * q + e1) * q;
                  }                    			  // Step 12. Hat acceptance
                  if (c * u * sign_u <= w * Math.exp(e - 0.5 * t * t)) {
                      x = s + 0.5 * t;
                      return (x * x * beta);
                  }
              }
          }
      }
focal_method.class_method_signature: Gamma.random(IRandomNumberGenerator rng)
focal_method.full_signature: @Override public double random(IRandomNumberGenerator rng)
focal_method.identifier: random
focal_method.invocations: nextDouble exp log log nextDouble log log nextDouble log sqrt nextDouble nextDouble sqrt log nextDouble abs log log log nextDouble nextDouble abs log exp exp
focal_method.modifiers: @Override public
focal_method.parameters: (IRandomNumberGenerator rng)
focal_method.return: double
focal_method.signature: double random(IRandomNumberGenerator rng)