Stats

Diofant statistics module

Introduces a random variable type into the Diofant language.

Random variables may be declared using prebuilt functions such as Normal, Exponential, Coin, Die, etc… or built with functions like FiniteRV.

Queries on random expressions can be made using the functions

Expression

Meaning

P(condition)

Probability

E(expression)

Expected value

variance(expression)

Variance

density(expression)

Probability Density Function

sample(expression)

Produce a realization

where(condition)

Where the condition is true

Examples

>>> from diofant.stats import P, E, variance, Die, Normal
>>> X, Y = Die('X', 6), Die('Y', 6)  # Define two six sided dice
>>> Z = Normal('Z', 0, 1)  # Declare a Normal random variable with mean 0, std 1
>>> P(X > 3)  # Probability X is greater than 3
1/2
>>> E(X + Y)  # Expectation of the sum of two dice
7
>>> variance(X + Y)  # Variance of the sum of two dice
35/6
>>> simplify(P(Z > 1))  # Probability of Z being greater than 1
-erf(sqrt(2)/2)/2 + 1/2

Random Variable Types

Finite Types

diofant.stats.DiscreteUniform(name, items)[source]

Create a Finite Random Variable representing a uniform distribution over the input set.

Returns a RandomSymbol.

Examples

>>> from diofant.stats import density
>>> X = DiscreteUniform('X', (a, b, c))  # equally likely over a, b, c
>>> density(X).dict
{a: 1/3, b: 1/3, c: 1/3}
>>> Y = DiscreteUniform('Y', list(range(5)))  # distribution over a range
>>> density(Y).dict
{0: 1/5, 1: 1/5, 2: 1/5, 3: 1/5, 4: 1/5}
diofant.stats.Die(name, sides=6)[source]

Create a Finite Random Variable representing a fair die.

Returns a RandomSymbol.

>>> from diofant.stats import density
>>> D6 = Die('D6', 6)  # Six sided Die
>>> density(D6).dict
{1: 1/6, 2: 1/6, 3: 1/6, 4: 1/6, 5: 1/6, 6: 1/6}
>>> D4 = Die('D4', 4)  # Four sided Die
>>> density(D4).dict
{1: 1/4, 2: 1/4, 3: 1/4, 4: 1/4}
diofant.stats.Bernoulli(name, p, succ=1, fail=0)[source]

Create a Finite Random Variable representing a Bernoulli process.

Returns a RandomSymbol

>>> from diofant.stats import density
>>> X = Bernoulli('X', Rational(3, 4))  # 1-0 Bernoulli variable, probability = 3/4
>>> density(X).dict
{0: 1/4, 1: 3/4}
>>> X = Bernoulli('X', Rational(1, 2), 'Heads', 'Tails')  # A fair coin toss
>>> density(X).dict
{Heads: 1/2, Tails: 1/2}
diofant.stats.Coin(name, p=Rational(1, 2))[source]

Create a Finite Random Variable representing a Coin toss.

Probability p is the chance of getting “Heads.” Half by default

Returns a RandomSymbol.

>>> from diofant.stats import density
>>> H, T = Symbol('H'), Symbol('T')
>>> C = Coin('C')  # A fair coin toss
>>> density(C).dict
{H: 1/2, T: 1/2}
>>> C2 = Coin('C2', Rational(3, 5))  # An unfair coin
>>> density(C2).dict
{H: 3/5, T: 2/5}
diofant.stats.Binomial(name, n, p, succ=1, fail=0)[source]

Create a Finite Random Variable representing a binomial distribution.

Returns a RandomSymbol.

Examples

>>> from diofant.stats import density
>>> X = Binomial('X', 4, Rational(1, 2))  # Four "coin flips"
>>> density(X).dict
{0: 1/16, 1: 1/4, 2: 3/8, 3: 1/4, 4: 1/16}
diofant.stats.Hypergeometric(name, N, m, n)[source]

Create a Finite Random Variable representing a hypergeometric distribution.

Returns a RandomSymbol.

Examples

>>> from diofant.stats import density
>>> X = Hypergeometric('X', 10, 5, 3)  # 10 marbles, 5 white (success), 3 draws
>>> density(X).dict
{0: 1/12, 1: 5/12, 2: 5/12, 3: 1/12}
diofant.stats.FiniteRV(name, density)[source]

Create a Finite Random Variable given a dict representing the density.

Returns a RandomSymbol.

>>> from diofant.stats import P, E
>>> density = {0: .1, 1: .2, 2: .3, 3: .4}
>>> X = FiniteRV('X', density)
>>> E(X)
2.00000000000000
>>> P(X >= 2)
0.700000000000000

Discrete Types

diofant.stats.Geometric(name, p)[source]

Create a discrete random variable with a Geometric distribution.

The density of the Geometric distribution is given by

\[f(k) := p (1 - p)^{k - 1}\]
Parameters

p (A probability between 0 and 1)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, E, variance
>>> p = Rational(1, 5)
>>> X = Geometric('x', p)
>>> density(X)(z)
(4/5)**(z - 1)/5
>>> E(X)
5
>>> variance(X)
20

References

[1] https://en.wikipedia.org/wiki/Geometric_distribution [2] https://mathworld.wolfram.com/GeometricDistribution.html

diofant.stats.Poisson(name, lamda)[source]

Create a discrete random variable with a Poisson distribution.

The density of the Poisson distribution is given by

\[f(k) := \frac{\lambda^{k} e^{- \lambda}}{k!}\]
Parameters

lamda (Positive number, a rate)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, E, variance
>>> rate = Symbol('lambda', positive=True)
>>> X = Poisson('x', rate)
>>> density(X)(z)
E**(-lambda)*lambda**z/factorial(z)
>>> E(X)
lambda
>>> simplify(variance(X))
lambda

References

[1] https://en.wikipedia.org/wiki/Poisson_distribution [2] https://mathworld.wolfram.com/PoissonDistribution.html

