Debian Codegraph @

contrib/ms-sbvar/TZcode/MatlabFiles/fn_a0cfreegrad_tv.m

raw
1 function [g,badg] = fn_a0cfreegrad_tv(b,nvar,nStates,n0cumsum,Ui,Tkave,Del00invcell_ave,dpDelp0cell_ave)
2 % [g,badg] = fn_a0cfreegrad_tv(b,nvar,nStates,n0cumsum,Ui,Tkave,Del00invcell_ave,dpDelp0cell_ave)
3 % Analytical gradient for fn_a0cfreegrad_tv.m when using csminwel.m.
4 % Note: (1) columns correspond to equations; (2) c stands for constant.
5 % See TBVAR NOTE pp. 61-62, 34a-34c.
6 %
7 % b: n0cumsum(end)-by-1 vector of free constant A0 parameters, vectorized from b_ihatcell.
8 % nvar: Number of endogeous variables.
9 % nStates: Number of states.
10 % n0cumsum: [0;cumsum(n0)] where n0 is nvar-by-1 and its ith element represents the number of
11 % free constant A0 parameters in ith equation for *all states*.
12 % Ui: nvar-by-1 cell. In each cell, nvar*nStates-by-(qi+si) orthonormal basis for the null of the ith
13 % equation contemporaneous restriction matrix where qi is the number of free parameters
14 % within the state and si is the number of free parameters across the states.
15 % With this transformation, we have ai = Ui*bi or Ui'*ai = bi where ai is a vector
16 % of total original parameters and bi is a vector of free parameters. When no
17 % restrictions are imposed, we have Ui = I. There must be at least one free
18 % parameter left for the ith equation.
19 % Tkave: nStates-by-1 of sample sizes (excluding lags but including dummies if provided) for different states k,
20 % averaged (ave) over E-step draws. For T_k. See p.61.
21 % Del00invcell_ave: Quardratic term for b0_j in each cell. See p.61.
22 % dpDelp0cell_ave: Cross d+_j and b0_j term in each cell. See p.61.
23 %----------------
24 % g: n0cumsum(end)-by-1 analytical gradient for fn_a0cfreegrad_tv.m.
25 % badg: 0, the value that is used in csminwel.m.
26 %
27 % Tao Zha, September 2001
28 %
29 % Copyright (C) 1997-2012 Tao Zha
30 %
31 % This free software: you can redistribute it and/or modify
32 % it under the terms of the GNU General Public License as published by
33 % the Free Software Foundation, either version 3 of the License, or
34 % (at your option) any later version.
35 %
36 % It is distributed in the hope that it will be useful,
37 % but WITHOUT ANY WARRANTY; without even the implied warranty of
38 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
39 % GNU General Public License for more details.
40 %
41 % If you did not received a copy of the GNU General Public License
42 % with this software, see <http://www.gnu.org/licenses/>.
43 %
44
45
46 A0_shat=zeros(nvar,nvar,nStates); % Arranged A0 matrix across states.
47 B_shat=zeros(nvar,nvar,nStates); % inv(A0_shat);
48
49 badg = 0;
50 g = zeros(size(b(:)));
51
52 %**** The derivative of the exponential term w.r.t. each free constanta A0 paramater. See pp. 34a and 62.
53 for kj = 1:nvar
54 indxn0j = [n0cumsum(kj)+1:n0cumsum(kj+1)]; % Index for the parameters in the ith equation.
55 bj = b(indxn0j);
56 g(indxn0j) = Del00invcell_ave{kj}*bj - dpDelp0cell_ave{kj}';
57 A0_shat(:,kj,:) = reshape(Ui{kj}*bj,nvar,nStates);
58 end
59
60
61 %for si=1:nStates % See p.34a.
62
63 %end
64 %**** Add the derivative of -Tk_ave*log|A0(k)| w.r.t. each free constanta A0 paramater. See pp. 62 and 34a.
65 for kj = 1:nvar
66 indxn0j = [n0cumsum(kj)+1:n0cumsum(kj+1)]; % Index for the parameters in the ith equation.
67 for si=1:nStates % See p.34a.
68 B_shat(:,:,si)=inv(A0_shat(:,:,si)); % See p.62.
69 g(indxn0j) = g(indxn0j) - Tkave(si)*( B_shat(kj,:,si)*Ui{kj}((si-1)*nvar+1:si*nvar,:) )'; % See p.62.
70 end
71 end
72