File: extended_path_homotopy.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 (139 lines) | stat: -rw-r--r-- 4,948 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
function [info_convergence, endo_simul] = extended_path_homotopy(endo_simul, exo_simul, M_, options_, oo_, pfm, ep, order, algo, method, debug)

% Copyright © 2016-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/>.


endo_simul0 = endo_simul;
if ismember(method, [1, 2])
    noconvergence = true;
    iteration = 0;
    weight = .1;
    maxiter = 100;
    increase_flag = false;
    increase_factor = 1.2;
    decrease_factor = 1.1;
    state = false(5,1);
    oldweight = weight;
    while noconvergence
        iteration = iteration + 1;
        oo_.endo_simul = endo_simul;
        oo_.endo_simul(:,1) = oo_.steady_state + weight*(endo_simul0(:,1) - oo_.steady_state);
        oo_.exo_simul = bsxfun(@plus, weight*exo_simul, (1-weight)*transpose(oo_.exo_steady_state));
        if order==0
            [endo_simul_new, success] = perfect_foresight_solver_core(oo_.endo_simul, oo_.exo_simul, oo_.steady_state, oo_.exo_steady_state, M_, options_);
        else
            switch(algo)
              case 0
                [flag, endo_simul_new] = ...
                    solve_stochastic_perfect_foresight_model(endo_simul, exo_simul, pfm, ep.stochastic.quadrature.nodes, ep.stochastic.order);
              case 1
                [flag, endo_simul_new] = ...
                    solve_stochastic_perfect_foresight_model_1(endo_simul, exo_simul, options_, pfm, ep.stochastic.order);
            end
        end
        if isequal(order, 0)
            % Logical variable flag is false iff the solver fails.
            flag = success;
        else
            % Fix convention issue on the value of flag.
            flag = ~flag;
        end
        if debug
            dprintf('%u\t %1.8f\t %u', iteration, weight, flag)
        end
        state(2:end) = state(1:end-1);
        state(1) = flag;
        if flag
            if isequal(weight, 1)
                noconvergence = false;
                break
            end
            if all(state)
                increase_factor = 1+(increase_factor-1)*1.1;
                state = false(size(state));
            end
            oldweight = weight;
            weight = min(weight*increase_factor, 1);
            increase_flag = true;
            endo_simul = endo_simul_new;
        else
            if increase_flag
                weight = oldweight + (weight-oldweight)/100;
            else
                weight = min(weight/decrease_factor, 1);
            end
        end
        if iteration>maxiter
            break
        end
        if weight<1e-9
            break
        end
    end
    info_convergence = ~noconvergence;
end

if isequal(method, 3) || (isequal(method, 2) && noconvergence)
    if isequal(method, 2)
        endo_simul = endo_simul0;
    end
    weights = 0:(1/1000):1;
    noconvergence = true;
    index = 1;
    jndex = 0;
    nweights = length(weights);
    while noconvergence
        weight = weights(index);
        oo_.endo_simul = endo_simul;
        oo_.exo_simul = bsxfun(@plus, weight*exo_simul, (1-weight)*transpose(oo_.exo_steady_state));
        if order==0
            [endo_simul_new, success] = perfect_foresight_solver_core(oo_.endo_simul, oo_.exo_simul, oo_.steady_state, oo_.exo_steady_state, M_, options_);
        else
            switch(algo)
              case 0
                [flag, endo_simul_new] = ...
                    solve_stochastic_perfect_foresight_model(endo_simul, exo_simul, pfm, ep.stochastic.quadrature.nodes, ep.stochastic.order);
              case 1
                [flag, endo_simul_new] = ...
                    solve_stochastic_perfect_foresight_model_1(endo_simul, exo_simul, options_, pfm, ep.stochastic.order);
            end
        end
        if isequal(order, 0)
            % Logical variable flag is false iff the solver fails.
            flag = success;
        else
            % Fix convention issue on the value of flag.
            flag = ~flag;
        end
        if debug
            dprintf('%u\t %1.8f\t %u', index, weight, flag)
        end
        if flag
            jndex = index;
            if isequal(weight, 1)
                noconvergence = false;
                continue
            end
            index = index+1;
            endo_simul = endo_simul_new;
        else
            break
        end
    end
    info_convergence = ~noconvergence;
end