Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a pdf from scipy.stats in a generic way

I am running some goodness of fit tests using scipy.stats in Python 2.7.10.

for distrName in distrNameList:
    distr = getattr(distributions, distrName)
    param = distr.fit(sample)
    pdf   = distr.pdf(???)

What do I pass into distr.pdf() to get the values of the best-fit pdf on the list of sample points of interest, called abscissas?

like image 214
gt6989b Avatar asked Jun 18 '15 20:06

gt6989b


People also ask

How do I get SciPy stats?

All of the statistics functions are located in the sub-package scipy. stats and a fairly complete listing of these functions can be obtained using info(stats) function. A list of random variables available can also be obtained from the docstring for the stats sub-package.

What is PDF in SciPy?

Probability density function at x of the given RV. Parameters xarray_like.

How do I download SciPy from Python?

We can install the SciPy library by using pip command; run the following command in the terminal: pip install scipy.

What is SciPy stats uniform?

scipy. stats. uniform() is a Uniform continuous random variable. It is inherited from the of generic methods as an instance of the rv_continuous class. It completes the methods with details specific for this particular distribution.


2 Answers

From the documentation, the .fit() method returns:

shape, loc, scale : tuple of floats MLEs for any shape statistics, followed by those for location and scale.

and the .pdf() method accepts:

x : array_like quantiles

arg1, arg2, arg3,... : array_like The shape parameter(s) for the distribution (see docstring of the instance object for more information)

loc : array_like, optional location parameter (default=0)

scale : array_like, optional

So essentially you would do something like this:

import numpy as np
from scipy import stats
from matplotlib import pyplot as plt


# some random variates drawn from a beta distribution
rvs = stats.beta.rvs(2, 5, loc=0, scale=1, size=1000)

# estimate distribution parameters, in this case (a, b, loc, scale)
params = stats.beta.fit(rvs)

# evaluate PDF
x = np.linspace(0, 1, 1000)
pdf = stats.beta.pdf(x, *params)

# plot
fig, ax = plt.subplots(1, 1)
ax.hold(True)
ax.hist(rvs, normed=True)
ax.plot(x, pdf, '--r')

enter image description here

like image 94
ali_m Avatar answered Sep 19 '22 14:09

ali_m


To evaluate the pdf at abscissas, you would pass abcissas as the first argument to pdf. To specify the parameters, use the * operator to unpack the param tuple and pass those values to distr.pdf:

pdf = distr.pdf(abscissas, *param)

For example,

import numpy as np
import scipy.stats as stats

distrNameList = ['beta', 'expon', 'gamma']
sample = stats.norm(0, 1).rvs(1000)
abscissas = np.linspace(0,1, 10)
for distrName in distrNameList:
    distr = getattr(stats.distributions, distrName)
    param = distr.fit(sample)
    pdf = distr.pdf(abscissas, *param)
    print(pdf)
like image 34
unutbu Avatar answered Sep 19 '22 14:09

unutbu