how to estimate monthly output gap?

Discussion of State Space and Dynamic Stochastic General Equilibrium Models
hardmann
Posts: 252
Joined: Sat Feb 26, 2011 9:49 pm

how to estimate monthly output gap?

Unread post by hardmann »

Dear Tom:

I have an idea that using unobserved component filter to estimate the monthly output gap based on quarterly real log GDP data. I know that this is a difficult task, even impossible task, or trivial one. When I retrieve academic paper, it seems no one try to do it.

I present my idea in detail. I had two way to get my aim: Two stage and one stage method. Firstly, I use @disaggredate procedure to distribute quarterly real GDP to monthly GDP, then I use UC model to decompose it into trend and cycle or gap. However I get wrong outcome. Meantime I use HP filter to get HP cycle based on monthly and quarterly frequency respectively, these outcomes seem reasonable. Monthly HP cycle has same pattern with quarterly HP cycle, but more refined. As for UC cycle, the monthly one has different pattern with quarterly one, is more like white noise.

I use same HP filter option, though the variance ratio,lamba is different in 1600 for quarterly and 14400 for monthly.

For UC model, I use same configuration, eg, local trend model for quarterly and monthly model.

In UC model, HP filter is used to get initial guess value, but these outcomes is not accurate but reasonable. however, the outcome of monthly UC model can not be accepted. Why and how to improve it.

monthlu code

Code: Select all

open data US_rmGDP.xls
calendar(m) 1947:01
data(format=xls,org=col) 1947:01 2019:10 rmgdp3

set lgdp = log(rmgdp3) * 100

* Do a standard HP filter
*
filter(type=hp) lgdp / hptrend
graph(footer="Log GDP with HP Estimated Trend",klabels=||"GDP","HP Trend"||,key=upleft) 2
# lgdp
# hptrend
set hpcycle = lgdp - hptrend
graph(footer="Cycle Estimate from HP Filter")
# hpcycle

graph(footer="Cycle Estimate from HP Filter 2000:01-2002:12")
# hpcycle  2000:01 2002:12




****************************************************
*
* UC with AR(2) process for cycle
*
* Standard local trend model
*
dec rect at(2,2) ft(2,2)
compute at=||1.0,1.0|0.0,1.0||
compute ft=%identity(2)
*
* Standard AR(2) model
*
dec real phi1 phi2
dec rect ar2(2,2) far2(2,1)
compute ar2=||phi1,phi2|1.0,0.0||
compute far2=||1.0|0.0||
*
* Function to patch the current g's into the overall transition matrix
*
function afunc t
type rect afunc
type integer t
*
compute %psubmat(ar2,1,1,||phi1,phi2||)
compute afunc=at~\ar2
end
*
* Combined "F" and "C" matrices
*
compute f=ft~\far2
compute c=||1.0,0.0,1.0,0.0||
*

dec rect g(2,4)
input g
 0 0 1 0
 0 0 0 1


linreg hpcycle
# hpcycle{1 2}

compute phi1=%beta(1),phi2=%beta(2),se=sqrt(%seesq)
compute sigmac=.01*se, sigmaz=0.01*se, sigmat=.050*se

nonlin phi1 phi2 sigmac sigmaz sigmat
*
* Do Kalman smooth for estimate of the trend
*
dlm(a=afunc(t),c=c,f=f,sw=%diag(||sigmaz,sigmat,sigmac||),$
  type=smooth,presample=ergodic,y=lgdp,method=bfgs,iters=100,$
  pmethod=simplex,piter=10,reject=(abs(phi1+phi2)>=1.0)) / xstatesf

set fixtrend = xstatesf(t)(1)
graph(footer="Log GDP with UC",$
   klabels=||"GDP","UC-AR(2) Trend"||,key=upleft) 2
# lgdp
# fixtrend
set SM_cycle = xstatesf(t)(3)
graph(footer="Smooth: Cycle Estimate with UC")
# SM_cycle
*





*
dlm(a=afunc(t),c=c,f=f,sw=%diag(||sigmaz,sigmat,sigmac||),$
  type=filter,presample=ergodic,y=lgdp) / xstatesx

set fixtrend = xstatesf(t)(1)
graph(footer="Log GDP with UC Cycle",$
   klabels=||"GDP","UC Trend"||,key=upleft) 2
