time-varying kalman filter

Discussion of State Space and Dynamic Stochastic General Equilibrium Models
ege_man
Posts: 85
Joined: Sat Jul 07, 2012 2:39 pm

time-varying kalman filter

Unread post by ege_man »

Dear Tom,
I tried to modify time-varying examples in Tsay's textbook. I have the data only with 53 number of observation and estimate oil demand equation using real oil price and real gdp as explanatory variables. I got the following error.
## MAT15. Subscripts Too Large or Non-Positive Error was evaluating entry 1
Could you please tell me what is wrong with this code. The code and data are attached
Best Regards

Code: Select all

open data 2017_oil_demand.xlsx
cal(a) 1960
data(for=xlsx,org=col) 1960 2012
table

linreg qtot 1960 *
# constant poil ygdp
*
equation(lastreg) mdeq
dec vect sigmav(%nreg)
dec real sigmae
*
* Get guess values based upon the results of a linear regression. All
* standard deviations are scales of the corresponding variances in the
* least squares regression. (The drift ones a much smaller multiple).
*
compute sigmae=.5*sqrt(%seesq)
compute sigmav=.01*%stderrs
*
nonlin sigmae sigmav
*
* The first DLM is Kim and Nelson's way to handle the pre-sample
* variances for the coefficients; large finite values with condition on
* the first 10 data points. A more technically correct way to do this is
* to use PRESAMPLE=DIFFUSE in place of SX0=50.0*%identity(5) as is done
* in the second DLM.
*
* If you use PRESAMPLE=DIFFUSE and don't use the CONDITION option,
* you'll automatically get the likelihood conditional on the first five
* data points (in general, the number of regressors) since there are
* five unit roots in the state-space model.
*
dlm(y=qtot,c=%eqnxvector(mdeq,t),sw=%diag(sigmav.^2),sv=sigmae^2,$
   condition=5,method=bfgs) 1970 2012 xstates vstates
table
set b1 = xstates(t)(2)
set b2 = xstates(t)(3)

set lower1 1970 * = b1-1*sqrt(vstates(t)(2,2))
set upper1 1970 * = b1+1*sqrt(vstates(t)(2,2))
set lower2 1970 * = b2-1*sqrt(vstates(t)(3,3))
set upper2 1970 * = b2+1*sqrt(vstates(t)(3,3))

graph(footer="Time-varying regression coefficient of Oil Price") 3
# b1 1970 *
# lower1 1970 *
# upper1 1970 *
graph(footer="Time-varying regression coefficient of Y") 3
# b2 1970 *
# lower1 1970 *
# upper1 1970 *
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: time-varying kalman filter

Unread post by TomDoan »

Fix your DATA instruction as shown. 1960 without the :1 is entry number 1960.

data(for=xlsx,org=col) 1960:1 2012:1
ege_man
Posts: 85
Joined: Sat Jul 07, 2012 2:39 pm

Re: time-varying kalman filter

Unread post by ege_man »

Thanks
This time I have the following error with DLM instruction

## DLM2. No Observations Produce Valid Output. Check Data and Initial Values
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: time-varying kalman filter

Unread post by TomDoan »

You have other similar problems with the date ranges.
ege_man
Posts: 85
Joined: Sat Jul 07, 2012 2:39 pm

Re: time-varying kalman filter

Unread post by ege_man »

I got some results but I am not able to produce time-varying coefficients due to the following error message
## M4. A memory request for an additional -15288 bytes cannot be satisfied
ege_man
Posts: 85
Joined: Sat Jul 07, 2012 2:39 pm

Re: time-varying kalman filter

Unread post by ege_man »

Dear Tom
I made some changes. But my question here is I found positive and significant price elasticity results similar to OLS since this code takes those parameter estimates as initial values. Could you please guide me to set initial values of the parameters. For example I would like to start oil price coefficient estimate from negative and ygdp variable from positive initial values.

Thanks

Code: Select all

open data 2017_oil_demand.xlsx
cal(a) 1960
data(for=xlsx,org=col) 1960:1 2012:1
table

linreg qtot
# constant rpoil ygdp
*
equation(lastreg) mdeq
dec vect sigmav(%nreg)
dec real sigmae
*
* Get guess values based upon the results of a linear regression. All
* standard deviations are scales of the corresponding variances in the
* least squares regression. (The drift ones a much smaller multiple).
*
compute sigmae=.5*sqrt(%seesq)
compute sigmav=.01*%stderrs
*
nonlin sigmae sigmav
*
* The first DLM is Kim and Nelson's way to handle the pre-sample
* variances for the coefficients; large finite values with condition on
* the first 10 data points. A more technically correct way to do this is
* to use PRESAMPLE=DIFFUSE in place of SX0=50.0*%identity(5) as is done
* in the second DLM.
*
* If you use PRESAMPLE=DIFFUSE and don't use the CONDITION option,
* you'll automatically get the likelihood conditional on the first five
* data points (in general, the number of regressors) since there are
* five unit roots in the state-space model.
*
dlm(y=qtot,c=%eqnxvector(mdeq,t),sw=%diag(sigmav.^2),sv=sigmae^2,$
   PRESAMPLE=DIFFUSE,method=bfgs) 1960:1 2012:1 xstates vstates

set b1 1965:1 2012:1 = xstates(t)(2)
set b2 1965:1 2012:1 = xstates(t)(3)

