%% Homework 8 Section 4.1, Problem 1 (first part)

N = 10000;
%Generates N outcomes from the uniform distribution from -1 to 1
X = random('uniform', -1, 1, N, 1);

% Computes the Y outcomes from the X outcomes
Y = sqrt(abs(X));
Z = -log(abs(X));

figure(1);
clf;

%Plots the histogram of X
subplot(2,1,1);
[n, xout] = hist(X, 10);
bar(xout,n/N);
title('Uniform Distribution of random variable X from -1 to 1');
ylabel('PMF Probability');

%Plots the histogram of Y
subplot(2,1,2);
[n, xout] = hist(Y, 10);
bar(xout,n/N);
title('Distribution of random variable Y');
ylabel('PMF Probability');

% These essentially give you discrete PMFs above.
% If you want to estimate the actual probabilities for a continuous
% distribution, then you need to make sure that you have an area that is
% equal to 1. The following code thus takes into account the widths of the
% bins that you select.

figure(2);
clf;

%Plots the histogram of X
subplot(2,1,1);
[n, xout] = hist(X, 10);
bar(xout,n/N / (xout(2) - xout(1)));
title('Uniform Distribution of random variable X from -1 to 1');

%Plots the histogram of Y
subplot(2,1,2);
[n, xout] = hist(Y, 10);
bar(xout,n/N / (xout(2) - xout(1)));
title('Distribution of random variable Y');

figure(3);
clf;

%Plots the histogram of X
subplot(2,1,1);
[n, xout] = hist(X, 10);
bar(xout,n/N);
title('Uniform Distribution of random variable X from -1 to 1');
ylabel('PMF Probability');

%Plots the histogram of Y
subplot(2,1,2);
[n, xout] = hist(Y, 10);
bar(xout,n/N);
title('Distribution of random variable Y');
ylabel('PMF Probability');

% These essentially give you discrete PMFs above.
% If you want to estimate the actual probabilities for a continuous
% distribution, then you need to make sure that you have an area that is
% equal to 1. The following code thus takes into account the widths of the
% bins that you select.

figure(4);
clf;

%Plots the histogram of X
subplot(2,1,1);
[n, xout] = hist(X, 10);
bar(xout,n/N / (xout(2) - xout(1)));
title('Uniform Distribution of random variable X from -1 to 1');

%Plots the histogram of Y
subplot(2,1,2);
[n, xout] = hist(Z, 10);
bar(xout,n/N / (xout(2) - xout(1)));
title('Distribution of random variable Z');