Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

stats.chi2

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.

How it works

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.

Syntax

scipy.stats.chi2

Parameters

NameTypeDescription
dfarray_likeDegrees of freedom, used as the distribution's shape parameter.
locarray_like, optionalLocation parameter; default is 0.
scalearray_like, optionalScale parameter; default is 1.

Returns

A continuous chi-squared distribution object; calling its inherited methods returns probabilities, quantiles, moments, or random variates as appropriate.

Key points

  • chi2 represents the central chi-squared continuous distribution.
  • Pass degrees of freedom through the df shape parameter.
  • The object inherits the standard continuous-distribution API, including probability, quantile, moment, and sampling operations.
  • It can be viewed as a gamma distribution with the documented parameter mapping.
  • Use ncx2 rather than chi2 when a noncentrality parameter is required.
  • Optional location and scale arguments allow transformed versions of the distribution.

Common mistakes

  • The documented density applies for positive observations and positive degrees of freedom; values outside the distribution's support do not represent ordinary chi-squared outcomes.
  • Using loc or scale changes the distribution, so omit them when the standard chi-squared distribution is intended.

Example: Tail probability and sampling

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.

When to use it

Hypothesis-test calculations

Evaluate significance or tail probabilities for a chi-squared statistic.

Critical-value lookup

Convert probabilities into chi-squared critical values.

Random variate generation

Create simulated chi-squared observations for testing or modeling.

Location and scale transformation

Use a transformed distribution with a nondefault location or scale.

Interview questions

QWhat is 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.

QWhat are the support and density characteristics of the chi-squared distribution?

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.

QWhich common operations does 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.ncx2
  • scipy.stats.gamma
  • scipy.stats.rv_continuous

Source: scipy.stats.chi2. Checked 2026-10-15.