set lower1 1965:1 2012:1 = b1-1*sqrt(vstates(t)(2,2))
set upper1 1965:1 2012:1 = b1+1*sqrt(vstates(t)(2,2))
set lower2 1965:1 2012:1 = b2-1*sqrt(vstates(t)(3,3))
set upper2 1965:1 2012:1 = b2+1*sqrt(vstates(t)(3,3))

graph(footer="Time-varying regression coefficient of Oil Price") 3
# b1 1965:1 *
# lower1 1965:1 *
# upper1 1965:1 *
graph(footer="Time-varying regression coefficient of Y") 3
# b2 1965:1 *
# lower2 1965:1 *
# upper2 1965:1 *
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: time-varying kalman filter

Unread post by TomDoan »

Well, you're doing a time-varying version of a spurious regression, so it's not surprising that the results aren't what one would hope.
ege_man
Posts: 85
Joined: Sat Jul 07, 2012 2:39 pm

Re: time-varying kalman filter

Unread post by ege_man »

Dear Tom
Thanks. Yes you are right. Could you give any example code for the determination of initial values?
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: time-varying kalman filter

Unread post by TomDoan »

For setting initial values, use X0 and SX0 options. As to what would be appropriate values, that's up to you. What you really need to do is to fix the I(1) problem with your regressors.
ege_man
Posts: 85
Joined: Sat Jul 07, 2012 2:39 pm

Re: time-varying kalman filter

Unread post by ege_man »

Dear Tom
Is there any available example on how to set X0 and SX0? for example I would like to set initial values as b1= -.10 and b2=0.50.
I have also SSM DSGE Course materials.
Thanks
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: time-varying kalman filter

Unread post by TomDoan »

That's two of the three. What about the intercept? And what's the initial covariance matrix?
ege_man
Posts: 85
Joined: Sat Jul 07, 2012 2:39 pm

Re: time-varying kalman filter

Unread post by ege_man »

Dear Tom,
Thank you for your suggestions. You were right about the stationarity of the variables. The variables are I(1), I need to use stationary variables in the estimation. I found sensible results when I estimated oil demand with log-first differenced data. Please see the attached code. I have several questions.
1. I need to apply some diagnostic tests for the misspecification check and godness of fit of the model like in STAMP program.
2. Another question is the model is estimated in log first differenced form therefore the parameters do not directly show elasticities which I would like to plot. There are lots of papers published in respected journals applying kalman filter for the time-varying parameters and they mistakenly use directly log-level of the variables without unit root check. Do you have any suggestion for retrieving elasticities from the differenced regression model?

Code: Select all

open data 2017_oil_demand.xlsx
cal(a) 1960
data(for=xlsx,org=col) 1960:1 2012:1
table
diff qtot / qtot
diff rpoil / rpoil
diff ygdp / ygdp
linreg qtot
# constant rpoil ygdp
*
equation(lastreg) mdeq
dec vect sigmav(%nreg)
dec real sigmae
*
* Get guess values based upon the results of a linear regression. All
* standard deviations are scales of the corresponding variances in the
* least squares regression. (The drift ones a much smaller multiple).
*
compute sigmae=.5*sqrt(%seesq)
compute sigmav=.01*%stderrs
*
nonlin sigmae sigmav

dlm(y=qtot,c=%eqnxvector(mdeq,t),sw=%diag(sigmav.^2),sv=sigmae^2,$
   PRESAMPLE=DIFFUSE,method=bfgs) 1960:1 2012:1 xstates vstates

set b1 1963:1 2012:1 = xstates(t)(2)
set b2 1963:1 2012:1 = xstates(t)(3)

set lower1 1963:1 2012:1 = b1-1.96*sqrt(vstates(t)(2,2))
set upper1 1963:1 2012:1 = b1+1.96*sqrt(vstates(t)(2,2))
set lower2 1963:1 2012:1 = b2-1.96*sqrt(vstates(t)(3,3))
set upper2 1963:1 2012:1 = b2+1.96*sqrt(vstates(t)(3,3))

graph(footer="Time-varying regression coefficient of Oil Price Change") 3
# b1 1963:1 *
# lower1 1963:1 *
# upper1 1963:1 *
graph(footer="Time-varying regression coefficient of Growth") 3
# b2 1963:1 *
# lower2 1963:1 *
# upper2 1963:1 *
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: time-varying kalman filter

Unread post by TomDoan »

If log q = eta log p the delta log q = eta delta log p so you theoretically should be able to get estimates of the elasticity from the differenced regression. When I have seen careful analyses of elasticities with time series data, they have typically tried to get rid of trends that aren't related to price signals, as is described in that link to the spurious regressions earlier in the thread.

If you want the diagnostics like in STAMP, use the @STAMPDIAGS procedure.
ege_man
Posts: 85
Joined: Sat Jul 07, 2012 2:39 pm

Re: time-varying kalman filter

Unread post by ege_man »

Dear Tom,

Thanks. I have read the thread, I used deflated variables but all of them are still I(1), that is why I took the first difference. Therefore I can not use partial adjustment model to get long-run coefficients. Can you please show me how to transform the time-varying parameters of the log first differenced variables in to elasticity or can you refer me any good paper estimating time-varying elasticities using kalman filter?

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

Re: time-varying kalman filter

Unread post by TomDoan »

Baltagi's Econometrics (Springer) has an example (actually several of them) of estimates of elasticity of gasoline consumption in the U.S. using time series data where the raw data are heavily trending. As to time-varying coefficients estimates of elasticities, I haven't seen anything.
Post Reply