From f4c3482acddd6b95dce83318be2f6dc919abdac1 Mon Sep 17 00:00:00 2001 From: george Date: Wed, 8 Jul 2009 12:20:21 +0000 Subject: [PATCH] P convergence likelihood bug fix git-svn-id: https://www.dynare.org/svn/dynare/trunk@2824 ac1d8469-bf42-47a9-8791-bf33cf982152 --- matlab/DiffuseLikelihood1.m | 27 ++++++++++++++------------- matlab/DiffuseLikelihood1_Z.m | 25 +++++++++++++------------ matlab/DiffuseLikelihoodH1.m | 27 ++++++++++++++------------- matlab/DiffuseLikelihoodH1_Z.m | 25 +++++++++++++------------ 4 files changed, 54 insertions(+), 50 deletions(-) diff --git a/matlab/DiffuseLikelihood1.m b/matlab/DiffuseLikelihood1.m index 88d4a82d0..505912ce9 100644 --- a/matlab/DiffuseLikelihood1.m +++ b/matlab/DiffuseLikelihood1.m @@ -51,12 +51,10 @@ function [LIK, lik] = DiffuseLikelihood1(T,R,Q,Pinf,Pstar,Y,trend,start) dF = 1; QQ = R*Q*transpose(R); t = 0; - lik = zeros(smpl+1,1); + lik = zeros(smpl,1); LIK = Inf; - lik(smpl+1) = smpl*pp*log(2*pi); notsteady = 1; crit = options_.kalman_tol; - reste = 0; while rank(Pinf,crit) & t < smpl t = t+1; v = Y(:,t)-a(mf)-trend(:,t); @@ -116,14 +114,17 @@ function [LIK, lik] = DiffuseLikelihood1(T,R,Q,Pinf,Pstar,Y,trend,start) error(['The variance of the forecast error remains singular until the' ... 'end of the sample']) end - reste = smpl-t; - while t < smpl - t = t+1; - v = Y(:,t)-a(mf)-trend(:,t); - a = T*(a+K*v); - lik(t) = transpose(v)*iF*v; - end - lik(t) = lik(t) + reste*log(dF); + if t < smpl + t0 = t+1; + while t < smpl + t = t+1; + v = Y(:,t)-a(mf)-trend(:,t); + a = T*(a+K*v); + lik(t) = transpose(v)*iF*v; + end + lik(t0:smpl) = lik(t0:smpl) + log(dF); + end + % adding log-likelihhod constants + lik = (lik + pp*log(2*pi))/2; - - LIK = .5*(sum(lik(start:end))-(start-1)*lik(smpl+1)/smpl);% Minus the log-likelihood. + LIK = sum(lik(start:end)); % Minus the log-likelihood. \ No newline at end of file diff --git a/matlab/DiffuseLikelihood1_Z.m b/matlab/DiffuseLikelihood1_Z.m index 5f90ba5a9..018577742 100644 --- a/matlab/DiffuseLikelihood1_Z.m +++ b/matlab/DiffuseLikelihood1_Z.m @@ -50,12 +50,10 @@ function [LIK, lik] = DiffuseLikelihood1_Z(T,Z,R,Q,Pinf,Pstar,Y,start) dF = 1; QQ = R*Q*transpose(R); t = 0; - lik = zeros(smpl+1,1); + lik = zeros(smpl,1); LIK = Inf; - lik(smpl+1) = smpl*pp*log(2*pi); notsteady = 1; crit = options_.kalman_tol; - reste = 0; while rank(Pinf,crit) & t < smpl t = t+1; v = Y(:,t)-Z*a; @@ -116,14 +114,17 @@ function [LIK, lik] = DiffuseLikelihood1_Z(T,Z,R,Q,Pinf,Pstar,Y,start) error(['The variance of the forecast error remains singular until the' ... 'end of the sample']) end - reste = smpl-t; - while t < smpl - t = t+1; - v = Y(:,t)-Z*a; - a = T*(a+K*v); - lik(t) = v'*iF*v; + if t < smpl + t0 = t+1; + while t < smpl + t = t+1; + v = Y(:,t)-Z*a; + a = T*(a+K*v); + lik(t) = v'*iF*v; + end + lik(t0:smpl) = lik(t0:smpl) + log(dF); end - lik(t) = lik(t) + reste*log(dF); + % adding log-likelihhod constants + lik = (lik + pp*log(2*pi))/2; - - LIK = .5*(sum(lik(start:end))-(start-1)*lik(smpl+1)/smpl);% Minus the log-likelihood. + LIK = sum(lik(start:end)); % Minus the log-likelihood. \ No newline at end of file diff --git a/matlab/DiffuseLikelihoodH1.m b/matlab/DiffuseLikelihoodH1.m index 1b7bb51fa..6d2e6da85 100644 --- a/matlab/DiffuseLikelihoodH1.m +++ b/matlab/DiffuseLikelihoodH1.m @@ -52,12 +52,10 @@ function [LIK, lik] = DiffuseLikelihoodH1(T,R,Q,H,Pinf,Pstar,Y,trend,start) dF = 1; QQ = R*Q*transpose(R); t = 0; - lik = zeros(smpl+1,1); + lik = zeros(smpl,1); LIK = Inf; - lik(smpl+1) = smpl*pp*log(2*pi); notsteady = 1; crit = options_.kalman_tol; - reste = 0; while rank(Pinf,crit) & t < smpl t = t+1; v = Y(:,t)-a(mf)-trend(:,t); @@ -117,14 +115,17 @@ function [LIK, lik] = DiffuseLikelihoodH1(T,R,Q,H,Pinf,Pstar,Y,trend,start) error(['The variance of the forecast error remains singular until the' ... 'end of the sample']) end - reste = smpl-t; - while t < smpl - t = t+1; - v = Y(:,t)-a(mf)-trend(:,t); - a = T*(a+K*v); - lik(t) = transpose(v)*iF*v; + if t < smpl + t0 = t+1; + while t < smpl + t = t+1; + v = Y(:,t)-a(mf)-trend(:,t); + a = T*(a+K*v); + lik(t) = transpose(v)*iF*v; + end + lik(t0:smpl) = lik(t0:smpl) + log(dF); end - lik(t) = lik(t) + reste*log(dF); - LIK = .5*(sum(lik(start:end))-(start-1)*lik(smpl+1)/smpl);% Minus the - % log-likelihood. - + % adding log-likelihhod constants + lik = (lik + pp*log(2*pi))/2; + + LIK = sum(lik(start:end)); % Minus the log-likelihood. diff --git a/matlab/DiffuseLikelihoodH1_Z.m b/matlab/DiffuseLikelihoodH1_Z.m index 8533d5dbf..2de68dc9f 100644 --- a/matlab/DiffuseLikelihoodH1_Z.m +++ b/matlab/DiffuseLikelihoodH1_Z.m @@ -52,12 +52,10 @@ function [LIK, lik] = DiffuseLikelihoodH1_Z(T,Z,R,Q,H,Pinf,Pstar,Y,start) dF = 1; QQ = R*Q*transpose(R); t = 0; - lik = zeros(smpl+1,1); + lik = zeros(smpl,1); LIK = Inf; - lik(smpl+1) = smpl*pp*log(2*pi); notsteady = 1; crit = options_.kalman_tol; - reste = 0; while rank(Pinf,crit) & t < smpl t = t+1; v = Y(:,t)-Z*a; @@ -118,14 +116,17 @@ function [LIK, lik] = DiffuseLikelihoodH1_Z(T,Z,R,Q,H,Pinf,Pstar,Y,start) error(['The variance of the forecast error remains singular until the' ... 'end of the sample']) end - reste = smpl-t; - while t < smpl - t = t+1; - v = Y(:,t)-Z*a; - a = T*(a+K*v); - lik(t) = v'*iF*v; + if t < smpl + t0 = t+1; + while t < smpl + t = t+1; + v = Y(:,t)-Z*a; + a = T*(a+K*v); + lik(t) = v'*iF*v; + end + lik(t0:smpl) = lik(t0:smpl) + log(dF); end - lik(t) = lik(t) + reste*log(dF); + % adding log-likelihhod constants + lik = (lik + pp*log(2*pi))/2; - - LIK = .5*(sum(lik(start:end))-(start-1)*lik(smpl+1)/smpl);% Minus the log-likelihood. + LIK = sum(lik(start:end)); % Minus the log-likelihood. \ No newline at end of file