Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

@ This GAUSS file controls smoothed inference and standard errors

for Kalman filter analysis @

output file=junk reset;

/*
================================================================
======= */
@ 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; @

@ Set Kalman filter control parameters @


rx = 1; @ rx is dimension of state space @
n = 1; @ n is dimension of observed vector @
k = 1; @ k is dimension of exogenous variables @

@ Set values for parameter vector @


phi= 0.913724;
sigv= 0.977454;
mu= 1.427100;
sigw= 1.343377 ;
th = phi|sigv|mu|sigw;

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;

/*
================================================================
====== */

@ Monte Carlo standard errors are calculated here @


itm = 1; @ itm will index Monte Carlo draw @
maxitm = 200;
chsi0 = chsismo;
mchsi = zeros(capt,rx); @ mchsi accumulates accumulate sum of
squares of chsi @
mpsmo = zeros(capt,rx^2); @ pchsi accumulates sum of psmo @
mth = zeros(nth,1); @ mth accumulates sum of generated thetas @
mthsq = zeros(nth,nth); @ mthsq accumulates sum of squares
of deviations of generated vectors
from population values @
seed1 = 3937841;
do until itm > maxitm;
thm = rndns(nth,1,seed1);
thm = th + cholom'*thm;
mth = mth + thm;
mthsq = mthsq + (thm - th)*(thm - th)';
call ofn(thm);
call smooth(thm);
mchsi = mchsi + (thm[3,1] + chsismo - chsi0)^2;
mpsmo = mpsmo + psmo;
itm = itm + 1;
endo;
mchsi = mchsi/maxitm;
mpsmo = mpsmo/maxitm;
mpstd = sqrt(mchsi + mpsmo);
"-----------------------------------------------------------------";
"True value for MLE";
th';
"Average theta for simulations";
mth'/maxitm;
"True variance-covariance matrix for MLE";
om;
"Average variance-covariance for simulations";
mthsq/maxitm;
"";
"Date param unc. filter uncer. standard error ";
ind~mchsi~mpsmo~mpstd;

@ This GAUSS file checks Kalman filter estimation by constructing augmented


Kalman filter @

output file=junk reset;

/*
================================================================
======= */
@ 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; @

@ Set Kalman filter control parameters @


rx = 4; @ rx is dimension of state space @
n = 1; @ n is dimension of observed vector @
k = 1; @ k is dimension of exogenous variables @

@ guess initial parameter values @


phi= 0.927000;
sigv= 0.809506;
mu= 1.476473;
sigw= 1.501635;
th = phi|sigv|mu|sigw;

/*
================================================================
======= */
@ 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 kfcheck.2;

/*
================================================================
= */
@ Echo initial parameter values @
"Starting values as follows";
th';
"Value of log likelihood";
z=-ofn(th);z;

"The Kalman filter inferences are";


ind~chsi;
"";"";"The Kalman filter variances are";
ind~P;
/* The proc ofn(th) performs Kalman filter and evaluates likelihood
function
global variables are as follows:
rx = dimension of state-space
n = dimension of observation vector
k = dimension of exogenous vector
capt = sample size
X0 = capt x k matrix of observations on exogenous variables
y = capt x n matrix of observations on endogenous variables
chsi = capt x r matrix in which the filter means (chsi t given t)
are stored
P = capt x r^2 matrix in which the filter variances (P t given t)
are stored
*/
proc ofn(th);
local phi, sigv, mu, sigw, FX, Q, A, H, R, f0, yhat, yvar, yvarinv,
chsi10, chsi11, P10, P11 , it, eps;

@ first read in parameter values from vector @


phi = th[1,1];
sigv = th[2,1];
mu = th[3,1];
sigw = th[4,1];

@ next translate these into standard state-space matrices @


