fitting location parameter in the gamma distribution with scipy
<p>Would somebody be able to explain to me how to use the location parameter with the gamma.fit function in Scipy?</p>

<p>It seems to me that a location parameter (&mu;) changes the support of the distribution from x &ge; 0 to y = ( x - &mu; ) &ge; 0. If &mu; is positive then aren't we losing all the data which doesn't satisfy x - &mu; &ge; 0?</p>

<p>Thanks!</p>

<p>The <code>fit</code> function takes all of the data into consideration when finding a fit.  Adding noise to your data will alter the fit parameters and can give a distribution that does not represent the data very well.  So we have to be a bit clever when we are using <code>fit</code>.</p>

<p>Below is some code that generates data, <code>y1</code>, with <code>loc=2</code> and <code>scale=1</code> using numpy.  It also adds noise to the data over the range 0 to 10 to create <code>y2</code>.  Fitting <code>y1</code> yield excellent results, but attempting to fit the noisy <code>y2</code> is problematic.  The noise we added smears out the distribution.  However, we can also hold 1 or more parameters constant when fitting the data.  In this case we pass <code>floc=2</code> to the <code>fit</code>, which forces the location to be held at <code>2</code> when performing the fit, returning much better results.</p>

<pre><code>from scipy.stats import gamma
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(0,10,.1)
y1 = np.random.gamma(shape=1, scale=1, size=1000) + 2  # sets loc = 2 
y2 = np.hstack((y1, 10*np.random.rand(100)))  # add noise from 0 to 10

# fit the distributions, get the PDF distribution using the parameters
shape1, loc1, scale1 = gamma.fit(y1)
g1 = gamma.pdf(x=x, a=shape1, loc=loc1, scale=scale1)

shape2, loc2, scale2 = gamma.fit(y2)
g2 = gamma.pdf(x=x, a=shape2, loc=loc2, scale=scale2)

# again fit the distribution, but force loc=2
shape3, loc3, scale3 = gamma.fit(y2, floc=2)
g3 = gamma.pdf(x=x, a=shape3, loc=loc3, scale=scale3)
</code></pre>

<p>And make some plots...</p>

<pre><code># plot the distributions and fits.  to lazy to do iteration today
fig, axes = plt.subplots(1, 3, figsize=(13,4))
ax = axes[0]
ax.hist(y1, bins=40, normed=True);
ax.plot(x, g1, 'r-', linewidth=6, alpha=.6)
ax.annotate(s='shape = %.3f\nloc = %.3f\nscale = %.3f' %(shape1, loc1, scale1), xy=(6,.2))
ax.set_title('gamma fit')

ax = axes[1]
ax.hist(y2, bins=40, normed=True);
ax.plot(x, g2, 'r-', linewidth=6, alpha=.6)
ax.annotate(s='shape = %.3f\nloc = %.3f\nscale = %.3f' %(shape2, loc2, scale2), xy=(6,.2))
ax.set_title('gamma fit with noise')

ax = axes[2]
ax.hist(y2, bins=40, normed=True);
ax.plot(x, g3, 'r-', linewidth=6, alpha=.6)
ax.annotate(s='shape = %.3f\nloc = %.3f\nscale = %.3f' %(shape3, loc3, scale3), xy=(6,.2))
ax.set_title('gamma fit w/ noise, location forced')
</code></pre>

<p><a href="http://i.stack.imgur.com/JoY9O.png" rel="nofollow"><img src="http://i.stack.imgur.com/JoY9O.png" alt="enter image description here"></a></p>

python scipy distribution gamma