Getting pdf file from scipy.stats in general

I am doing some fitness 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 am I passing to distr.pdf() to get the best pdf values ​​for a list sample points of interest called abscissas ?

+5
source share
2 answers

To evaluate pdf in abscissas , you must pass abcissas as the first argument to pdf . To specify parameters, use the * operator to unpack the param tuple and pass these values ​​to distr.pdf :

 pdf = distr.pdf(abscissas, *param) 

For instance,

 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) 
+4
source

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

shape, loc, scale: a tuple of MLE floats for any shape statistics, and then for location and scale.

and .pdf() method accepts:

x: array_like quantiles

arg1, arg2, arg3, ...: array_like The form parameter for distribution (see Docstring of an object instance for more information)

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

scale: array_like, optional

So you would do the following:

 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

+6
source

All Articles