FX = zeros(rx,rx);
FX[1,1] = phi;
FX[2,1] = 1;
FX[3,2] = 1;
FX[4,3] = 1;
Q = zeros(rx,rx);
Q[1,1] = sigv^2;
A = mu;
H = eye(rx);
H = H[.,1];
R = sigw^2;
@ set initial value for filter @
chsi10 = zeros(rx,1);
P10 = inv(eye(rx^2) - (FX .*. FX)) * vec(Q) ;
P10 = reshape(P10',rx,rx);
if det(P10) <= 0;
P10 = 100*Q*eye(rx);
P10 = reshape(P10',rx,rx);
endif;

f0 = 0; @ f0 will be the log likelihood function @


it = 1; @ it will index the iteration @
do until it > capt;
yhat = A'*x0[it,.]' + H'*chsi10;
yvar = (H'*P10*H + R);
yvarinv = inv(yvar);
eps = y[it,.]' - yhat;
f0 = f0 - ln(det(yvar)) - eps'*yvarinv*eps;
chsi11 = chsi10 + P10*h*yvarinv*eps;
chsi[it,.] = chsi11';
P11 = P10 - P10*h*yvarinv*H'*P10;
P[it,.] = vec(P11)';
chsi10 = FX*chsi11;
P10 = FX*P11*FX' + Q;
it = it +1;
endo;

f0 = -(capt*n/2) * log(2*pi) + f0/2;


retp(-f0);
endp;

/* The proc ofn(th) performs Kalman filter and evaluates likelihood


function
global variables are as follows:
rx = dimension of state-space
n = dimension of observation vector
k = dimension of exogenous vector
capt = sample size
X0 = capt x k matrix of observations on exogenous variables
y = capt x n matrix of observations on endogenous variables
chsi = capt x r matrix in which the filter means (chsi t given t)
are stored
P = capt x r^2 matrix in which the filter variances (P t given t)
are stored
*/
proc ofn(th);
local phi, sigv, mu, sigw, FX, Q, A, H, R, f0, yhat, yvar, yvarinv,
chsi10, chsi11, P10, P11 , it, eps;

@ first read in parameter values from vector @


phi = th[1,1];
sigv = th[2,1];
mu = th[3,1];
sigw = th[4,1];

@ next translate these into standard state-space matrices @


FX = phi;
Q = sigv^2;
A = mu;
H = 1;
R = sigw^2;

@ set initial value for filter @


chsi10 = zeros(rx,1);
P10 = inv(eye(rx^2) - (FX .*. FX)) * vec(Q) ;
P10 = reshape(P10',rx,rx);
if det(P10) <= 0; @ This corrects initial variance to be robust
for case of explosive eigenvalues in FX @
P10 = 100*Q[1,1]*eye(rx);
P10 = reshape(P10',rx,rx);
endif;

f0 = 0; @ f0 will be the log likelihood function @


it = 1; @ it will index the iteration @
do until it > capt;
yhat = A'*x0[it,.]' + H'*chsi10;
yvar = (H'*P10*H + R);
yvarinv = inv(yvar);
eps = y[it,.]' - yhat;
f0 = f0 - ln(det(yvar)) - eps'*yvarinv*eps;
chsi11 = chsi10 + P10*h*yvarinv*eps;
chsi[it,.] = chsi11';
P11 = P10 - P10*h*yvarinv*H'*P10;
P[it,.] = vec(P11)';
chsi10 = FX*chsi11;
P10 = FX*P11*FX' + Q;
it = it +1;
endo;

f0 = -(capt*n/2) * ln(2*pi) + f0/2;


retp(-f0);
endp;

/* The proc ofn(th) performs Kalman filter and evaluates likelihood


function
global variables are as follows:
rx = dimension of state-space
n = dimension of observation vector
k = dimension of exogenous vector
capt = sample size
X0 = capt x k matrix of observations on exogenous variables
y = capt x n matrix of observations on endogenous variables
chsi = capt x r matrix in which the filter means (chsi t given t)
are stored
P = capt x r^2 matrix in which the filter variances (P t given t)
are stored
*/
proc ofn(th);
local phi, sigv, mu, sigw, FX, Q, A, H, R, f0, yhat, yvar, yvarinv,
chsi10, chsi11, P10, P11 , it, eps;
@ first read in parameter values from vector @
phi = th[1,1];
sigv = th[2,1];
mu = th[3,1];
sigw = th[4,1];

@ next translate these into standard state-space matrices @


FX = phi;
Q = sigv^2;
A = mu;
H = 1;
R = sigw^2;

@ set initial value for filter @


chsi10 = zeros(rx,1);
P10 = inv(eye(rx^2) - (FX .*. FX)) * vec(Q) ;
P10 = reshape(P10',rx,rx);
if det(P10) <= 0; @ This corrects initial variance to be robust
for case of explosive eigenvalues in FX @
P10 = 100*Q[1,1]*eye(rx);
P10 = reshape(P10',rx,rx);
endif;

f0 = 0; @ f0 will be the log likelihood function @


it = 1; @ it will index the iteration @
do until it > capt;
yhat = A'*x0[it,.]' + H'*chsi10;
yvar = (H'*P10*H + R);
yvarinv = inv(yvar);
eps = y[it,.]' - yhat;
f0 = f0 - ln(det(yvar)) - eps'*yvarinv*eps;
chsi11 = chsi10 + P10*h*yvarinv*eps;
chsi[it,.] = chsi11';
P11 = P10 - P10*h*yvarinv*H'*P10;
P[it,.] = vec(P11)';
chsi10 = FX*chsi11;
P10 = FX*P11*FX' + Q;
it = it +1;
endo;

f0 = -(capt*n/2) * log(2*pi) + f0/2;


retp(-f0);
endp;
@ This GAUSS file controls Kalman filter estimation @

output file=junk reset;

/*
================================================================
======= */
@ 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; @

@ Set Kalman filter control parameters @


rx = 1; @ rx is dimension of state space @
n = 1; @ n is dimension of observed vector @
k = 1; @ k is dimension of exogenous variables @

@ guess initial parameter values @


phi = 0.8;
sigv = 1;
mu = 1;
sigw = 1;
th = phi|sigv|mu|sigw;

/*
================================================================
======= */
@ 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;

"";"";"MLE as parameterized for numerical optimization ";


"Coefficients:";x';
phi = x[1,1];
sigv = x[2,1];
mu = x[3,1];
sigw = x[4,1];
"phi:";;phi;;"sigv:";;sigv;;"mu:";;mu;;"sigw:";;sigw;
"";"Value of log likelihood:";;-f;
"";"Gradient vector:";g';

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;

/* This GAUSS procedure calculates Kalman smoother


global variables are as follows:
rx = dimension of state-space
n = dimension of observation vector
k = dimension of exogenous vector
capt = sample size
X0 = capt x k matrix of observations on exogenous variables
y = capt x n matrix of observations on endogenous variables
chsi = capt x r matrix in which the filter means (chsi t given t)
are stored
P = capt x r^2 matrix in which the filter variances (P t given t)
are stored
chismo = capt x r matrix in which the smoothed means (chsi t given T)
are stored
Psmo = capt x r^2 matrix in which the smoothed variances
(P t given T) are stored
*/
proc smooth(th);
local phi, sigv, mu, sigw, FX, Q, A, H, R, f0, yhat, yvar, yvarinv,
chsi0T, P0T, Pfilt, Pnext, it, eps, Jt;

@ first read in parameter values from vector @


phi = th[1,1];
sigv = th[2,1];
mu = th[3,1];
sigw = th[4,1];

@ next translate these into standard state-space matrices @


FX = phi;
Q = sigv^2;
A = mu;
H = 1;
R = sigw^2;

@ set terminal values @


chsismo[capt,.] = chsi[capt,.];
Psmo[capt,.] = P[capt,.];

it = 1; @ (capt - it) denotes the date analyzed at this iteration @


do until it > capt - 1;
P0T = reshape(Psmo[capt-it+1,.],rx,rx);
chsi0T = chsismo[capt-it+1,.];
Pfilt = reshape(P[capt-it,.],rx,rx); @ this is P t given t @
Pnext = FX*Pfilt*FX' + Q; @ this is P t+1 given t @
Jt = Pfilt*FX'*inv(Pnext);
P0T = Pfilt + Jt*(P0T - Pnext)*Jt';
chsi0T = chsi[capt-it,.]' + Jt*(chsi0T - FX*chsi[capt-it,.]');
chsismo[capt-it,.] = chsi0T';
Psmo[capt-it,.] = vec(P0T)';
it = it + 1;
endo;

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

You might also like