Course Problem On Statistics
Course Problem On Statistics
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
Part b
=================Simulating
from
a
Gamma
distribution====================
==============with noninteger shape and scalling factor==================
%
%
%
%
%
%
%
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
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
11
12
13