File: dynare_solve.m

package info (click to toggle)
dynare 6.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,648 kB
  • sloc: cpp: 79,109; ansic: 28,917; objc: 12,430; yacc: 4,528; pascal: 1,993; lex: 1,441; sh: 1,129; python: 634; makefile: 626; lisp: 163; xml: 18
file content (341 lines) | stat: -rw-r--r-- 14,782 bytes parent folder | download | duplicates (2)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
function [x, errorflag, fvec, fjac, errorcode] = dynare_solve(f, x, maxit, tolf, tolx, options, varargin)

% Solves a nonlinear system of equations, f(x) = 0 with n unknowns
% and n equations.
%
% INPUTS
% - f            [char, fhandle]    function to be solved
% - x            [double]           n×1 vector, initial guess.
% - options      [struct]           Dynare options, aka options_.
% - varargin                        list of additional arguments to be passed to func.
%
% OUTPUTS
% - x            [double]           n×1 vector, solution.
% - errorflag    [logical]          scalar, true iff the model can not be solved.
% - fvec         [double]           n×1 vector, function value at x (f(x), used for debugging when errorflag is true).
% - fjac         [double]           n×n matrix, Jacobian value at x (J(x), used for debugging when errorflag is true).
% - errorcode    [integer]          scalar.
%
% REMARKS
% Interpretation of the error code depends on the algorithm, except if value of errorcode is
%
%        -10  -> System of equation ill-behaved at the initial guess (Inf, Nans or complex numbers).
%        -11  -> Initial guess is a solution of the system of equations.

% Copyright © 2001-2023 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 <https://www.gnu.org/licenses/>.

jacobian_flag = options.jacobian_flag; % true iff Jacobian is returned by f routine (as a second output argument).

errorflag = false; % Let's be optimistic!
nn = size(x,1);

% Keep a copy of the initial guess.
x0 = x;

% Get status of the initial guess (default values?)
if any(x)
    % The current initial guess is not the default for all the variables.
    idx = find(x);      % Indices of the variables with default initial guess values.
    in0 = length(idx);
else
    % The current initial guess is the default for all the variables.
    idx = transpose(1:nn);
    in0 = nn;
end

% checking initial values
if jacobian_flag
    [fvec, fjac] = feval(f, x, varargin{:});
    wrong_initial_guess_flag = false;
    if ~all(isfinite(fvec)) || any(isinf(fjac(:))) || any(isnan((fjac(:)))) || any(~isreal(fvec)) || any(~isreal(fjac(:)))
        if ~ismember(options.solve_algo,[10,11]) && ~any(isnan(fvec)) && max(abs(fvec))< tolf
            % return if initial value solves the problem except if a mixed complementarity problem is to be solved (complementarity conditions may not be satisfied)
            % max([NaN, 0])=0, so explicitly exclude the case where fvec contains a NaN
            errorcode = -11;
            return;
        end
        if options.solve_randomize_initial_guess
            if any(~isreal(fvec)) || any(~isreal(fjac(:)))
                disp_verbose('dynare_solve: starting value results in complex values. Randomize initial guess...', options.verbosity)
            else
                disp_verbose('dynare_solve: starting value results in nonfinite/NaN value. Randomize initial guess...', options.verbosity)
            end
            % Let's try random numbers for the variables initialized with the default value.
            wrong_initial_guess_flag = true;
            % First try with positive numbers.
            tentative_number = 0;
            while wrong_initial_guess_flag && tentative_number<=in0*10
                tentative_number = tentative_number+1;
                x(idx) = rand(in0, 1)*10;
                [fvec, fjac] = feval(f, x, varargin{:});
                wrong_initial_guess_flag = ~all(isfinite(fvec)) || any(isinf(fjac(:))) || any(isnan((fjac(:)))) || any(~isreal(fvec)) || any(~isreal(fjac(:)));
            end
            % If all previous attempts failed, try with real numbers.
            tentative_number = 0;
            while wrong_initial_guess_flag && tentative_number<=in0*10
                tentative_number = tentative_number+1;
                x(idx) = randn(in0, 1)*10;
                [fvec, fjac] = feval(f, x, varargin{:});
                wrong_initial_guess_flag = ~all(isfinite(fvec)) || any(isinf(fjac(:))) || any(isnan((fjac(:)))) || any(~isreal(fvec)) || any(~isreal(fjac(:)));
            end
            % Last tentative, ff all previous attempts failed, try with negative numbers.
            tentative_number = 0;
            while wrong_initial_guess_flag && tentative_number<=in0*10
                tentative_number = tentative_number+1;
                x(idx) = -rand(in0, 1)*10;
                [fvec, fjac] = feval(f, x, varargin{:});
                wrong_initial_guess_flag = ~all(isfinite(fvec)) || any(isinf(fjac(:))) || any(isnan((fjac(:)))) || any(~isreal(fvec)) || any(~isreal(fjac(:)));
            end
        end
    end
