Answers

advertisement
Stat 992 Assignment – Presentation Day 1 - MATLAB
Question 1. Adapt the Matlab program in Code 6.2 to illustrate the Beta(α, β) distribution
where α = 2 and β = 3.
Answer
%% Explore the beta distribution, beta( alpha , beta )
alpha = 2;
beta = 3;
xmin = 0; % minimum x value for pdf and cdf plot
xmax = 1; % maximum x value for pdf and cdf plot
n = 100; % number of points on pdf and cdf plot
k = 10000; % number of random draws for histogram
% create a set of values ranging from xmin to xmax
x = linspace( xmin , xmax , n );
p = betapdf( x , alpha , beta ); % calculate the pdf
c = betacdf( x , alpha , beta ); % calculate the cdf
figure( 1 ); clf; % create a new figure and clear the contents
subplot( 1,3,1 );
plot( x , p , 'k' ); Not sure what ‘k’ does
xlabel( 'x' ); ylabel( 'pdf' );
title( 'Probability Density Function' );
subplot( 1,3,2 );
plot( x , c , 'r' );
xlabel( 'x' ); ylabel( 'cdf' );
title( 'Cumulative Density Function' );
% draw k random numbers from a beta( 2 , 3 ) distribution
seed=1; rand('state',seed);randn('state',seed)
y = betarnd( alpha , beta , k , 1 );
subplot( 1,3,3 );
hist( y , 20 );
xlabel( 'x' ); ylabel( 'frequency' );
title( 'Histogram of beta random values' );
Question 2. Adapt the matlab program above to illustrate the Binomial(N, θ) distribution
where N = 10 and θ = 0.7.
Answer
% Explore the binomial distribution; binom(n, θ)
clear all;
n = 10; % number of points on pdf and cdf plot
p1=0.7;
k = 10000; % number of random draws for histogram
% create a set of values ranging from xmin to xmax
x =0:n;
p = binopdf( x , n , p1 ); % calculate the pdf
c = binocdf( x , n , p1 ); % calculate the cdf
figure( 1 ); clf; % create a new figure and clear the contents
subplot( 1,3,1 );
bar( x , p , 'k' );
xlabel( 'x' ); ylabel( 'pdf' );
title( 'Probability Density Function' );
subplot( 1,3,2 );
bar( x , c , 'k' );
xlabel( 'x' ); ylabel( 'cdf' );
title( 'Cumulative Density Function' );
% draw k random numbers from a binomial distribution
seed=1;rand('state',seed);
y = binornd( n , p1 , k , 1 );
subplot( 1,3,3 );
hist( y );
xlabel( 'x' ); ylabel( 'frequency' );
title( 'Histogram of binomial random values' );
Question 3: Write a demonstration program to sample 10 values from a Bernoulli(θ)
distribution with θ = 0.3. Recall that there are only two possible outcomes, 0 and 1. With
probability θ, the outcome is 1, and with probability 1 − θ, the outcome is 0. In other
words, p(X = 1) = θ, and p(X = 0) = 1 − θ. In Matlab, you can simulate the Bernoulli
distribution using the binomial distribution with N = 1. However, for the purpose of this
exercise, please write the code needed to randomly sample Bernoulli distributed values
that does not make use of the built-in binomial distribution. Using a seed of 21, verify
that the first four observations agree with 1, 1, 0 and 1 respectively. Your MATLAB code
could use the following line for the seeding:
>>
seed=21; rand(’state’,seed); randn(’state’,seed);
Answer
% probabilities for each digit
theta = [0.7; ... % digit 0
0.3] ... % digit 1
seed = 21; rand( 'state' , seed );randn('state',seed);
K = 10;
% Draw K random values
digitset = 0:1;
Y = randsample(digitset,K,true,theta);
Y =
1
1
0
1
1
0
0
0
1
Y(1:4)
ans =
1
1
0
0
1
Question 4: In this exercise, we want to generate the random variable x=(x1, x2) from
Beta(1, 𝛽) by inverse transform method.
a)
Write down the algorithm for generating x.
b)
Use MATLAB to construct two sets of N=1000 observations each from Beta (1,4).
Set seed =131. Plot histograms of both sets on the same graph and print out the first 5
observations from each set. Observe that the first observations are x1=0.0251 and x2 =
0.0348.
c)
Find the mean of each set generated.
Answer:
a) 1. Draw U ~ Uniform (0,1)
2. Set X = 𝐹 −1 (𝑈) = 1 − (1 − 𝑢)1/4
b) Generating RVs from Beta(1,4)
seed=131; rand('state',seed)
%u = rand(1,1000);
u=unifrnd(0,1,1000,2); % generate two sets of 1000 samples
x = 1-(1-u).^(1/4);
%beta random values
subplot(1,2,1),hist(x(:,1)), title('set 1')
subplot(1,2,2),hist(x(:,2)), title('set 2')
first_5_obs=x(1:5,:)
first_5_obs =
0.0251
0.0348
0.1092
0.1802
0.1459
0.2727
0.0158
0.1693
0.1839
0.3776
% get first 5 observations of each set
sample_mean=mean(x)
sample_mean =
0.2006
0.1963
So, mean of set1 = 0.2006, and mean of set2 =0.1963
Download