%% Problem Set 5, question 1 (Michele Piffer, LSE, 15/11/2011)
clc 
clear % get rid of data already in matlab

%% Generate DGP

n = 100;               % number of rows
k = 5;                 % number of regressors
X = 100*rand(n,k);     % random values assigned
X = [ones(n,1), X];    % add a constant term

epsilon = randn(n,1);  % true errors

beta_true = [2 5 -12 4 -2 16]'; % true parameters, which you want to estimate

y = X*beta_true + epsilon; 

%% Estimate model using X and y

beta_hat = (X'*X)^(-1)*X'*y; % OLS regressors

display(beta_true')
display(beta_hat')     % compare values, note that they are  ...
                       ...quite close to true values

%% Check what happens if one regressor is multiplied by a scalar (part a)

lambda = 2;

A = eye(k+1);
A(2,2) = lambda ; 

Xbis = X*A;            % second regressor has been multiplied by two (check it)
clear A lambda

beta_hat_bis = (Xbis'*Xbis)^(-1)*Xbis'*y; 

display(beta_true')
display(beta_hat')
display(beta_hat_bis')    % note, estimate for beta_hat2 is now a half, ...
                           ...all the others are unchanged

%% Check what happens if a constant is added to a regressor (part b)

lambda = 4;

A = zeros(n, k+1);
a = lambda*ones(n,1);
A(:,2) = a; 

Xtris = X + A;         % add constant to second regressor (check it)
clear A a lambda

beta_hat_tris = (Xtris'*Xtris)^(-1)*Xtris'*y; 

display(beta_true')
display(beta_hat')
display(beta_hat_tris')   % note, estimates are the same, apart from the constant,...
                          ...from which twice as much as beta_hat2 was subtracted
