%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Uniform Random Variable Simulator
%
%  This code simulates several trials of a random variable with a uniform
%  random variable on the interval [0,1].
%
%  This code plots the density for emperical the mean of a uniform random
%  variable on the interval [0,1] when drawing the mean from N
%  realizations.
%
%  Inputs:
%  1. n=number of points to sample random variable over
%  2. M=number of bins to use
%  3. N=number of realizations
%
%  Ouputs:
%   1. Plot of historgram as well as the exact density for the uniform
%   random variable.
%   2. Plot of the emperical mean for i=1:N realizations
%   3. Plot of the density of the emperical mean for n samples.
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

set(groot,'defaultLineLineWidth',4.0)

%% Inputs

n=100000;
M=100;
N=1000;

%% Generation of random samples
X=rand(1,n);

%% Generation of exact pdf:
x=linspace(0,1,M);
y=ones(1,M);

%% Plotting Histogram of random variable
figure('units','normalized','outerposition',[0 0 1 1]);

hold on;
histogram(X,M,'Normalization','pdf');
plot(x,y,'linewidth',4);

xlabel('$$x$$','interpreter','latex')
ylabel('$$f(x)$$','interpreter','latex')

ax=gca;
ax.FontSize = 40;

%% Plotting Cumulative Distribution
figure('units','normalized','outerposition',[0 0 1 1]);
cdfplot(X)
xlabel('$$x$$','interpreter','latex')
ylabel('$$F(x)$$','interpreter','latex')

ax=gca;
ax.FontSize = 40;

%% Plotting Emperical mean as a function of number of realizations.
for i=1:N
    mu(i)=MeanUniform(i);
end

figure('units','normalized','outerposition',[0 0 1 1]);
plot(mu,'o','linewidth',2);

xlabel('$$N$$','interpreter','latex')
ylabel('$$\mu$$','interpreter','latex')

ax=gca;
ax.FontSize = 40;

%% Plotting Probability Density as a function of number of realizations.
for i=1:n
    muN(i)=MeanUniform(N);
end

figure('units','normalized','outerposition',[0 0 1 1]);

hold on;
histogram(muN,M,'Normalization','pdf');
axis([0,1,0,50])
xlabel('$$\mu_N$$','interpreter','latex')
ylabel('$$f(x)$$','interpreter','latex')

ax=gca;
ax.FontSize = 40;


ax=gca;
ax.FontSize = 40;


