File Exchange

image thumbnail


version (5.45 KB) by Damith Senaratne
Provides a MATLAB class for simulating random variables (RV).

1 Download

Updated 14 Jun 2012

View Version History

View License

Each object of this class is a RV, which can be instantiated and used to compute the expectation of functions or the PDF (via Monte Carlo simulation).

The RVs may combined to form others ('rndvar' uses a synchronization mechanism to ensure that the dependencies of RVs are correctly represented when the RVs are instantiated).

The probability distribution corresponding to a 'rndvar' object may be specified in one of the following ways:

(i) selecting from MATLAB's built-in distributions
>> X = rndvar.builtInVar('Gamma',1,2);
>> mean(X) % note: same as X.expecationOf(@(x)x)
ans =

for more info: help rndvar.builtInVar

(ii) explicit definition (providing the inverse CDF)
>> Y = rndvar.defineVar(@(y)-log(1-y));
% note: exponential CDF is y = F(x) = 1 - exp(-x)
>> mean(Y)
ans =

for more info: help rndvar.defineVar

(iii) as functions of other RVs
(iii-a) using overloaded basic arithmetic and elementary operators
>> X = rndvar.builtInVar('Gamma',1,2);
>> Y = rndvar.builtInVar('Gamma',1,4);
>> Z = X^2 + sin(Y) + 1;
>> mean(Z)
ans =

>> U = X + 1; V = X + Y; mean(U*V)-mean(X^2)-mean(X*Y)-mean(Y)-mean(X)
ans =
% reaches 0 for higher # MC points

overloaded operators:
plus (+), minus (-), uminus (unary -), uplus (unary +), mtimes (*), mrdivide (/), mldivide (\), mpower (^), lt (<), gt (>), le (<=), ge (>=), ne (!=), eq (==)

overloaded functions:
sin, sinh, asin, asinh, cos, cosh, acos, acosh, tan, tanh, atan, atanh, sec, sech, asec, asech, csc, csch, acsc, acsch, cot, coth, acot, acoth, exp, log, log10, log2, sqrt, abs, angle, complex (binary), conj, imag, real, isreal, fix, floor, ceil, round, mod (binary), rem (binary), sign, mean

(iii-b.) using rndvar.compositeVar (for more complicated relations)
>> Z = rndvar.compositeVar(X,Y,@(x,y)max(x.^2,y.^3));

Each `rndvar' object is automatically assigned a unique identifier.
The RVs can be explicitly labeled using setLabel method.
>> X = rndvar.builtInVar('Gamma',1,2); X.setLabel('Variable 1');
>> X.Label
ans =
Variable 1

Public methods: (use `help rndvar.method_name' to get help for `method_name')
instantiate, pdf, expecationOf, setLabel
>> help rndvar.instantiate

A java.util.TreeMap data structure is used to internally keep track of SyncState of each `rndvar' object. (static method `rndvar.reset' clears the storage it uses).

Cite As

Damith Senaratne (2021). rndvar (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (0)

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!