Continuous Types

diofant.stats.Arcsin(name, a=0, b=1)[source]

Create a Continuous Random Variable with an arcsin distribution.

The density of the arcsin distribution is given by

\[f(x) := \frac{1}{\pi\sqrt{(x-a)(b-x)}}\]

with \(x \in [a,b]\). It must hold that \(-\infty < a < b < \infty\).

Parameters
  • a (Real number, the left interval boundary)

  • b (Real number, the right interval boundary)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> a, b = symbols('a b', real=True)
>>> X = Arcsin('x', a, b)
>>> density(X)(z)
1/(pi*sqrt((-a + z)*(b - z)))

References

diofant.stats.Benini(name, alpha, beta, sigma)[source]

Create a Continuous Random Variable with a Benini distribution.

The density of the Benini distribution is given by

\[f(x) := e^{-\alpha\log{\frac{x}{\sigma}} -\beta\log^2\left[{\frac{x}{\sigma}}\right]} \left(\frac{\alpha}{x}+\frac{2\beta\log{\frac{x}{\sigma}}}{x}\right)\]

This is a heavy-tailed distribution and is also known as the log-Rayleigh distribution.

Parameters
  • alpha (Real number, \(\alpha > 0\), a shape)

  • beta (Real number, \(\beta > 0\), a shape)

  • sigma (Real number, \(\sigma > 0\), a scale)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> alpha = Symbol('alpha', positive=True)
>>> beta = Symbol('beta', positive=True)
>>> sigma = Symbol('sigma', positive=True)
>>> X = Benini('x', alpha, beta, sigma)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
            /  z  \           2/  z  \ /                  /  z  \\
 - alpha*log|-----| - beta*log |-----| |        2*beta*log|-----||
            \sigma/            \sigma/ |alpha             \sigma/|
E                                     *|----- + -----------------|
                                       \  z             z        /

References

diofant.stats.Beta(name, alpha, beta)[source]

Create a Continuous Random Variable with a Beta distribution.

The density of the Beta distribution is given by

\[f(x) := \frac{x^{\alpha-1}(1-x)^{\beta-1}} {\mathrm{B}(\alpha,\beta)}\]

with \(x \in [0,1]\).

Parameters
  • alpha (Real number, \(\alpha > 0\), a shape)

  • beta (Real number, \(\beta > 0\), a shape)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, E
>>> alpha = Symbol('alpha', positive=True)
>>> beta = Symbol('beta', positive=True)
>>> X = Beta('x', alpha, beta)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
 alpha - 1         beta - 1
z         *(-z + 1)
---------------------------
     beta(alpha, beta)
>>> expand_func(simplify(E(X, meijerg=True)))
alpha/(alpha + beta)

References

diofant.stats.BetaPrime(name, alpha, beta)[source]

Create a continuous random variable with a Beta prime distribution.

The density of the Beta prime distribution is given by

\[f(x) := \frac{x^{\alpha-1} (1+x)^{-\alpha -\beta}}{B(\alpha,\beta)}\]

with \(x > 0\).

Parameters
  • alpha (Real number, \(\alpha > 0\), a shape)

  • beta (Real number, \(\beta > 0\), a shape)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> alpha = Symbol('alpha', positive=True)
>>> beta = Symbol('beta', positive=True)
>>> X = BetaPrime('x', alpha, beta)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
 alpha - 1        -alpha - beta
z         *(z + 1)
-------------------------------
       beta(alpha, beta)

References

diofant.stats.Cauchy(name, x0, gamma)[source]

Create a continuous random variable with a Cauchy distribution.

The density of the Cauchy distribution is given by

\[f(x) := \frac{1}{\pi} \arctan\left(\frac{x-x_0}{\gamma}\right) +\frac{1}{2}\]
Parameters
  • x0 (Real number, the location)

  • gamma (Real number, \(\gamma > 0\), the scale)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> x0 = Symbol('x0')
>>> gamma = Symbol('gamma', positive=True)
>>> X = Cauchy('x', x0, gamma)
>>> density(X)(z)
1/(pi*gamma*(1 + (-x0 + z)**2/gamma**2))

References

diofant.stats.Chi(name, k)[source]

Create a continuous random variable with a Chi distribution.

The density of the Chi distribution is given by

\[f(x) := \frac{2^{1-k/2}x^{k-1}e^{-x^2/2}}{\Gamma(k/2)}\]

with \(x \geq 0\).

Parameters

k (A positive Integer, \(k > 0\), the number of degrees of freedom)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, Chi
>>> X = Chi('x', k)
>>> density(X)(z)
2**(-k/2 + 1)*E**(-z**2/2)*z**(k - 1)/gamma(k/2)

References

diofant.stats.ChiNoncentral(name, k, l)[source]

Create a continuous random variable with a non-central Chi distribution.

The density of the non-central Chi distribution is given by

\[f(x) := \frac{e^{-(x^2+\lambda^2)/2} x^k\lambda} {(\lambda x)^{k/2}} I_{k/2-1}(\lambda x)\]

with \(x \geq 0\). Here, \(I_\nu (x)\) is the modified Bessel function of the first kind.

Parameters
  • k (A positive Integer, \(k > 0\), the number of degrees of freedom)

  • l (Shift parameter)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> l = Symbol('l')
>>> X = ChiNoncentral('x', k, l)
>>> density(X)(z)
E**(-l**2/2 - z**2/2)*l*z**k*(l*z)**(-k/2)*besseli(k/2 - 1, l*z)

References

diofant.stats.ChiSquared(name, k)[source]

Create a continuous random variable with a Chi-squared distribution.

The density of the Chi-squared distribution is given by

\[f(x) := \frac{1}{2^{\frac{k}{2}}\Gamma\left(\frac{k}{2}\right)} x^{\frac{k}{2}-1} e^{-\frac{x}{2}}\]

