Chi-Squared Distribution Algorithm
| This page was generated with the assistance of AI. Please report any inaccuracies. |
ChiSqDist implements the chi-squared ( ) distribution as described in section 6.14.8 of Numerical Recipes, 3rd Edition (see Reference).
For usage-oriented examples, see Statistical Distributions; this page focuses on the underlying formulas and how they map onto the gamma function described in Gamma Function.
Probability density function
The chi-squared distribution has a single parameter , usually an integer known as the number of degrees of freedom. Writing , its density is
Its mean and variance are simply
and, for , the density has a single mode at .
ChiSqDist avoids repeated calls to the gamma function by precomputing, once per instance, the logarithm of the normalizing constant:
so that p(x2) reduces to a single exp call, exp(-0.5 * (x2 - (nu - 2) * log(x2)) - fac).
Cumulative distribution function
The chi-squared distribution is a special case of the gamma distribution, so its cumulative distribution function is the regularized incomplete gamma function from Gamma Function:
Its complement can be obtained either as or, more accurately when is close to 1, directly as :
ChiSqDist.cdf(x2) calls Gamma.gammp(0.5 * nu, 0.5 * x2) and therefore inherits the same series/continued-fraction/quadrature selection, and the same MaxIterationsExceededException, documented in Gamma Function.
Inverse cumulative distribution function
The inverse cdf is expressed through the inverse of on its second argument, denoted :
ChiSqDist.invcdf(p) calls Gamma.invgammp(p, 0.5 * nu) and doubles the result, so it can likewise throw MaxIterationsExceededException in the rare cases where Halley’s method does not converge within its iteration budget.
import com.irurueta.statistics.ChiSqDist;
import com.irurueta.statistics.MaxIterationsExceededException;
ChiSqDist chiSqDist = new ChiSqDist(5.0);
double density = chiSqDist.p(3.0);
try {
double probabilityBelow = chiSqDist.cdf(3.0);
double valueAt95Percent = chiSqDist.invcdf(0.95);
} catch (final MaxIterationsExceededException e) {
// convergence was not reached for these inputs
}