title: Fitting probability distribution to data
body: <p>I'm trying to fit data to probability distribution (gamma function in my case).</p>
  
  <p>With the method of moments I achieved some success:</p>
  
  <pre><code>mean, var = data.mean(), data.var()
  α, β = mean ** 2 / var, var / mean
  
  x = np.linspace(0, 100)
  
  plt.plot(x, gamma.pdf(x, α, 0, β))
  # Pandas is in use
  data.plot(kind='hist', xlim=(0, 100), bins=500, normed=True, color='lightblue')
  data.dropna().plot(kind='kde', xlim=(0, 100), style='r--')
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/QUabS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QUabS.png" alt="Gamma test 01"></a></p>
  
  <p>However this is not satisfying so I decided to use scipy's method <code>fit</code>:</p>
  
  <pre><code>args = gamma.fit(data)
  
  x = np.linspace(0, 100)
  
  plt.plot(x, gamma.pdf(x, *args))
  data.plot(kind='hist', xlim=(0, 100), bins=500, normed=True, color='lightblue')
  data.dropna().plot(kind='kde', xlim=(0, 100), style='r--')
  </code></pre>
  
  <p>But the only result I achieved was this distribution:</p>
  
  <p><a href="https://i.stack.imgur.com/mUfRB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mUfRB.png" alt="Gamma test 02"></a></p>
  
  <p>Could someone describe me what I'm doing wrong?
  I thought that <code>gamma.fit</code> should do at least as good as method of moments.</p>
  
  <p><strong>UPD:</strong></p>
  
  <p><code>gamma.fit</code> returns this: <code>(0.00077655597754514266, -6.0499462017751218e-25, 3.6337557495676194)</code></p>
  
  <p><code>data.describe()</code> shows that data is ok:</p>
  
  <pre><code>count    5546.000000
  mean       45.601515
  std        28.563211
  min         0.000000
  25%        35.000000
  50%        42.000000
  75%        52.000000
  max      1488.000000
  dtype: float64
  </code></pre>
  
  <p>Trying to filtering data with <code>data[[data &gt; 0]]</code> force location to 0</p>
  
  <pre><code>args = gamma.fit(list(data[[data &gt; 0]]), floc=0)
  args
  
  (7.897834117836718, 0, 5.7749678991293987)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/gpm8v.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gpm8v.png" alt="Gamma test 03"></a></p>
  
  <p>gives better results. Seems like it's working now. Thanks!</p>