% This is a simple illustration of the post model selection sampling % distribution of the OLS estimator. clear all S=1000; % the number of iterations for the monte carlo simulation n=500; % the sample size for s=1:S, X=mvnrnd(zeros(2,1),[1 .8; .8 1],n); Y=X*[1;0.2]+normrnd(0,1,n,1); W1=[ones(n,1) X]; W2=[ones(n,1) X(:,1)]; bhat1(:,s)=inv(W1'*W1)*W1'*Y; Vbhat1=inv(W1'*W1)*((Y-W1*bhat1(:,s))'*(Y-W1*bhat1(:,s))/n); reject(s)=1*(abs(bhat1(3,s)./sqrt(Vbhat1(3,3)))>1.96); bhat2(:,s)=inv(W2'*W2)*W2'*Y; bhat(:,s)=(1-reject(s))*bhat2(2,s)+reject(s)*bhat1(2,s); end hist(bhat)