Debian Codegraph @

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

raw
1 function [g,badg] = fn_a0sfreegrad2(b,Uistar,Uibar,nvar,nStates,n0,n0cumsum,tvstate,tvtot,constot,...
2 tvinx,constinx,indxTV,indxConst,Tkave,Sgm0tldinvaveConst,Sgm0tldinvaveTV)
3 % Analytical gradient for both time-varying and constant parameters for fn_a0sfreefun2.m when using csminwel.m.
4 % The case of no asymmetric prior and no lag restrictions.
5 % Note: (1) columns correspond to equations; s stands for state.
6 % See Time-Varying BVAR NOTE pp.34a-34c.
7 %
8 % b: tvtot+constot-by-1 vector of free A0 parameters, vectorized from b_shat for both time-varying and constant parameter matrices.
9 % Uistar: cell(nvar,1). In each cell, nvar*nStates-by-qi orthonormal basis for the null of the ith
10 % equation contemporaneous restriction matrix where qi is the number of free parameters
11 % where in each state, nvar-by-qi is the same. With this transformation, we have
12 % ai = Ui*bi or Ui'*ai = bi where ai is a vector of total original parameters and
13 % bi is a vector of free parameters. When no restrictions are imposed, we have Ui = I.
14 % There must be at least one free parameter left for the ith equation. See p.33.
15 % Uibar: cell(nvar,1). In each cell, we have nvar*nStates-by-qi*nStates, rearranged
16 % from Uistar or Ui. See p.33.
17 % nvar: Number of endogenous variables.
18 % nStates: NUmber of states.
19 % n0: nvar-by-1. n0(i)=qi where ith element represents the number of free A0 parameters in ith equation for each state.
20 % n0cumsum=[0;cumsum(n0)]: nvar+1-by-1. Cumulative sums of n0 with zero added.
21 % tvstate=length(tvinx): Number of time-varying parameters for all equations under *each* state.
22 % constot=length(constinx): Total number of constant parameters for all equations.
23 % tvtot = tvstate*nStates: Total number of time-varying parameters for all equations under all states.
24 % tvinx: Vectorized index to include time-varying parameters for all equations under *each* state.
25 % constinx: Vectorized index to include constant parameters for all equations.
26 % indxTV: Index vector of ascending order for equations with time-varying parameters.
27 % indxConst: Index vector of ascending order for equations with constant parameters. When [], all equations
28 % are time varying; when [1:nvar], all equations have constant parameters.
29 % Tkave: nStates-by-1 of sample sizes (excluding lags but including dummies if provided) for different states k,
30 % averaged (ave) over E-step draws. For T_k. See p.40.
31 % Sgm0tldinvaveConst
32 % Sgm0tldinvaveTV
33 % Sgm0tldinvave: nvar*nStates-by-nvar*nStates. The matrix inv(\Sigma~_0) averaged (ave)
34 % over E-step draws. Same for all equations because of no asymmetric prior and no lag
35 % restrictions. Resembles old SpH in the exponent term in posterior of A0,
36 % but NOT divided by fss (T) yet. See p.40.
37 %----------------
38 % g: tvtot+constot-by-1 analytical gradient for fn_a0sfreefun2.m. Vectorized first from the
39 % time-varying parameter matrix and then from constant parameter and constant parameter matrix.
40 % badg: Always 0 --- the value that is used in csminwel.m.
41 %
42 % Tao Zha, August 2001.
43 %
44 % Copyright (C) 1997-2012 Tao Zha
45 %
46 % This free software: you can redistribute it and/or modify
47 % it under the terms of the GNU General Public License as published by
48 % the Free Software Foundation, either version 3 of the License, or
49 % (at your option) any later version.
50 %
51 % It is distributed in the hope that it will be useful,
52 % but WITHOUT ANY WARRANTY; without even the implied warranty of
53 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
54 % GNU General Public License for more details.
55 %
56 % If you did not received a copy of the GNU General Public License
57 % with this software, see <http://www.gnu.org/licenses/>.
58 %
59
60
61 n0=n0(:);
62 A0=zeros(nvar,nvar,nStates);
63 B0=A0;
64 b_shat = zeros(n0cumsum(end),nStates);
65 b_shat(tvinx,:) = reshape(b(1:tvtot),tvstate,nStates); % Time-varying parameter matrix.
66 b_shat(constinx,:) = repmat(b(tvtot+1:end), [1 nStates]); % Constant parameter matrix.
67 %
68 gmix = zeros(n0cumsum(end),nStates);
69 % Gradient matrix mixing time-varying and constant parameters.
70 % Rows of gmix, indexed by constinx, will not be used but instead treated
71 % different under "for kj=indxConst."
72 gtv = zeros(tvstate,nStates); % Gradient wrt time-varying parameters, which will be vectorized later.
73 gconst1 = zeros(constot,1); % The first gradient wrt constant parameters for the exponetial term.
74 gconst2=zeros(constot,1); % The second gradient wrt constant parameters for -T_klog|A0(k)|.
75
76
77 %---------------------------------------------------
78 % Time-varying situation. See p. 34a.
79 %---------------------------------------------------
80 %*** The derivative of the exponential term w.r.t. each free time-varying paramater.
81 for kj = 1:nvar % For all equations
82 lenbjs=length(n0cumsum(kj)+1:n0cumsum(kj+1));
83 bj = zeros(nStates*lenbjs,1);
84 for si=1:nStates
85 bj((si-1)*lenbjs+1:si*lenbjs) = b_shat(n0cumsum(kj)+1:n0cumsum(kj+1),si); % bj(si). See p.34a.
86 A0(:,kj,si) = Uistar{kj}((si-1)*nvar+1:si*nvar,:)*b_shat(n0cumsum(kj)+1:n0cumsum(kj+1),si);
87 end
88 gj = (Uibar{kj}'*Sgm0tldinvaveTV*Uibar{kj})*bj;
89 if find(kj==indxTV) % For time-varying equations.
90 for si=1:nStates
91 gmix(n0cumsum(kj)+1:n0cumsum(kj+1),si) = gj((si-1)*lenbjs+1:si*lenbjs);
92 end
93 end
94 end
95
96 %*** Add the derivative of -T_klog|A0(k)| w.r.t. each free time-varying paramater.
97 for si=1:nStates % See p.34a.
98 B0(:,:,si)=inv(A0(:,:,si)');
99 for ki = tvinx % Those from 1 to sum(q_i) that pertain to time-varying parameters.
100 n = max(find( (ki-n0cumsum)>0 )); % note, 1<=n<=nvar equations.
101 gmix(ki,si) = gmix(ki,si) - Tkave(si)*B0(:,n,si)'*Uistar{n}((si-1)*nvar+1:si*nvar,ki-n0cumsum(n)); % See p.34a.
102 end
103 end
104
105 %---------------------------------------------------
106 % Constant-parameter situation. See pp. 34b-34c.
107 %---------------------------------------------------
108 kcount = 0; % Counts.
109 for kj = indxConst % Equations
110 klocat=kcount+n0(kj); % Losition for the last constant element in the jth equation.
111 Bbar = repmat(eye(n0(kj)),[1 nStates]); % See p.34b.
112 lenbjs=length(n0cumsum(kj)+1:n0cumsum(kj+1));
113 bj = zeros(nStates*lenbjs,1);
114 for si=1:nStates
115 bj((si-1)*lenbjs+1:si*lenbjs) = b_shat(n0cumsum(kj)+1:n0cumsum(kj+1),si); % bj(si). See p.34a.
116 A0(:,kj,si) = Uistar{kj}((si-1)*nvar+1:si*nvar,:)*b_shat(n0cumsum(kj)+1:n0cumsum(kj+1),si);
117 end
118 gconst1(kcount+1:klocat) = Bbar*( (Uibar{kj}'*Sgm0tldinvaveConst*Uibar{kj})*bj );
119 kcount=klocat;
120 end
121 %*** Add the derivative of -T_klog|A0(k)| w.r.t. each free time-varying paramater.
122 kcount = 0; % Counts.
123 for ki = constinx % Those from 1 to sum(q_i) that pertain to constant parameters.
124 kcount=kcount+1;
125 n = max(find( (ki-n0cumsum)>0 )); % note, 1<=n<=nvar equations.
126 gi=0; % Initializes a gradient summed across the states. See p. 34c.
127 for si=1:nStates % See p.34c.
128 gi = gi - Tkave(si)*B0(:,n,si)'*Uistar{n}((si-1)*nvar+1:si*nvar,ki-n0cumsum(n)); % See p. 34a.
129 end
130 gconst2(kcount)=gi;
131 end
132
133
134 %---------------------------------------------------
135 % Conclusion.
136 %---------------------------------------------------
137 gtv = gmix(tvinx,:); % Extract the gradient wrt time-varying parameters.
138 gconst = gconst1+gconst2; % Gradient wrt to constant parameters.
139 %
140 g = [gtv(:);gconst]; % Vectorized the same way as b is vectorized.
141 badg = 0; % This term is used for Sims's csminwel program.