0% found this document useful (0 votes)
46 views

Course Problem On Statistics

solution
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Course Problem On Statistics

solution
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

ModifiedHandinAssignment1

RandomvariablegenerationandMonteCarlointegration

ComputerIntensiveStatisticalMethods
MajidKhorsandVakilzadeh

Lecturer:AndersSjgren
2013Nov03

Table of Contents
Introduction ....................................................................................................................... 1
Hand-in assignment 1 .......................................................................................................... 1
Task 1- Generate Random Variables ...................................................................................... 1
Part a- Standard normal variables from cauchy distribution ........................................................ 1
Part b ............................................................................................................................... 4
Task 2 .............................................................................................................................. 7
part a ............................................................................................................................... 7
part b .............................................................................................................................. 10

Introduction
% This report provides the Matlab code written for hand-in assignment 1
% accompanied with a brief explanation of different parts which for the
% sake of simplicity are included in Matlab script in green color.

Hand-in assignment 1
-------Random Variable Generation and Monte-Carlo Integration-----------clc
clear

Task 1- Generate Random Variables


Part a- Standard normal variables from cauchy
distribution
% In this part two embeded accept-reject algorithm is used:
% 1. To yield Cauchy variable from uniformly distributed R.V.s
%
so, in the first part f= (pi*b*(1+((x-a)/b)^2))^-1 (cauchy pdf)
%
and g= U[xi,xf](defined on the support of interest),
%
% 2. Accepted variable in the previous step are used in the second
%
Accept-Reject step to generate the Standard Normal Variables
%
so, in the second part
%
f = (sqrt(2*pi*sig^2))^-1*exp(-(x-mio)^2/2/sig^2) (Normal pdf)
%
and g is obtained cauchy distribution in the previous step,
%
Ns = 100000; % Desired number of samples
a=0; % Parameter values for the standard cauchy
b=1; % Parameter values for the standard cauchy
% support of the uniform distribution
xi=-10;
xf=10;

% Normalizing Factor for the 2nd accept-reject method


M2= 1.5; % Is chosen such that the peak value of the cauchy at x=0 be more
% that standard normal distribution
% Normal distribution
sig=1; mio=0;
% Algorithm
i=0;r=0;j=0;
while j<Ns
i=i+1;
u1 = rand(1);
x = tan(pi*(u1-.5));
% One Accept-reject step to generate Cauchy R.V.s
f = (pi*b*(1+((x-a)/b)^2))^-1; % Evaluate Cauchy PDF at uniform variable
u2 = rand(1);
N(i) = (sqrt(2*pi*sig^2))^-1*exp(-(x-mio)^2/2/sig^2);
if u2 < N(i)/M2/f % Second rejection step
j = j+1;
S(j) = x;
end
end
% The obsreved acceptance rate
fprintf('The obsreved acceptance rate is %4.2f%%\n',j/i*100)
fprintf('The theoretical acceptance rate is %4.2f%%\n',1/M2*100)
% the true acceptance rate is 1/M
% ploting the normalized histogram
[f,x]=hist(S,50);
dx=abs(x(2)-x(1));
Npdf = normpdf(x);
figure;bar(x,f/sum(f*dx));hold on;
plot(x,Npdf,'r')
% True random variable
[Y]=randn(1,Ns);
% CDF plot
figure
h2=cdfplot(Y);hold on;h1=cdfplot(S);
set(h2,'Color','r','LineStyle','. ')
set(h1,'Color','k')
legend('Theoretical','Empirical')
% Quantile plot
figure
qqplot(S,Y)
title('Q-Q plot of the true and empirical distribution')
xlabel('Empirical distribution')
ylabel('true distribution')
The obsreved acceptance rate is 66.71%
The theoretical acceptance rate is 66.67%

Part b
=================Simulating
from
a
Gamma
distribution====================
==============with noninteger shape and scalling factor==================
%
%
%
%
%
%
%

In this part first the exp(1) R.V.s are generated by transforming


u~U[0,1] using -log(1-u), then we know that if Xj are i.i.d exp(1)
variables, then Y=B*(sum(Xj)), for j=1:A, is distributed by ga(a,b)
Note: A and B are natural numbers
Finally the resulted gamma(A,B) are used in Accept-Reject step tp generate
ga(4.3,6.2)

Ns = 10000; % Desired number of samples


A1=4;
B1=7;

% shape factor for gamma distribution(instrumental pdf)


% Scale factor for gamma distribution(instrumental pdf)

A2= 4.3; % shape factor for gamma distribution (Target pdf)


B2= 6.2; % scale factor for gamma distribution (Target pdf)
% Normalizing constant
M=1.2;
% Algorithm

i=0;r=0;
while i<Ns
r=r+1;
% Gamma Random variables using exponential transform
x=B1*sum(-log(1-rand(1,A1))); % x ~ga(4,7)
Gd1(r)=gampdf(x,A1,B1);
Gd2(r)=gampdf(x,A2,B2);
u=rand(1);
if u <Gd2(r)/M/Gd1(r)
% accept-reject step
i=i+1;
sg(i)=x;
end
end
% The obsreved acceptance rate
fprintf('The obsreved acceptance rate is %4.2f%%\n',i/r*100)
fprintf('The theoretical acceptance rate is %4.2f%%\n',1/M*100)
% ploting the normalized histogram
[f,x]=hist(sg,50);
Gd2=gampdf(x,A2,B2);
dx=abs(x(2)-x(1));
figure;bar(x,f/sum(f*dx)); hold on;
plot(x,Gd2,'r')
% True random variable
[Y]=gamrnd(A2,B2,1,Ns);
% CDF plot
figure
h2=cdfplot(Y);hold on;h1=cdfplot(sg);
set(h1,'Color','k')
set(h2,'Color','r','LineStyle','. ')
legend('Theoretical','Empirical')
% Quantile plot
figure
qqplot(sg,Y)
title('Q-Q plot of the true and empirical distribution')
xlabel('Empirical distribution')
ylabel('true distribution')
The obsreved acceptance rate is 83.42%
The theoretical acceptance rate is 83.33%