else
    fvec = feval(f, x, varargin{:});
    fjac = zeros(nn, nn);
    if ~ismember(options.solve_algo,[10,11]) && ~any(isnan(fvec)) && max(abs(fvec)) < tolf
        % return if initial value solves the problem except if a mixed complementarity problem is to be solved (complementarity conditions may not be satisfied)
        % max([NaN, 0])=0, so explicitly exclude the case where fvec contains a NaN
        errorcode = -11;
        return;
    end
    wrong_initial_guess_flag = false;
    if ~all(isfinite(fvec))
        % Let's try random numbers for the variables initialized with the default value.
        wrong_initial_guess_flag = true;
        % First try with positive numbers.
        tentative_number = 0;
        while wrong_initial_guess_flag && tentative_number<=in0*10
            tentative_number = tentative_number+1;
            x(idx) = rand(in0, 1)*10;
            fvec = feval(f, x, varargin{:});
            wrong_initial_guess_flag = ~all(isfinite(fvec));
        end
        % If all previous attempts failed, try with real numbers.
        tentative_number = 0;
        while wrong_initial_guess_flag && tentative_number<=in0*10
            tentative_number = tentative_number+1;
            x(idx) = randn(in0, 1)*10;
            fvec = feval(f, x, varargin{:});
            wrong_initial_guess_flag = ~all(isfinite(fvec));
        end
        % Last tentative, ff all previous attempts failed, try with negative numbers.
        tentative_number = 0;
        while wrong_initial_guess_flag && tentative_number<=in0*10
            tentative_number = tentative_number+1;
            x(idx) = -rand(in0, 1)*10;
            fvec = feval(f, x, varargin{:});
            wrong_initial_guess_flag = ~all(isfinite(fvec));
        end
    end
end

% Exit with error if no initial guess has been found.
if wrong_initial_guess_flag
    errorcode = -10;
    errorflag = true;
    x = x0;
    return
end

if options.solve_algo == 0
    if ~isoctave
        if ~user_has_matlab_license('optimization_toolbox')
            error('You can''t use solve_algo=0 since you don''t have MATLAB''s Optimization Toolbox')
        end
    end
    if isoctave
        options4fsolve = optimset('fsolve');
    else
        options4fsolve = optimoptions('fsolve');
    end
    if isoctave
        options4fsolve.MaxFunEvals = 50000;
        options4fsolve.MaxIter = maxit;
        options4fsolve.TolFun = tolf;
        options4fsolve.TolX = tolx;
        if jacobian_flag
            options4fsolve.Jacobian = 'on';
        else
            options4fsolve.Jacobian = 'off';
        end
    else
        options4fsolve.MaxFunctionEvaluations = 50000;
        options4fsolve.MaxIterations = maxit;
        options4fsolve.FunctionTolerance = tolf;
        options4fsolve.StepTolerance = tolx;
        options4fsolve.SpecifyObjectiveGradient = jacobian_flag;
    end
    %% NB: The Display option is accepted but not honoured under Octave (as of version 7)
    if options.debug
        options4fsolve.Display = 'final';
    else
        options4fsolve.Display = 'off';
    end
    %% This one comes last, so that the user can override Dynare
    if ~isempty(options.fsolve_options)
        if isoctave
            eval(['options4fsolve = optimset(options4fsolve,' options.fsolve_options ');']);
        else
            eval(['options4fsolve = optimoptions(options4fsolve,' options.fsolve_options ');']);
        end
    end

    if ~isoctave
        [x, fvec, errorcode, ~, fjac] = fsolve(f, x, options4fsolve, varargin{:});
    else
        % Under Octave, use a wrapper, since fsolve() does not have a 4th arg
        if ischar(f)
            f2 = str2func(f);
        else
            f2 = f;
        end
        [x, fvec, errorcode, ~, fjac] = fsolve(@(x) f2(x, varargin{:}), x, options4fsolve);
    end
    if errorcode==1
        errorflag = false;
    elseif errorcode>1
        if max(abs(fvec)) > tolf
            errorflag = true;
        else
            errorflag = false;
        end
    else
        errorflag = true;
    end
elseif ismember(options.solve_algo, [1, 12])
    %% NB: It is the responsibility of the caller to deal with the block decomposition if solve_algo=12
    [x, errorflag, errorcode] = solve1(f, x, 1:nn, 1:nn, jacobian_flag, options.gstep, tolf, tolx, maxit, [], options.debug, varargin{:});
    [fvec, fjac] = feval(f, x, varargin{:});
elseif options.solve_algo==9
    [x, errorflag, errorcode] = trust_region(f, x, 1:nn, 1:nn, jacobian_flag, options.gstep, tolf, tolx, maxit, options.trust_region_initial_step_bound_factor, options.debug, varargin{:});
    [fvec, fjac] = feval(f, x, varargin{:});
