parameter estimation with Kalman Filter

For technical questions regarding estimation of single equations, systems, VARs, Factor analysis and State Space Models in EViews. General econometric questions and advice should go in the Econometric Discussions forum.

Moderators: EViews Gareth, EViews Moderator

tsnowfield
Posts: 3
Joined: Fri Jan 04, 2013 8:39 pm

parameter estimation with Kalman Filter

Postby tsnowfield » Thu Jan 10, 2013 2:31 pm

Hi
I’m trying to make a Sspace model to estimate the state variable and parameter of the Smith & Schwartz (2000) two-factor price model. I’m using data from WTI future contract with maturity in 1,6,12,18 and 24 months.

Eviews is reporting a syntax error in my code: “(1-exp(-C(1)*(1/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e3]”

Transition and measurement equations are described is the attached file with the Smith & Schwartz (2000)paper.

Anyone can identify what is wrong in my code:

Code: Select all

@state sv1=sv1(-1)*exp(-C(1)*(1/252))+[ename=e1] @state sv2=sv2(-1)+C(5)*(1/252)+[ename=e2] @evar var(e1)= (1-exp(-2*C(1)*(1/252))*((C(2)*C(2))/(2*C(1)) @evar var(e2)=C(4)*C(4)*(1/252) @evar cov(e1,e2)=(1-exp(-C(1) *(1/252)))*(C(7)*C(2)*C(4)/C(1)) @signal lcl1=exp(-C(1)*(1/12))*sv1+sv2+C(6)*(1/12)-(1-exp(-C(1)*(1/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(1/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*1/12)+ (1-exp(-1*C(1)*(1/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e3] @signal lcl6=exp(-C(1)*(6/12))*sv1+sv2+C(6)*(6/12)-(1-exp(-C(1)*(6/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(6/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*6/12)+ (1-exp(-C(1)*(6/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e4] @signal lcl12=exp(-C(1)*(12/12))*sv1+sv2+C(6)*(12/12)-(1-exp(-C(1)*(12/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(12/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*12/12)+ (1-exp(-C(1)*(12/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e5] @signal lcl18=exp(-C(1)*(18/12))*sv1+sv2+C(6)*(18/12)-(1-exp(-C(1)*(18/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(18/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*18/12)+ (1-exp(-C(1)*(18/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e6] @signal lcl24=exp(-C(1)*(24/12))*sv1+sv2+C(6)*(24/12)-(1-exp(-C(1)*(24/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(24/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*24/12)+ (1-exp(-C(1)*(24/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e7] @evar var(e3)=C(8)*C(8) @evar var(e4)=C(9)*C(9) @evar var(e5)=C(10)*C(10) @evar var(e6)=C(11)*C(11) @evar var(e5)=C(12)*C(12)
Parameters are set as:
K = C(1)
sigma_X = C(2)
lambda_X = C(3)
sigma_E = C(4)
mu_E = C(5)
mu_E_star = C(6)
ro_X_E = C(7)
sigma_v1 = c(8)
sigma_v6 = C(9)
sigma_v12 = C(10)
sigma_v18 = C(11)
sigma_v24 = C(12)

thanks
tsnowfield
Attachments
SchwartzSmith_TwoFactorModel(1).pdf
Smith & Schwartz (2000)
(1.39 MiB) Downloaded 756 times
eviews forum.docx
equations
(104.84 KiB) Downloaded 987 times

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: parameter estimation with Kalman Filter

Postby EViews Gareth » Thu Jan 10, 2013 3:19 pm

Should:

Code: Select all

@signal lcl1=exp(-C(1)*(1/12))*sv1+sv2+C(6)*(1/12)-(1-exp(-C(1)*(1/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(1/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*1/12)+ (1-exp(-1*C(1)*(1/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e3]
be:

Code: Select all

@signal lcl1=exp(-C(1)*(1/12))*sv1+sv2+C(6)*(1/12)-(1-exp(-C(1)*(1/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(1/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*1/12)+(1-exp(-1*C(1)*(1/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e3]

tsnowfield
Posts: 3
Joined: Fri Jan 04, 2013 8:39 pm

Re: parameter estimation with Kalman Filter

Postby tsnowfield » Thu Jan 10, 2013 3:34 pm

Should:

Code: Select all

@signal lcl1=exp(-C(1)*(1/12))*sv1+sv2+C(6)*(1/12)-(1-exp(-C(1)*(1/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(1/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*1/12)+ (1-exp(-1*C(1)*(1/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e3]
be:

Code: Select all

@signal lcl1=exp(-C(1)*(1/12))*sv1+sv2+C(6)*(1/12)-(1-exp(-C(1)*(1/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(1/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*1/12)+(1-exp(-1*C(1)*(1/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e3]

Thanks
now syntax error message is gone, but when click to estimate (proc/estimate) it is showing: "near singular matrix"

the updated code is:

@state sv1=sv1(-1)*exp(-C(1)*(1/252))+[ename=e1]
@state sv2=sv2(-1)+C(5)*(1/252)+[ename=e2]

@evar var(e1)= (1-exp(-2*C(1)*(1/252)))*(C(2)*C(2)/C(1))*(1/2)
@evar var(e2)=C(4)*C(4)*(1/252)
@evar cov(e1,e2)=(1-exp(-C(1) *(1/252)))*(C(7)*C(2)*C(4)/C(1))

@signal lcl1=exp(-C(1)*(1/12))*sv1+sv2+C(6)*(1/12)-(1-exp(-C(1)*(1/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(1/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*1/12)+(1-exp(-C(1)*(1/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e3]

@signal lcl6=exp(-C(1)*(6/12))*sv1+sv2+C(6)*(6/12)-(1-exp(-C(1)*(6/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(6/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*6/12)+(1-exp(-C(1)*(6/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e4]

@signal lcl12=exp(-C(1)*(12/12))*sv1+sv2+C(6)*(12/12)-(1-exp(-C(1)*(12/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(12/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*12/12)+(1-exp(-C(1)*(12/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e5]

@signal lcl18=exp(-C(1)*(18/12))*sv1+sv2+C(6)*(18/12)-(1-exp(-C(1)*(18/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(18/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*18/12)+(1-exp(-C(1)*(18/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e6]

@signal lcl24=exp(-C(1)*(24/12))*sv1+sv2+C(6)*(24/12)-(1-exp(-C(1)*(24/12)))*(C(3)/C(1))+(1/2)*(1-exp(-2*C(1)*(24/12)))*((C(2)*C(2))/(2*C(1)))+(1/2)*(C(4)*C(4)*24/12)+(1-exp(-C(1)*(24/12)))*C(7)*C(2)*C(4)/C(1)+[ename=e7]

@evar var(e3)=C(8)*C(8)
@evar var(e4)=C(9)*C(9)
@evar var(e5)=C(10)*C(10)
@evar var(e6)=C(11)*C(11)
@evar var(e7)=C(12)*C(12)

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13604
Joined: Tue Sep 16, 2008 5:38 pm

Re: parameter estimation with Kalman Filter

Postby EViews Gareth » Thu Jan 10, 2013 4:07 pm

You have a singular matrix during estimation. Try playing with starting values. It may be that your model is simply badly specified and is truly singular at all coefficient values though.

tsnowfield
Posts: 3
Joined: Fri Jan 04, 2013 8:39 pm

Re: parameter estimation with Kalman Filter

Postby tsnowfield » Thu Jan 10, 2013 5:36 pm

You have a singular matrix during estimation. Try playing with starting values. It may be that your model is simply badly specified and is truly singular at all coefficient values though.
I changed the start value and now it is working, thanks !!!

shm83
Posts: 35
Joined: Tue Sep 10, 2013 5:00 am

Re: parameter estimation with Kalman Filter

Postby shm83 » Tue Oct 01, 2013 2:39 am

Good morning. If you "play" a bit with the parameters starting values, you can reach to some strange results such as some negative volatilities.
How could you set some boundary conditions in the state space?
Thanks in advance

shm83
Posts: 35
Joined: Tue Sep 10, 2013 5:00 am

Re: parameter estimation with Kalman Filter

Postby shm83 » Wed Oct 02, 2013 9:12 am

A thingy issue this one... I have been testing this model with Eviews, and it worked with different samples (different historic prices and commodities).
Well, if I work with the same source data both in matlab and eviews, matlab can reach to the same solution changing the initial parameter values, but Eviews gives me different final values.
My assumption is that if it works correctly with two different data sets (of about 300 observations each forward), the code is correct. I cannot imaging it reaching to the solution of 7 parameters + 2 state variables "by chance".
After studying again the code, and realizing that the equations set in the sspace are correct, (following the Schwartz and Smith paper), I am thinking that probably the error is in the way Eviews algorithm of ML converges, or that it has problems when there are thousands of observations.
If a model is strong, it should not depend on the initial values, although the speed of convergence depends on them.
Has anyone had some problems like this one with Eviews 7?
Thank you in advance!

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: parameter estimation with Kalman Filter

Postby EViews Glenn » Thu Oct 03, 2013 11:17 am

Hard to say without seeing your data and the Matlab results.

shm83
Posts: 35
Joined: Tue Sep 10, 2013 5:00 am

Re: parameter estimation with Kalman Filter

Postby shm83 » Fri Oct 04, 2013 3:09 am

Assuming the code above is correct, as it is just a "copy" of the Equations read in the Schwartz Smith model adapted to a States Space, I have made different trials.
After those, I found two issues:
- The first one, just happened a few times (could be considered as some outliers). Taking exactly the same initial values, the process not always reach to the same solution. It has a convergence tolerance of 0.0001, so a change in the forth decimal or less would be acceptable. But there were some cases in which the second decimal of a parameter changed. I have to say, once more, that this is not the normal case, most of the times it converges to the same solution.
- The second one, and this is the one which worries me, is that if the code of the model is correct, and the model is consistent, changes in the initial values would change the speed of convergence of the process, it might need more/less iterations, but it should converge to the same final values.
Here I can show some data.
Eviews k=4 Eviews k=3.5 Matlab k=4 or k=3.5
c(1) = 4 ' K 1.262096 0.995229 6.008180317
c(2) = 0.6 ' Sigmax 1.086059 1.276214 1.042941153
c(3) = 0.1 ' Lambdax -0.409563 -0.392285 -1.007858295
c(4) = 0.4 ' Sigmae 0.647821 -0.833742 0.488264026
c(5) = -0.1 ' Mu -0.021548 -0.077432 0.011015518
c(6) = 0.01 ' Mu star -0.117928 -0.181615 -0.058022108
c(7) = -0.7 ' Pxe -0.916518 0.951662 -0.764285262
c(8) = 0.1 ' forward1 volatility 0.100693 0.098925 0.181797887
c(9) = 0.1 ' forward2 volatility 0.005577 0.005639 0
c(10) = 0.1 ' forward3 volatility 0.086725 0.086447 0.100170814
c(11) = 0.1 ' forward4 volatility 0.111914 0.111694 0
c(12) = 0.1 ' forward5 volatility 1.31E-06 -1.88E-05 0.163564037
c(13) = 0.1 ' forward6 volatility 0.164685 0.168673 0.227700407

Final SV1 (short term deviations) -0.306044 -0.419387
Final SV2 (equilibrium price level) 3.758073 3.877571


With K=4 both Eviews and Matlab achieve similar results, except for the k parameter. SV1 and SV2 were also quite similar with k=4 both in eviews and matlab. Meanwhile Matlab converges to the same solution changing just a bit a parameter (K, in this example), eviews gives a very different solution, at least in the K and sigma parameters, which makes me suspect either the code is not totally correct (I think this should be the problem) or the convergence algorithm of Eviews does not work in a total reliable way.
What gives me a chance of thinking this option is that it is almost impossible to calibrate 7 parameters + volatilities+2 state variables of two different data sets with good looking results "by coincidence". By the way, one of the data sets which converged properly had more than 2000 observations, so it should not be a problem of that amount.

shm83
Posts: 35
Joined: Tue Sep 10, 2013 5:00 am

Re: parameter estimation with Kalman Filter

Postby shm83 » Fri Oct 04, 2013 4:39 am

After observing the results slower, with k=3.5 I get a negative volatility, which is not logical, that is why I would like to know how I I could place some boundary/border conditions.
Also, c(7) changes from negative to positive, which is not very likely to be ok, even changing the initial conditions :(

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: parameter estimation with Kalman Filter

Postby EViews Glenn » Fri Oct 04, 2013 8:37 am

Again, without seeing the data and results it is difficult to comment intelligently (but I won't let that stop me :) )

Nonlinear models can have multiple local roots and/or flat regions so that different starting values may lead to different "solutions". It does not surprise me that different final values are obtained from different starting values. Note that model consistency or correctness has nothing to do with this behavior, as it is a function of the shape of the objective function. Unless the function is globally concave (which is unlikely in this context), multiple roots will always be a possibility, as will very flat regions of the likelihood. However, I will say that your statement that using the same initial values produces different results is not what I would expect.

One thing to keep in mind (especially with very flat likelihoods) is that a reported convergence based on coefficient differences does not mean that the summed gradients of the objective function are near zero. Accordingly, I always recommend that users look at the gradients at the reported convergence value to see if the first order conditions are satisfied numerically. Another useful diagnostic is to use the final EViews values as Matlab starting values and vice versa to see whether the solutions are stable in each program. Comparing likelihood values can also be useful, but this is sometimes difficult as different programs may use different representations of the likelihood function.

As to boundary conditions, you can only do so provided that you can specify a transformation of the parameters that satisfies the conditions both of your boundary condition, and of the linear state space model representation. There is no ability to restrict a specified parameter to lie in a particular subset of R1, nor a method of restricting states.

Lastly, note also that your convergence tolerance of .0001 is a bit higher than I usually use. As I said, the likelihoods for state space models are generally quite flat, and I find it's better to use something on the order of 1e-6 (or even smaller) and let the program report that it can't find steps then to have premature convergence reported. Again, in this regard, looking at the gradients is highly recommended.

shm83
Posts: 35
Joined: Tue Sep 10, 2013 5:00 am

Re: parameter estimation with Kalman Filter

Postby shm83 » Mon Oct 07, 2013 1:16 am

First of all, thank you again, Glenn. I have changed the tolerance in case that was the problem (it was not). That one was the default one in my computer (maybe the previous user, nevermind).
Anytime I check the code and the equations trying to find a mistake, I get more and more confused, because I cannot see it (and it should be somewhere).
As I said before, thank you for all this implication with the issue. Please find attached the code and the data used in the paper (not using the spot, just the forwards).
The initial values given are kind of generic ones, but with some logical sense, so I reckon they will take us to the right solution.
According to the original paper, this should be the parameters estimation:
k: 1.49, sigmax 0.286, lambdax 0.157, sigmae 0.145, mu -0.0125, mu star 0.0115, pxe 0.3, and forward volatilities close to zero (but >0).
Results achieved with this code and data:
k: 1.57, sigmax 0.2757, lambdax 0.1247, sigmae 0.156, mu -0.0085, mu star 0.009, pxe 0.195.
They are not the same but they are quite close, both in class and sign. However, some forward volatilities, being all close to zero, are <0 (with not physical sense, that is why I wanted to set some boundary conditions).
It is quite unlikely to get similar results of 13 different parameters if the code is not correct, so I assume (hopefully right) that the code is not bad.
Can anyone find a mistake in the code?
Glenn, the gradients are shown below.


Gradients of the objective function at estimated parameters
Sspace: ESTIMATIONSS
Method: Maximum likelihood
Computed using accurate numeric derivatives

Coefficient Sum Mean Newton Dir.

C(1) -2.854837 -0.010652 0.021276
C(2) -55.72591 -0.207933 0.018825
C(3) 0.999495 0.003729 -0.034985
C(4) -13.43086 -0.050115 0.000481
C(5) 0.758553 0.002830 -0.003270
C(6) -32.92388 -0.122850 0.000360
C(7) 1.812148 0.006762 -0.039372
C(8) -32.40059 -0.120898 0.000385
C(9) -8.552920 -0.031914 -0.359073
C(10) 516.8702 1.928620 -0.000296
C(11) 132.7352 0.495281 0.000188
C(12) 263.9167 0.984764 7.45E-05
Attachments
codess.txt
code
(3.85 KiB) Downloaded 650 times
futureslog.xlsx
Logarithm of forward prices
(28.97 KiB) Downloaded 836 times

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: parameter estimation with Kalman Filter

Postby EViews Glenn » Mon Oct 07, 2013 10:54 am

Those gradients are not particularly small.

shm83
Posts: 35
Joined: Tue Sep 10, 2013 5:00 am

Re: parameter estimation with Kalman Filter

Postby shm83 » Tue Oct 08, 2013 12:01 am

I don´t know if it has anything to do with the number of observations (only 268). With other set of data it finishes with these gradients:

Gradients of the objective function at estimated parameters
Sspace: ESTIMACION
Method: Maximum likelihood
Computed using accurate numeric derivatives

Coefficient Sum Mean Newton Dir.

C(1) -13.74508 -0.008422 -0.135062
C(2) 11.24951 0.006893 -0.015030
C(3) 0.034530 2.12E-05 -1.837239
C(4) 12.17259 0.007459 -0.067755
C(5) 0.227816 0.000140 1.275067
C(6) -0.005879 -3.60E-06 0.043708
C(7) 126.4169 0.077461 -0.006654
C(8) 0.675166 0.000414 -0.001098
C(9) 58.66507 0.035947 -0.002810
C(10) 0.975857 0.000598 -0.000251
C(11) -2.168892 -0.001329 0.000749
C(12) 3.777636 0.002315 37.20099
C(13) 2.134878 0.001308 1.47E-05


which might be also big, but smaller than the other. This estimation was made with 1300 obs and 6 forwards instead of 5.
I have checked again the code against the original paper and I cannot see a better way to transcript it :s

EViews Glenn
EViews Developer
Posts: 2682
Joined: Wed Oct 15, 2008 9:17 am

Re: parameter estimation with Kalman Filter

Postby EViews Glenn » Tue Oct 08, 2013 9:43 am

Can you post your workfile?


Return to “Estimation”

Who is online

Users browsing this forum: No registered users and 1 guest