Task 2
=================Monte-Carlo integration=================================
Ns=10000; % Number of samples
T1 = binornd(1,.2,1,Ns).*lognrnd(1,.1,1,Ns);
T2 = lognrnd(0,.25,1,Ns);
T3 = lognrnd(0,.3,1,Ns);
T4 = lognrnd(0,.2,1,Ns);
T = T1 + T2 + T3 + T4;
I=T>4;

part a
part a.a
ET = mean(T);
fprintf('The expected time-to-completion is %4.2f%%\n',ET)

% part a.b
Sig = sqrt(sum(T-ET)/Ns);
CI95= Sig/sqrt(Ns)*1.96;
% 95% confidence interval
fprintf('\n The 95%% confidence interval for time-to-completion is %4.2f%%\n',CI95)
% Probability of delay

fprintf('The probability of delay is %4.2f%%\n',sum(I)/Ns*100)


% part a.c
Delay_Pr = cumsum(I)./(1:Ns);
sigma=sqrt(cumsum((I-Delay_Pr).^2))./(1:Ns);
figure
plot(1:Ns,Delay_Pr,'r');hold on
plot(1:Ns,Delay_Pr+1.96*sigma,'g');hold on
plot(1:Ns,Delay_Pr-1.96*sigma,'g')
title('convergence plot for Probability of delay')
legend('Probability of delay','Based on CLT')
xlabel('sample size')
figure
for R=1:100
T1 = binornd(1,.2,1,Ns).*lognrnd(1,.1,1,Ns);
T2 = lognrnd(0,.25,1,Ns);
T3 = lognrnd(0,.3,1,Ns);
T4 = lognrnd(0,.2,1,Ns);
T = T1 + T2 + T3 + T4;
I=T>4;
Delay_Pr = cumsum(I)./(1:Ns);
plot(1:Ns,Delay_Pr,'r');hold on
end
title('convergence plot for Probability of delay')
xlabel('sample size')
% As seen the variability of the expected profit is more when we repeat the
% experiment 100 times and it shows that CLT provides uncertainty bounds
% which are too confident
The expected time-to-completion is 3.64%
The 95% confidence interval for time-to-completion is 0.00%
The probability of delay is 22.91%

part b
T1 = binornd(1,.2,1,Ns).*lognrnd(1,.1,1,Ns);
T2 = lognrnd(0,.25,1,Ns);
T3 = lognrnd(0,.3,1,Ns);
T4 = lognrnd(0,.2,1,Ns);
T = T1 + T2 + T3 + T4; % Time-To-Completion
Profit = 200 + 50 * randn(1,Ns);
D=(T-4);D(D<0)=0;
delayfee=D*100;
Tot_profit = Profit-delayfee;
[TP,xs]=hist(Tot_profit,30);
dx=abs(xs(2)-xs(1));
figure;bar(xs,TP/sum(TP*dx));
title('distribution of expected profit')
% Part b.a
Ex_Tot_profit = cumsum(Tot_profit)./(1:Ns);
DP_sigma = sqrt(cumsum((Tot_profit-Ex_Tot_profit).^2))./(1:Ns);
figure
plot(1:Ns,Ex_Tot_profit,'r');hold on
plot(1:Ns,Ex_Tot_profit+1.96*DP_sigma,'g');hold on
plot(1:Ns,Ex_Tot_profit-1.96*DP_sigma,'g')
title('convergence plot for expected profit')
legend('Expected profit','Based on CLT')
xlabel('sample size')
figure
for R=1:100
T1 = binornd(1,.2,1,Ns).*lognrnd(1,.1,1,Ns);
T2 = lognrnd(0,.25,1,Ns);
T3 = lognrnd(0,.3,1,Ns);
T4 = lognrnd(0,.2,1,Ns);
T = T1 + T2 + T3 + T4; % Time-To-Completion
% Profit of project is tought to be normally distributed as N(200,50^2)
Profit = 200 + 50 * randn(1,Ns); % Profit
D=(T-4);D(D<0)=0;
delayfee=D*100;
Tot_profit = Profit-delayfee;
Ex_Tot_profit = cumsum(Tot_profit)./(1:Ns);
plot(1:Ns,Ex_Tot_profit,'r');hold on
end
title('convergence plot for expected profit')
xlabel('sample size')
% part b.b
Pr_make_100 = length(find(Tot_profit>100))/length(Tot_profit);
fprintf('Probability of profit of 100MSEK or more is %4.2f%%\n',Pr_make_100)
% part b.c
Pr_lost = length(find(Tot_profit<0))/length(Tot_profit);

10

fprintf('Probability of loosing money is %4.2f%%\n',CI95)


Probability of profit of 100MSEK or more is 0.81%
Probability of loosing money is 0.00%

11

12

Published with MATLAB R2013a

13

You might also like