with \(x \geq 0\).

Parameters

k (A positive Integer, \(k > 0\), the number of degrees of freedom)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, E, variance
>>> k = Symbol('k', integer=True, positive=True)
>>> X = ChiSquared('x', k)
>>> density(X)(z)
2**(-k/2)*E**(-z/2)*z**(k/2 - 1)/gamma(k/2)
>>> combsimp(E(X))
k
>>> simplify(expand_func(variance(X)))
2*k

References

diofant.stats.Dagum(name, p, a, b)[source]

Create a continuous random variable with a Dagum distribution.

The density of the Dagum distribution is given by

\[f(x) := \frac{a p}{x} \left( \frac{\left(\tfrac{x}{b}\right)^{a p}} {\left(\left(\tfrac{x}{b}\right)^a + 1 \right)^{p+1}} \right)\]

with \(x > 0\).

Parameters
  • p (Real number, \(p > 0\), a shape)

  • a (Real number, \(a > 0\), a shape)

  • b (Real number, \(b > 0\), a scale)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> p = Symbol('p', positive=True)
>>> b = Symbol('b', positive=True)
>>> a = Symbol('a', positive=True)
>>> X = Dagum('x', p, a, b)
>>> density(X)(z)
a*p*(z/b)**(a*p)*((z/b)**a + 1)**(-p - 1)/z

References

diofant.stats.Erlang(name, k, l)[source]

Create a continuous random variable with an Erlang distribution.

The density of the Erlang distribution is given by

\[f(x) := \frac{\lambda^k x^{k-1} e^{-\lambda x}}{(k-1)!}\]

with \(x \in [0,\infty]\).

Parameters
  • k (Integer)

  • l (Real number, \(\lambda > 0\), the rate)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, cdf, E, variance
>>> k = Symbol('k', integer=True, positive=True)
>>> l = Symbol('l', positive=True)
>>> X = Erlang('x', k, l)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
 -l*z  k  k - 1
E    *l *z
---------------
    gamma(k)
>>> C = cdf(X, meijerg=True)(z)
>>> pprint(C, use_unicode=False)
/  k*lowergamma(k, 0)   k*lowergamma(k, l*z)
|- ------------------ + --------------------  for z >= 0
<     gamma(k + 1)          gamma(k + 1)
|
\                     0                       otherwise
>>> simplify(E(X))
k/l
>>> simplify(variance(X))
k/l**2

References

diofant.stats.Exponential(name, rate)[source]

Create a continuous random variable with an Exponential distribution.

The density of the exponential distribution is given by

\[f(x) := \lambda \exp(-\lambda x)\]

with \(x > 0\). Note that the expected value is \(1/\lambda\).

Parameters

rate (A positive Real number, \(\lambda > 0\), the rate (or inverse scale/inverse mean))

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, cdf, E, variance, std, skewness
>>> l = Symbol('lambda', positive=True)
>>> X = Exponential('x', l)
>>> density(X)(z)
E**(-lambda*z)*lambda
>>> cdf(X)(z)
Piecewise((1 - E**(-lambda*z), z >= 0), (0, true))
>>> E(X)
1/lambda
>>> variance(X)
lambda**(-2)
>>> skewness(X)
2
>>> X = Exponential('x', 10)
>>> density(X)(z)
10*E**(-10*z)
>>> E(X)
1/10
>>> std(X)
1/10

References

diofant.stats.FDistribution(name, d1, d2)[source]

Create a continuous random variable with a F distribution.

The density of the F distribution is given by

\[f(x) := \frac{\sqrt{\frac{(d_1 x)^{d_1} d_2^{d_2}} {(d_1 x + d_2)^{d_1 + d_2}}}} {x \mathrm{B} \left(\frac{d_1}{2}, \frac{d_2}{2}\right)}\]

with \(x > 0\).

Parameters
  • d1 (\(d_1 > 0\) a parameter)

  • d2 (\(d_2 > 0\) a parameter)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> d1 = Symbol('d1', positive=True)
>>> d2 = Symbol('d2', positive=True)
>>> X = FDistribution('x', d1, d2)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
  d2
  --    ______________________________
  2    /       d1            -d1 - d2
d2  *\/  (d1*z)  *(d1*z + d2)
--------------------------------------
                  /d1  d2\
            z*beta|--, --|
                  \2   2 /

References

diofant.stats.FisherZ(name, d1, d2)[source]

Create a Continuous Random Variable with an Fisher’s Z distribution.

The density of the Fisher’s Z distribution is given by

\[f(x) := \frac{2d_1^{d_1/2} d_2^{d_2/2}} {\mathrm{B}(d_1/2, d_2/2)} \frac{e^{d_1z}}{\left(d_1e^{2z}+d_2\right)^{\left(d_1+d_2\right)/2}}\]
Parameters
  • d1 (\(d_1 > 0\), degree of freedom)

  • d2 (\(d_2 > 0\), degree of freedom)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> d1 = Symbol('d1', positive=True)
>>> d2 = Symbol('d2', positive=True)
>>> X = FisherZ('x', d1, d2)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
                                  d1   d2
          d1   d2               - -- - --
          --   --                 2    2
   d1*z   2    2  / 2*z        \
2*E    *d1  *d2  *\E   *d1 + d2/
-----------------------------------------
                   /d1  d2\
               beta|--, --|
                   \2   2 /

References

diofant.stats.Frechet(name, a, s=1, m=0)[source]

Create a continuous random variable with a Frechet distribution.

The density of the Frechet distribution is given by

\[f(x) := \frac{\alpha}{s} \left(\frac{x-m}{s}\right)^{-1-\alpha} e^{-(\frac{x-m}{s})^{-\alpha}}\]

with \(x \geq m\).

Parameters
  • a (Real number, \(a \in \left(0, \infty\right)\) the shape)

  • s (Real number, \(s \in \left(0, \infty\right)\) the scale)

  • m (Real number, \(m \in \left(-\infty, \infty\right)\) the minimum)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> a, s = symbols('a s', positive=True)
