Multivariate HP filter

Discussion of State Space and Dynamic Stochastic General Equilibrium Models
mskare69
Posts: 27
Joined: Tue May 10, 2011 3:35 pm

Multivariate HP filter

Unread post by mskare69 »

Dear Tom,

Would be possible to get the code (replication) for the Multivariate HP filter estimating Natural interest rate as in http://ideas.repec.org/p/col/000094/007667.html
or http://www.rbnz.govt.nz/research_and_pu ... p04_01.pdf? I am looking for the Rats code to estimate natural rate of interest using Multivariate HP filter as in above papers.

If not, could you give me a hand in rewriting the Stock-Watson example 6.1. from the Estima State-Space Models course materials I bought?

Fondly,

Marinko Skare
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Multivariate HP filter

Unread post by TomDoan »

The state-space model in the Colombian paper uses
MVHP.gif
MVHP.gif (1.43 KiB) Viewed 23805 times
1. Their "T" is our A, which is a fixed matrix.

2. Their HH' would be done with

F=||0.0|1.0|0.0|| and SW=sigma1^2

3. Their GG' would be done with

SV=%diag(||sigma0^2,sigma2^2||

4. Their z is RATS C', so it would be

C=||1,0|0,0|0,-alpha2||

5. Their dX is done with the RATS MU option,

MU=||0.0|alpha1*ytilde{1}+alpha2*r{1}+alpha3*qtilde{1}||

6. At long last, their Y is our Y, Y=||r,ytilde||

The free parameters are alpha1, alpha3, alpha3, and the three standard devations. However, it looks like they're pegging the ratio between sigma1^2 and sigma0^2 to the standard HP ratio of 14400 for monthly data. You would estimate the state-space model and use TYPE=SMOOTH to get the Kalman smoothed output.
mskare69
Posts: 27
Joined: Tue May 10, 2011 3:35 pm

Re: Multivariate HP filter

Unread post by mskare69 »

Dear Tom,

I tried to write a piece of code that could replicate the Colombian paper, I use the same notation as in the paper, can you help me in building the code for the Colombian paper replication?

open data nri.xls
calendar (m) 1982:1
data (format=xls,org=columns) 1982:1 2009:1 90-day deposit overnight nominal interest rates CPI GDP gap inflation target
compute n=5
frml[vect] yf
frml yf=||90-day deposit overnight nominal interest rates CPI GDP gap inflation target||
dec rect a(10,10)
compute ar2=||0.0,0.0|1.0,0.0||
compute a=ar2tilde\ar2tilde\ar2tilde\ar2tilde\ar2
dec rect f[10,5]
compute f=||0.0|1.0|0.0||
compute f=f2tilde\f2tilde\f2tilde\f2tilde\f2
load dlm(type=trend, shock=trend, a=a, f=f)
compute sw=sigma1^2
compute[symm] sv=%diag(||sigma0^2, sigma2^2||
declare rect gamma(u)
declare rect theta(n-1)
C=||1,0|0,0|0-alpha2||
MU=||0.0|alpha1*tilde{1}+alpha2*r{1}+alpha3*qtilde{1}
Y=||r,ytilde||
dlm(type=smooth) / xstates vstates

also if you could ad the code to extract and graph the calculated natural interest rate. I used the example of 6.1 and 6.2 from the Estima State space guide.

Many thanks for your help.

Marinko
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Multivariate HP filter

Unread post by TomDoan »

It would help if you would attach the data file. One obvious problem is that "90-day deposit overnight nominal interest rates" isn't a valid RATS variable name. You'll either have to re-label that on the data file, or use TOP=2,NOLABELS options on the DATA to skip the included labels and relabel everything.

The Colombian paper is rather explicitly bivariate, but you're trying to graft in code for a larger system. The RATS A matrix (their T) is given in the paper, and I gave you the formula for the F matrix. Don't use the formulas from the other example.
mskare69
Posts: 27
Joined: Tue May 10, 2011 3:35 pm

Re: Multivariate HP filter

Unread post by mskare69 »

Dear Tom,

I am attaching the data file with the variable names the same as in the Colombia paper. I tried to rewrite the code following your instructions but still no luck to get it through and do the natural rate estimation with the IS equation as in the Colombian paper. Can you help me?
data.xls
(47.5 KiB) Downloaded 854 times

Code: Select all

open data data.xls
calendar(a) 1800
data(format=xls, org=columns) 1800:12 2012:12 yt qt rt
compute a=||1.0,1.0,0.0|0.0,1.0,0.0|1.0,0.0,0.0||
compute f=||0.0|1.0|0.0||
compute SV=%diag(||sigma0^2,sigma2^2||
compute SW=sigma1^2
compute c=||1,0|0,0|0-alpha2||
calculate mu=||0.0|alpha1*ytilde{1}+alpha2*r{1}+alpha3*qtilde{1}||
dlm(start=%%DLMSetup(),y=||r,ytilde||,sw=sw,c=c,a=a,f=f,presample=ergodic,$
   type=smooth) 1800:12 * xstates
set NRI 1800:12 * = xstates(t)(1)
acc NRI
graph(footer="Natural interest rate, HPMV estimation")
# NRI
Fondly,

Marinko
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Multivariate HP filter

Unread post by TomDoan »

1. You're missing a NONLIN instruction for the free parameters (the sigma's and the alpha's)
2. You're missing instructions initializing the free parameters. The default 0's won't work for the variances.
3. compute SV=%diag(||sigma0^2,sigma2^2|| is missing a )
4. compute c=||1,0|0,0|0-alpha2|| is missing a , between 0 and -alpha2
5. what are qtilde and ytilde? They're treated as observables in the "regression" but aren't in the data set.

Note that this is not a general bivariate HP filter but sort of a graft of an HP filter with a regression relation. A general bivariate HP filter (with common trend) is shown at http://www.estima.com/forum/viewtopic.php?f=8&t=1395.
mskare69
Posts: 27
Joined: Tue May 10, 2011 3:35 pm

Re: Multivariate HP filter

Unread post by mskare69 »

data.xls
(47.5 KiB) Downloaded 835 times
Dear Tom,

I tried to change the code following your 1-5 steps, please check the code and if demands more changes let me know how to do it. I know this is not bivariate HP, in fact I am trying to estimate natural interest rate using IS curve equation as in the Colombian paper. I change the data (ytilde, qtilde) accordingly (r=real interest rate, ytilde =output gap, qtilde=exchange rate gap).

Many thanks for help,

Marinko


open data data.xls
calendar(a) 1800
data(format=xls, org=columns) 1800:12 2012:12 ytilde qtilde r
compute a=||1.0,1.0,0.0|0.0,1.0,0.0|1.0,0.0,0.0||
compute f=||0.0|1.0|0.0||
nonlin sigma alpha
dlm(start=%%DLMSetup(),y=yf,sw=sw,c=c,a=a,f=f,presampled=ergodic,$
pmeth=simplex,piters=10,method=bfgs,type=filter) 1800:12 * xstates
compute SV=%diag(||sigma0^2,sigma2^2||
compute SW=sigma1^2
compute c=||1,0|0,0|0-alpha2||
calculate mu=||0.0|alpha1*ytilde{1}+alpha2*r{1}+alpha3*qtilde{1}||
dlm(start=%%DLMSetup(),y=||r,ytilde||,sw=sw,c=c,a=a,f=f,presample=ergodic,$
type=smooth) 1800:12 * xstates
set NRI 1800:12 * = xstates(t)(1)
acc NRI
graph(footer="Natural interest rate, HPMV estimation")
# NRI
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Multivariate HP filter

Unread post by TomDoan »

Please do not post a program and ask me to fix typos. Run it, fix the problems that it reports, and if you're stuck once you've done that, then ask for help. You, in fact, did not fix #2, #3 or #4, and your nonlin doesn't list the parameters that you're actually using which are sigma0 sigma1 sigma2 alpha1 alpha2 and alpha3 not sigma and alpha.
mskare69
Posts: 27
Joined: Tue May 10, 2011 3:35 pm

Re: Multivariate HP filter

Unread post by mskare69 »

Dear Tom,

Sorry, my mistake. I corrected the code and run it and after the DLM instruction I am getting DLM2. No Observations Produce Valid Output. Check Data and Initial Values

Here is the corrected code.


*
open data data.xls
calendar(a) 1800
data(format=xls, org=columns) 1800:12 2012:12 ytilde qtilde r
compute a=||1.0,1.0,0.0|0.0,1.0,0.0|1.0,0.0,0.0||
compute f=||0.0|1.0|0.0||
dec vect alpha(3) sigma(3)
nonlin sigma0 sigma1 sigma2 alpha1 alpha2 alpha3
compute SV=%diag(||sigma0^2,sigma2^2||)
compute SW=sigma1^2
compute c=||1,0|0,0|0,-alpha2||
dlm(sw=sw,c=c,a=a,f=f,y=||r,ytilde||,presample=ergodic,type=smooth, mu=||0.0|alpha1*ytilde{1}+alpha2*r{1}+alpha3*qtilde{1}||) 1800:12 * xstates
set NRI 1800:12 * = xstates(t)(1)
acc NRI
graph(footer="Natural interest rate, HPMV estimation")
# NRI

Fondly,

Marinko
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Multivariate HP filter

Unread post by TomDoan »

You still haven't set guess values for the free parameters. The defaults are zero which won't work for the variances.
mskare69
Posts: 27
Joined: Tue May 10, 2011 3:35 pm

Re: Multivariate HP filter

Unread post by mskare69 »

Dear Tom,

How can I do that? I tried this way (using means) but I am getting ## MAT13. Store into Out-of-Range Matrix or Series Element

Fondly,

Marinko

*
open data data.xls
calendar(a) 1800
data(format=xls, org=columns) 1800:12 2012:12 ytilde qtilde r
compute a=||1.0,1.0,0.0|0.0,1.0,0.0|1.0,0.0,0.0||
compute f=||0.0|1.0|0.0||
dec vect alpha(3) sigma(3)
compute alpha(1)=%mean,sigma(0)=%variance*.5
compute alpha(2)=%mean,sigma(1)=%variance*.5
compute alpha(3)=%mean,sigma(2)=%variance*.5
nonlin sigma0 sigma1 sigma2 alpha1 alpha2 alpha3
compute SV=%diag(||sigma0^2,sigma2^2||)
compute SW=sigma1^2
compute c=||1,0|0,0|0,-alpha2||
dlm(sw=sw,c=c,a=a,f=f,y=||r,ytilde||,presample=ergodic,type=smooth, mu=||0.0|alpha1*ytilde{1}+alpha2*r{1}+alpha3*qtilde{1}||) 1800:12 * xstates
set NRI 1800:12 * = xstates(t)(1)
acc NRI
graph(footer="Natural interest rate, HPMV estimation")
# NRI
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: Multivariate HP filter

Unread post by TomDoan »

alpha1, alpha2 and alpha3 are not the same thing as alpha(1), alpha(2) and alpha(3). You wrote all the formulas with alpha1, ...; you should keep them that way. The specific error was that you used sigma(0). When you get an error, do Edit-Show Last Error and it will take you to the instruction that produced the error.

The obvious source for the alpha's are a linear regression of y on the three observable regressors, and the guess for sigma2 would be the square root of the residual variance from that. sigma0 and sigma1 are standard deviations from a local trend model for the real rate of interest. You can get a guess for sigma1 (which is the standard deviation of the trend component) using @LOCALDLMINIT applied to the real rate of interest. The authors actually peg sigma0 to be the HP filter ratio relative to sigma1, which would be sqrt(14400)*sigma1.

The way this model is constructed, the r-bar series should be exactly the same as the HP filtered version of the real interest rate, and the regression coefficients should be exactly the same as what you get from treating the HP filtered output as data. The only advantage in estimating the model simultaneously is that you get a joint covariance matrix on the parameters.
mskare69
Posts: 27
Joined: Tue May 10, 2011 3:35 pm

Re: Multivariate HP filter

Unread post by mskare69 »

Dear Tom,

I followed your instruction, manually calculated a2 (rt-1-rmeant-1) to calculate sigma2 (square root of the residual variance from regression) from the equation 2.27 in the Colombian paper. Using the same equation I calculated sigma1 and from that sigma0 using sort(100)*sigma1 for annual data. Alphas 1,2,3 were calculated from the regression i putted in the code. Still no luck, after running dlm instruction I get DLM2. No Observations Produce Valid Output. Check Data and Initial Values. Since I had to calculated alpha2 with rmean being a non observable natural interest rate I used standard HP filter to find cyclical component of real interest rate and used that as proxy for rmean (I don't know how to write the rats code to estimate alpha1,2 and 3 and sigmas directly within the rats program).

Hope you can fix it.

Marinko

New data file with a2 variable
data1.xls
(56.5 KiB) Downloaded 878 times
New code

Code: Select all

*
open data data1.xls
calendar(a) 1800
data(format=xls, org=columns) 1800:12 2012:12 ytilde qtilde r a2
compute a=||1.0,1.0,0.0|0.0,1.0,0.0|1.0,0.0,0.0||
compute f=||0.0|1.0|0.0||
nonlin sigma0 sigma1 sigma2 alpha1 alpha2 alpha3
linreg ytilde
# constant ytilde{1} a2{1} qtilde{1}
compute alpha1=%beta(1),alpha2=%beta(2),alpha3=%beta(3),sigma2=sqrt(%seesq)
compute sigma1=3.008
compute sigma0=sqrt(100)*sigma1
compute SV=%diag(||sigma0^2,sigma2^2||)
compute SW=sigma1^2
compute c=||1,0|0,0|0,-alpha2||
dlm(sw=sw,c=c,a=a,f=f,y=||r,ytilde||,presample=ergodic,type=smooth, mu=||0.0|alpha1*ytilde{1}+alpha2*r{1}+alpha3*qtilde{1}||) 1800:12 * xstates
set NRI 1800:12 * = xstates(t)(1)
acc NRI
graph(footer="Natural interest rate, HPMV estimation")
# NRI
mskare69
Posts: 27
Joined: Tue May 10, 2011 3:35 pm

Re: Multivariate HP filter

Unread post by mskare69 »

Dear Tom,

You are right about what reman should be from the code and the code is actually working but I get results for rmean much higher then the HP filtered version of the real interest rate.

Any idea what I am doing wrong in the code?

Marinko
mskare69
Posts: 27
Joined: Tue May 10, 2011 3:35 pm

Re: Multivariate HP filter

Unread post by mskare69 »

To correct, I get smoothed Natural interest rate of 200 and more and I dont see why from the code.

Fondly,

Marinko
Post Reply