Skip to main content

Chi Square Distribution in Python


In this tutorial you will learn:

  • What is Chi Square Distribution?
  • Chi Square Distribution Implementation in python
  • Visualization of Chi Square Distribution

Chi Square Distribution

Chi Square Distribution (also known as Chi-Squared Distribution) is a continuous distribution formed by taking the sum of squared standard normal deviates, while a standard normal deviates is the random sample from the normal distribution. As in Chi Square Distribution, the values are squared, so the result produced are always positive. Chi Square distribution is a special case of the gamma distribution. The mean of a Chi Square distribution is its degrees of freedom.

Square Distribution in Python

Chi Square distribution in python is implemented using an inbuilt function chisquare() which is included in the random module of NumPy library. The chisquare () function takes in two mandatory parameters. First parameter “size” is the size of the output array which could be 1D, 2D, 3D or n-dimensional (depending on the programmer’s requirements). The second parameter “df” is the degree of freedom and it can always be an unsigned integer.
Lets take an example, we will generate a 1D array(1,4) of Chi Square distribution with degree of freedom 3. In the second line of code we are importing random module from NumPy library and in fourth line we are calling the Chi Square function and passing it the size of output array and degree of freedom. In results of this example, you can observe that all the values generated are positive.

  1. #importing the random module
  2. from numpy import random
  3. #applying the Chi Square function
  4. res_arr= random. chisquare(df= 2.5, size=4)
  5. #printing the results
  6. print('1D array of size(1,4) having Chi Square distribution with degree of freedom 3:\n')
  7. print(res_arr)

In this example we will generate a 2D array of Chi Square distribution having size (3,3) with degree of freedom 2.
  1. #importing the random module
  2. from numpy import random
  3. #here we are using Chi Square function to generate Chi Square distribution of size 3 x 3 with default scale value
  4. res_arr = random.chisquare(size=(3,3),df=2)
  5. print('2D Chi Square Distribution as output from chisquare() function:\n')
  6. #printing the result
  7. print(res_arr)

Lets take another example, in this example we will generate a 3D array of Chi Square distribution of the size(3,2,6) with degree of freedom 2
  1. #importing the random module
  2. from numpy import random
  3. #here we are using Chi Square function to generate Chi Square distribution of size 3 x 2 x 6
  4. res = random.chisquare(size=(3,2,6), df=2)
  5. print('3D Chi Square Distribution as output from chisquare() function:\n')
  6. #printing the result
  7. print(res)

Visualization of Chi Square Distribution

In this example we will visualize the Chi Square Distribution with degree of freedom 4.

  1. #importing all the required modules and packages
  2. from numpy import random
  3. import matplotlib.pyplot as mpl
  4. import seaborn as sb
  5. #here we are using Chi Square function to generate distributions of size 1000 with 4 degrees of freedom
  6. sb.distplot(random.chisquare(size=1000, df=4), hist=False, label='Chi_Sq Distr')
  7. #plotting the graph
  8. mpl.show()

Tags

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.