>>> m = Symbol('m', real=True)
>>> X = Frechet('x', a, s, m)
>>> density(X)(z)
E**(-((-m + z)/s)**(-a))*a*((-m + z)/s)**(-a - 1)/s

References

diofant.stats.Gamma(name, k, theta)[source]

Create a continuous random variable with a Gamma distribution.

The density of the Gamma distribution is given by

\[f(x) := \frac{1}{\Gamma(k) \theta^k} x^{k - 1} e^{-\frac{x}{\theta}}\]

with \(x \in [0,1]\).

Parameters
  • k (Real number, \(k > 0\), a shape)

  • theta (Real number, \(\theta > 0\), a scale)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, cdf, E, variance
>>> k = Symbol('k', positive=True)
>>> theta = Symbol('theta', positive=True)
>>> X = Gamma('x', k, theta)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
  -z
 -----
 theta      -k  k - 1
E     *theta  *z
---------------------
       gamma(k)
>>> C = cdf(X, meijerg=True)(z)
>>> pprint(C, use_unicode=False)
/                                   /     z  \
|                       k*lowergamma|k, -----|
|  k*lowergamma(k, 0)               \   theta/
<- ------------------ + ----------------------  for z >= 0
|     gamma(k + 1)           gamma(k + 1)
|
\                      0                        otherwise
>>> E(X)
theta*gamma(k + 1)/gamma(k)
>>> V = simplify(variance(X))
>>> pprint(V, use_unicode=False)
       2
k*theta

References

diofant.stats.GammaInverse(name, a, b)[source]

Create a continuous random variable with an inverse Gamma distribution.

The density of the inverse Gamma distribution is given by

\[f(x) := \frac{\beta^\alpha}{\Gamma(\alpha)} x^{-\alpha - 1} \exp\left(\frac{-\beta}{x}\right)\]

with \(x > 0\).

Parameters
  • a (Real number, \(a > 0\) a shape)

  • b (Real number, \(b > 0\) a scale)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> a = Symbol('a', positive=True)
>>> b = Symbol('b', positive=True)
>>> X = GammaInverse('x', a, b)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
 -b
 ---
  z   a  -a - 1
E   *b *z
---------------
    gamma(a)

References

diofant.stats.Kumaraswamy(name, a, b)[source]

Create a Continuous Random Variable with a Kumaraswamy distribution.

The density of the Kumaraswamy distribution is given by

\[f(x) := a b x^{a-1} (1-x^a)^{b-1}\]

with \(x \in [0,1]\).

Parameters
  • a (Real number, \(a > 0\) a shape)

  • b (Real number, \(b > 0\) a shape)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> a = Symbol('a', positive=True)
>>> b = Symbol('b', positive=True)
>>> X = Kumaraswamy('x', a, b)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
                     b - 1
     a - 1 /   a    \
a*b*z     *\- z  + 1/

References

diofant.stats.Laplace(name, mu, b)[source]

Create a continuous random variable with a Laplace distribution.

The density of the Laplace distribution is given by

\[f(x) := \frac{1}{2 b} \exp \left(-\frac{|x-\mu|}b \right)\]
Parameters
  • mu (Real number, the location (mean))

  • b (Real number, \(b > 0\), a scale)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> mu = Symbol('mu')
>>> b = Symbol('b', positive=True)
>>> X = Laplace('x', mu, b)
>>> density(X)(z)
E**(-Abs(mu - z)/b)/(2*b)

References

diofant.stats.Logistic(name, mu, s)[source]

Create a continuous random variable with a logistic distribution.

The density of the logistic distribution is given by

\[f(x) := \frac{e^{-(x-\mu)/s}} {s\left(1+e^{-(x-\mu)/s}\right)^2}\]
Parameters
  • mu (Real number, the location (mean))

  • s (Real number, \(s > 0\) a scale)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> mu = Symbol('mu', real=True)
>>> s = Symbol('s', positive=True)
>>> X = Logistic('x', mu, s)
>>> density(X)(z)
E**((mu - z)/s)/(s*(E**((mu - z)/s) + 1)**2)

References

diofant.stats.LogNormal(name, mean, std)[source]

Create a continuous random variable with a log-normal distribution.

The density of the log-normal distribution is given by

\[f(x) := \frac{1}{x\sqrt{2\pi\sigma^2}} e^{-\frac{\left(\ln x-\mu\right)^2}{2\sigma^2}}\]

with \(x \geq 0\).

Parameters
  • mu (Real number, the log-scale)

  • sigma (Real number, \(\sigma^2 > 0\) a shape)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> mu = Symbol('mu', real=True)
>>> sigma = Symbol('sigma', positive=True)
>>> X = LogNormal('x', mu, sigma)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
                      2
       -(-mu + log(z))
       -----------------
                   2
  ___       2*sigma
\/ 2 *E
------------------------
        ____
    2*\/ pi *sigma*z
>>> X = LogNormal('x', 0, 1)  # Mean 0, standard deviation 1
>>> density(X)(z)
sqrt(2)*E**(-log(z)**2/2)/(2*sqrt(pi)*z)

References

diofant.stats.Maxwell(name, a)[source]

Create a continuous random variable with a Maxwell distribution.

The density of the Maxwell distribution is given by

\[f(x) := \sqrt{\frac{2}{\pi}} \frac{x^2 e^{-x^2/(2a^2)}}{a^3}\]

with \(x \geq 0\).

Parameters

a (Real number, \(a > 0\))

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, E, variance
>>> a = Symbol('a', positive=True)
>>> X = Maxwell('x', a)
>>> density(X)(z)
sqrt(2)*E**(-z**2/(2*a**2))*z**2/(sqrt(pi)*a**3)
>>> E(X)
2*sqrt(2)*a/sqrt(pi)
>>> simplify(variance(X))
a**2*(-8 + 3*pi)/pi

