简体   繁体   中英

Show the pdf of a chi-squared distribution using python

I'm trying to reconstruct the pdf of the chi-squared distribution with 3 degrees of freedom from a simulated sample. Here is my python code:

import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt

norm = stats.norm(0, 1)

x1 = [x * x for x in np.random.randn(1000)]
x2 = [x * x for x in np.random.randn(1000)]
x3 = [x * x for x in np.random.randn(1000)]

f = x1 + x2 + x3

plt.hist(f, 100)
plt.show()

The result I got was this.

具有3个自由度的Chi分布

Of course this is wrong. As shown in Wikipedia, the pdf of the chi-squared distribution with 3 degrees of freedom should go upwards first from zero and go downwards later, not something keep climbing like mine. Is there anything wrong with my code? The formula I used was as follows:

Q = x1^2 + x2^2 + x3^2

where x1, x2 and x3 are independent, standard normal random variables.

Although I tried your code and got the same result as you, if you use your 'norm' variable to generate the random values it seems to work.

import numpy as np
import scipy.stats as stats
import matplotlib.pyplot as plt

norm = stats.norm(0, 1)

x1 = norm.rvs(size=100000)**2
x2 = norm.rvs(size=100000)**2
x3 = norm.rvs(size=100000)**2

f = x1 + x2 + x3

plt.hist(f, 60, normed=True)

# Plot the theoretical density of f
x = np.arange(0, 30, .05)
plt.plot(x, stats.chi2.pdf(x, df=3), color='r', lw=2)
plt.show()

The result I got was

Chi2的直方图

The '+' operator works differently on Python lists than on Numpy arrays.

f = x1 + x2 + x3

concatenates three lists into one. However, you want to add the content of the three lists element-wise, which could be done like this:

f = np.array(x1) + np.array(x2) + np.array(x3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM