%% Distributions (Michele Piffer, LSE, 1/12/2011)
% Distribution of OLS estimates

clc 
clear 

           
%% Simulation of the distribution

iter = 5000;
betas = zeros(iter,4);

for i=1:iter
    
    n = 50;               % number of rows
    k = 3;                 % number of regressors
    X = 100*randn(n,k);     % random values assigned
    X = [ones(n,1), X];    % add a constant term

    epsilon = 10*randn(n,1);  % true errors
    
    beta_true = [2 5 -12 4]'; % true parameters, which you want to estimate

    y = X*beta_true + epsilon;
    
    beta_hat = (X'*X)^(-1)*X'*y;
    
    betas(i,:) = beta_hat;
    
end

clear step1 step2

figure(1)
subplot(2,2,1)
hist(betas(:,1),80), hold on
plot(beta_true(1), betas(:,1),'r'), hold off

subplot(2,2,2)
hist(betas(:,2),80), hold on
plot(beta_true(2), betas(:,2),'r'), hold off

subplot(2,2,3)
hist(betas(:,3),80), hold on
plot(beta_true(3), betas(:,3),'r'), hold off

subplot(2,2,4)
hist(betas(:,4),80), hold on
plot(beta_true(4), betas(:,4),'r'), hold off