References

diofant.stats.Nakagami(name, mu, omega)[source]

Create a continuous random variable with a Nakagami distribution.

The density of the Nakagami distribution is given by

\[f(x) := \frac{2\mu^\mu}{\Gamma(\mu)\omega^\mu} x^{2\mu-1} \exp\left(-\frac{\mu}{\omega}x^2 \right)\]

with \(x > 0\).

Parameters
  • mu (Real number, \(\mu \geq \frac{1}{2}\) a shape)

  • omega (Real number, \(\omega > 0\), the spread)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, E, variance
>>> mu = Symbol('mu', positive=True)
>>> omega = Symbol('omega', positive=True)
>>> X = Nakagami('x', mu, omega)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
        2
   -mu*z
   -------
    omega    mu      -mu  2*mu - 1
2*E       *mu  *omega   *z
----------------------------------
            gamma(mu)
>>> simplify(E(X, meijerg=True))
sqrt(mu)*sqrt(omega)*gamma(mu + 1/2)/gamma(mu + 1)
>>> V = simplify(variance(X, meijerg=True))
>>> pprint(V, use_unicode=False)
                    2
         omega*gamma (mu + 1/2)
omega - -----------------------
        gamma(mu)*gamma(mu + 1)

References

diofant.stats.Normal(name, mean, std)[source]

Create a continuous random variable with a Normal distribution.

The density of the Normal distribution is given by

\[f(x) := \frac{1}{\sigma\sqrt{2\pi}} e^{ -\frac{(x-\mu)^2}{2\sigma^2} }\]
Parameters
  • mu (Real number, the mean)

  • sigma (Real number, \(\sigma^2 > 0\) the variance)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, E, std, cdf, skewness
>>> mu = Symbol('mu')
>>> sigma = Symbol('sigma', positive=True)
>>> X = Normal('x', mu, sigma)
>>> density(X)(z)
sqrt(2)*E**(-(-mu + z)**2/(2*sigma**2))/(2*sqrt(pi)*sigma)
>>> C = simplify(cdf(X))(z)  # it needs a little more help...
>>> pprint(C, use_unicode=False)
     /  ___         \
     |\/ 2 *(mu - z)|
  erf|--------------|
     \    2*sigma   /   1
- ------------------- + -
           2            2
>>> simplify(skewness(X))
0
>>> X = Normal('x', 0, 1)  # Mean 0, standard deviation 1
>>> density(X)(z)
sqrt(2)*E**(-z**2/2)/(2*sqrt(pi))
>>> E(2*X + 1)
1
>>> simplify(std(2*X + 1))
2

References

diofant.stats.Pareto(name, xm, alpha)[source]

Create a continuous random variable with the Pareto distribution.

The density of the Pareto distribution is given by

\[f(x) := \frac{\alpha\,x_m^\alpha}{x^{\alpha+1}}\]

with \(x \in [x_m,\infty]\).

Parameters
  • xm (Real number, \(x_m > 0\), a scale)

  • alpha (Real number, \(\alpha > 0\), a shape)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> xm = Symbol('xm', positive=True)
>>> beta = Symbol('beta', positive=True)
>>> X = Pareto('x', xm, beta)
>>> density(X)(z)
beta*xm**beta*z**(-beta - 1)

References

diofant.stats.QuadraticU(name, a, b)[source]

Create a Continuous Random Variable with a U-quadratic distribution.

The density of the U-quadratic distribution is given by

\[f(x) := \alpha (x-\beta)^2\]

with \(x \in [a,b]\).

Parameters
  • a (Real number)

  • b (Real number, \(a < b\))

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> a, b = symbols('a b', real=True)
>>> X = QuadraticU('x', a, b)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
/                2
|   /  a   b    \
|12*|- - - - + z|
|   \  2   2    /
<-----------------  for And(a <= z, z <= b)
|            3
|    (-a + b)
|
\        0                 otherwise

References

diofant.stats.RaisedCosine(name, mu, s)[source]

Create a Continuous Random Variable with a raised cosine distribution.

The density of the raised cosine distribution is given by

\[f(x) := \frac{1}{2s}\left(1+\cos\left(\frac{x-\mu}{s}\pi\right)\right)\]

with \(x \in [\mu-s,\mu+s]\).

Parameters
  • mu (Real number)

  • s (Real number, \(s > 0\))

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> mu = Symbol('mu', real=True)
>>> s = Symbol('s', positive=True)
>>> X = RaisedCosine('x', mu, s)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
/   /pi*(-mu + z)\
|cos|------------| + 1
|   \     s      /
<---------------------  for And(z <= mu + s, mu - s <= z)
|         2*s
|
\          0                        otherwise

References

diofant.stats.Rayleigh(name, sigma)[source]

Create a continuous random variable with a Rayleigh distribution.

The density of the Rayleigh distribution is given by

\[f(x) := \frac{x}{\sigma^2} e^{-x^2/2\sigma^2}\]

with \(x > 0\).

Parameters

sigma (Real number, \(\sigma > 0\))

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, E, variance
>>> sigma = Symbol('sigma', positive=True)
>>> X = Rayleigh('x', sigma)
>>> density(X)(z)
E**(-z**2/(2*sigma**2))*z/sigma**2
>>> E(X)
sqrt(2)*sqrt(pi)*sigma/2
>>> variance(X)
-pi*sigma**2/2 + 2*sigma**2

References

diofant.stats.StudentT(name, nu)[source]

Create a continuous random variable with a student’s t distribution.

The density of the student’s t distribution is given by

\[f(x) := \frac{\Gamma \left(\frac{\nu+1}{2} \right)} {\sqrt{\nu\pi}\Gamma \left(\frac{\nu}{2} \right)} \left(1+\frac{x^2}{\nu} \right)^{-\frac{\nu+1}{2}}\]
Parameters

