Home > matlab > score.m

score

PURPOSE ^

function [DLIK] = score(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,riccati_tol)

SYNOPSIS ^

function [DLIK] = score(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,riccati_tol)

DESCRIPTION ^

 function [DLIK] = score(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,riccati_tol)

 computes the derivative of the log-likelihood function of
 a state space model (notation as in kalman_filter.m in DYNARE
 thanks to Nikolai Iskrev

 NOTE: the derivative matrices (DT,DR ...) are 3-dim. arrays with last
 dimension equal to the number of structural parameters

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [DLIK] = score(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,riccati_tol)
0002 % function [DLIK] = score(T,R,Q,H,P,Y,DT,DYss,DOm,DH,DP,start,mf,kalman_tol,riccati_tol)
0003 %
0004 % computes the derivative of the log-likelihood function of
0005 % a state space model (notation as in kalman_filter.m in DYNARE
0006 % thanks to Nikolai Iskrev
0007 %
0008 % NOTE: the derivative matrices (DT,DR ...) are 3-dim. arrays with last
0009 % dimension equal to the number of structural parameters
0010 
0011 % Copyright (C) 2009 Dynare Team
0012 %
0013 % This file is part of Dynare.
0014 %
0015 % Dynare is free software: you can redistribute it and/or modify
0016 % it under the terms of the GNU General Public License as published by
0017 % the Free Software Foundation, either version 3 of the License, or
0018 % (at your option) any later version.
0019 %
0020 % Dynare is distributed in the hope that it will be useful,
0021 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0022 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0023 % GNU General Public License for more details.
0024 %
0025 % You should have received a copy of the GNU General Public License
0026 % along with Dynare.  If not, see <http://www.gnu.org/licen
0027 
0028     k = size(DT,3);                                 % number of structural parameters
0029     smpl = size(Y,2);                               % Sample size.
0030     mm   = size(T,2);                               % Number of state variables.
0031     a    = zeros(mm,1);                             % State vector.
0032     Om   = R*Q*transpose(R);                        % Variance of R times the vector of structural innovations.
0033     t    = 0;                                       % Initialization of the time index.
0034     oldK = 0;
0035     notsteady   = 1;                                % Steady state flag.
0036     F_singular  = 1;
0037 
0038     DLIK  = zeros(k,1);                             % Initialization of the score.
0039     Da    = zeros(mm,k);                            % State vector.
0040     Dv    = zeros(length(mf),k);                    % observation vector.
0041     
0042 %     for ii = 1:k
0043 %         DOm = DR(:,:,ii)*Q*transpose(R) + R*DQ(:,:,ii)*transpose(R) + R*Q*transpose(DR(:,:,ii));
0044 %     end
0045     
0046     while notsteady & t<smpl
0047         t  = t+1;
0048         v  = Y(:,t)-a(mf);
0049         F  = P(mf,mf) + H;
0050         if rcond(F) < kalman_tol
0051             if ~all(abs(F(:))<kalman_tol)
0052                 return
0053             else
0054                 a = T*a;
0055                 P = T*P*transpose(T)+Om;
0056             end
0057         else
0058             F_singular = 0;
0059             iF     = inv(F);
0060             K      = P(:,mf)*iF;
0061 
0062             [DK,DF,DP1] = computeDKalman(T,DT,DOm,P,DP,DH,mf,iF,K);
0063             for ii = 1:k
0064                 Dv(:,ii)   = -Da(mf,ii)-DYss(mf,ii);
0065                 Da(:,ii)   = DT(:,:,ii)*(a+K*v) + T*(Da(:,ii)+DK(:,:,ii)*v + K*Dv(:,ii));
0066                 if t>=start
0067                    DLIK(ii,1)  = DLIK(ii,1) + trace( iF*DF(:,:,ii) ) + 2*Dv(:,ii)'*iF*v - v'*(iF*DF(:,:,ii)*iF)*v;
0068                 end
0069             end
0070             a      = T*(a+K*v);                   
0071             P      = T*(P-K*P(mf,:))*transpose(T)+Om;
0072             DP     = DP1;
0073         end
0074         notsteady = max(max(abs(K-oldK))) > riccati_tol;
0075         oldK = K;
0076     end
0077 
0078     if F_singular
0079         error('The variance of the forecast error remains singular until the end of the sample')
0080     end
0081 
0082     for ii = 1:k
0083         tmp0(:,:,ii) = iF*DF(:,:,ii)*iF;
0084     end
0085     
0086     if t < smpl
0087         t0 = t+1;
0088         while t < smpl
0089             t = t+1;
0090             v = Y(:,t)-a(mf);
0091             for ii = 1:k
0092                 Dv(:,ii)   = -Da(mf,ii)-DYss(mf,ii);
0093                 Da(:,ii)   = DT(:,:,ii)*(a+K*v) + T*(Da(:,ii)+DK(:,:,ii)*v + K*Dv(:,ii));
0094                 if t>=start
0095                    DLIK(ii,1)  = DLIK(ii,1) + trace( iF*DF(:,:,ii) ) + 2*Dv(:,ii)'*iF*v - v'*(iF*DF(:,:,ii)*iF)*v;
0096                 end
0097             end
0098             a = T*(a+K*v);
0099         end
0100         for ii = 1:k
0101 %             DLIK(ii,1)  = DLIK(ii,1) + (smpl-t0+1)*trace( iF*DF(:,:,ii) );
0102         end
0103         
0104     end    
0105     
0106     DLIK = DLIK/2;
0107     
0108 % end of main function
0109     
0110 function [DK,DF,DP1] = computeDKalman(T,DT,DOm,P,DP,DH,mf,iF,K)
0111 
0112             k      = size(DT,3);
0113             tmp    = P-K*P(mf,:);
0114 
0115 for ii = 1:k
0116     DF(:,:,ii)  = DP(mf,mf,ii) + DH(:,:,ii); 
0117     DiF(:,:,ii) = -iF*DF(:,:,ii)*iF;
0118     DK(:,:,ii)  = DP(:,mf,ii)*iF + P(:,mf)*DiF(:,:,ii);
0119     Dtmp        = DP(:,:,ii) - DK(:,:,ii)*P(mf,:) - K*DP(mf,:,ii);
0120     DP1(:,:,ii) = DT(:,:,ii)*tmp*T' + T*Dtmp*T' + T*tmp*DT(:,:,ii)' + DOm(:,:,ii);
0121 end
0122 
0123 % end of computeDKalman
0124 
0125 
0126

Generated on Tue 22-May-2012 02:40:23 by m2html © 2005