# lgdp
# fixtrend
set Fl_cycle = xstatesf(t)(3)
graph(footer="Filter: Cycle Estimate with UC")
# Fl_cycle
graph(footer="Cycle Estimate with UC ") 2
# SM_cycle
# Fl_cycle

graph(footer="Filter: Cycle Estimate with UC 2000:01-2002:12")
# Fl_cycle 2000:01 2002:12
quarterly code:

Code: Select all

open data us_gdp.xls
calendar(q) 1947:1
data(format=xls,org=columns) 1947:01 2019:03  gdp
*
* Everything is run on 100*log data

set lgdp = 100.0 * log(gdp)
*
@NBERCycles(down=recession)

*@LSUNIT(MODEL=BREAK,BREAKS=3) LGDP
*@ZIVOT(BREAK=TREND) LGDP
* @LPUNIT(BREAK=BOTH,METHOD=AIC) LGDP



*
* Do a standard HP filter
*
filter(type=hp) lgdp / hptrend
graph(footer="Log GDP with HP Estimated Trend",klabels=||"GDP","HP Trend"||,key=upleft) 2
# lgdp
# hptrend
set hpcycle = lgdp - hptrend
graph(footer="Cycle Estimate from HP Filter")
# hpcycle

graph(footer="Cycle Estimate from HP Filter")
# hpcycle  2000:01 2002:04


linreg hpcycle
# hpcycle{1 2}
compute phi1=%beta(1),phi2=%beta(2),se=sqrt(%seesq)

****************************************************
*
* UC with AR(2) process for cycle
*
* Standard local trend model
*
dec rect at(2,2) ft(2,2)
compute at=||1.0,1.0|0.0,1.0||
compute ft=%identity(2)
*
* Standard AR(2) model
*
dec real phi1 phi2
dec rect ar2(2,2) far2(2,1)
compute ar2=||phi1,phi2|1.0,0.0||
compute far2=||1.0|0.0||
*
* Function to patch the current g's into the overall transition matrix
*
function afunc t
type rect afunc
type integer t
*
compute %psubmat(ar2,1,1,||phi1,phi2||)
compute afunc=at~\ar2
end
*
* Combined "F" and "C" matrices
*
compute f=ft~\far2
compute c=||1.0,0.0,1.0,0.0||
*
* "G" matrix for handling state space model with combined unit roots
* (for the local trend states) and stationary states (for the cycle).
*
dec rect g(2,4)
input g
 0 0 1 0
 0 0 0 1
*
nonlin phi1 phi2 sigmac sigmaz sigmat
compute sigmaz=.0001,sigmat=.0001,sigmac=.001


*
* Do Kalman smooth for estimate of the trend
*
dlm(a=afunc(t),c=c,f=f,sw=%diag(||sigmaz,sigmat,sigmac||),$
  type=smooth,presample=ergodic,y=lgdp,method=bfgs,iters=100,$
  pmethod=simplex,piter=10,reject=(abs(phi1+phi2)>=1.0)) / xstatesf

set fixtrend = xstatesf(t)(1)
graph(footer="Log GDP with UC-AR(2)  Cycle",$
   klabels=||"GDP","UC-AR(2) Trend"||,key=upleft) 2
# lgdp
# fixtrend
set SM_cycle = xstatesf(t)(3)
graph(footer="Smooth: Cycle Estimate with UC-AR(2)",shaded=recession)
# SM_cycle
*


*
dlm(a=afunc(t),c=c,f=f,sw=%diag(||sigmaz,sigmat,sigmac||),$
  type=filter,presample=ergodic,y=lgdp) / xstatesx

set fixtrend = xstatesf(t)(1)
graph(footer="Log GDP with UC-AR(2) Cycle",$
   klabels=||"GDP","Fixed AR(2) Trend"||,key=upleft) 2
# lgdp
# fixtrend
set Fl_cycle = xstatesf(t)(3)
graph(footer="Filter: Cycle Estimate with UC-AR(2)",shaded=recession)
# Fl_cycle
graph(footer="Cycle Estimate with AR(2) ",shaded=recession) 2
# SM_cycle
# Fl_cycle

graph(footer="Filter: Cycle Estimate with UC-AR(2)")
# Fl_cycle 2000:01 2002:04
Please Tom help me.
Attachments
us_gdp.xls
(55.5 KiB) Downloaded 1258 times
US_rmGDP.xls
(132.62 KiB) Downloaded 1240 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: how to estimate monthly output gap?

