-
Notifications
You must be signed in to change notification settings - Fork 1
Random Variables
RV(func,support,ftype)
Arguments:
-
func: a list of functions. If the distribution is described by a piecewise function, func will contain one element for each piece of the function.
-
support: the support of the random variable. For continuous distributions, support must contain one more element than func. For discrete distributions, func and support will contain the same number of elements.
-
ftype: a list consisting of two elements. The first is a string describing whether the distribution is continuous or discrete. This element can have the value
'continuous'
,'discrete'
or'Discrete'
. The'Discrete'
option is a discrete random variable that is described by a function, rather than as a list of observations and associated pmf values. The second element is a string describing the representation of the random variable. This element can have the value'cdf'
,'chf'
,'hf'
,'idf'
,'pdf'
or'sf'
. If ftype is not specified, APPLPy will assume that the user is initializing a continuous PDF.
In [3]: # Create a continuous, piecewise PDF
In [4]: X=RV([x-1,3-x],[1,2,3],['continuous','pdf'])
In [5]: X.display()
continuous pdf
for 1 <= x <= 2
---------------------------
x - 1
---------------------------
for 2 <= x <= 3
---------------------------
-x + 3
---------------------------
In [6]: # Create a discrete random variable
In [7]: Y=RV([1/4,1/4,1/4,1/4],[1,2,3,4],['discrete','pdf'])
In [8]: Y.display()
discrete pdf where {x->f(x)}:
{1 -> 0.25}, {2 -> 0.25}, {3 -> 0.25}, {4 -> 0.25}
APPLPy comes has pre-coded a number of commonly used distribution in order to streamline the creation of random variables. The types of distributions included in APPLPy are listed below using the synax required to generate them. Each distribution is coded as a sub-class of the main random variable class. In distributions whose parameters have restrictions, the relevant assumptions have been built into the sub-class. For instance, the ExponentialRV class assumes that the parameter is a positive number and that the distribution is defined for positive real numbers. If the distribution is called with no arguments, then a distribution with unspecified parameters is returned. The following sytax shows how to generate a fully specified, partially specified, and unspecified Weibull distribution. Note that lambda cannot be used as a parameter, because lambda
is a predefined Python keyword used to create an inline function. For a full list of distribution types, type Menu()
in an APPLPy session. Also note that rational inputs must be in the form of a SymPy rational number. Python will automatically evaluate any input that is given using the division operator. For instance, entering the command ExponentialRV(1/3)
will result in the creation of an exponential random variable with a parameter equal to 0.333333. The syntax required to produced an Exponential distribution using a rational parameter value is ExponentialRV(Rational(1,3))
.
Continuous Distributions: ArcSinRV(),ArcTanRV(alpha,phi),BetaRV(alpha,beta),CauchyRV(a,alpha),ChiRV(N), ChiSquareRV(N),ErlangRV(theta,N)ErrorRV(mu,alpha,d),ErrorIIRV(a,b,c), ExponentialRV(theta),ExponentialPowerRV(theta,kappa),ExtremeValueRV(alpha,beta), FRV(n1,n2),GammaRV(theta,kappa),GompertzRV(theta,kappa), GeneralizedParetoRV(theta,delta,kappa),IDBRV(theta,delta,kappa), InverseGaussianRV(theta,mu),InverseGammaRV(alpha,beta),KSRV(n),LaPlaceRV(omega,theta), LogGammaRV(alpha,beta),LogisticRV(kappa,theta),LogLogisticRV(theta,kappa), LogNormalRV(mu,sigma),LomaxRV(kappa,theta),MakehamRV(theta,delta,kappa),MuthRV(kappa), NormalRV(mu,sigma),ParetoRV(theta,kappa),RayleighRV(theta),TriangularRV(a,b,c) TRV(N),UniformRV(a,b),WeibullRV(theta,kappa)
Discrete Distributions:
BenfordRV(),BinomialRV(n,p),GeometricRV(p),PoissonRV(theta)
In [9]: # Create an unspecified Weibull distribution
In [10]: X=WeibullRV()
In [11]: X.display()
continuous pdf
for 0 <= x <= oo
---------------------------
κ
κ κ - 1 -(θ⋅x)
κ⋅θ ⋅x ⋅ℯ
---------------------------
In [12]: # Create a partially specified Weibull distribution
In [13]: X=WeibullRV(kappa=Rational(1,2))
In [14]: X.display()
continuous pdf
for 0 <= x <= oo
---------------------------
___ ___
___ -╲╱ θ ⋅╲╱ x
╲╱ θ ⋅ℯ
───────────────────
___
2⋅╲╱ x
---------------------------
In [15]: # Create a fully specified Weibull distribution
In [16]: X=WeibullRV(Rational(1,2),2)
In [17]: X.display()
continuous pdf
for 0 <= x <= oo
---------------------------
2
-x
────
4
x⋅ℯ
───────
2
---------------------------
APPLPy has a procedure that will verify whether or not a PDF is valid. This procedure takes no arguments, and returns a print statement that confirms or denies that validity of the PDF. Along with the print statement, the procedure will return True
if the random variable is valid and False
if the random variable is not valid.
X.verifyPDF()
In [21]: X=TriangularRV(2,4,7)
In [22]: X.verifyPDF()
Now checking for area...
The area under f(x) is: 1
Now checking for absolute value...
The pdf of the random variable:
[x/5 - 2/5, -2*x/15 + 14/15]
continuous pdf with support [2, 4, 7]
is valid
In [23]: Y=RV([x**2],[0,10],['continuous','pdf'])
In [24]: Y.verifyPDF()
Now checking for area...
The area under f(x) is: 1000/3
Now checking for absolute value...
The pdf of the random variable:
[x**2]
continuous pdf with support [0, 10]
is not valid
This procedure transforms discrete random variables with a functional representation into discrete random variables with a PMF representation.
Convert(X,inc=1)
-
X: a discrete random variable with a functional representation. Note,
Convert
does not work on discrete random variables with infinite support -
inc (default: 1): the interval between values in the discrete random variable
In [15]: X=BinomialRV(5,0.25)
In [16]: X.display()
Discrete pdf
for 0 <= x <= 5
---------------------------
x -x + 5
120⋅0.25 ⋅0.75
────────────────────
x!⋅(-x + 5)!
---------------------------
In [17]: Convert(X)
Out[17]: discrete pdf where {x->f(x)}:
{0 -> 0.237304687500000}, {1 -> 0.395507812500000}, {2 -> 0.263671875000000}, {3 -> 0.0878906250000000}, {4 -> 0.0146484375000000}, {5 -> 0.000976562500000000}
APPLPy contains a procedure for generating the LaTeX code for a random variable
X.latex()
In [3]: T=TriangularRV(1,4,6)
In [4]: T.display()
continuous pdf
for 1 <= x <= 4
---------------------------
2⋅x 2
─── - ──
15 15
---------------------------
for 4 <= x <= 6
---------------------------
x 6
- ─ + ─
5 5
---------------------------
In [5]: print T.latex()
Out[5]: \begin{cases} \frac{2 x}{15} - 0.133333333333333 & \text{for}\: x \geq 1 \\- \frac{x}{5} + 1.2 & \text{for}\: x \geq 4 \\0 & \text{otherwise} \end{cases}
Sympy occasionally returns output that is not fully simplified. For instance, Sympy sometimes fails to simplify log(exp(x)) to x. The simplify command performs additional simplification on APPLPy random variables that is more robust than the built-in Sympy simplification routines.
X.simplify()
In [88]: U=UniformRV(Rational(1),Rational(2))
In [89]: Z = ConvolutionIID(U,3)
In [90]: Z.display()
continuous pdf
for 3 <= x <= 4
---------------------------
2⎛ x⎞
log ⎝ℯ ⎠ ⎛ x⎞ 9
──────── - 3⋅log⎝ℯ ⎠ + ─
2 2
---------------------------
for 4 <= x <= 5
---------------------------
2⎛ x⎞ ⎛ x⎞ 39
- log ⎝ℯ ⎠ + 9⋅log⎝ℯ ⎠ - ──
2
---------------------------
for 5 <= x <= 6
---------------------------
2⎛ x⎞
log ⎝ℯ ⎠ ⎛ x⎞
──────── - 6⋅log⎝ℯ ⎠ + 18
2
---------------------------
In [91]: Z.simplify()
continuous pdf
for 3 <= x <= 4
---------------------------
2
x 9
── - 3⋅x + ─
2 2
---------------------------
for 4 <= x <= 5
---------------------------
2 39
- x + 9⋅x - ──
2
---------------------------
for 5 <= x <= 6
---------------------------
2
x
── - 6⋅x + 18
2
---------------------------