-
Notifications
You must be signed in to change notification settings - Fork 1
Random Variable Algebra
APPLPy is capable of computing probability distributions for complex stochastic models using simple and intuitive syntax. The random variable class is designed to interact with Python's built-in +,-,*,/ and ** operators. When interpreting a series of operations, APPLPy follows the order of operations in the expected manner.
The + and - operators are set to perform sums and shifts of random variables. When a random variable is passed on either side of the operator, the Convolution(X,Y) command (described below) is called to compute the sum of the two random variables. If a constant is passed one on side of the operator, the random variable will be shifted left or right by the magnitude of the constant. Finally, the - operator can be used to compute the negative reflection of the random variable across the y-axis.
In [4]: X=ExponentialRV(Rational(1,2))
In [5]: Y=ExponentialRV(Rational(1,3))
In [6]: # Compute the sum of two random variables
In [7]: Z=X+Y
In [8]: Z.display()
continuous pdf
for 0 <= x <= oo
---------------------------
______
╱ -x -x -x
╱ ─── ─── ───
3 ╱ 2 3 3
- ╲╱ ℯ ⋅ℯ + ℯ
---------------------------
In [9]: # Shift a random variable left
In [10]: Z=X-4
In [11]: Z.display()
continuous pdf
for -4 <= x <= oo
---------------------------
x
- ─ - 2
2
ℯ
────────
2
---------------------------
In [12]: # Compute the reflection of a random variable
In [13]: Z=-Y
In [14]: Z.display()
continuous pdf
for -oo <= x <= 0
---------------------------
x
─
3
ℯ
──
3
---------------------------The * and / operators are set to perform products and scaling of random variables. When a random variable is passed on either side of the operator, the Product(X,Y) command (described below) is called to compute the product of the two random variables. If a constant is passed one on side of the operator, the random variable will be scaled by the magnitude of the constant. Additionally, the ** operator can be used to compute a transformation by x^n.
In [23]: U=UniformRV(Rational(1),Rational(3))
In [24]: U2=UniformRV(Rational(4),Rational(5))
In [25]: # Compute the product of two random variables
In [26]: Z=U*U2
In [27]: Z.display()
continuous pdf
for 4 <= x <= 5
---------------------------
log(x)
────── - log(2)
2
---------------------------
for 5 <= x <= 12
---------------------------
log(5)
-log(2) + ──────
2
---------------------------
for 12 <= x <= 15
---------------------------
log(x) log(15)
- ────── + ───────
2 2
---------------------------
In [28]: # Scale a random variable
In [29]: Z=U/4
In [30]: Z.display()
continuous pdf
for 1/4 <= x <= 3/4
---------------------------
2
---------------------------
In [31]: # Compute the transformation of a random variable by X^3
In [32]: Z=U**3
In [33]: Z.display()
continuous pdf
for 1 <= x <= 27
---------------------------
1
──────
2/3
6⋅x
---------------------------The +,-,* and / operators call the Convolution and Product procedures to compute sums and products of random variables. These procedures are detailed below. In the Maple implementation of APPL, users call the functions explicitly instead of using operators. APPLPy continues to support the use of traditional APPL syntax, as well as the use of operators.
The convolution procedures allows for the summation of two random variables. For lifetime distributions, convolutions are computed directly. However, for all other distributions, the convolution is computed by first finding the product of exp(X) and exp(Y), and then transforming the result. ConvolutionIID(X,n) computes the convolution of n IID random variables.
Convolution(X,Y)
ConvolutionIID(X,n)In [49]: U=UniformRV(Rational(1),Rational(2))
In [50]: U2=UniformRV(Rational(3),Rational(4))
In [51]: # Compute the convolution of two random variables
In [52]: Z=Convolution(U,U2)
In [53]: Z.display()
continuous pdf
for 4 <= x <= 5
---------------------------
⎛ x⎞
log⎝ℯ ⎠ - 4
---------------------------
for 5 <= x <= 6
---------------------------
⎛ x⎞
- log⎝ℯ ⎠ + 6
---------------------------
In [54]: # Compute the convolution of three IID random variables
In [55]: Z=ConvolutionIID(U,3)
In [56]: 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
---------------------------The product procedure computes the product of two random variables. In addition, the ProductIID(X,n) procedure can be called to compute the product of n IID random variables.
Product(X,Y)
ProductIID(X,n)In [57]: X=UniformRV(Rational(2),Rational(4))
In [58]: Y=UniformRV(Rational(3),Rational(5))
In [59]: # Compute the product of two random variables
In [60]: Z=Product(X,Y)
In [61]: Z.display()
continuous pdf
for 6 <= x <= 10
---------------------------
log(x) log(6)
────── - ──────
4 4
---------------------------
for 10 <= x <= 12
---------------------------
log(3) log(5)
- ────── + ──────
4 4
---------------------------
for 12 <= x <= 20
---------------------------
log(x) log(2) log(5)
- ────── + ────── + ──────
4 2 4
---------------------------
In [62]: # Compute the product of three IID random variables
In [63]: Z=ProductIID(X,3)
In [64]: Z.display()
continuous pdf
for 8 <= x <= 16
---------------------------
2 2
log (x) 3⋅log(2)⋅log(x) 9⋅log (2)
─────── - ─────────────── + ─────────
16 8 16
---------------------------
for 16 <= x <= 32
---------------------------
2 2
log (x) 9⋅log(2)⋅log(x) 39⋅log (2)
- ─────── + ─────────────── - ──────────
8 8 16
---------------------------
for 32 <= x <= 64
---------------------------
2 2
log (x) 3⋅log(2)⋅log(x) 9⋅log (2)
─────── - ─────────────── + ─────────
16 4 4
---------------------------