NonCentralChiSquaredDistribution implements ProbabilityDistribution<Double> { @Override public double getCDF(final Double x) { Validate.notNull(x, "x"); if (x < 0) { return 0.0; } if ((_dofOverTwo + _lambdaOverTwo) > 1000) { return getFraserApproxCDF(x); } double regGammaStart = 0; final double halfX = x / 2.0; final double logX = Math.log(halfX); try { regGammaStart = Gamma.regularizedGammaP(_dofOverTwo + _k, halfX); } catch (final org.apache.commons.math.MathException ex) { throw new MathException(ex); } double sum = _pStart * regGammaStart; double oldSum = Double.NEGATIVE_INFINITY; double p = _pStart; double regGamma = regGammaStart; double temp; int i = _k; while (i > 0 && Math.abs(sum - oldSum) / sum > _eps) { i--; p *= (i + 1) / _lambdaOverTwo; temp = (_dofOverTwo + i) * logX - halfX - Gamma.logGamma(_dofOverTwo + i + 1); regGamma += Math.exp(temp); oldSum = sum; sum += p * regGamma; } p = _pStart; regGamma = regGammaStart; oldSum = Double.NEGATIVE_INFINITY; i = _k; while (Math.abs(sum - oldSum) / sum > _eps) { i++; p *= _lambdaOverTwo / i; temp = (_dofOverTwo + i - 1) * logX - halfX - Gamma.logGamma(_dofOverTwo + i); regGamma -= Math.exp(temp); oldSum = sum; sum += p * regGamma; } return sum; } NonCentralChiSquaredDistribution(final double degrees, final double nonCentrality); @Override double getCDF(final Double x); @Override double getInverseCDF(final Double p); @Override double getPDF(final Double x); @Override double nextRandom(); double getDegrees(); double getNonCentrality(); @Override int hashCode(); @Override boolean equals(final Object obj);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullX() { DIST.getCDF(null); }
@Test public void testLargeValues() { double x = 123; double dof = 6.4; double nonCent = 100.34; NonCentralChiSquaredDistribution dist = new NonCentralChiSquaredDistribution(dof, nonCent); assertEquals(0.7930769, dist.getCDF(x), 1e-6); x = 455.038; dof = 12; nonCent = 444.44; dist = new NonCentralChiSquaredDistribution(dof, nonCent); assertEquals(0.4961805, dist.getCDF(x), 1e-6); x = 999400; dof = 500; nonCent = 1000000; dist = new NonCentralChiSquaredDistribution(dof, nonCent); assertEquals(0.2913029, dist.getCDF(x), 1e-6); }
@Test public void debugTest() { final double dof = 3.666; final double nonCentrality = 75; final double x = 13.89; final NonCentralChiSquaredDistribution chiSq1 = new NonCentralChiSquaredDistribution(dof, nonCentrality); final double y1 = Math.log(chiSq1.getCDF(x)); assertEquals(-15.92129, y1, 1e-5); }