Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fit t distribution using scipy with predetermined mean and std(loc & scale)?

Tags:

python

scipy

How can I fit t distribution using scipy.stats.t.fit() with predetermined mean and std?

The question is, I have a standardized dataset, with mean=0 and std=1, I only want to get df of t distribution. But when I do scipy.stats.t.fit(data), it outputs df, loc, scale, and loc&sclae not necessarily equal to 0&1.

How can I solve this? Thanks!

like image 480
user5025141 Avatar asked Aug 17 '16 15:08

user5025141


1 Answers

In the call to .fit(), use the arguments floc=0 and fscale=1 to fix these parameters.

Here's an example. First, import t and generate a sample to work with:

In [24]: from scipy.stats import t

In [25]: np.random.seed(123)

In [26]: sample = t.rvs(3, loc=0, scale=1, size=10000)

Now use the .fit() method to fit the t distribution to the sample, constraining the location to 0 and the scale to 1:

In [27]: t.fit(sample, floc=0, fscale=1)
Out[27]: (3.1099609375000048, 0, 1)

There are more examples (using different distributions) in the fit docstring and here on stackoverflow.

like image 85
Warren Weckesser Avatar answered Nov 18 '22 21:11

Warren Weckesser