Data Science Day 7:
Where does the name of Chi-square distribution come from?
From the first day,we knowChi-squaredistribution is thesum ofthe squared standard deviates, known as variance.
If we investigate the standard deviates, then we find an interesting relation betweenChi-squareandNormal Distribution:
If a variable follows the standard normal distribution, then its square follows the Chi-square distribution.
Data Visualization:
We first generate 1000random normal variablesand use the histogram graph to show their distribution. We see it approaches a normal distribution.
[caption id="attachment_550" align="alignnone" width="556"]
Normal Distribution of a Random Variable[/caption]

Wesquare the random normal variablesand plot the values. We see it approaches a Chi-square distribution. This graph is one random variable squared, so we will show another distribution with a higher degree.
[caption id="attachment_551" align="alignnone" width="551"]
Chi-square Distribution of the Random Number Square with degree 1[/caption]

We use the square ofthree normal random variablesand we can see the graph indeed follows Chi-square distribution.
[caption id="attachment_552" align="alignnone" width="570"]
Chi-square distribution of random variable square with degree 3[/caption]

Source code
import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt
#generate random normal variables
n=np.random.normal(0,1,1000)
#histogram plot of random normal variables
count, bins, ignored=plt.hist(n,30, density=True )
plt.plot(bins, 1/(np.sqrt(2*np.pi)) *np.exp(-(bins)**2/(2*1**2)), linewidth=2, color="green")
plt.show()
#Square random normal variables
chisq=n**2
#plot Square random normal variables
plt.hist(chisq, 20, normed=True )
x=np.arange(0,15,0.1)
plt.plot(x, stats.chi2.pdf(x,df=1), color="r", lw=2)
plt.show()
#generate Chi-square with three random normal variables
norm=stats.norm(0,1)
x1=norm.rvs(size=1000)**2
x2=norm.rvs(size=1000)**2
x3=norm.rvs(size=1000)**2
f=x1+x2+x3
#plot f
plt.hist(f,20,normed=True,color="yellow")
x=np.arange(0,20,0.1)
plt.plot(x,stats.chi2.pdf(x,df=3),color="pink",lw=2)
plt.show()
by GitHub
Thanks very much to Jie Chen, she helped me to understand the connection between the Normal and Chi-Square Distribution.
网友评论