% This program is example of MC %
% By Kyongwook Choi
% 2/22/1999
% Modified 12/20/2000
randn('seed',0); % Set the 'seed'
format short;
nobs = 100;
ntrials=1000;   % Number of try

beta1_out=zeros(ntrials,1);
beta2_out=zeros(ntrials,1);
tstat=zeros(ntrials,1);


for i=1:ntrials
   u=0.05*randn(nobs,1);
   exinf=randn(nobs,1)/20;
   obinf=exinf+u;
   real_r=0.04;
   norm_r=real_r+exinf;
   
   y=obinf;
   x1=norm_r;
   x=[ones(nobs,1) x1];
   
   % Run OLS of Nominal Interest Rate on Actual Inflation Rate
   
   b=reg(y,x);  % I made another function to run regression.
   beta1_out(i)=b(1,1);
   beta2_out(i)=b(2,1);
   beta=[beta1_out beta2_out]; % Creates new matrix beta
   
   
   % This part is redundant but just make it sure.
   
   t=length(x);
	k=2;
	df=t-k;
	e=y-x*b;
	sse=e'*e;
	sighat2=sse/df;
	ixx=inv(x'*x);
	varb=sighat2*ixx;
   
   tstat1(i)=(b(1,1)-0)/sqrt(varb(1,1));  % T-statistics on beta1=0. 
   tstat2(i)=(b(2,1)-1)/sqrt(varb(2,2));  % T-statistics on beta2=1. 
   tstat1;
   tstat2;
   bmean = mean(beta);
   bstd=std(beta1_out);
   meant=mean(tstat);
   
   fid=fopen('d:\temp\project.txt', 'w');
   fprintf(fid, '%8.4f', bmean, bstd, meant);
   status=fclose(fid);
end
   
   subplot(221), hist(beta1_out); ylabel('frequency of \beta1 outcomes');
   xlabel('\beta1');
   title('The Distribution of \beta1')
   legend('\beta_1');
   
   subplot(222), hist(beta2_out); ylabel('frequency of \beta2 outcomes');
   xlabel('\beta2');
   title('The Distribution of \beta2');
   legend('\beta_2');
   
   subplot(223), hist(tstat1); ylabel('frequency of t for \beta1');
   xlabel('t values');
   title('The Distribution of t stat for \beta1')
   legend('\beta_2');
   
   subplot(224), hist(tstat2); ylabel('frequency of t for \beta2');
   xlabel('t value');
   title('The Distribution of t stat for \beta2')
   legend('\pi');
   

   
   

