% Problem Set 13 % Wed. Nov. 28 clc; clear; close all; %2. a. %Input x = [1:1:15]'; %T = number of observations T = size(x,1); %Output y = [0.58, 1.10, 1.20, 1.30, 1.95, 2.55, 2.60, 2.90, 3.45, 3.50, 3.60, 4.10, 4.35, 4.40, 4.50]'; %Add constant to Input x = [ones(15,1), x]; %K = number of regressors K = size(x,2); xprimx = x'*x; xprimy = x'*y; b = inv(xprimx)*xprimy; b1 = b(1,1); b2 = b(2,1); %2.b Written. %2. c. e = y - b1*x(:,1) - b2*x(:,2); sig2 = e'*e/(T-K); %2.d. W = sig2*inv(xprimx); %2.e. lny = log(y); %When we include the first row of constants and then take the log it %becomes zero. Re-estimate with ln(b1) = 1 to get an estimate, say a, we will be %able to recover b1 via exp(a) = b1. lnx = [ones(15,1), log(x(:,2))]; lnb = inv(lnx'*lnx)*(lnx'*lny); b12e = exp(lnb(1,1)); b22e = lnb(2,1); %2.f Written. %2.g y2a = x*b; y2e = b12e*x(:,2).^(b22e); plot(y2a,'r-'); hold on; plot(y2e,'k:'); plot(y,'b-+'); legend('linear','log linear','actual','location','NorthWest'); %matrix size m = 15; n = 1000; mu = 0; sig2 = (y-b12e*x(:,2).^(b22e))'*(y-b12e*x(:,2).^(b22e))/(T-K); %fill a 15x1000 matrix with the log linear estimates of output temp1 = ones(m,n); temp2 = b12e*x(:,2).^(b22e); temp3 = zeros(m,n); for i = 1:1:15 temp3(i,:) = temp1(i,:)*temp2(i,1); end rand('seed',1234); egen = mu + sqrt(sig2)*randn(m,n); y2ematrix = temp3 + egen; lny2ematrix = log(y2ematrix); lnbmatrix = 9999*ones(2,1000); for i = 1:1:1000; lnbmatrix(:,i) = inv(lnx'*lnx)*(lnx'*lny2ematrix(:,1)); end