1function dr=set_state_space(dr,DynareModel,DynareOptions)
2% Write the state space representation of the reduced form solution.
3
4%@info:
5%! @deftypefn {Function File} {[@var{dr} =} set_state_space (@var{dr},@var{DynareModel},@var{DynareOptions})
6%! @anchor{set_state_space}
7%! @sp 1
8%! Write the state space representation of the reduced form solution.
9%! @sp 2
10%! @strong{Inputs}
11%! @sp 1
12%! @table @ @var
13%! @item dr
14%! Matlab's structure describing decision and transition rules.
15%! @item DynareModel
16%! Matlab's structure describing the model (initialized by dynare, see @ref{M_})
17%! @item DynareOptions
18%! Matlab's structure describing the current options (initialized by dynare, see @ref{options_}).
19%! @end table
20%! @sp 2
21%! @strong{Outputs}
22%! @sp 1
23%! @table @ @var
24%! @item dr
25%! Matlab's structure describing decision and transition rules.
26%! @end table
27%! @sp 2
28%! @strong{This function is called by:}
29%! @sp 1
30%! @ref{check}, @ref{discretionary_policy_1}, @ref{dynare_estimation_init}, @ref{dyn_risky_steady_state_solver}, @ref{osr1}, @ref{partial_information/dr1_PI}, @ref{pea/pea_initialization}, @ref{stochastic_solvers}, @ref{stoch_simul}
31%! @sp 2
32%! @strong{This function calls:}
33%! @sp 2
34%! @end deftypefn
35%@eod:
36
37% Copyright (C) 1996-2017 Dynare Team
38%
39% This file is part of Dynare.
40%
41% Dynare is free software: you can redistribute it and/or modify
42% it under the terms of the GNU General Public License as published by
43% the Free Software Foundation, either version 3 of the License, or
44% (at your option) any later version.
45%
46% Dynare is distributed in the hope that it will be useful,
47% but WITHOUT ANY WARRANTY; without even the implied warranty of
48% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
49% GNU General Public License for more details.
50%
51% You should have received a copy of the GNU General Public License
52% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
53
54max_lead = DynareModel.maximum_endo_lead;
55max_lag = DynareModel.maximum_endo_lag;
56endo_nbr = DynareModel.endo_nbr;
57lead_lag_incidence = DynareModel.lead_lag_incidence;
58klen = max_lag + max_lead + 1;
59
60fwrd_var = find(lead_lag_incidence(max_lag+2:end,:))';
61if max_lag > 0
62    pred_var = find(lead_lag_incidence(1,:))';
63    both_var = intersect(pred_var,fwrd_var);
64    pred_var = setdiff(pred_var,both_var);
65    fwrd_var = setdiff(fwrd_var,both_var);
66    stat_var = setdiff([1:endo_nbr]',union(union(pred_var,both_var),fwrd_var));  % static variables
67else
68    pred_var = [];
69    both_var = [];
70    stat_var = setdiff([1:endo_nbr]',fwrd_var);
71end
72if DynareOptions.block
73    order_var = DynareModel.block_structure.variable_reordered;
74else
75    order_var = [ stat_var(:); pred_var(:); both_var(:); fwrd_var(:)];
76end
77inv_order_var(order_var) = (1:endo_nbr);
78
79% building kmask for z state vector in t+1
80if max_lag > 0
81    kmask = [];
82    if max_lead > 0
83        kmask = lead_lag_incidence(max_lag+2,order_var) ;
84    end
85    kmask = [kmask; lead_lag_incidence(1,order_var)] ;
86else
87    if max_lead==0 %%in this case lead_lag_incidence has no entry max_lag+2
88        error('Dynare currently does not allow to solve purely static models in a stochastic context.')
89    end
90    kmask = lead_lag_incidence(max_lag+2,order_var) ;
91end
92
93kmask = kmask';
94kmask = kmask(:);
95i_kmask = find(kmask);
96nd = nnz(kmask);           % size of the state vector
97kmask(i_kmask) = (1:nd);
98% auxiliary equations
99
100% composition of state vector
101% col 1: variable (index in DR-order);  col 2: lead/lag in z(t+1);
102% col 3: A cols for t+1 (D);            col 4: A cols for t (E)
103kstate = [ repmat([1:endo_nbr]',klen-1,1) kron([klen:-1:2]',ones(endo_nbr,1)) ...
104           zeros((klen-1)*endo_nbr,2)];
105kiy = flipud(lead_lag_incidence(:,order_var))';
106kiy = kiy(:);
107if max_lead > 0
108    kstate(1:endo_nbr,3) = kiy(1:endo_nbr)-nnz(lead_lag_incidence(max_lag+1,:));
109    kstate(kstate(:,3) < 0,3) = 0;
110    kstate(endo_nbr+1:end,4) = kiy(2*endo_nbr+1:end);
111else
112    kstate(:,4) = kiy(endo_nbr+1:end);
113end
114kstate = kstate(i_kmask,:);
115
116dr.order_var = order_var;
117dr.inv_order_var = inv_order_var';
118dr.kstate = kstate;
119
120dr.transition_auxiliary_variables = [];
121