VOOZH about

URL: https://www.geeksforgeeks.org/python/numpy-random-chisquare-in-python/

⇱ Chi-Square Distribution in NumPy - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Chi-Square Distribution in NumPy

Last Updated : 10 Dec, 2025

The Chi-Square Distribution appears when you add up the squares of independent standard normal random variables. It is widely used in hypothesis testing, goodness-of-fit tests, variance testing, and statistical modeling. In NumPy, we generate Chi-Square values using numpy.random.chisquare().

Example: Here, we generate one Chi-Square random value using df = 2 (degrees of freedom).


Output
0.3396810372458067

Explanation: np.random.chisquare(df=2) generates one value formed by summing the squares of 2 standard normal variables.

Syntax

numpy.random.chisquare(df, size=None)

Parameters:

  • df: Degrees of freedom (controls the shape of the curve)
  • size: Shape of output array

Examples

Example 1: In this example, we generate 5 Chi-Square random values with df = 2.


Output
[0.9664276 0.9718178 0.05315296 5.76413224 0.17793754]

Explanation: np.random.chisquare(..., size=5) returns an array of 5 simulated Chi-Square outcomes.

Example 2: Here, we generate Chi-Square values with a higher degree of freedom (df = 5).


Output
[5.5554776 8.39987081 1.96062558 1.15995049]

Explanation: np.random.chisquare(5) produces values based on 5 summed squares, giving a wider and more symmetric shape.

Example 3: In this example, we generate a 2×3 matrix of Chi-Square random values.


Output
[[0.04478657 4.99709829 0.51742473]
 [0.87978466 1.96904351 3.51846182]]

Explanation: size=(2,3) creates a matrix where each element is a Chi-Square-distributed value.

Visualizing the Chi-Square Distribution

Visualizing the generated numbers helps in understanding how the Chi-Square curve behaves for different degrees of freedom.

Output

👁 ChisquareDistributionPlot
Chi-square Distribution Plot

Explanation:

  • np.random.chisquare(df, size) simulates 1000 Chi-Square values.
  • plt.hist(..., density=True) displays their frequency.
  • chi2.pdf(x, df) computes the true theoretical curve.
  • The red line shows how the actual Chi-Square distribution should look for df = 2.
  • Chi-Square distributions are right-skewed, especially at lower degrees of freedom.
Comment
Article Tags: