Statistics Module

goodok edited this page Nov 17, 2011 · 1 revision

Statistic Module

The statistics module in SymPy (currently in SVN) implements standard probability distributions and related tools. Its contents can be imported with the following statement:

  from sympy.statistics import *

Normal distributions

**Normal(mu, sigma)* creates a normal distribution with mean value mu and standard deviation sigma. The Normal class defines several useful methods and properties. Various properties can be accessed directly as follows:

  >>> from sympy.statistics import *
  >>> N = Normal(0, 1)
  >>> N.mean
  >>> N.median
  >>> N.variance
  >>> N.stddev

You can generate random numbers from the desired distribution with the random method:

  >>> N = Normal(10, 5)
  >>> N.random()                  #doctest: +SKIP
  >>> N.random()                  #doctest: +SKIP
  >>> N.random()                  #doctest: +SKIP
  >>> N.random()                  #doctest: +SKIP

The probability density function (pdf) and cumulative distribution function (cdf) of a distribution can be computed, either in symbolic form or for particular values:

  >>> from sympy import Symbol, oo
  >>> from sympy.statistics import *
  >>> N = Normal(1, 1)
  >>> x = Symbol('x')
  >>> N.pdf(1)
  >>> N.pdf(3).evalf()    #doctest: +SKIP
  >>> N.cdf(x)
  erf(2**(1/2)*(x - 1)/2)/2 + 1/2
  >>> N.cdf(-oo), N.cdf(1), N.cdf(oo)
  (0, 1/2, 1)
  >>> N.cdf(5).evalf()

The method probability gives the total probability on a given interval (a convenient alternative syntax for cdf(b)-cdf(a)):

  >>> N = Normal(0, 1)
  >>> N.probability(-oo, 0)
  >>> N.probability(-1, 1)
  >>> _.evalf()

You can also generate a symmetric confidence interval from a given desired confidence level (given as a fraction 0-1). For the normal distribution, 68%, 95% and 99.7% confidence levels respectively correspond to approximately 1, 2 and 3 standard deviations:

  >>> N = Normal(0, 1)
  >>> N.confidence(0.68)
  (-0.994457883209753, 0.994457883209753)
  >>> N.confidence(0.95)
  (-1.95996398454005, 1.95996398454005)
  >>> N.confidence(0.997)
  (-2.96773792534179, 2.96773792534179)

Plug the interval back in to see that the value is correct:

  >>> N.probability(*N.confidence(0.95)).evalf()

Other distributions

Besides the normal distribution, uniform continuous distributions are also supported. Uniform(a, b) represents the distribution with uniform probability on the interval [a, b] and zero probability everywhere else. The Uniform class supports the same methods as the Normal class.

Additional distributions, including support for arbitrary user-defined distributions, are planned for the future. Category:Modules

Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.