| 1 |
function [g,badg] = pmddg235(x,idmat0,idmat1,fss,nvar, ... |
| 2 |
ncoef,phi,y,A0b,sg0bid,sgpbid); |
| 3 |
% Leeper-Sims-Zha BVAR setup, analytical gradient for "csminwel.m" |
| 4 |
% general program to setup A0 matrix and compute the likelihood |
| 5 |
% requires x (parameter vector), a0indx (matrix indicating the free |
| 6 |
% parameters in A0), fss (forecast sample size), nvar (number of variables), |
| 7 |
% ncoef (number of coefficients in a single equation in A+), phi (r.h.s. |
| 8 |
% observations in the system for A+), y (l.h.s. observations for A0), |
| 9 |
% ymy (Y'*M*Y), xd0 (X(0) for dummy observations y*(1)), ys1 (dummy initial |
| 10 |
% observations y*(1)), xtx (X'X or phi'*phi), A0b (initial prior on A0), |
| 11 |
% sg0bid (diagonal of Sigma0_bar on the parameters in i-th equation in A0, |
| 12 |
% initial prior covariace matrix), sgpbid (diagonal of Sigma+_bar on the |
| 13 |
% parameters in i-th equation in A0, initial prior covariance matrix). |
| 14 |
% |
| 15 |
% Copyright (C) 1997-2012 Tao Zha |
| 16 |
% |
| 17 |
% This free software: you can redistribute it and/or modify |
| 18 |
% it under the terms of the GNU General Public License as published by |
| 19 |
% the Free Software Foundation, either version 3 of the License, or |
| 20 |
% (at your option) any later version. |
| 21 |
% |
| 22 |
% It is distributed in the hope that it will be useful, |
| 23 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 24 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 25 |
% GNU General Public License for more details. |
| 26 |
% |
| 27 |
% If you did not received a copy of the GNU General Public License |
| 28 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 29 |
% |
| 30 |
|
| 31 |
global vaha0 vaMMa0 FRESHFUNCTION |
| 32 |
% CAS added FRESHFUNCTION 8/20/96 to allow use of pmddg23 in a numerical Hessian calculation, where |
| 33 |
% it is invoked repeatedly without new function evaluations. |
| 34 |
if ~FRESHFUNCTION |
| 35 |
fjnk = pmddf235(x,idmat0,idmat1,fss,nvar,ncoef,phi,y,A0b,sg0bid,sgpbid); |
| 36 |
end |
| 37 |
FRESHFUNCTION=0; |
| 38 |
badg = 0; |
| 39 |
% |
| 40 |
a0indx=find(idmat0); |
| 41 |
nhp = 0; % <<>> 4 hyperparameters |
| 42 |
na0p = length(a0indx); % <<>> number of A0 parameters |
| 43 |
nfp = na0p+nhp; |
| 44 |
g = zeros(nfp,1); |
| 45 |
A0h= zeros(nvar); |
| 46 |
A0h(a0indx) = x(1:na0p); |
| 47 |
% restrictions on A0 |
| 48 |
|
| 49 |
%disp(sprintf('Starting loop (gradient): %g',toc)) |
| 50 |
|
| 51 |
|
| 52 |
%%%%%%% |
| 53 |
%%% analytical gradient for A0 below |
| 54 |
%%%%%%% |
| 55 |
% |
| 56 |
% ** dla0 = dlog|A0|/d(a0??) |
| 57 |
vdla0 = zeros(na0p,1); |
| 58 |
dla0 = inv(A0h)'; |
| 59 |
dla0 = dla0(:); |
| 60 |
vdla0 = dla0(a0indx); |
| 61 |
|
| 62 |
% without the prior |A0|^k |
| 63 |
g = -fss*vdla0 + 0.5*vaha0 + 0.5*vaMMa0; |
| 64 |
|
| 65 |
%disp(sprintf('Loop end (gradient): %g', toc)) |