%% Asymptotics (Michele Piffer, LSE, 1/12/2011)

clc 
clear 

%% Data

n = 500;
x = 10*rand(n,1);           % first population moment = 5

xbar = ones(1,n)*x/n        % sample average

xbar - 5                    

% n^(0.5)*(xbar - 5)

%%  Simulations of Law of Large Number
% how long does it take to collapse to population moment?

xbar_iter = zeros(n,1);

for i=1:n
    step1 = ones(1,i)*x(1:i,1)/i;
    xbar_iter(i) = step1;
end
clear step1

figure(1)
plot(xbar_iter), hold on
plot(5*ones(n,1), 'r--'), hold off

% figure(2)
% plot(xbar_iter - 5), hold on
% plot(zeros(n,1), 'r--'), hold off


%% Simulation of Central Limit Theorem
% show that by premultiplying by root of n you get a normal

iter = 500;
approx = zeros(iter,1);

for i=1:iter
    x = 10*rand(n,1);
    step1 = ones(1,n)*x/n; 
    step2 = n^(0.5)*(step1 - 5);
    approx(i) = step2;
end
 
clear step1 step2

figure(3) 
hist(approx,80)