Algebraic Manipulation of Random Variables (Numerical)

Numerically approximate the PDF of a combination of the random variables X and Y.
538 Downloads
Updated 2 Aug 2010

View License

% [z zC Z ZC] = transRV(jointXY,C,opp,n) Numerically approximate the PDF of
% a nonlinear combination of the random variables X and Y given the joint
% distribution of X and Y. The joint distribution (jointXY) should be
% generated using [jointXY, C] = hist3([x(:) y(:)]).
%
% z = transRV(jointXY,C,opp,n) Use the opperation opp which can
% be: '+', '-', '*', or '/', to generate a PDF z of length n.
%
% z = transRV(...,'ZLim',[minz maxz]) specify the limits on z.
%
% z = transRV(...,'Visual') display the area of integration and CDF of Z.
%
% [z zC Z ZC zlim] = transRV(...) returns the PDF z with values centered at
% zC, and the CDF Z with values centered at ZC. Also returns the limits on
% z.
%
% NOTE: Results may be noisy if the output vector z has high resolution
% (larger n) than the input distribution.
%
% EXAMPLE
%
% % Set the number of bins
% n = 200;
%
% % Generate random variables
% x = 2*randn(1000000,1);
% y = .5*randn(1000000,1);
%
% % Display joint distribution
% [jointXY C] = hist3([x y],[n n]);
% figure; imagesc(C{1},C{2},jointXY');
% set(gca,'YDir','normal')
%
% % Approximate new distribution
% [z zC Z ZC zlim] = transRV(jointXY,C,'*',n,'ZLim',[-5,5],'Visual');
%
% % Compare to the distribution of data under the same opperation
% zdata = x.*y;
% [N xc] = hist(zdata(zdata >= zlim(1) & zdata <= zlim(2)),n);
% bin_width = xc(2)-xc(1);
% N = N/sum(N.*bin_width);
%
% figure; bar(xc,N,'hist')
% hold on; plot(zC,z,'r')
% legend('Dist of Data','Approximation')

% Created by Aaron Swan - Summer 2010
% MERS Lab - Brigham Young University

Cite As

Aaron Swan (2024). Algebraic Manipulation of Random Variables (Numerical) (https://www.mathworks.com/matlabcentral/fileexchange/28332-algebraic-manipulation-of-random-variables-numerical), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2008b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Random Number Generation in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes
1.1.0.0

Clarified the description and license

1.0.0.0