% ———————————————————————————-
% This Matlab code allows you to verify the Weak Law of Large Numbers (WLLN),
% the Central Limit Theorem (CLT) and Slutsky’s Theorem.
% You can play with the sample size and the parameters of the distribution
Copyright By PowCoder代写 加微信 powcoder
% (I use Normal, Exponential and Binomial).
% ———————————————————————————-
% clean up the environment
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Please modify this section to try different theorems, different distributions and different
% parameters for each distribution
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
theorem = 3; % Choose 1 for the WLLN, 2 for the CLT without standardization, 3 for the CLT
% with standardization and 4 for Slutsky’s theorem
distribution = 1; % Choose 1 for the Normal Distribution, 2 for the Exponential, 3 for the Binomial
sample_size = 1000; % Choose the number of observations to show convergence
number_of_samples = 1000; % This is for the CLT and Slutsky’s theorem only (theorem = 2,3 or 4)
true_mean = 2; % mean of the population (for Normal and Exponential)
true_variance = 1; % variance of the population (for Normal)
prob_success = .2; % prob of success for the Binomial distribution
number_trials = 50; % number of trials for the Binomilal distribution
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ————————————————————————————————————————–
% The law of large numbers at work – As the number of observations increases, the sample mean approaches the population mean
% ————————————————————————————————————————–
if theorem == 1 % Notice the “if” loop
% initialize vectors
true_mean_vector = ones(sample_size,1).*true_mean;
sample_mean_vector = ones(sample_size,1);
sz = size(sample_mean_vector);
% generate sample from distribution
if distribution == 1 % Again, notice the “if” loop
X = random(‘Normal’, true_mean, sqrt(true_variance), sz);
elseif distribution == 2
X = random(‘Exponential’, true_mean, sz);
elseif distribution == 3
X = random(‘Binomial’, number_trials, prob_success, sz);
true_mean = number_trials * prob_success;
true_mean_vector = ones(sample_size,1).*true_mean;
% compute sample mean by adding one observation at the time
for i=1:sample_size
sample_mean_vector(i,1) = mean(X(1:i,1));
% First, we plot the histogram of the simulated data
subplot(2,1,1);
histogram(X, ‘normalization’, ‘pdf’)
title(‘The empirical distribution of the observations’)
xlabel(‘draws’)
ylabel(‘Empirical pdf’)
% Second, we represent the convergence of the sample mean to the true mean
subplot(2,1,2);
plot(sample_mean_vector,’LineWidth’, 2)
plot(true_mean_vector,’r’,’LineWidth’,2)
title(‘The sample mean approaching the expected value’)
xlabel(‘Sample size’)
ylabel(‘The sample mean’)
%—————————————————————————————————————–
% The CLT for the mean WITHOUT standardization – as the number of observations increases,
% the distribution of the sample mean becomes more and more concentrated around the population mean –
% In fact, the variance of the sample mean goes to zero
%—————————————————————————————————————–
elseif theorem == 2
smaller_sample_size = 10;
small_sample = ones(smaller_sample_size, number_of_samples);
sz = size(small_sample);
larger_sample = ones(sample_size, number_of_samples);
sz1 = size(larger_sample);
if distribution == 1
X_small = random(‘Normal’, true_mean, sqrt(true_variance), sz);
X_large = random(‘Normal’, true_mean, sqrt(true_variance), sz1);
elseif distribution == 2
X_small = random(‘Exponential’, true_mean, sz);
X_large = random(‘Exponential’, true_mean, sz1);
elseif distribution == 3
X_small = random(‘Binomial’, number_trials, prob_success, sz);
X_large = random(‘Binomial’, number_trials, prob_success, sz1);
true_mean = number_trials * prob_success;
true_mean_vector = ones(sample_size,1).*true_mean;
mean_small = mean(X_small);
mean_large = mean(X_large);
histogram(mean_small,’normalization’,’pdf’);
histogram(mean_large,’normalization’,’pdf’);
%————————————————————————————————————–
% The CLT for the mean WITH a standardization – as the number of observations increases,
% the empirical distribution of the standardized sample mean approaches that of a normal 0,1 more and more
%————————————————————————————————————–
elseif theorem == 3
smaller_sample_size = 10;
small_sample = ones(smaller_sample_size, number_of_samples);
sz = size(small_sample);
larger_sample = ones(sample_size, number_of_samples);
sz1 = size(larger_sample);
if distribution == 1
X_small = random(‘Normal’, true_mean, sqrt(true_variance), sz);
X_large = random(‘Normal’, true_mean, sqrt(true_variance), sz1);
elseif distribution == 2
X_small = random(‘Exponential’, true_mean, sz);
X_large = random(‘Exponential’, true_mean, sz1);
elseif distribution == 3
X_small = random(‘Binomial’, number_trials, prob_success, sz);
X_large = random(‘Binomial’, number_trials, prob_success, sz1);
true_mean = number_trials * prob_success;
true_variance = number_trials * prob_success * (1-prob_success);
true_mean_vector = ones(sample_size,1).*true_mean;
mean_small = mean(X_small);
mean_large = mean(X_large);
if distribution == 1
histogram(sqrt(smaller_sample_size)*(mean_small-true_mean)/sqrt(true_variance),’normalization’,’pdf’);
histogram(sqrt(sample_size)*(mean_large-true_mean)/sqrt(true_variance),’normalization’,’pdf’);
elseif distribution == 2
histogram(sqrt(smaller_sample_size)*(mean_small-true_mean)/true_mean,’normalization’,’pdf’);
histogram(sqrt(sample_size)*(mean_large-true_mean)/true_mean,’normalization’,’pdf’);
elseif distribution == 3
histogram(sqrt(smaller_sample_size)*(mean_small-true_mean)/sqrt(true_variance),’normalization’,’pdf’);
histogram(sqrt(sample_size)*(mean_large-true_mean)/sqrt(true_variance),’normalization’,’pdf’);
%—————————————————————————————————–
% Slutsky’s theorem: the first sample uses the true standard deviation to standardize, the second
% sample uses an “estimated” standard deviation (by Slutsky’s the two are equivalent – asymptotically)
%——————————————————————————————————
elseif theorem == 4
sample = ones(sample_size, number_of_samples);
sz = size(sample);
if distribution == 1
X = random(‘Normal’, true_mean, sqrt(true_variance), sz);
elseif distribution == 2
X = random(‘Exponential’, true_mean, sz);
elseif distribution == 3
X = random(‘Binomial’, number_trials, prob_success, sz);
true_mean = number_trials * prob_success;
true_variance = number_trials * prob_success * (1-prob_success);
true_mean_vector = ones(sample_size,1).*true_mean;
mean = mean(X);
std_dev = std(X);
if distribution == 1
histogram(sqrt(sample_size)*(mean-true_mean)/sqrt(true_variance),’normalization’,’pdf’);
histogram(sqrt(sample_size)*(mean-true_mean)./std_dev,’normalization’,’pdf’, ‘FaceColor’,’yellow’);
elseif distribution == 2
histogram(sqrt(sample_size)*(mean-true_mean)/true_mean,’normalization’,’pdf’);
histogram(sqrt(sample_size)*(mean-true_mean)./std_dev,’normalization’,’pdf’);
elseif distribution == 3
histogram(sqrt(sample_size)*(mean-true_mean)/sqrt(true_variance),’normalization’,’pdf’);
histogram(sqrt(sample_size)*(mean-true_mean)./std_dev,’normalization’,’pdf’);
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com