File: lyapunov_solver.m

package info (click to toggle)
dynare 4.5.7-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 49,408 kB
  • sloc: cpp: 84,998; ansic: 29,058; pascal: 13,843; sh: 4,833; objc: 4,236; yacc: 3,622; makefile: 2,278; lex: 1,541; python: 236; lisp: 69; xml: 8
file content (186 lines) | stat: -rw-r--r-- 5,939 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
function P=lyapunov_solver(T,R,Q,DynareOptions) % --*-- Unitary tests --*--
% function P=lyapunov_solver(T,R,Q,DynareOptions)
% Solves the Lyapunov equation P-T*P*T' = R*Q*R' arising in a state-space
% system, where P is the variance of the states
%
% Inputs
%   T               [double]    n*n matrix.
%   R               [double]    n*m matrix.
%   Q               [double]    m*m matrix.
%   DynareOptions   [structure] Dynare options
%
% Outputs
%   P               [double]    n*n matrix.
%
% Algorithms
%   Default, if none of the other algorithms is selected:
%       Reordered Schur decomposition (Bartels-Stewart algorithm)
%   DynareOptions.lyapunov_fp == 1
%       iteration-based fixed point algorithm
%   DynareOptions.lyapunov_db == 1
%       doubling algorithm
%   DynareOptions.lyapunov_srs == 1
%       Square-root solver for discrete-time Lyapunov equations (requires Matlab System Control toolbox
%       or Octave control package)

% Copyright (C) 2016-2017 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

if DynareOptions.lyapunov_fp == 1
    P = lyapunov_symm(T,R*Q'*R',DynareOptions.lyapunov_fixed_point_tol,DynareOptions.qz_criterium,DynareOptions.lyapunov_complex_threshold, 3, DynareOptions.debug);
elseif DynareOptions.lyapunov_db == 1
    [P, errorflag] = disclyap_fast(T,R*Q*R',DynareOptions.lyapunov_doubling_tol);
    if errorflag %use Schur-based method
        P = lyapunov_symm(T,R*Q*R',DynareOptions.lyapunov_fixed_point_tol,DynareOptions.qz_criterium,DynareOptions.lyapunov_complex_threshold, [], DynareOptions.debug);
    end
elseif DynareOptions.lyapunov_srs == 1
    % works only with Matlab System Control toolbox or Octave control package,
    if isoctave
        if ~user_has_octave_forge_package('control')
            error('lyapunov=square_root_solver not available; you must install the control package from Octave Forge')
        end
    else
        if ~user_has_matlab_license('control_toolbox')
            error('lyapunov=square_root_solver not available; you must install the control system toolbox')
        end
    end
    chol_Q = R*chol(Q,'lower');
    R_P = dlyapchol(T,chol_Q);
    P = R_P' * R_P;
else
    P = lyapunov_symm(T,R*Q*R',DynareOptions.lyapunov_fixed_point_tol,DynareOptions.qz_criterium,DynareOptions.lyapunov_complex_threshold, [], DynareOptions.debug);
end

%@test:1
%$ t = NaN(10,1);
%$ options_.lyapunov_complex_threshold = 1e-15;
%$ options_.qz_zero_threshold = 1e-6;
%$ options_.qz_criterium=1-options_.qz_zero_threshold;
%$ options_.lyapunov_fixed_point_tol = 1e-10;
%$ options_.lyapunov_doubling_tol = 1e-16;
%$ options_.debug=0;
%$
%$ n_small=8;
%$ m_small=10;
%$ T_small=randn(n_small,n_small);
%$ T_small=0.99*T_small/max(abs(eigs(T_small)));
%$ tmp2=randn(m_small,m_small);
%$ Q_small=tmp2*tmp2';
%$ R_small=randn(n_small,m_small);
%$
%$ n_large=9;
%$ m_large=11;
%$ T_large=randn(n_large,n_large);
%$ T_large=0.99*T_large/max(abs(eigs(T_large)));
%$ tmp2=randn(m_large,m_large);
%$ Q_large=tmp2*tmp2';
%$ R_large=randn(n_large,m_large);
%$
%$ % DynareOptions.lyapunov_fp == 1
%$ options_.lyapunov_fp = 1;
%$ try
%$    Pstar1_small = lyapunov_solver(T_small,R_small,Q_small,options_);
%$    Pstar1_large = lyapunov_solver(T_large,R_large,Q_large,options_);
%$    t(1) = 1;
%$ catch
%$    t(1) = 0;
%$ end
%$
%$ % Dynareoptions.lyapunov_db == 1
%$ options_.lyapunov_fp = 0;
%$ options_.lyapunov_db = 1;
%$ try
%$    Pstar2_small = lyapunov_solver(T_small,R_small,Q_small,options_);
%$    Pstar2_large = lyapunov_solver(T_large,R_large,Q_large,options_);
%$    t(2) = 1;
%$ catch
%$    t(2) = 0;
%$ end
%$
%$ % Dynareoptions.lyapunov_srs == 1
%$ if (isoctave && user_has_octave_forge_package('control')) || (~isoctave && user_has_matlab_license('control_toolbox'))
%$     options_.lyapunov_db = 0;
%$     options_.lyapunov_srs = 1;
%$     try
%$        Pstar3_small = lyapunov_solver(T_small,R_small,Q_small,options_);
%$        Pstar3_large = lyapunov_solver(T_large,R_large,Q_large,options_);
%$        t(3) = 1;
%$     catch
%$        t(3) = 0;
%$     end
%$ else
%$     t(3) = 1;
%$ end
%$
%$ % Standard
%$     options_.lyapunov_srs = 0;
%$ try
%$    Pstar4_small = lyapunov_solver(T_small,R_small,Q_small,options_);
%$    Pstar4_large = lyapunov_solver(T_large,R_large,Q_large,options_);
%$    t(4) = 1;
%$ catch
%$    t(4) = 0;
%$ end
%$
%$ % Test the results.
%$
%$ if max(max(abs(Pstar1_small-Pstar2_small)))>1e-8
%$    t(5) = 0;
%$ else
%$    t(5) = 1;
%$ end
%$
%$ if (isoctave && user_has_octave_forge_package('control')) || (~isoctave && user_has_matlab_license('control_toolbox'))
%$    if max(max(abs(Pstar1_small-Pstar3_small)))>1e-8
%$       t(6) = 0;
%$    else
%$       t(6) = 1;
%$    end
%$ else
%$    t(6) = 1;
%$ end
%$
%$ if max(max(abs(Pstar1_small-Pstar4_small)))>1e-8
%$    t(7) = 0;
%$ else
%$    t(7) = 1;
%$ end
%$
%$ if max(max(abs(Pstar1_large-Pstar2_large)))>1e-8
%$    t(8) = 0;
%$ else
%$    t(8) = 1;
%$ end
%$
%$ if (isoctave && user_has_octave_forge_package('control')) || (~isoctave && user_has_matlab_license('control_toolbox'))
%$    if max(max(abs(Pstar1_large-Pstar3_large)))>1e-8
%$       t(9) = 0;
%$    else
%$       t(9) = 1;
%$    end
%$ else
%$    t(9) = 1;
%$ end
%$
%$ if max(max(abs(Pstar1_large-Pstar4_large)))>1e-8
%$    t(10) = 0;
%$ else
%$    t(10) = 1;
%$ end
%$
%$ T = all(t);
%@eof:1