elseif ismember(options.solve_algo, [2, 4])
    if options.solve_algo == 2
        solver = @solve1;
    else
        solver = @trust_region;
    end
    if ~jacobian_flag
        fjac = zeros(nn,nn) ;
        dh = max(abs(x), options.gstep(1)*ones(nn,1))*eps^(1/3);
        for j = 1:nn
            xdh = x ;
            xdh(j) = xdh(j)+dh(j) ;
            fjac(:,j) = (feval(f, xdh, varargin{:})-fvec)./dh(j) ;
        end
    end
    [j1,j2,r,s] = dmperm(fjac);
    if options.debug
        disp(['DYNARE_SOLVE (solve_algo=2|4): number of blocks = ' num2str(length(r)-1)]);
    end
    for i=length(r)-1:-1:1
        blocklength = r(i+1)-r(i);
        j = r(i):r(i+1)-1;
        blockcolumns=s(i+1)-s(i);
        if blockcolumns ~= blocklength
            %non-square-block in DM; check whether initial value is solution
            [fval_check, fjac] = feval(f, x, varargin{:});
            if norm(fval_check(j1(j))) < tolf
                errorflag = false;
                errorcode = 0;
                continue
            end
        end
        if blockcolumns>=blocklength
            %(under-)determined block
            [x, errorflag, errorcode] = solver(f, x, j1(j), j2(j), jacobian_flag, ...
                options.gstep, ...
                tolf, options.solve_tolx, maxit, ...
                options.trust_region_initial_step_bound_factor, ...
                options.debug, varargin{:});
        else
            fprintf('\nDYNARE_SOLVE (solve_algo=2|4): the Dulmage-Mendelsohn decomposition returned a non-square block. This means that the Jacobian is singular. You may want to try another value for solve_algo.\n')
            %overdetermined block
            errorflag = true;
            errorcode = 0;
        end
        if errorflag
            return
        end
    end
    fvec = feval(f, x, varargin{:});
    if max(abs(fvec))>tolf
        disp_verbose('Call solver on the full nonlinear problem.',options.verbosity)
        [x, errorflag, errorcode] = solver(f, x, 1:nn, 1:nn, jacobian_flag, ...
                                           options.gstep, tolf, options.solve_tolx, maxit, ...
                                           options.trust_region_initial_step_bound_factor, ...
                                           options.debug, varargin{:});
    end
    [fvec, fjac] = feval(f, x, varargin{:});
elseif options.solve_algo==3
    if jacobian_flag
        [x, errorcode] = csolve(f, x, f, tolf, maxit, varargin{:});
    else
        [x, errorcode] = csolve(f, x, [], tolf, maxit, varargin{:});
    end
    if errorcode==0
        errorflag = false;
    else
        errorflag = true;
    end
    [fvec, fjac] = feval(f, x, varargin{:});
elseif options.solve_algo==10
    % LMMCP
    olmmcp = options.lmmcp;
    [x, fvec, errorcode, ~, fjac] = lmmcp(f, x, olmmcp.lb, olmmcp.ub, olmmcp, varargin{:});
    eq_to_check=find(isfinite(olmmcp.lb) | isfinite(olmmcp.ub));
    eq_to_ignore=eq_to_check(x(eq_to_check,:)<=olmmcp.lb(eq_to_check)+eps | x(eq_to_check,:)>=olmmcp.ub(eq_to_check)-eps);
    fvec(eq_to_ignore)=0;
    if errorcode==1
        errorflag = false;
    else
        errorflag = true;
    end
elseif options.solve_algo == 11
    % PATH mixed complementary problem
    % PATH linear mixed complementary problem
    if ~exist('mcppath')
        error(['PATH can''t be provided with Dynare. You need to install it ' ...
               'yourself and add its location to Matlab/Octave path before ' ...
               'running Dynare'])
    end
    omcppath = options.mcppath;
    global mcp_data
    mcp_data.func = f;
    mcp_data.args = varargin;
    try
        x = pathmcp(x,omcppath.lb,omcppath.ub,'mcp_func',omcppath.A,omcppath.b,omcppath.t,omcppath.mu0);
    catch
        errorflag = true;
    end
    errorcode = nan; % There is no error code for this algorithm, as PATH is closed source it is unlikely we can fix that.
    eq_to_check=find(isfinite(omcppath.lb) | isfinite(omcppath.ub));
    eq_to_ignore=eq_to_check(x(eq_to_check,:)<=omcppath.lb(eq_to_check)+eps | x(eq_to_check,:)>=omcppath.ub(eq_to_check)-eps);
    fvec(eq_to_ignore)=0;
elseif ismember(options.solve_algo, [13, 14])
    %% NB: It is the responsibility of the caller to deal with the block decomposition if solve_algo=14
    if ~jacobian_flag
        error('DYNARE_SOLVE: option solve_algo=13 needs computed Jacobian')
    end
    [x, errorflag, errorcode] = block_trust_region(f, x, tolf, options.solve_tolx, maxit, ...
                                                   options.trust_region_initial_step_bound_factor, ...
                                                   options.solve_algo == 13, ... % Only block-decompose with Dulmage-Mendelsohn for 13, not for 14
                                                   options.debug, varargin{:});
    [fvec, fjac] = feval(f, x, varargin{:});
else
    error('DYNARE_SOLVE: option solve_algo must be one of [0,1,2,3,4,9,10,11,12,13,14]')
end