Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does anyone have example code of using scipy.stats.distributions?

Tags:

python

scipy

I am struggling to figure out how to use the scipy.distributions package and wondered if anyone could post some example code for me. It appears to do everything I need, I just can't figure out how to use it.

I need to generate two distributions, one log-normal and one poisson. I know the variance and lambda for each.

Links to resources would work just as well.

like image 268
Simon Avatar asked Jan 27 '09 20:01

Simon


People also ask

What is SciPy stats used for?

The scipy. stats is the SciPy sub-package. It is mainly used for probabilistic distributions and statistical operations. There is a wide range of probability functions.

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 the use of the describe () function when working with SciPy stat module?

describe() function | Python. scipy. stats. describe(array, axis=0) computes the descriptive statistics of the passed array elements along the specified axis of the array.

What is RVS in SciPy stats?

Random variates of given type. The shape parameter(s) for the distribution (see docstring of the instance object for more information).


2 Answers

I assume you mean the distributions in scipy.stats. To create a distribution, generate random variates and calculate the pdf:

Python 2.5.1 (r251:54863, Feb 4 2008, 21:48:13) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information.

>>> from scipy.stats import poisson, lognorm
>>> myShape = 5;myMu=10
>>> ln = lognorm(myShape)
>>> p = poisson(myMu)
>>> ln.rvs((10,)) #generate 10 RVs from ln
array([  2.09164812e+00,   3.29062874e-01,   1.22453941e-03,
         3.80101527e+02,   7.67464002e-02,   2.53530952e+01,
         1.41850880e+03,   8.36347923e+03,   8.69209870e+03,
         1.64317413e-01])
>>> p.rvs((10,)) #generate 10 RVs from p
array([ 8,  9,  7, 12,  6, 13, 11, 11, 10,  8])
>>> ln.pdf(3) #lognorm PDF at x=3
array(0.02596183475208955)

Other methods (and the rest of the scipy.stats documentation) can be found at the new SciPy documentation site.

like image 69
Barry Wark Avatar answered Oct 15 '22 22:10

Barry Wark


Here's some sample code: Probability distributions in SciPy

like image 32
John D. Cook Avatar answered Oct 15 '22 21:10

John D. Cook