title: Show the pdf of a chi-squared distribution using python
body: <p>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:</p>
  
  <pre><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()
  </code></pre>
  
  <p>The result I got was this.</p>
  
  <p><a href="https://i.stack.imgur.com/yNHjC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yNHjC.png" alt="Chi distribution with a freedom of 3"></a></p>
  
  <p>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:</p>
  
  <blockquote>
    <p>Q = x1^2 + x2^2 + x3^2</p>
  </blockquote>
  
  <p>where x1, x2 and x3 are independent, standard normal random variables.</p>