Hi there
I am implementing Kalman Filter in C++
There however appear that some apparently minor numerical discrepancies between Matlab and C++ versions of T and R matrices (i.e. the ghx and ghu matrices, and probably the other calculations too), are gradually creeping-up to a rather significan difference in the KF log-likelihood calculation and I am trying to figure out what can be done about it.
The main impact seem to be on the inv(F) due to rather low values appearing in F. where F=ZPZ'+H resulting in a significant impact on the log likelihood and, as well, the next step P and system state forecast since both depend on the gain matrix K=P*Z' *invF and thus on invF;
I.e. C++ F: F.data[0] = 0.00037206242498542022 F.data[1] =[2] = -0.00045917810077178716 F.data[3] = 0.0008672061055033099 and (rounded up) inverse of C++ F invF = [7756 4106.76; 4106.76 3327.6]
(or using Matlab for inversion calculation invF = 1.0e+003 * 7.7560 4.1068 4.1068 3.3276)
whilst Matlab Dynare F = 1.0e-003 * 0.3370 -0.4437 -0.4437 0.8383
and its iF = inv(F) = 1.0e+003 * 9.7907 5.1823 5.1823 3.9360
Then, e.g. Matlab likelihood factor v'*iF*v = 963.6491 whilst the C++ one is 771.88779, thus, discrepancies pretty much affecting the total LL calculation to say the least.
It may also be a question which one is the more correct result.
Any suggestions? PS: both, C++ and Matlab Dynare use the same C++<model>_dynamic.dll to calculate the initial jacobian but the jacobians differ.
Best regards, George
This is not small. The second significant digit of the elements of F are different in Matlab and C++. We need to find out the origin of that difference.
Best
Michel
Quoting George Perendia george@perendia.orangehome.co.uk:
Hi there
I am implementing Kalman Filter in C++
There however appear that some apparently minor numerical discrepancies between Matlab and C++ versions of T and R matrices (i.e. the ghx and ghu matrices, and probably the other calculations too), are gradually creeping-up to a rather significan difference in the KF log-likelihood calculation and I am trying to figure out what can be done about it.
The main impact seem to be on the inv(F) due to rather low values appearing in F. where F=ZPZ'+H resulting in a significant impact on the log likelihood and, as well, the next step P and system state forecast since both depend on the gain matrix K=P*Z' *invF and thus on invF;
I.e. C++ F: F.data[0] = 0.00037206242498542022 F.data[1] =[2] = -0.00045917810077178716 F.data[3] = 0.0008672061055033099 and (rounded up) inverse of C++ F invF = [7756 4106.76; 4106.76 3327.6]
(or using Matlab for inversion calculation invF = 1.0e+003 * 7.7560 4.1068 4.1068 3.3276)
whilst Matlab Dynare F = 1.0e-003 * 0.3370 -0.4437 -0.4437 0.8383
and its iF = inv(F) = 1.0e+003 * 9.7907 5.1823 5.1823 3.9360
Then, e.g. Matlab likelihood factor v'*iF*v = 963.6491 whilst the C++ one is 771.88779, thus, discrepancies pretty much affecting the total LL calculation to say the least.
It may also be a question which one is the more correct result.
Any suggestions? PS: both, C++ and Matlab Dynare use the same C++<model>_dynamic.dll to calculate the initial jacobian but the jacobians differ.
Best regards, George
It is OK now.
PS: I copied from Matlab and used in the stand allone C++ test program a lower precision representation of the steady state than what Matlab Dynare was actually using internally and that small difference gradually built-up to a large one!
Best regards George
On 21/04/2010 15:00, Michel Juillard wrote:
This is not small. The second significant digit of the elements of F are different in Matlab and C++. We need to find out the origin of that difference.
Best
Michel
Quoting George Perendiageorge@perendia.orangehome.co.uk:
Hi there
I am implementing Kalman Filter in C++
There however appear that some apparently minor numerical discrepancies between Matlab and C++ versions of T and R matrices (i.e. the ghx and ghu matrices, and probably the other calculations too), are gradually creeping-up to a rather significan difference in the KF log-likelihood calculation and I am trying to figure out what can be done about it.
The main impact seem to be on the inv(F) due to rather low values appearing in F. where F=ZPZ'+H resulting in a significant impact on the log likelihood and, as well, the next step P and system state forecast since both depend on the gain matrix K=P*Z' *invF and thus on invF;
I.e. C++ F: F.data[0] = 0.00037206242498542022 F.data[1] =[2] = -0.00045917810077178716 F.data[3] = 0.0008672061055033099 and (rounded up) inverse of C++ F invF = [7756 4106.76; 4106.76 3327.6]
(or using Matlab for inversion calculation invF = 1.0e+003 * 7.7560 4.1068 4.1068 3.3276)
whilst Matlab Dynare F = 1.0e-003 * 0.3370 -0.4437 -0.4437 0.8383
and its iF = inv(F) = 1.0e+003 * 9.7907 5.1823 5.1823 3.9360
Then, e.g. Matlab likelihood factor v'*iF*v = 963.6491 whilst the C++ one is 771.88779, thus, discrepancies pretty much affecting the total LL calculation to say the least.
It may also be a question which one is the more correct result.
Any suggestions? PS: both, C++ and Matlab Dynare use the same C++<model>_dynamic.dll to calculate the initial jacobian but the jacobians differ.
Best regards, George