| 1 |
function [g,badg] = fn_a0sfreegrad(b,Uistar,Uibar,nvar,nStates,n0,Tkave,Sgm0tldinvave) |
| 2 |
% [g,badg] = fn_a0sfreegrad(b,Uistar,Uibar,nvar,nStates,n0,Tkave,Sgm0tldinvave) |
| 3 |
% Analytical gradient for fn_a0sfreefun.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 TBVAR NOTE p.34a. |
| 7 |
% |
| 8 |
% b: sum(n0)*nStates-by-1 vector of free A0 parameters, vectorized from the sum(n0)-by-nStates matrix. |
| 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 |
% With this transformation, we have ai = Ui*bi or Ui'*ai = bi where ai is a vector |
| 12 |
% of total original parameters and bi is a vector of free parameters. When no |
| 13 |
% restrictions are imposed, we have Ui = I. There must be at least one free |
| 14 |
% 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 endogeous 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 |
% Tkave: nStates-by-1 of sample sizes (excluding lags but including dummies if provided) for different states k, |
| 21 |
% averaged (ave) over E-step draws. For T_k. See p.38. |
| 22 |
% Sgm0tldinvave: nvar*nStates-by-nvar*nStates. The matrix inv(\Sigma~_0) averaged (ave) |
| 23 |
% over E-step draws. Same for all equations because of no asymmetric prior and no lag |
| 24 |
% restrictions. Resembles old SpH in the exponent term in posterior of A0, |
| 25 |
% but NOT divided by fss (T) yet. See p.38. |
| 26 |
%---------------- |
| 27 |
% g: sum(n0)*nStates-by-1 analytical gradient for fn_a0sfreefun.m. |
| 28 |
% Vectorized frmo the sum(n0)-by-nStates matrix. |
| 29 |
% badg: 0, the value that is used in csminwel.m. |
| 30 |
% |
| 31 |
% Tao Zha, March 2001 |
| 32 |
% |
| 33 |
% Copyright (C) 1997-2012 Tao Zha |
| 34 |
% |
| 35 |
% This free software: you can redistribute it and/or modify |
| 36 |
% it under the terms of the GNU General Public License as published by |
| 37 |
% the Free Software Foundation, either version 3 of the License, or |
| 38 |
% (at your option) any later version. |
| 39 |
% |
| 40 |
% It is distributed in the hope that it will be useful, |
| 41 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 42 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 43 |
% GNU General Public License for more details. |
| 44 |
% |
| 45 |
% If you did not received a copy of the GNU General Public License |
| 46 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 47 |
% |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
n0=n0(:); |
| 52 |
A0=zeros(nvar,nvar,nStates); |
| 53 |
n0cum = [0;cumsum(n0)]; |
| 54 |
b=reshape(b,n0cum(end),nStates); |
| 55 |
|
| 56 |
g = zeros(n0cum(end),nStates); % which will be vectorized later. |
| 57 |
badg = 0; |
| 58 |
|
| 59 |
|
| 60 |
%*** The derivative of the exponential term w.r.t. each free paramater |
| 61 |
for kj = 1:nvar |
| 62 |
lenbjs=length(n0cum(kj)+1:n0cum(kj+1)); |
| 63 |
bj = zeros(nStates*lenbjs,1); |
| 64 |
for si=1:nStates |
| 65 |
bj((si-1)*lenbjs+1:si*lenbjs) = b(n0cum(kj)+1:n0cum(kj+1),si); % bj(si). See p.34a. |
| 66 |
A0(:,kj,si) = Uistar{kj}((si-1)*nvar+1:si*nvar,:)*b(n0cum(kj)+1:n0cum(kj+1),si); |
| 67 |
end |
| 68 |
gj = (Uibar{kj}'*Sgm0tldinvave*Uibar{kj})*bj; |
| 69 |
for si=1:nStates |
| 70 |
g(n0cum(kj)+1:n0cum(kj+1),si) = gj((si-1)*lenbjs+1:si*lenbjs); |
| 71 |
end |
| 72 |
end |
| 73 |
|
| 74 |
%*** Add the derivative of -T_klog|A0(k)| w.r.t. each free paramater |
| 75 |
for si=1:nStates % See p.34a. |
| 76 |
B=inv(A0(:,:,si)'); |
| 77 |
for ki = 1:n0cum(end) % from 1 to sum(q_i) |
| 78 |
n = max(find( (ki-n0cum)>0 )); % note, 1<=n<=nvar equations. |
| 79 |
g(ki,si) = g(ki,si) - Tkave(si)*B(:,n)'*Uistar{n}((si-1)*nvar+1:si*nvar,ki-n0cum(n)); % See p.34a. |
| 80 |
end |
| 81 |
end |
| 82 |
g = g(:); % vectorized the same way as b is vectorized. |