1function plan = flip_plan(plan, exogenous, endogenous, expectation_type, date, value)
2% Adds to the forecast scenario a conditional forecast shock (the path of an endogenous variable is constrained and the values compatible values of the related exogenous variable will be compued)
3%
4% INPUTS
5%  o plan                 [structure]       A structure describing the different shocks and the implied variables, the date of the shocks and the path of the shock (forecast scenario).
6%                                           The plan structure is created by the functions init_plan, basic_plan and flip_plan
7%  o exogenous            [string]          A string containing the name of the endogenous variable with a constrained path.
8%  o endogenous           [string]          A string containing the name of the exogenous. This exogenous variable will be en endogenous variable when the conditional forecast will be perform.
9%  o expectation_type     [string]          A string indicating the type of expectation: 'surprise' for an unexpected shock, and 'perfect_foresight' for a perfectly anticpated shock.
10%  o date                 [dates]           The period of the shock
11%  o value                [array of double] A vector of double containing the constrined path on the endogenous variable
12%
13%
14% OUTPUTS
15%  plan                   [structure]        Returns a structure containing the updated forecast scenario.
16%
17%
18% Copyright (C) 2013-2017 Dynare Team
19%
20% This file is part of Dynare.
21%
22% Dynare is free software: you can redistribute it and/or modify
23% it under the terms of the GNU General Public License as published by
24% the Free Software Foundation, either version 3 of the License, or
25% (at your option) any later version.
26%
27% Dynare is distributed in the hope that it will be useful,
28% but WITHOUT ANY WARRANTY; without even the implied warranty of
29% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30% GNU General Public License for more details.
31%
32% You should have received a copy of the GNU General Public License
33% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
34if ~ischar(expectation_type) || size(expectation_type,1) ~= 1
35    error(['in flip_plan the fourth argument should be a string containing the simulation type (''perfect_foresight'' or ''surprise'')']);
36end
37exogenous = strtrim(exogenous);
38ix = find(strcmp(exogenous, plan.endo_names));
39if  isempty(ix)
40    error(['in flip_plan the second argument ' exogenous ' is not an endogenous variable']);
41end
42endogenous = strtrim(endogenous);
43iy = find(strcmp(endogenous, plan.exo_names));
44if  isempty(iy)
45    error(['in flip_plan the third argument ' endogenous ' is not an exogenous variable']);
46end
47sdate = length(date);
48if sdate > 1
49    if date(1) < plan.date(1) || date(end) > plan.date(end)
50        error(['in flip_plan the fifth argument (date='  date ') must lay inside the plan.date ' plan.date]);
51    end
52else
53    if date < plan.date(1) || date > plan.date(end)
54        error(['in flip_plan the fifth argument (date='  date ') must lay iside the plan.date ' plan.date]);
55    end
56end
57if ~isempty(plan.shock_vars_)
58    common_var = find(iy == plan.shock_vars_);
59    if ~isempty(common_var)
60        common_date = intersect(date, plan.shock_date_{common_var});
61        if ~isempty(common_date)
62            if common_date.length > 1
63                the_dates = [cell2mat(strings(common_date(1))) ':' cell2mat(strings(common_date(end)))];
64            else
65                the_dates = cell2mat(strings(common_date));
66            end
67            error(['Impossible case: ' plan.exo_names{plan.shock_vars_(common_var)} ' is used both as a shock and as an endogenous variable to control the path of ' plan.endo_names{ix} ' at the dates ' the_dates]);
68        end
69    end
70end
71i_ix = find(ix == plan.constrained_vars_);
72if isempty(i_ix)
73    if isempty(plan.constrained_vars_)
74        plan.constrained_vars_ = ix;
75        plan.options_cond_fcst_.controlled_varexo  = iy;
76        if strcmp(expectation_type, 'perfect_foresight')
77            plan.constrained_perfect_foresight_ = 1;
78        else
79            plan.constrained_perfect_foresight_ = 0;
80        end
81    else
82        plan.constrained_vars_ = [plan.constrained_vars_ ; ix];
83        plan.options_cond_fcst_.controlled_varexo  = [plan.options_cond_fcst_.controlled_varexo ; iy];
84        if strcmp(expectation_type, 'perfect_foresight')
85            plan.constrained_perfect_foresight_ = [plan.constrained_perfect_foresight_ ; 1];
86        else
87            plan.constrained_perfect_foresight_ = [plan.constrained_perfect_foresight_ ; 0];
88        end
89    end
90    plan.constrained_date_{length(plan.constrained_date_) + 1} = date;
91    plan.constrained_str_date_{length(plan.constrained_str_date_) + 1} = strings(date);
92    plan.constrained_int_date_{length(plan.constrained_int_date_) + 1} = date - plan.date(1) + 1;
93    plan.constrained_paths_{length(plan.constrained_paths_) + 1} = value;
94elseif plan.options_cond_fcst_.controlled_varexo(i_ix) == iy % same exogenous and endogenous hard tune
95[plan.constrained_str_date_{i_ix}, i1, i2] = union(strings(date), plan.constrained_str_date_{i_ix});
96plan.constrained_date_{i_ix} = [date(i1) plan.constrained_date_{i_ix}(i2)];
97plan.constrained_int_date_{i_ix} = [date(i1) - plan.date(1) + 1; plan.constrained_int_date_{i_ix}(i2)];
98plan.constrained_paths_{i_ix} = [value(i1)'; plan.constrained_paths_{i_ix}(i2)];
99else
100    error(['impossible case you have two conditional forecasts:\n - one involving ' plan.endo_names{plan.options_cond_fcst_.controlled_varexo(i_ix),:} ' as control and ' plan_exo_names{plan.constrained_vars_(ix_)} ' as constrined endogenous\n - the other involving  ' plan.endo_names{plan.options_cond_fcst_.controlled_varexo(iy),:} ' as control and ' plan_exo_names{plan.constrained_vars_(ix)} ' as constrined endogenous\n']);
101end
102