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

BootcampStat Code Sol

Uploaded by

rehanair08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

BootcampStat Code Sol

Uploaded by

rehanair08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Computational Experiments in Statistics

Download following +2 book chapter on probability theory and


refer for theory behind the exercise.

https://ncert.nic.in/ncerts/l/lemh207.pdf

You need Excel/Matlab/Octave/Numpy for doing the Computational


experiments.

This will be taught during Bootcamp

Main functions used for Experiments

rand(n,1) generates n random numbers from U(0,1)

randn(n,1) generates n random numbers from N(0,1)

sum(x) sum elements in the array x

length(x) give number of elements in array x

histogram(x) draw histogram for elements in array x

cumsum(x) find cumulative sequential sum

1. Generate N (=10000) U(0,1) Random numbers and store in x.


Assume Random Variable X follows U(0,1). The process is called
sampling from distribution X.
2. Find sum(x)/N . This is an estimate of E(X). What is theoretical

3. Find sum(x.^2)/N. This is an estimate of . What is


theoretical

1
4. Find sum(x.^3)/N. This is an estimate of . What is
theoretical
5. Find sum(exp(x))/N. This is an estimate of . What is
theoretical
6. Find sum(x.^2)/N- (sum(x)/N)^2 . This is estimate of Variance .
Find Theoretical variance . Show that
it is 1/12
7. Generate 100000 x 2 U(0,1) random number matrix. Add 'row
wise' to repesent sampling from Z=X1+X2 where X1 and X2
are two IID Random variables( independently and identically
distributed RVs). Draw the histogram. What is the shape it is
tending to?.
8. Generate 1000 x 3 U(0,1) random number matrix. Add 'row wise'
to repesent sampling from Z=X1+X2+X3 . Draw the histogram.
What is the shape it is tending to?.
9. Generate 100000 x 12 U(0,1) random number matrix.
Add 'row wise' and subtract 6 to represent sampling from
Z=(X1+X2+..+X12) - 6 . Draw the histogram. What is the shape it
is tending to?. How close this is to N(0,1) distribution?.
10. Generate 100000 N(0,1) random number vector. Draw the
histogram. What is the shape it is tending to?. Find Matlab
command for drawing 'smooth' histogram and plot again.
11. Generate 100000 Random numbers from following

discrete distribution . . Find mean and

standared deviation of the random vector created.

2
12. Find Theoretical mean and standared deviation using the formula

for the data in 11

Solution to Problem 1 and 2.

Generate N (=10000) U(0,1) Random numbers and store in x. Assume


Random Variable X follows U(0,1). The process is called sampling from
distribution X.

Find sum(x)/N . This is an estimate of E(X). What is theoretical

x=rand(10000,1);
Expected_x=sum(x)/length(x)

Expected_x = 0.5013

3
3. Find sum(x.^2)/N. This is an estimate of . What is theoretical

x=rand(10000,1);
Expected_xsquare=sum(x.^2)/length(x)

Expected_x = 0.3363

4. Find sum(x.^3)/N. This is an estimate of . What is theoretical

x=rand(10000,1);
Expected_xcube=sum(x.^3)/length(x)

Expected_xcube = 0.2440

5. Find sum(exp(x))/N. This is an estimate of . What is theoretical

x=rand(10000,1);
Expected_expox=sum(exp(x))/length(x)

Expected_expox = 1.7154

4
6. Find sum(x.^2)/N- (sum(x)/N)^2 . This is estimate of Variance . Find
Theoretical variance . Show that it is 1/12

N=10000;
x=rand(N,1);
Varx=sum(x.^2)/N-(sum(x)/N)^2

Varx = 0.0842

7. Generate 100000 x 2 U(0,1) random number matrix. Add 'row wise'


to repesent sampling from Z=X1+X2 where X1 and X2 are two IID
Random variables( independently and identically distributed RVs). Draw
the histogram. What is the shape it is tending to?.

% Sum of Uniform Random variables


x=rand(100000,2);
y=sum(x,2);
histogram(y)

5
Shape it is tending to Triangular distribution centred around x=1

8. Generate 100000 x 3 U(0,1) random number matrix. Add 'row wise'


to repesent sampling from Z=X1+X2+X3 . Draw the histogram. What is
the shape it is tending to?.

% Sum of Uniform Random variables


x=rand(100000,3);
y=sum(x,2);
histogram(y)

6
% Histogram tends to symmetric gaussian

9. Generate 100000 x 12 U(0,1) random number matrix. Add 'row wise'


and subtract 6 to represent sampling from Z=(X1+X2+..+X12) - 6 . Draw
the histogram. What is the shape it is tending to?. How close this is to
N(0,1) distribution?.

% Sum of 12 Uniform Random variables minus 6


x=rand(100000,12);
y=sum(x,2)-6;
histogram(y)

7
% Histogram tends to symmetric standared gaussian

10. Generate 10000 N(0,1) random number vector. Draw the


histogram. What is the shape it is tending to?. Find Matlab command
for drawing 'smooth' histogram and plot again.

x=randn(10000,1);
histogram(x)

8
[f,xi] = ksdensity(x); % Kernel density
figure
plot(xi,f)

% Histogram tends to symmetric standared gaussian

11. Generate 1000 Random numbers from following discrete

distribution . . Find mean and standared

deviation of the random vector created.

Prob=[0.3 0.2 .1 .4];


x=[1 2 3 4]

x = 1×4
1 2 3 4

Cumprob=cumsum(Prob);
% Cumprob=cumulative prob : [0.3 .5 .6 1]

9
d(1:1000)=0;
for i=1:1000
y=rand();
if y<Cumprob(1)
d(i)=x(1);
elseif y<Cumprob(2)
d(i)=x(2);
elseif y<Cumprob(3)
d(i)=x(3);
else
d(i)=x(4);
end
end
figure
histogram(d,"Normalization","probability")

xbar=mean(d) % mean of elements in x vector

xbar = 2.5720

Stddev=std(d) % standared deviavion of x

Stddev = 1.2839

10
12. Find Theoretical mean and standared deviation using the formula for

the data in 11

Prob=[0.3 0.2 .1 .4];


x=[1 2 3 4];
Cumprob=cumsum(Prob);
% Cumprob=cumulative prob : [0.3 .5 .6 1]
d(1:1000)=0;
for i=1:1000
y=rand();
if y<Cumprob(1)
d(i)=x(1);
elseif y<Cumprob(2)
d(i)=x(2);
elseif y<Cumprob(3)
d(i)=x(3);
else
d(i)=x(4);
end
end
% To find E(X)
x=x(:); % Make it a column vector
p=Prob(:); % Make it a column vector and store in p
Ex=sum(x.*p ) ; % Mean
Ex2=sum((x.^2).*p);
Vx=Ex2-(Ex*Ex); % variance
disp(Ex) % Theoretical Mean Value

2.6000

disp(sqrt(Vx)) % Theoretical std deviation

1.2806

11
12

You might also like