nu (Real number, \(\nu > 0\), the degrees of freedom)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> nu = Symbol('nu', positive=True)
>>> X = StudentT('x', nu)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
            nu   1
          - -- - -
            2    2
  /     2\
  |    z |
  |1 + --|
  \    nu/
--------------------
  ____     /     nu\
\/ nu *beta|1/2, --|
           \     2 /

References

diofant.stats.Triangular(name, a, b, c)[source]

Create a continuous random variable with a triangular distribution.

The density of the triangular distribution is given by

\[\begin{split}f(x) := \begin{cases} 0 & \mathrm{for\ } x < a, \\ \frac{2(x-a)}{(b-a)(c-a)} & \mathrm{for\ } a \le x < c, \\ \frac{2}{b-a} & \mathrm{for\ } x = c, \\ \frac{2(b-x)}{(b-a)(b-c)} & \mathrm{for\ } c < x \le b, \\ 0 & \mathrm{for\ } b < x. \end{cases}\end{split}\]
Parameters
  • a (Real number, \(a \in \left(-\infty, \infty\right)\))

  • b (Real number, \(a < b\))

  • c (Real number, \(a \leq c \leq b\))

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> X = Triangular('x', a, b, c)
>>> pprint(density(X)(z), use_unicode=False)
/    -2*a + 2*z
|-----------------  for And(a <= z, z < c)
|(-a + b)*(-a + c)
|
|       2
|     ------              for z = c
<     -a + b
|
|   2*b - 2*z
|----------------   for And(z <= b, c < z)
|(-a + b)*(b - c)
|
\        0                otherwise

References

diofant.stats.Uniform(name, left, right)[source]

Create a continuous random variable with a uniform distribution.

The density of the uniform distribution is given by

\[\begin{split}f(x) := \begin{cases} \frac{1}{b - a} & \text{for } x \in [a,b] \\ 0 & \text{otherwise} \end{cases}\end{split}\]

with \(x \in [a,b]\).

Parameters
  • a (Real number, \(-\infty < a\) the left boundary)

  • b (Real number, \(a < b < \infty\) the right boundary)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, cdf, E, variance
>>> a = Symbol('a', negative=True)
>>> b = Symbol('b', positive=True)
>>> X = Uniform('x', a, b)
>>> density(X)(z)
Piecewise((1/(-a + b), (a <= z) & (z <= b)), (0, true))
>>> cdf(X)(z)  
-a/(-a + b) + z/(-a + b)
>>> simplify(E(X))
a/2 + b/2
>>> simplify(variance(X))
a**2/12 - a*b/6 + b**2/12

References

diofant.stats.UniformSum(name, n)[source]

Create a continuous random variable with an Irwin-Hall distribution.

The probability distribution function depends on a single parameter \(n\) which is an integer.

The density of the Irwin-Hall distribution is given by

\[f(x) := \frac{1}{(n-1)!}\sum_{k=0}^{\lfloor x\rfloor}(-1)^k \binom{n}{k}(x-k)^{n-1}\]
Parameters

