Professional Documents
Culture Documents
Kalman Filter Hamilton
Kalman Filter Hamilton
/*
================================================================
======= */
@ Replace this first section with a section to read in your data @
capt = 131; @ t is the sample size @
load y[capt,1] = real.dat;
x0 = ones(capt,1);
ind= seqa(1960,0.25,capt);
@ "raw data"; ind~y; @
let om[4,4] =
0.001716 -0.003957 -0.000982 0.001827
-0.003957 0.031305 0.000753 -0.013247
-0.000982 0.000753 0.866317 0.000103
0.001827 -0.013247 0.000103 0.019480 ;
/*
================================================================
======= */
@ In general no parts of this section should be changed @
nth = rows(th); @ nth is the number of parameters @
cholom = chol(om);
chsi = zeros(capt,rx); @ This is where filter inverences will be stored @
chsismo = zeros(capt,rx); @ This is where the smoothed inferences will
be stored @
P = zeros(capt,rx^2); @ This is where filter variances will be stored @
Psmo = zeros(capt,rx^2); @ This is where smoothed inferences will be stored
@
#include kffilt;
#include kfsmo;
/*
================================================================
= */
@ Echo parameter values @
"Parameter values as follows";
phi = th[1,1];
sigv = th[2,1];
mu = th[3,1];
sigw = th[4,1];
"phi:";;phi;;"sigv:";;sigv;;"mu:";;mu;;"sigw:";;sigw;
"Value of log likelihood";
z=-ofn(th);z;
"Do you wish to continue (y or n)?";;
zzs = cons;
if zzs $== "n";
end;
endif;
/*
================================================================
==== */
call smooth(th);
chsi = chsi + mu;
chsismo = chsismo + mu;
"";"-------------------------------";"";
"The smoothed inferences and filter inferences are";
ind~chsismo~chsi;
"";"";"The smoothed variances and filter variances are";
ind~Psmo~P;
/*
================================================================
====== */
/*
================================================================
======= */
@ Replace this first section with a section to read in your data @
capt = 132; @ capt is the sample size @
load y[capt,1] = real.dat;
x0 = ones(capt,1);
ind= seqa(1960,0.25,132);
@ "raw data";
ind~y; @
/*
================================================================
======= */
@ In general no parts of this section should be changed @
#include kfcheck.2;
/*
================================================================
= */
@ Echo initial parameter values @
"Starting values as follows";
th';
"Value of log likelihood";
z=-ofn(th);z;
/*
================================================================
======= */
@ Replace this first section with a section to read in your data @
capt = 131; @ capt is the sample size @
load y[capt,1] = real.dat;
x0 = ones(capt,1);
ind= seqa(1960,0.25,131);
@ "raw data";
ind~y; @
/*
================================================================
======= */
@ In general no parts of this section should be changed @
proc startval; @ This defines starting value for iteration to be th @
retp(th); endp;
chsi = zeros(capt,rx); @ This is where filter inverences will be stored @
P = zeros(capt,rx^2); @ This is where filter variances will be stored @
#include kffilt;
/*
================================================================
= */
@ Echo initial parameter values @
"Starting values as follows";
th';
"Value of log likelihood";
z=-ofn(th);z;
"Do you wish to continue (y or n)?";;
zzs = cons;
if zzs $== "n";
end;
endif;
/*
================================================================
==== */
@ Set parameters to use Gauss numerical optimizer @
library optmum;
#include optmum.ext;
__btol = 1.e-06; @ This controls convergence criterion for coefficients@
__gtol = 1.e-06; @ This controls convergence criterion for gradient @
__algr = 1; @ This chooses BFGS optimization @
__miter = 150; @ This controls the maximum number of iterations @
__output = 1; @ This causes extra output to be displayed @
__covp = 0; @ This speeds up return from OPTMUM; note that the
program makes a reparameterization to calculate
std. errors @
@ Next call the GAUSS numerical optimizer @
output off;
{x,f,g,h} =optmum(&ofn,startval);
output file=junk on;
h = (hessp(&ofn,x));
va = eigrs(h);
call ofn(x);
if minc(eigrs(h)) <= 0;
"Negative of Hessian is not positive definite";
"Either you have not found local maximum, or else estimates are up "
"against boundary condition. In latter case, impose the restricted "
"params rather than estimate them to calculate standard errors";
else;
h = invpd(h);
std = diag(h)^.5;
"For vector of coefficients parameterized as follows,";x';
"the standard errors are";std';
"and the variance-covariance matrix is";
format /m3;
h;
format /m1;
endif;
"";"-------------------------------";"";
"The Kalman filter inferences are";
ind~chsi;
"";"";"The Kalman filter variances are";
ind~P;
retp(it);
endp;
KFSEEK
Gauss program that controls numerical estimation for Kalman filter
KFFILT
Gauss procs that include Kalman filter
KFANAL
Gauss program that analyzes output for Kalman filter
KFSMO
Gauss procs that evaluate smoothed inference and do simple monte carlo
REAL.DAT
Data on real interest rates used for analysis described in the Handbook
of Econometrics chapter