%gridmse2.m
%Program to compute learning with productivity
% shocks. Agents estimate steady state with constant gain
% We search for optimal del
% NORMAL SHOCKS
t1=clock;
T=100000;
randn('state',0);
eps=.25;
sig=.1;
capa=.0805;
a=.025;
alp=.9;
lam=.5;
k=40;
istar=19.5;
bet=1.007;
gam=0.04;
del=.15;

tau=0.20;

initn=2.2;
n=zeros(T,1);
x=zeros(T,1);
xe=zeros(T+1,1);
xe(1)=(initn^(1+eps))/alp;
for j=1:T;
n(j)=(alp*xe(j))^(1/(1+eps));
psi=capa*(max(istar,lam*k*n(j)/(1+a*lam*k*n(j))))^bet;

%nu shock here will have lognormal with mean one and same 
%variance as uniform [-tau,tau], i.e. (tau^2)/12

u=((log(1+(tau^2)/12))^0.5)*randn(1);
nu=exp(u-0.5*log(1+(tau^2)/12));

q=n(j)^alp*psi*nu;
x(j)=((1-gam)*q)^(1-sig);
xe(j+1)=xe(j)+del*(x(j)-xe(j));
end;
d=0.05:0.05:0.95;
p=length(d);
mse=zeros(p,1);
xed=zeros(T+1,1);
xed(1)=xe(1);
for i=1:p;
	for j=1:T-1;
	mse(i)=mse(i)+(x(j+1)-xed(j))^2;
	xed(j+1)=xed(j)+d(i)*(x(j)-xed(j));
	end;
mse(i)=mse(i)/(T-1);
end;
t2=clock;
emin=etime(t2,t1)/60;
emin
del
tau
T
msecurve=[d' mse];
save tab151 msecurve del tau T;
msecurve
stdnnshock=(log(1+(tau^2)/12))^0.5
