%% Temple university CIS 2033
% A modern Introduction to Probability and Statistics
% Chapter 6: Simulation
%
% Author: Djordje Gligorijevic
% email: gligorijevic@temple.edu

% NOTE: These excercise repeat from 5. class. They were put there for
% motivational purpose before the lecture on sampling.


%% Example 3 (a)
%
% Generate samples from a specific distribution
% Generate 100000 samples from U(0, 2)
distnames = {'Uniform'};
colors = {'b','c','k','m','r'};
betas=[2];
fig = figure;
numOfPoints = 10^5;
for i=1:length(betas)
    uds = makedist(distnames{1},'lower',0,'upper',betas(i));
    ys = random(uds,numOfPoints,1);
    hist(ys);
    hold on;
end
legend('U(0, 2)');
saveas(fig,'D3a.eps','epsc');

% Generate 100000 samples from Par(2)
distnames = {'GeneralizedPareto'};
alphas = [2];
fig = figure;
for i=1:length(alphas)
    k1 = 1/alphas(i);
    sigma1 = 1/alphas(i);    
    theta1 = 1;
    uds = makedist(distnames{1},'k',k1, 'sigma',sigma1,'theta',theta1);
    ys = random(uds,numOfPoints,1);
    hist(ys);
    hold on;
end
legend('Par(2)');
saveas(fig,'D3b.eps','epsc');

%% Example 3 (b)
%
% Generate samples from a specific distribution
% Generate 100000 samples from U(0, 2)
colors = {'b','c','k','m','r'};
betas=[2];
fig = figure;
numOfPoints = 10^5;
for i=1:length(betas)
    ys = random('Uniform',0,betas(i),[numOfPoints,1]);
    hist(ys);
    hold on;
end
legend('U(0, 2)');
saveas(fig,'D3a.eps','epsc');

% Generate 100000 samples from Par(2)
alphas = [2];
fig = figure;
for i=1:length(alphas)
    k1 = 1/alphas(i);
    sigma1 = 1/alphas(i);    
    theta1 = 1;
    ys = random('Generalized Pareto',k1,sigma1,theta1,[numOfPoints,1]);
    hist(ys);
    hold on;
end
legend('Par(2)');
saveas(fig,'D3b.eps','epsc');

%% Example 4 (a)
% Generate samples with X distribution from a Y distribution
% Generate datapoints with U(0, 1) from a Par(2) 
distnames = {'GeneralizedPareto'};
alphas = [2];
numOfPoints = 10^5;
fig = figure;
for i=1:length(alphas)
    k1 = 1/alphas(i);
    sigma1 = 1/alphas(i);    
    theta1 = 1;
    uds = makedist(distnames{1},'k',k1, 'sigma',sigma1,'theta',theta1);
    ys = random(uds,numOfPoints,1);
    zs = 1-1./(ys.^2);
    %zs = 1./(ys.^2);
    hist(zs);
    hold on;    
end
legend('U(0, 1)');
saveas(fig,'D4a.eps','epsc');

%% Example 4 (b)
%
% Generate samples with X distribution from a Y distribution
% Generate datapoints with U(0, 1) from a Par(2) 
alphas = [2];
numOfPoints = 10^5;
fig = figure;
for i=1:length(alphas)
    k1 = 1/alphas(i);
    sigma1 = 1/alphas(i);    
    theta1 = 1;
    uds = random('Generalized Pareto',k1,sigma1,theta1,[numOfPoints,1]);
    zs = 1-1./(uds.^2);
    %zs = 1./(ys.^2);
    hist(zs);
    hold on;    
end
legend('U(0, 1)');
saveas(fig,'D4a.eps','epsc');