| 1 |
function [G1cell_sw, err] = fn_msv_sw(G1cell_0, A11cell, A12cell, A21cell, A22cell, Hcell, P) |
| 2 |
%G1cell_sw = fn_msv_sw(G1cell_0, A11cell, A12cell, A21cell, A22cell, Hcell,P) |
| 3 |
% |
| 4 |
% Model: X_{t+1} = A11_{t+1} X_t + A12_{t+1} x_t + C_{t+1} episilon_{t+1} |
| 5 |
% E_t(H_{t+1} x_{t+1}) = A21_t X_t + A22_t x_t |
| 6 |
% Solution: x_t = G1_t X_t where G1_t is nx-by-nX. |
| 7 |
% |
| 8 |
%Output: |
| 9 |
% G1cell_sw: solution. |
| 10 |
% err: if the value > sqrt(eps), then not converged ==> no solution. |
| 11 |
%Inputs: |
| 12 |
% G1cell_0: initial guess (starting point) for the Svensson-William algorithm. |
| 13 |
% If G1cell_0 is empty, G1_0 will be randomly selected. |
| 14 |
% A11cell, A12cell, A21cell, A22cell, and Hcell are all ns-by-1 cells -- the model coefficients depending on regime. |
| 15 |
% In each cell, the dimensions must be |
| 16 |
% A11=zeros(nX,nX); A12=zeros(nX,nx); |
| 17 |
% A21=zeros(nx,nX); A22=zeros(nx,nx); |
| 18 |
% H=diag(ones(nx,1)); |
| 19 |
% P: ns-by-ns transition matrix with each column summing up to 1. |
| 20 |
% |
| 21 |
% See ZhaNotes on FWZ MSV solution, p. AA.1. For the SW solution, see (7) on p. AA.1. |
| 22 |
% |
| 23 |
% Copyright (C) 1997-2012 Tao Zha |
| 24 |
% |
| 25 |
% This free software: you can redistribute it and/or modify |
| 26 |
% it under the terms of the GNU General Public License as published by |
| 27 |
% the Free Software Foundation, either version 3 of the License, or |
| 28 |
% (at your option) any later version. |
| 29 |
% |
| 30 |
% It is distributed in the hope that it will be useful, |
| 31 |
% but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 32 |
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 33 |
% GNU General Public License for more details. |
| 34 |
% |
| 35 |
% If you did not received a copy of the GNU General Public License |
| 36 |
% with this software, see <http://www.gnu.org/licenses/>. |
| 37 |
% |
| 38 |
|
| 39 |
%--- Convergence criteria |
| 40 |
tol = 1.0e-09; |
| 41 |
maxiter = 1e+04; |
| 42 |
|
| 43 |
%--- Dimensions. |
| 44 |
ns = length(A11cell); %number of states (regimes) |
| 45 |
nX = size(A11cell{1},1); |
| 46 |
nx = size(A22cell{1},1); |
| 47 |
|
| 48 |
%--- Initialization. |
| 49 |
if isempty(G1cell_0) |
| 50 |
G1cell_0 = cell(ns,1); |
| 51 |
for si=1:ns |
| 52 |
G1cell_0{si} = 1000.0*randn(nx,nX); |
| 53 |
end |
| 54 |
end |
| 55 |
|
| 56 |
%====== Iterative procedure. ====== |
| 57 |
Gerr = zeros(ns,1); |
| 58 |
err = 1.0; |
| 59 |
iter = 1; |
| 60 |
while ( (iter<maxiter) && (err>tol) ) |
| 61 |
for j=1:ns |
| 62 |
M1 = zeros(nx,nx); |
| 63 |
M2 = zeros(nx,nX); |
| 64 |
for k=1:ns |
| 65 |
HGk = Hcell{k}*G1cell_0{k}; |
| 66 |
M1 = M1 + P(k,j)*HGk*A12cell{k}; |
| 67 |
M2 = M2 + P(k,j)*HGk*A11cell{k}; |
| 68 |
end % k loop |
| 69 |
Mden = A22cell{j} - M1; |
| 70 |
Mnum = M2 - A21cell{j}; |
| 71 |
G1cell_sw{j} = Mden\Mnum; |
| 72 |
Gerr(j)=norm(G1cell_sw{j} - G1cell_0{j}); |
| 73 |
G1cell_0{j} = G1cell_sw{j}; % Updated for the next iteration. |
| 74 |
end %j loop |
| 75 |
err=sum(Gerr); |
| 76 |
iter=iter+1; |
| 77 |
end % err loop |
| 78 |
|