Kalman Filter in Dynare vs MATLAB

Hello,

I am having problem in replicating Dynare estimation results in MATLAB.
I have the same solution matrices both in Dynare and MATLAB as well as observation equations and related var-covar matrices.
In dynare estimation, I use mode_compute=4 and lik_init=1 and everything works well. However, in MATLAB, Kalman filter produces very different log-likelihood with same parameter values and csminwel in MATLAB produces very different posterior modes.
I couldn’t find any argument why Kalman filter should produce different results using the same arguments. Maybe someone can help me?
Thank you.

Burak

Dear Burak,

I am not familiar with Kalman filter routines in Matlab and I do not think we ever compared the results. Could you please post files for replicating the issue?

Best,
Stéphane.

Actually, I compared the likelihood and modes with various Kalman filter implementations in Matlab and was able to obtain the same results. So there must be something wrong with your approach.

Dear Stéphane and Johannes,

Thank you very much for your replies.

Stéphane,
I attached the files. If you run Comparison.m file, you will see the comparison of solution matrices and loglikelihood values. The mod files are in Dynare_Files folder. For MATLAB, files are in Mfiles folder and model_solution.m solves the model, sysmat.m defines measurement equation and kalman.m implements the Kalman filter. I also plot the likelihood values of each period and there is clear downward trend over time.

Johannes,
In other models, I managed to obtain similar (but not exact) results in Dynare and MATLAB but this time the differences are dramatic. May I kindly ask, if you could share one of your implementations in MATLAB, if possible? Because I follow the approach of Herbst and Schorfheide from their book and I was thinking the differences are derived from the initialization of the filter since I have exactly the same matrices in transition and measurement equations. I would be more than appreciated for your help.

Thank you once more time for your replies.

Burak

ModelBurak.zip (3.0 MB)

Did you take into account that your mod-file uses ´presample=4` to initialize the Kalman filter recursions?

Kalman-Filter recursions start from the first data point in MATLAB.
I use ‘presample=4’ in MATLAB only when I am computing the sum of log-likelihood and I drop the first 4 values of the log-likelihood values and sum up the remaining log-likelihood values.
I thought this is what Dynare does when presample option is used.

Ok. I found the reason. Dynare detrends the data first and then initiates Kalman Filter. So, there is no constant in the observation equation in Dynare.
But in MATLAB, I put trend values as constant in the measurement equation. So, I guess and I don’t know why, the prediction becomes erroneous over time because of the constant term in the measurement equation.

And I realized, Dynare initiates Kalman filter at period ‘presample+1’. Then I do not understand what the difference is between first_obs and presample options.

  1. From what I can see in the description of your kalman.m, it does not allow a constant in the state transition equation. So not demeaning the data will cause problems.
  2. presample starts the Kalman recursion at observation 1 and updates the respective matrices. But when computing the joint likelihood, the sum over the values for the respective observations only starts at presample+1. In contrast, with first_obs you start both the recursion and the sum at time first_obs. Put differently, presample allows you to already move from the initialization with the unconditional mean and variance to the steady state Kalman filter values.
  1. The variables in the model are detrended and log-linearized around steady-state and that’s why there is no constant in the transition equation in “kalman.m”

  2. Yes, I agree. I missed the starting point in “kalman_filter.m” of Dynare.

And Prof. Johannes, thank you for your help and time.

Best,

Burak