n (A positive Integer, \(n > 0\))

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> X = UniformSum('x', n)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
floor(z)
  ___
  \  `
   \         k        n - 1 /n\
    )    (-1) *(z - k)     *| |
   /                        \k/
  /__,
 k = 0
-------------------------------
            (n - 1)!

References

diofant.stats.VonMises(name, mu, k)[source]

Create a Continuous Random Variable with a von Mises distribution.

The density of the von Mises distribution is given by

\[f(x) := \frac{e^{\kappa\cos(x-\mu)}}{2\pi I_0(\kappa)}\]

with \(x \in [0,2\pi]\).

Parameters
  • mu (Real number, measure of location)

  • k (Real number, measure of concentration)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density
>>> mu = Symbol('mu')
>>> k = Symbol('k', positive=True)
>>> X = VonMises('x', mu, k)
>>> D = density(X)(z)
>>> pprint(D, use_unicode=False)
   k*cos(mu - z)
  E
------------------
2*pi*besseli(0, k)

References

diofant.stats.Weibull(name, alpha, beta)[source]

Create a continuous random variable with a Weibull distribution.

The density of the Weibull distribution is given by

\[\begin{split}f(x) := \begin{cases} \frac{k}{\lambda}\left(\frac{x}{\lambda}\right)^{k-1} e^{-(x/\lambda)^{k}} & x\geq0\\ 0 & x<0 \end{cases}\end{split}\]
Parameters
  • lambda (Real number, \(\lambda > 0\) a scale)

  • k (Real number, \(k > 0\) a shape)

Returns

A RandomSymbol.

Examples

>>> from diofant.stats import density, E, variance
>>> l = Symbol('lambda', positive=True)
>>> k = Symbol('k', positive=True, real=True)
>>> X = Weibull('x', l, k)
>>> density(X)(z)
E**(-(z/lambda)**k)*k*(z/lambda)**(k - 1)/lambda
>>> simplify(E(X))
lambda*gamma(1 + 1/k)
>>> simplify(variance(X))
lambda**2*(-gamma(1 + 1/k)**2 + gamma(1 + 2/k))

References

diofant.stats.WignerSemicircle(name, R)[source]

Create a continuous random variable with a Wigner semicircle distribution.

The density of the Wigner semicircle distribution is given by

\[f(x) := \frac2{\pi R^2}\,\sqrt{R^2-x^2}\]

with \(x \in [-R,R]\).

Parameters

R (Real number, \(R > 0\), the radius)

Returns

A \(RandomSymbol\).

Examples

>>> from diofant.stats import density, E
>>> R = Symbol('R', positive=True)
>>> X = WignerSemicircle('x', R)
>>> density(X)(z)
2*sqrt(R**2 - z**2)/(pi*R**2)
>>> E(X)
0

References

diofant.stats.ContinuousRV(symbol, density, set=Interval(- oo, oo, true, true))[source]

Create a Continuous Random Variable given the following:

– a symbol – a probability density function – set on which the pdf is valid (defaults to entire real line)

Returns a RandomSymbol.

Many common continuous random variable types are already implemented. This function should be necessary only very rarely.

Examples

>>> from diofant.stats import P, E
>>> pdf = sqrt(2)*exp(-x**2/2)/(2*sqrt(pi))  # Normal distribution
>>> X = ContinuousRV(x, pdf)
>>> E(X)
0
>>> P(X > 0)
1/2

Interface

diofant.stats.P(condition, given_condition=None, numsamples=None, evaluate=True, **kwargs)[source]

Probability that a condition is true, optionally given a second condition

Parameters
  • condition (Combination of Relationals containing RandomSymbols) – The condition of which you want to compute the probability

  • given_condition (Combination of Relationals containing RandomSymbols) – A conditional expression. P(X > 1, X > 0) is expectation of X > 1 given X > 0

  • numsamples (int) – Enables sampling and approximates the probability with this many samples

  • evaluate (Bool (defaults to True)) – In case of continuous systems return unevaluated integral

Examples

>>> from diofant.stats import P, Die
>>> X, Y = Die('X', 6), Die('Y', 6)
>>> P(X > 3)
1/2
>>> P(Eq(X, 5), X > 2)  # Probability that X == 5 given that X > 2
1/4
>>> P(X > Y)
5/12
diofant.stats.E(expr, condition=None, numsamples=None, evaluate=True, **kwargs)[source]

Returns the expected value of a random expression

Parameters
  • expr (Expr containing RandomSymbols) – The expression of which you want to compute the expectation value

  • given (Expr containing RandomSymbols) – A conditional expression. E(X, X>0) is expectation of X given X > 0

  • numsamples (int) – Enables sampling and approximates the expectation with this many samples

  • evalf (Bool (defaults to True)) – If sampling return a number rather than a complex expression

  • evaluate (Bool (defaults to True)) – In case of continuous systems return unevaluated integral

Examples

>>> from diofant.stats import E, Die
>>> X = Die('X', 6)
>>> E(X)
7/2
>>> E(2*X + 1)
8
>>> E(X, X > 3)  # Expectation of X given that it is above 3
5
diofant.stats.density(expr, condition=None, evaluate=True, numsamples=None, **kwargs)[source]

Probability density of a random expression, optionally given a second condition.

This density will take on different forms for different types of probability spaces. Discrete variables produce Dicts. Continuous variables produce Lambdas.

Parameters
  • expr (Expr containing RandomSymbols) – The expression of which you want to compute the density value

  • condition (Relational containing RandomSymbols) – A conditional expression. density(X > 1, X > 0) is density of X > 1 given X > 0

  • numsamples (int) – Enables sampling and approximates the density with this many samples

Examples

>>> from diofant.stats import Die, Normal
>>> D = Die('D', 6)
>>> X = Normal(x, 0, 1)
>>> density(D).dict
{1: 1/6, 2: 1/6, 3: 1/6, 4: 1/6, 5: 1/6, 6: 1/6}
>>> density(2*D).dict
{2: 1/6, 4: 1/6, 6: 1/6, 8: 1/6, 10: 1/6, 12: 1/6}
>>> density(X)(x)
sqrt(2)*E**(-x**2/2)/(2*sqrt(pi))
diofant.stats.given(expr, condition=None, **kwargs)[source]

Conditional Random Expression.

From a random expression and a condition on that expression creates a new probability space from the condition and returns the same expression on that conditional probability space.

Examples

>>> from diofant.stats import Die
>>> X = Die('X', 6)
>>> Y = given(X, X > 3)
>>> density(Y).dict
{4: 1/3, 5: 1/3, 6: 1/3}

Following convention, if the condition is a random symbol then that symbol is considered fixed.

>>> from diofant.stats import Normal
>>> X = Normal('X', 0, 1)
>>> Y = Normal('Y', 0, 1)
>>> pprint(density(X + Y, Y)(z), use_unicode=False)
                2
       -(-Y + z)
       -----------
  ___       2
\/ 2 *E
------------------
         ____
     2*\/ pi
diofant.stats.where(condition, given_condition=None, **kwargs)[source]

Returns the domain where a condition is True.

Examples

>>> from diofant.stats import Die, Normal
>>> D1, D2 = Die('a', 6), Die('b', 6)
>>> a, b = D1.symbol, D2.symbol
>>> X = Normal('x', 0, 1)
>>> where(X**2 < 1)
Domain: (-1 < x) & (x < 1)
>>> where(X**2 < 1).set
(-1, 1)
>>> where(And(D1 <= D2, D2 < 3))
Domain: (Eq(a, 1) & Eq(b, 1)) | (Eq(a, 1) & Eq(b, 2)) | (Eq(a, 2) & Eq(b, 2))
diofant.stats.variance(X, condition=None, **kwargs)[source]

Variance of a random expression

Expectation of (X-E(X))**2

Examples

>>> from diofant.stats import Die, Bernoulli
>>> X = Die('X', 6)
>>> p = Symbol('p')
>>> B = Bernoulli('B', p, 1, 0)
>>> variance(2*X)
35/3
>>> simplify(variance(B))
p*(-p + 1)
diofant.stats.std(X, condition=None, **kwargs)[source]

Standard Deviation of a random expression

Square root of the Expectation of (X-E(X))**2

Examples

>>> from diofant.stats import Bernoulli
>>> p = Symbol('p')
>>> B = Bernoulli('B', p, 1, 0)
>>> simplify(std(B))
sqrt(p*(-p + 1))
diofant.stats.sample(expr, condition=None, **kwargs)[source]

A realization of the random expression

Examples

>>> from diofant.stats import Die
>>> X, Y, Z = Die('X', 6), Die('Y', 6), Die('Z', 6)
>>> die_roll = sample(X + Y + Z)  # A random realization of three dice
diofant.stats.sample_iter(expr, condition=None, numsamples=oo, **kwargs)[source]

Returns an iterator of realizations from the expression given a condition

expr: Random expression to be realized condition: A conditional expression (optional) numsamples: Length of the iterator (defaults to infinity)

Examples

>>> from diofant.stats import Normal
>>> X = Normal('X', 0, 1)
>>> expr = X*X + 3
>>> iterator = sample_iter(expr, numsamples=3)
>>> list(iterator)  
[12, 4, 7]

Mechanics

Diofant Stats employs a relatively complex class hierarchy.

RandomDomains are a mapping of variables to possible values. For example we might say that the symbol Symbol('x') can take on the values \(\{1,2,3,4,5,6\}\).

class diofant.stats.rv.RandomDomain[source]

A PSpace, or Probability Space, combines a RandomDomain with a density to provide probabilistic information. For example the above domain could be enhanced by a finite density {1:1/6, 2:1/6, 3:1/6, 4:1/6, 5:1/6, 6:1/6} to fully define the roll of a fair die named x.

class diofant.stats.rv.PSpace[source]

A RandomSymbol represents the PSpace’s symbol ‘x’ inside of Diofant expressions.

class diofant.stats.rv.RandomSymbol[source]

The RandomDomain and PSpace classes are almost never directly instantiated. Instead they are subclassed for a variety of situations.

RandomDomains and PSpaces must be sufficiently general to represent domains and spaces of several variables with arbitrarily complex densities. This generality is often unnecessary. Instead we often build SingleDomains and SinglePSpaces to represent single, univariate events and processes such as a single die or a single normal variable.

class diofant.stats.rv.SinglePSpace[source]
class diofant.stats.rv.SingleDomain[source]

Another common case is to collect together a set of such univariate random variables. A collection of independent SinglePSpaces or SingleDomains can be brought together to form a ProductDomain or ProductPSpace. These objects would be useful in representing three dice rolled together for example.

class diofant.stats.rv.ProductDomain[source]
class diofant.stats.rv.ProductPSpace[source]

The Conditional adjective is added whenever we add a global condition to a RandomDomain or PSpace. A common example would be three independent dice where we know their sum to be greater than 12.

class diofant.stats.rv.ConditionalDomain[source]

We specialize further into Finite and Continuous versions of these classes to represent finite (such as dice) and continuous (such as normals) random variables.

class diofant.stats.frv.FiniteDomain[source]
class diofant.stats.frv.FinitePSpace[source]
class diofant.stats.crv.ContinuousDomain[source]
class diofant.stats.crv.ContinuousPSpace[source]

Additionally there are a few specialized classes that implement certain common random variable types. There is for example a DiePSpace that implements SingleFinitePSpace and a NormalPSpace that implements SingleContinuousPSpace.

class diofant.stats.frv_types.DiePSpace
class diofant.stats.crv_types.NormalPSpace

RandomVariables can be extracted from these objects using the PSpace.values method.

As previously mentioned Diofant Stats employs a relatively complex class structure. Inheritance is widely used in the implementation of end-level classes. This tactic was chosen to balance between the need to allow Diofant to represent arbitrarily defined random variables and optimizing for common cases. This complicates the code but is structured to only be important to those working on extending Diofant Stats to other random variable types.

Users will not use this class structure. Instead these mechanics are exposed through variable creation functions Die, Coin, FiniteRV, Normal, Exponential, etc…. These build the appropriate SinglePSpaces and return the corresponding RandomVariable. Conditional and Product spaces are formed in the natural construction of Diofant expressions and the use of interface functions E, Given, Density, etc….

diofant.stats.Die()
diofant.stats.Normal()

There are some additional functions that may be useful. They are largely used internally.

diofant.stats.rv.random_symbols(expr)[source]

Returns all RandomSymbols within a Diofant Expression.

diofant.stats.rv.pspace(expr)[source]

Returns the underlying Probability Space of a random expression.

For internal use.

Examples

>>> from diofant.stats import Normal
>>> X = Normal('X', 0, 1)
>>> pspace(2*X + 1) == X.pspace
True
diofant.stats.rv.rs_swap(a, b)[source]

Build a dictionary to swap RandomSymbols based on their underlying symbol.

i.e. if X = ('x', pspace1) and Y = ('x', pspace2) then X and Y match and the key, value pair {X:Y} will appear in the result

Inputs: collections a and b of random variables which share common symbols Output: dict mapping RVs in a to RVs in b

diofant.stats.rv.sampling_P(condition, given_condition=None, numsamples=1, evalf=True, **kwargs)[source]

Sampling version of P

diofant.stats.rv.sampling_E(expr, given_condition=None, numsamples=1, evalf=True, **kwargs)[source]

Sampling version of E

diofant.stats.rv.sampling_density(expr, given_condition=None, numsamples=1, **kwargs)[source]

Sampling version of density

diofant.stats.rv.independent(a, b)[source]

Independence of two random expressions

Two expressions are independent if knowledge of one does not change computations on the other.

Examples

>>> from diofant.stats import Normal
>>> X, Y = Normal('X', 0, 1), Normal('Y', 0, 1)
>>> independent(X, Y)
True
>>> independent(2*X + Y, -Y)
False
>>> X, Y = given(Tuple(X, Y), Eq(X + Y, 3))
>>> independent(X, Y)
False
diofant.stats.rv.dependent(a, b)[source]

Dependence of two random expressions

Two expressions are independent if knowledge of one does not change computations on the other.

Examples

>>> from diofant.stats import Normal
>>> X, Y = Normal('X', 0, 1), Normal('Y', 0, 1)
>>> dependent(X, Y)
False
>>> dependent(2*X + Y, -Y)
True
>>> X, Y = given(Tuple(X, Y), Eq(X + Y, 3))
>>> dependent(X, Y)
True