Skip to content

Random Variable Algebra

Matthew Robinson edited this page Aug 7, 2016 · 3 revisions

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

Syntax and Behavior

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.

Examples

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 
     ╱   ───   ───    ───
  32     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
---------------------------
 x3
 
──
3 
---------------------------

The * and / operators

Syntax and Behavior

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.

Examples

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
6x   
---------------------------

RV Algebra with APPL Syntax

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.

Convolution

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.

Syntax

Convolution(X,Y)
ConvolutionIID(X,n)

Examples

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
---------------------------xlog- 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
---------------------------
   2xlog ⎠        ⎛ x9
──────── - 3log+2                   2
---------------------------
 
 
for 4 <= x <= 5
---------------------------
     2x⎞        ⎛ x39
- log+ 9log- ──
                         2 
---------------------------
 
 
for 5 <= x <= 6
---------------------------
   2xlog ⎠        ⎛ x⎞     
──────── - 6log+ 18
   2                     
---------------------------

Product

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.

Syntax

Product(X,Y)
ProductIID(X,n)

Examples

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)   3log(2)⋅log(x)   9log (2)
─────── - ─────────────── + ─────────
   16            8              16   
---------------------------
 
 
for 16 <= x <= 32
---------------------------
     2                              2   
  log (x)   9log(2)⋅log(x)   39log (2)
- ─────── + ─────────────── - ──────────
     8             8              16    
---------------------------
 
 
for 32 <= x <= 64
---------------------------
   2                             2   
log (x)   3log(2)⋅log(x)   9log (2)
─────── - ─────────────── + ─────────
   16            4              4    
---------------------------
Clone this wiki locally