ITPub博客

首页 > IT职业 > IT图书 > Chi-square 用途5

Chi-square 用途5

IT图书 作者:oldmonst 时间:2021-09-09 10:08:34 0 删除 编辑

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.



作者:乌然娅措
链接:https://www.jianshu.com/p/7d1cb9e81fbd


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/4758/viewspace-2817296/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: javascript for循环
下一篇: Go(day
请登录后发表评论 登录
全部评论

注册时间:2019-04-25

  • 博文量
    67
  • 访问量
    495