Python Python Tutorial File Handling NumPy Tutorial NumPy Random NumPy ufunc Pandas Tutorial Pandas Cleaning Data Pandas Correlations Pandas Plotting SciPy Tutorial



NumPy Chi Square Distribution

The NumPy Chi Square Distribution is a probability distribution that is used to model the distribution of the sum of the squares of k independent standard normal random variables. It is a special case of the gamma distribution and is commonly used in statistical analysis to test the goodness of fit of a sample to a theoretical distribution.

The chi square distribution is characterized by a single parameter, the degrees of freedom (df), which determines the shape of the distribution. The degrees of freedom parameter is equal to the number of independent standard normal random variables that are squared and summed to obtain the chi square random variable.

The probability density function (PDF) of the chi square distribution is given by:

where k is the degrees of freedom parameter and Γ is the gamma function.

The cumulative distribution function (CDF) of the chi square distribution is given by:

where I is the regularized incomplete gamma function.

Code Examples

To generate random numbers from the chi square distribution, we can use the numpy.random.chisquare() function. The function takes two arguments, the degrees of freedom and the size of the output array.

<pre>
import numpy as np

# Generate 1000 random numbers from the chi square distribution with 3 degrees of freedom
x = np.random.chisquare(3, 1000)

# Print the first 10 numbers
print(x[:10])
</pre>

To calculate the PDF and CDF of the chi square distribution, we can use the scipy.stats.chi2() function. The function takes the degrees of freedom as its only argument and returns a frozen distribution object that can be used to calculate the PDF and CDF.

<pre>
import numpy as np
from scipy.stats import chi2

# Create a frozen distribution object with 3 degrees of freedom
dist = chi2(3)

# Calculate the PDF and CDF of the distribution at x=2
pdf = dist.pdf(2)
cdf = dist.cdf(2)

# Print the results
print("PDF at x=2:", pdf)
print("CDF at x=2:", cdf)
</pre>

References

Activity