1function histvalf(fname)
2%function histvalf(fname)
3% Sets initial values for simulation using values contained in `fname`, a
4% file possibly created by a call to `smoother2histval`
5%
6% INPUTS
7%    fname:                       name of file containing initial values
8%
9% OUTPUTS
10%    none
11%
12% SPECIAL REQUIREMENTS
13%    none
14
15
16% Copyright (C) 2014-2019 Dynare Team
17%
18% This file is part of Dynare.
19%
20% Dynare is free software: you can redistribute it and/or modify
21% it under the terms of the GNU General Public License as published by
22% the Free Software Foundation, either version 3 of the License, or
23% (at your option) any later version.
24%
25% Dynare is distributed in the hope that it will be useful,
26% but WITHOUT ANY WARRANTY; without even the implied warranty of
27% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28% GNU General Public License for more details.
29%
30% You should have received a copy of the GNU General Public License
31% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
32
33global M_ oo_ ex0_
34
35if ~exist(fname, 'file')
36    error(['Can''t find datafile: ' fname ]);
37end
38
39M_.endo_histval = repmat(oo_.steady_state, 1, M_.maximum_endo_lag);
40
41% Also fill in oo_.exo_simul: necessary if we are in deterministic context,
42% since aux vars for lagged exo are not created in this case
43if isempty(oo_.exo_simul)
44    if isempty(ex0_)
45        oo_.exo_simul = repmat(oo_.exo_steady_state',M_.maximum_lag,1);
46    else
47        oo_.exo_simul = repmat(ex0_',M_.maximum_lag,1);
48    end
49end
50
51S = load(fname);
52
53outvars = fieldnames(S);
54
55for i = 1:length(outvars)
56    ov_ = outvars{i};
57    if ov_(end) == '_'
58        ov = ov_(1:end-1);
59        j = strmatch(ov, M_.endo_names, 'exact');
60        if isempty(j)
61            warning(['smoother2histval: output variable ' ov ' does not exist.'])
62        end
63    else
64        % Lagged endogenous or exogenous, search through aux vars
65        undidx = find(ov_ == '_', 1, 'last'); % Index of last underscore in name
66        ov = ov_(1:(undidx-1));
67        lead_lag = ov_((undidx+1):end);
68        lead_lag = regexprep(lead_lag,'l','-');
69        lead_lag = str2num(lead_lag);
70        j = [];
71        for i = 1:length(M_.aux_vars)
72            if M_.aux_vars(i).type ~= 1 && M_.aux_vars(i).type ~= 3
73                continue
74            end
75            if M_.aux_vars(i).type == 1
76                % Endogenous
77                orig_var = M_.endo_names{M_.aux_vars(i).orig_index};
78            else
79                % Exogenous
80                orig_var = M_.exo_names{M_.aux_vars(i).orig_index};
81            end
82            if strcmp(orig_var, ov) && M_.aux_vars(i).orig_lead_lag == lead_lag
83                j = M_.aux_vars(i).endo_index;
84            end
85        end
86        if isempty(j)
87            % There is no aux var corresponding to (orig_var, lead_lag).
88            % If this is an exogenous variable, then it means we should put
89            % the value in oo_.exo_simul (we are probably in deterministic
90            % context).
91            k = strmatch(ov, M_.exo_names);
92            if isempty(k)
93                warning(['smoother2histval: output variable ' ov '(' lead_lag ') does not exist.'])
94            else
95                oo_.exo_simul((M_.maximum_lag-M_.maximum_endo_lag+1):M_.maximum_lag, k) = S.(ov_);
96            end
97            continue
98        end
99    end
100    M_.endo_histval(j, :) = S.(ov_);
101end
102