rndvar
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
e.g.
>> X = rndvar.builtInVar('Gamma',1,2);
>> mean(X) % note: same as X.expecationOf(@(x)x)
ans =
2.0003
for more info: help rndvar.builtInVar
(ii) explicit definition (providing the inverse CDF)
e.g.
>> Y = rndvar.defineVar(@(y)-log(1-y));
% note: exponential CDF is y = F(x) = 1 - exp(-x)
>> mean(Y)
ans =
1.004
for more info: help rndvar.defineVar
(iii) as functions of other RVs
(iii-a) using overloaded basic arithmetic and elementary operators
e.g.
>> X = rndvar.builtInVar('Gamma',1,2);
>> Y = rndvar.builtInVar('Gamma',1,4);
>> Z = X^2 + sin(Y) + 1;
>> mean(Z)
ans =
9.2311
>> U = X + 1; V = X + Y; mean(U*V)-mean(X^2)-mean(X*Y)-mean(Y)-mean(X)
ans =
0.0182
% 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)
e.g.
>> 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.
e.g.
>> 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
e.g.
>> 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 (2024). rndvar (https://www.mathworks.com/matlabcentral/fileexchange/37128-rndvar), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxCategories
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.