% ——————————————————————————–
% This Matlab code allows you to verify the Weak Law of Large Numbers (WLLN)
% The population in this example is Exponential with mean = true_mean.
% ——————————————————————————–
Copyright By PowCoder代写 加微信 powcoder
% clean up the environment
% sample size
sample_size = 10000; % Choose the number of observations in the sample
% population parameters
true_mean = 2; % mean of the Exponential population
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% —————————————————————————————————————-
% The law of large numbers at work – As the number of observations increases,
% the “sample” mean approaches the “population” mean (see slides for Lecture 1)
% —————————————————————————————————————-
% initialize vectors
true_mean_vector = ones(sample_size,1).*true_mean;
sample_mean_vector = ones(sample_size,1);
sz = size(sample_mean_vector);
% generate a sample from the Exponential distribution
X = random(‘Exponential’, true_mean, sz);
% compute the sample mean by adding one observation at the time (notice that “for” loop)
for i=1:sample_size
sample_mean_vector(i,1) = mean(X(1:i,1));
% First, we plot the histogram of the simulated data (notice that it looks Exponential)
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’)
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com