Debian Codegraph @

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

raw
1 function of = fn_a0sfreefun(b,Uistar,Uibar,nvar,nStates,n0,Tkave,Sgm0tldinvave)
2 % of = fn_a0sfreefun(b,Uistar,Uibar,nvar,nStates,n0,Tkave,Sgm0tldinvave)
3 % Negative logPosterior function for regime-switching squeesed free A0 parameters,
4 % which are b's in the WZ notation. 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 pp.34-34a,38.
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*nSates-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. This is NOT used for this function, but serves as an argument
17 % to keep it compatible with fn_a0sfreegrad.m which uses it.
18 % nvar: Number of endogeous variables.
19 % nStates: NUmber of states.
20 % n0: nvar-by-1, ith element represents the number of free A0 parameters in ith equation for each state.
21 % Tkave: nStates-by-1 of sample sizes (excluding lags but including dummies if provided) for different states k,
22 % averaged (ave) over E-step draws. For T_k. See p.38.
23 % Sgm0tldinvave: nvar*nStates-by-nvar*nStates. The matrix inv(\Sigma~_0) averaged (ave)
24 % over E-step draws. Same for all equations because of no asymmetric prior and no lag
25 % restrictions. Resembles old SpH in the exponent term in posterior of A0,
26 % but NOT divided by fss (T) yet. See p.38.
27 %----------------
28 % of: Objective function (negative logPosterior).
29 %
30 % This function is a special case of fn_a0sfreefun2.m.
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 tra = 0.0;
57 for kj = 1:nvar
58 lenbjs=length(n0cum(kj)+1:n0cum(kj+1));
59 bj = zeros(nStates*lenbjs,1);
60 a0j = zeros(nStates*nvar,1); % See p.33.
61 for si=1:nStates
62 bj((si-1)*lenbjs+1:si*lenbjs) = b(n0cum(kj)+1:n0cum(kj+1),si); % bj(si). See p.34a.
63 A0(:,kj,si) = Uistar{kj}((si-1)*nvar+1:si*nvar,:)*b(n0cum(kj)+1:n0cum(kj+1),si);
64 a0j((si-1)*nvar+1:si*nvar) = A0(:,kj,si); % a0j(si). See p.33.
65 end
66 %tra = tra + 0.5*a0j'*Sgm0tldinvave*a0j; % negative exponential term
67 tra = tra+0.5*bj'*(Uibar{kj}'*Sgm0tldinvave*Uibar{kj})*bj;
68 end
69
70 ada=0.0;
71 for si=1:nStates % See p.34a.
72 [A0l,A0u] = lu(A0(:,:,si));
73 ada = ada - Tkave(si)*sum(log(abs(diag(A0u)))); % negative log determinant of A0 raised to power T
74 end
75
76 of = ada + tra;