% Marcet's parameterized expectations method as applied to
% the optimal growth problem: 
%
%             max sum beta^t u(c[t])    
%           {c[t]} t 
%
%       s.t. c[t]+k[t]=theta[t]k[t-1]^alpha +mu k[t-1]
%            ln(theta[t])=rho ln(theta[t-1])+eps[t]
%

% Ellen R. McGrattan, 11-29-90

% Reference: Den Haan and Marcet, JBES, 1990.

beta=.95;
alpha=.33;
rho=.95;
tau=1;
sige=.1;
lambda=1;
mu=1;
delta=[1.74   -0.56   -0.55]';
maxn=500;
T=2500;


rand('normal');
rand('seed',0);
epsilon=rand(T+1,1)*sige;
omega(1,1)=0;
for i=2:T+1; 
  omega(i,1)=rho*omega(i-1)+epsilon(i); 
end;
theta=exp(omega);
tol=1e-5;

taui=1/tau;
k(1,1)=(alpha*beta/(1-beta*mu))^(1/(1-alpha))
c=zeros(T+1,1);
global zxv
for n=1:maxn;
  
  for i=2:T+1;   
    phi=delta(1)*k(i-1)^delta(2)*theta(i)^delta(3);
    k(i,1)=theta(i)*k(i-1)^alpha+mu*k(i-1)-(beta*phi)^(-taui);
  end;
  c(2:T+1,1)=(beta*delta(1)*k(1:T).^delta(2).*theta(2:T+1).^delta(3)).^ ..
              (-taui);
  gamma=delta;
  diff=1;
  while diff>tol
    phi=gamma(1)*k(1:T-1).^gamma(2).*theta(2:T).^gamma(3);
    deriv=[phi/gamma(1),phi.*log(k(1:T-1)),phi.*log(theta(2:T))];
    y=c(3:T+1).^(-tau).*(alpha*theta(3:T+1).*k(2:T).^(alpha-1)+mu)-phi+ ..
      deriv*gamma;
    gammn=(deriv'*deriv)\(deriv'*y);
    [gammn,gamma]
    diff=norm(gammn-gamma);
    gamma=gammn;
  end;
  if norm(gamma-delta)<tol; break; end;
  [gamma(:),delta(:)]
  delta=(1-lambda)*delta+lambda*gamma;
end;




