Hypothesis-test calculations
Evaluate significance or tail probabilities for a chi-squared statistic.
SciPy · Function
scipy.stats.chi2 is SciPy’s central chi-squared continuous distribution, parameterized by degrees of freedom (df). It provides the standard continuous-distribution methods for probabilities, quantiles, moments, and random sampling.
chi2 is a continuous distribution object parameterized by degrees of freedom. It supplies the standard rv_continuous methods, including density, cumulative probabilities, inverse quantiles, moments, and random sampling; loc and scale can shift or rescale the distribution.
scipy.stats.chi2| Name | Type | Description |
|---|---|---|
df | array_like | Degrees of freedom, used as the distribution's shape parameter. |
loc | array_like, optional | Location parameter; default is 0. |
scale | array_like, optional | Scale parameter; default is 1. |
A continuous chi-squared distribution object; calling its inherited methods returns probabilities, quantiles, moments, or random variates as appropriate.
chi2 represents the central chi-squared continuous distribution.df shape parameter.ncx2 rather than chi2 when a noncentrality parameter is required.loc or scale changes the distribution, so omit them when the standard chi-squared distribution is intended.from scipy.stats import chi2
# Upper-tail probability for a statistic of 10 with 4 degrees of freedom
p_value = chi2.sf(10, df=4)
print(p_value)
# Generate random values from the same distribution
samples = chi2.rvs(df=4, size=5, random_state=0)
print(samples)Compute an upper-tail probability and draw reproducible random values from a chi-squared distribution with four degrees of freedom.
Evaluate significance or tail probabilities for a chi-squared statistic.
Convert probabilities into chi-squared critical values.
Create simulated chi-squared observations for testing or modeling.
Use a transformed distribution with a nondefault location or scale.
scipy.stats.chi2 and what parameter defines it?chi2 is an instance of rv_continuous, so it inherits generic distribution methods such as pdf, cdf, ppf, and rvs. Its distribution-specific shape parameter is df, the degrees of freedom.
Follow-up: How is it related to the gamma distribution? It is the gamma special case with a = df/2, loc = 0, and scale = 2.
The probability density is proportional to x**(df/2 - 1) * exp(-x/2) and includes the normalization factor involving Gamma(df/2). The documented support requires x > 0 and df > 0.
Follow-up: What distribution should be used for a noncentral chi-square model? Use scipy.stats.ncx2.
chi2 provide?Use chi2.cdf(x, df) for the probability up to x, chi2.sf(x, df) for the upper-tail probability, and chi2.ppf(q, df) to invert a lower-tail probability. chi2.rvs(df, size=...) generates random variates.
Follow-up: How can repeated calculations reuse fixed parameters? Create a frozen object such as rv = chi2(df) and call its methods without passing df each time.
scipy.stats.ncx2scipy.stats.gammascipy.stats.rv_continuous