| 1 |
function of = fn_a0sfreefun2(b,Uistar,Uibar,nvar,nStates,n0,n0cumsum,tvstate,tvtot,constot,... |
| 2 |
tvinx,constinx,indxTV,indxConst,Tkave,Sgm0tldinvaveConst,Sgm0tldinvaveTV) |
| 3 |
% Negative logPosterior function for regime-switching vectorized free A0 parameters, |
| 4 |
% which are b's in the WZ notation. The case of no asymmetric prior and no lag restrictions. |
| 5 |
% It improves fn_a0sfreefun2.m in several aspects: |
| 6 |
% (a) allows some equations to have constant parameters. |
| 7 |
% It differs from fn_a0cfreefun_tv.m in several aspects: |
| 8 |
% (a) does not deal with equations with only structural variances time varying; |
| 9 |
% (b) cannot deal with lag restrictions; |
| 10 |
% (c) only deal with the marginal distribution of A0_s. |
| 11 |
% Note: (1) columns correspond to equations; (2) s stands for state. |
| 12 |
% See Time-Varying BVAR NOTE pp.34-34c,40. |
| 13 |
% |
| 14 |
% b: sum(n0)*nStates-by-1 vector of free A0 parameters, vectorized from the sum(n0)-by-nStates matrix. |
| 15 |
% Uistar: cell(nvar,1). In each cell, nvar*nSates-by-qi orthonormal basis for the null of the ith |
| 16 |
% equation contemporaneous restriction matrix where qi is the number of free parameters. |
| 17 |
% With this transformation, we have ai = Ui*bi or Ui'*ai = bi where ai is a vector |
| 18 |
% of total original parameters and bi is a vector of free parameters. When no |
| 19 |
% restrictions are imposed, we have Ui = I. There must be at least one free |
| 20 |
% parameter left for the ith equation. See p.33. |
| 21 |
% Uibar: cell(nvar,1). In each cell, we have nvar*nStates-by-qi*nStates, rearranged |
| 22 |
% from Uistar or Ui. See p.33. |
| 23 |
% nvar: Number of endogeous variables. |
| 24 |
% nStates: NUmber of states. |
| 25 |
% n0: nvar-by-1, ith element represents the number of free A0 parameters in ith equation for each state. |
| 26 |
% n0cumsum: Equal to [0;cumsum(n0)]; |
| 27 |
% tvstate: Number of time-varying parameters for all equations for each state. |
| 28 |
% tvtot: Total number of time-varying parameters. |
| 29 |
% constot: Total number of constant parameters. |
| 30 |
% tvinx: Vectorized index to include time-varying parameters for all equations under *each* state. |
| 31 |
% constinx: Vectorized index to include constant parameters for all equations. |
| 32 |
% indxTV: Index of acending order for equations with time-varying parameters. |
| 33 |
% indxConst: Index of ascending order for equations with constant parameters. When [], all equations |
| 34 |
% are time varying; when [1:nvar], all equations have constant parameters. |
| 35 |
% Tkave: nStates-by-1 of sample sizes (excluding lags but including dummies if provided) for different states k, |
| 36 |
% averaged (ave) over E-step draws. For T_k. See p.40. |
| 37 |
% Sgm0tldinvaveConst |
| 38 |
% Sgm0tldinvaveTV |
| 39 |
% Sgm0tldinvave: nvar*nStates-by-nvar*nStates. The matrix inv(\Sigma~_0) averaged (ave) |
| 40 |
% over E-step draws. Same for all equations because of no asymmetric prior and no lag |
| 41 |
% restrictions. Resembles old SpH in the exponent term in posterior of A0, |
| 42 |
% but NOT divided by fss (T) yet. See p.40. |
| 43 |
%---------------- |
| 44 |
% of: Objective function (negative logPosterior). |
| 45 |
% |
| 46 |
% This function is called by tveml*.m which is an old program compared with szeml*.m. Thus, |
| 47 |
% use fn_a0cfreefun_tv.m if possible. |
| 48 |
% Tao Zha, August 2001. |
| 49 |
% |
| 50 |
% Copyright (C) 1997-2012 Tao Zha |
| 51 |
% |
| 52 |
% This free software: you can redistribute it and/or modify |
| 53 |
% it under the terms of the GNU General Public License as published by |
| 54 |
% the Free Software Foundation, either version 3 of the License, or |
| 55 |
% (at your option) any later version. |
| 56 |
% |
| 57 |
% It is distributed in the hope that it will be useful, |
| 58 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 59 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 60 |
% GNU General Public License for more details. |
| 61 |
% |
| 62 |
% If you did not received a copy of the GNU General Public License |
| 63 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 64 |
% |
| 65 |
|
| 66 |
|
| 67 |
n0=n0(:); |
| 68 |
A0=zeros(nvar,nvar,nStates); |
| 69 |
b_shat = zeros(n0cumsum(end),nStates); |
| 70 |
b_shat(tvinx,:) = reshape(b(1:tvtot),tvstate,nStates); % Time-varying parameter matrix. |
| 71 |
b_shat(constinx,:) = repmat(b(tvtot+1:end), [1 nStates]); % Constant parameter matrix. |
| 72 |
|
| 73 |
tra = 0.0; |
| 74 |
for kj = 1:nvar |
| 75 |
lenbjs=length(n0cumsum(kj)+1:n0cumsum(kj+1)); |
| 76 |
bj = zeros(nStates*lenbjs,1); |
| 77 |
a0j = zeros(nStates*nvar,1); % See p.33. |
| 78 |
for si=1:nStates |
| 79 |
bj((si-1)*lenbjs+1:si*lenbjs) = b_shat(n0cumsum(kj)+1:n0cumsum(kj+1),si); % bj(si). See p.34a. |
| 80 |
A0(:,kj,si) = Uistar{kj}((si-1)*nvar+1:si*nvar,:)*b_shat(n0cumsum(kj)+1:n0cumsum(kj+1),si); |
| 81 |
a0j((si-1)*nvar+1:si*nvar) = A0(:,kj,si); % a0j(si). See p.33. |
| 82 |
end |
| 83 |
%tra = tra + 0.5*a0j'*Sgm0tldinvave*a0j; % negative exponential term |
| 84 |
if find(kj==indxTV) % For time-varying equations. |
| 85 |
tra = tra+0.5*bj'*(Uibar{kj}'*Sgm0tldinvaveTV*Uibar{kj})*bj; |
| 86 |
else % % For constant parameter equations. |
| 87 |
tra = tra+0.5*bj'*(Uibar{kj}'*Sgm0tldinvaveConst*Uibar{kj})*bj; |
| 88 |
end |
| 89 |
end |
| 90 |
|
| 91 |
ada=0.0; |
| 92 |
for si=1:nStates % See p.34a. |
| 93 |
[A0l,A0u] = lu(A0(:,:,si)); |
| 94 |
ada = ada - Tkave(si)*sum(log(abs(diag(A0u)))); % negative log determinant of A0 raised to power T |
| 95 |
end |
| 96 |
|
| 97 |
of = ada + tra; |