% This plots the pdf of the exponential and the uniform random variables.
figure(1);
subplot(2,1,1);
plot(0:0.001:10,pdf('exponential',0:0.001:10,1))
subplot(2,1,2);
plot(0:0.001:10,pdf('uniform',0:0.001:10,0,1))

% This plots the cdf of the exponential and the uniform random variables.
figure(2);
subplot(2,1,1);
plot(0:0.001:10,cdf('exponential',0:0.001:10,1))
subplot(2,1,2);
plot(0:0.001:10,cdf('uniform',0:0.001:10,0,1))

% This generates outcomes of the exponential and uniform random variables
N = 10000;
X = random('exponential', 1, N, 1);
Y = rand(N, 1);

% This plots outcomes for the exponential and uniform random variables.
figure(3);
subplot(2,1,1);
[n, xout] = hist(X, 20);
bar(xout,n/N);

subplot(2,1,2);
[n, xout] = hist(Y, 20);
bar(xout,n/N);

% This generates a joint pdf for independent marginals X and Y that
% are exponential and uniform.
figure(4);
plot(X, Y, 'x');

% This describes the event that x < 1 and y < 0.5
% Exact Calculations using CDF
Exponential_less_than_one = cdf('exponential', 1, 1)
Uniform_less_than_half = cdf('uniform', 0.5, 0,1)
Exact_event_prob = Exponential_less_than_one * Uniform_less_than_half

%Graphical Demonstration
figure(5);
count = 0;
for i = 1:N
    if (X(i) < 1) && (Y(i) < 0.5)
        count = count + 1;
        plot(X(i), Y(i), 'rX')
        hold on;
    else
        plot(X(i), Y(i), 'bX')
    end
end
hold off;

Estimated_prob = count / N

% Now computing moments
display('Simulated mean of X')
mean(X)
display('Simulated variance of X')
var(X)

display('Simulated mean of Y')
mean(Y)
display('Simulated variance of Y')
var(Y)
