Reputation: 95
I am trying to fit a t distribution to some data that I have. In order to test it out I first tried to generate a sample from a fixed distribution and try to fit on it. Below is the code that I am using.
samp = t.rvs(loc=0, scale=0.6, df=1.3, size=150)
param = t.fit(samp)
x = linspace(-5,5,100)
pdf_fitted = t.pdf(x,loc=param[0],scale=param[1],df=param[2])
pdf = t.pdf(x,loc=0,scale=0.6,df=1.3)
title('Student\'s t Distribution')
plot(x,pdf_fitted,'r-',x,pdf,'b-')
hist(samp, normed=1,alpha=0.3)
show()
print(param)
Now, one would expect that pdf
and pdf_fitted
be essentially the same. However, that is not the case. When the plots are shown the original and fitted distributions look very different. Moreover, the parameters obtained do not match the ones specified (loc=0, scale=0.6, df=1.3) at all! This confuses me because I am simply adapting the code from http://glowingpython.blogspot.com/2012/07/distribution-fitting-with-scipy.html to work with t distributions. Can someone tell me if there are any nuances with fitting t distributions? Thanks
Upvotes: 2
Views: 3806
Reputation: 114946
The fit
method of scipy.stats.t
returns (df, loc, scale)
, so this line
pdf_fitted = t.pdf(x,loc=param[0],scale=param[1],df=param[2])
should be
pdf_fitted = t.pdf(x, loc=param[1], scale=param[2], df=param[0])
The example that you linked to uses the normal distribution, which doesn't have an additional shape parameter, so in that case, param[0]
is the location and param[1]
is the scale.
Upvotes: 5