📜  Python中的sympy.stats.Erlang()(1)

📅  最后修改于: 2023-12-03 15:04:35.374000             🧑  作者: Mango

sympy.stats.Erlang() in Python

sympy.stats is a Python library that provides tools to work with probability distributions, random variables, and their properties. One of the distributions available in sympy.stats is the Erlang distribution, implemented by the sympy.stats.Erlang() class.

The Erlang distribution is a continuous probability distribution that models the time between successive events in a Poisson process, where each event has a gamma-distributed waiting time.

Usage

To use the sympy.stats.Erlang() class, first import it from the sympy.stats module:

from sympy.stats import Erlang

Then, create an instance of the distribution by specifying its parameters. The Erlang distribution has two parameters: the shape parameter k (a positive integer) and the scale parameter lambda (a positive real number). The distribution has a mean of k/lambda and a variance of k/lambda^2.

X = Erlang('X', k, lambda_)

Here, k and lambda_ are SymPy symbols representing the shape and scale parameters, respectively. The first argument to the Erlang() constructor is a string representing the name of the random variable.

Probability density function

The probability density function (PDF) of the Erlang distribution is given by:

f(x) = lambda**k * x**(k-1) * exp(-lambda * x) / (k-1)!

You can obtain the PDF of an Erlang distribution using the .pdf() method of an Erlang instance:

pdf = X.pdf(x)

Here, x is a SymPy symbol representing the variable of integration.

Cumulative distribution function

The cumulative distribution function (CDF) of the Erlang distribution is given by:

F(x) = 1 - sum(lambda**i * x**i * exp(-lambda * x) / i!, (i, 0, k-1))

You can obtain the CDF of an Erlang distribution using the .cdf() method of an Erlang instance:

cdf = X.cdf(x)
Examples
from sympy import symbols
from sympy.stats import Erlang

# Create a random variable with k=2 and lambda=1
k, lambda_ = symbols('k lambda', positive=True)
X = Erlang('X', k, lambda_)

# Calculate the mean and variance of the distribution
mean = X.mean()
variance = X.variance()

# Calculate the PDF and CDF of the distribution
x = symbols('x')
pdf = X.pdf(x)
cdf = X.cdf(x)
Conclusion

The sympy.stats.Erlang() class provides a convenient way to work with the Erlang distribution in Python. With it, you can calculate the PDF and CDF of the distribution, as well as its mean and variance. This can be useful in a variety of applications, such as queuing theory, reliability analysis, and telecommunications.