1function simulation = simul_static_model(samplesize, innovations)
2
3% Simulates a stochastic static model (with arbitrary precision).
4%
5% INPUTS
6% - samplesize          [integer]     scalar, number of periods for the simulation.
7% - innovations         [dseries]     innovations to be used for the simulation.
8%
9% OUTPUTS
10% - simulation          [dseries]     Simulated endogenous and exogenous variables.
11%
12% REMARKS
13% [1] The innovations used for the simulation are saved in DynareOutput.exo_simul, and the resulting paths for the endogenous
14%     variables are saved in DynareOutput.endo_simul.
15% [2] The last input argument is not mandatory. If absent we use random draws and rescale them with the informations provided
16%     through the shocks block.
17
18% Copyright (C) 2019 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/>.
34
35global M_ options_ oo_
36
37if M_.maximum_lag
38    error('%s.mod has lagged variables, but it should be a static model.', M_.fname)
39end
40
41if M_.maximum_lead
42    error('%s.mod has leaded variables, but it should be a static model.', M_.fname)
43end
44
45% Set innovations.
46if nargin<2 || isempty(innovations)
47    % Set the covariance matrix of the structural innovations.
48    variances = diag(M_.Sigma_e);
49    number_of_shocks = length(M_.Sigma_e);
50    positive_var_indx = find(variances>0);
51    effective_number_of_shocks = length(positive_var_indx);
52    covariance_matrix = M_.Sigma_e(positive_var_indx,positive_var_indx);
53    covariance_matrix_upper_cholesky = chol(covariance_matrix);
54    % Set seed to its default state.
55    if options_.bnlms.set_dynare_seed_to_default
56        set_dynare_seed('default');
57    end
58    % Simulate structural innovations.
59    switch options_.bnlms.innovation_distribution
60      case 'gaussian'
61        oo_.bnlms.shocks = randn(samplesize, effective_number_of_shocks)*covariance_matrix_upper_cholesky;
62      otherwise
63        error('%s distribution for the structural innovations is not (yet) implemented!', options_.bnlms.innovation_distribution)
64    end
65    % Put the simulated innovations in DynareOutput.exo_simul.
66    oo_.exo_simul = zeros(samplesize, number_of_shocks);
67    oo_.exo_simul(:,positive_var_indx) = oo_.bnlms.shocks;
68    innovations = [];
69else
70    if innovations.nobs<samplesize
71        error('Time span in third argument is too short (should not be less than %s, the value of the second argument)', num2str(samplesize))
72    end
73    % Set array holding innovations values.
74    Innovations = zeros(samplesize, M_.exo_nbr);
75    exonames = M_.exo_names;
76    for i=1:M_.exo_nbr
77        if ismember(exonames{i}, innovations.name)
78            Innovations(:,i) = innovations{exonames{i}}.data(1:samplesize);
79        else
80            dprintf('Exogenous variable %s is not available in third argument, default value is zero.', exonames{i});
81        end
82    end
83    oo_.exo_simul = Innovations;
84end
85
86staticmodel = sprintf('%s.static', M_.fname);
87
88% Simulations (call a Newton-like algorithm for each period).
89for t=1:samplesize
90    y = zeros(M_.endo_nbr, 1);
91    [oo_.endo_simul(:,t), info] = dynare_solve(staticmodel, y, options_, oo_.exo_simul(t,:), M_.params);
92    if info
93        error('Newton failed!')
94    end
95end
96
97ysim = oo_.endo_simul(1:M_.orig_endo_nbr,:);
98xsim = oo_.exo_simul;
99
100initperiod = dates('1Y');
101if isdseries(innovations)
102    initperiod = innovations.dates(1);
103end
104
105simulation = [dseries(ysim', initperiod, M_.endo_names(1:M_.orig_endo_nbr)), dseries(xsim, initperiod, M_.exo_names)];