title: Fitting a Weibull distribution using Scipy
body: <p>I am trying to recreate maximum likelihood distribution fitting, I can already do this in Matlab and R, but now I want to use scipy. In particular, I would like to estimate the Weibull distribution parameters for my data set.</p>
  <p>I have tried this:</p>
  <pre><code>import scipy.stats as s
  import numpy as np
  import matplotlib.pyplot as plt
  
  def weib(x,n,a):
      return (a / n) * (x / n)**(a - 1) * np.exp(-(x / n)**a)
  
  data = np.loadtxt(&quot;stack_data.csv&quot;)
  
  (loc, scale) = s.exponweib.fit_loc_scale(data, 1, 1)
  print loc, scale
  
  x = np.linspace(data.min(), data.max(), 1000)
  plt.plot(x, weib(x, loc, scale))
  plt.hist(data, data.max(), density=True)
  plt.show()
  </code></pre>
  <p>And get this:</p>
  <pre><code>(2.5827280639441961, 3.4955032285727947)
  </code></pre>
  <p>And a distribution that looks like this:</p>
  <p><img src="https://i.stack.imgur.com/kkMR2m.jpg" alt="Weibull distribution using Scipy" /></p>
  <p>I have been using the <code>exponweib</code> after reading this <a href="http://www.johndcook.com/distributions_scipy.html" rel="noreferrer">http://www.johndcook.com/distributions_scipy.html</a>.  I have also tried the other Weibull functions in scipy (just in case!).</p>
  <p>In Matlab (using the Distribution Fitting Tool - see screenshot) and in R (using both the MASS library function <code>fitdistr</code> and the GAMLSS package) I get a (loc) and b (scale) parameters more like 1.58463497 5.93030013.  I believe all three methods use the maximum likelihood method for distribution fitting.</p>
  <p><img src="https://i.stack.imgur.com/WtNWt.jpg" alt="Weibull distribution using Matlab" /></p>
  <p>I have posted my data <a href="https://i.stack.imgur.com/kkMR2m.jpg" rel="noreferrer">here</a> if you would like to have a go! And for completeness I am using Python 2.7.5, Scipy 0.12.0, R 2.15.2 and Matlab 2012b.</p>
  <p>Why am I getting a different result!?</p>