Unread post by TomDoan »

It's well-known that freely estimating the component variances in a trend-cycle decomposition generally results in poor performance, generally with the "trend" rather closely tracking the data. That's why the HP filter specifies a variance ratio---it forces a certain amount of stiffness in the trend that seems to match with the prior for what a (somewhat) flexible trend should look like.

If you are trying to do this from general principles, you would have a trend-cycle model at the monthly level with a measurement equation which applies only every three periods.
hardmann
Posts: 252
Joined: Sat Feb 26, 2011 9:49 pm

Re: how to estimate monthly output gap?

Unread post by hardmann »

Dear Tom:

I do not understand what your mean by "you would have a trend-cycle model at the monthly level with a measurement equation which applies only every three periods. "
Please you give me more details.

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

Re: how to estimate monthly output gap?

Unread post by TomDoan »

This was a year old thread. You are now doing something much more complicated than what I was suggesting because instead of just having every 3rd entry observable, you have every 3rd entry observable, with a linearized summing up to do the log linear distribution.
hardmann
Posts: 252
Joined: Sat Feb 26, 2011 9:49 pm

Re: how to estimate monthly output gap?

Unread post by hardmann »

Dear Tom:

I have initially implemented a UC model using an extended Kalman filter to simultaneously disaggragate and decompose quarterly log real GDP into monthly trends and cycles components. However, many bugs remained and new problems emerged again. After I've modified and revised the code and questions, I'll plea you to help me refine and enhance these further.

Best Regard
Hardmann
hardmann
Posts: 252
Joined: Sat Feb 26, 2011 9:49 pm

Re: how to estimate monthly output gap?

Unread post by hardmann »

Dear Tom:
We try to simultaneously disaggregates and decompose the US quarterly GDP into monthly trends and cycles. Due to logarithmic GDP and temporal aggregation, there is a non-linear constrains, we must use the Extend Kalman Filter(EKF) to deal with these, and our code strongly refer to the @disaggregate method and procedure. We use the latest 1947Q1-2021Q4 US quarterly GDP data from FRED as input series, which are seasonally adjusted at an annualized rate and deflated at 2012 prices before being imported into Rats. The series was rearranged, so it can be read into the monthly frame with data only for the final month of each quarter i.e. data is placed in March, June, September, and December respectively.

There are four problems with code A ,

1. The model is unstable and iteration does not converge effectively. In A code, different iterations settings produce different results. Moreover, the decomposition effect of the model is obviously wrong in the first 4-5 years of the sample.

2. The location of the NONLIN statement seems to affect the iteration. At tag A, Ninlin declares the parameters to be estimated, but the iteration doesn't seem to estimate the corresponding parameters (a1 a2 sigxi sigeps sigzeta), instead it gives the value of Rohx. When we reiterated NONLIN statement at Tag D, the iteration does estimate, but the results do not converge.

3. The initialization of each parameter strongly affects the final estimation result. I tried different combinations but the results turn out very different. It would be great if you could help me with how to properly initialize the 5 parameters(a1 a2 sigxi sigeps sigzeta), even the direction of the tuning of these parameters would be very helpful.

4.Problem with initialization of state variables. At Tag C, setting 0.00001*hpcycle{i-4} as the initial value guess of c gives the best result we've tried so far, but the results are wrong for the first 4 years of the sample. And without coefficient 0.00001, it expands to the first 10 years.


Code B

We reiterated NONLIN at Tag D, removed 0.00001 at Tag C, and reinitialized almost all of the parameters at Tag B. At the 10th iteration, a graph similar to the result of the first code was obtained of which the lowest point of 1950 is higher than that of code A, and the lowest point of 2020 is even lower. Also, of code B, SIGEPS is negative(-1.1848e-003).

We implore Doan to help us solve the problem.

Best Regard
Hardmann
Attachments
US Disag qGDP into mT&C B.RPF
(6.23 KiB) Downloaded 872 times
US Disag qGDP into mT&C A.RPF
(6.38 KiB) Downloaded 919 times
US_mGDP_2021Q4.xlsx
US quarterly GDP in Monthly frame
(50.27 KiB) Downloaded 873 times
Post Reply