Do not assign variables to base workspace by default

Related to https://git.dynare.org/Dynare/preprocessor/-/issues/95
covariance-quadratic-approximation
Johannes Pfeifer 2023-12-11 10:39:17 +01:00 committed by Sébastien Villemot
parent 3faaffacc6
commit 48380a1370
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
26 changed files with 175 additions and 72 deletions

View File

@ -8,6 +8,20 @@
Dynare misc commands
####################
.. matcomm:: send_endogenous_variables_to_workspace
Puts the simulation results for the endogenous variables stored in ``oo_.endo_simul``
into vectors with the same name as the respective variables into the base workspace.
.. matcomm:: send_exogenous_variables_to_workspace
Puts the simulation results for the exogenous variables stored in ``oo_.exo_simul``
into vectors with the same name as the respective variables into the base workspace.
.. matcomm:: send_irfs_to_workspace
Puts the IRFs stored in ``oo_.irfs`` into vectors with the same name into the base workspace.
.. command:: prior_function(OPTIONS);
Executes a user-defined function on parameter draws from the prior

View File

@ -4880,14 +4880,10 @@ Computing the stochastic solution
If the ``periods`` option is present, sets ``oo_.skewness``,
``oo_.kurtosis``, and ``oo_.endo_simul`` (see
:mvar:`oo_.endo_simul`), and also saves the simulated variables in
MATLAB/Octave vectors of the global workspace with the same name
as the endogenous variables.
:mvar:`oo_.endo_simul`).
If option ``irf`` is different from zero, sets ``oo_.irfs`` (see
below) and also saves the IRFs in MATLAB/Octave vectors of the
global workspace (this latter way of accessing the IRFs is
deprecated and will disappear in a future version).
below).
If the option ``contemporaneous_correlation`` is different from
``0``, sets ``oo_.contemporaneous_correlation``, which is
@ -5096,10 +5092,13 @@ Computing the stochastic solution
For example, ``oo_.irfs.gnp_ea`` contains the effect on ``gnp`` of
a one-standard deviation shock on ``ea``.
.. matcomm:: get_irf ('EXOGENOUS_NAME' [, 'ENDOGENOUS_NAME']... );
.. matcomm:: IRF_MATRIX=get_irf ('EXOGENOUS_NAME' [, 'ENDOGENOUS_NAME']... );
|br| Given the name of an exogenous variables, returns the IRFs for the
requested endogenous variable(s), as they are stored in ``oo_.irfs``.
|br| Given the name of an exogenous variable, returns the IRFs for the
requested endogenous variable(s) (as they are stored in ``oo_.irfs``) in the output
``IRF_MATRIX``. The periods are stored along the first dimension, with the steady
state in the first row. The variables are stored along the second dimension. If no
endogenous variables were specified, the matrix contains all variables stored in ``oo_.irfs``.
The approximated solution of a model takes the form of a set of
decision rules or transition equations expressing the current value of

View File

@ -1,20 +1,19 @@
function y0 = get_irf(exo,varargin)
% function x = get_irf(exoname, vname1, vname2, ...)
function y0 = get_irf(exoname,varargin)
% function x = get_irf(exoname, varargin)
% returns IRF to individual exogenous for a list of variables and adds the
% steady state
%
% INPUTS:
% exo: exo variable name
% exoname: exo variable name
% vname1, vname2, ... : list of variable names
%
% OUTPUTS
% x: irf matrix [time x number of variables]
% y0: irf matrix [time x number of variables]
%
% SPECIAL REQUIREMENTS
% none
% Copyright © 2019 Dynare Team
% Copyright © 2019-2023 Dynare Team
%
% This file is part of Dynare.
%
@ -33,14 +32,44 @@ function y0 = get_irf(exo,varargin)
global M_ oo_
ys_ = [oo_.steady_state];
y0=zeros(length(oo_.irfs.([varargin{1} '_' exo]))+1,length(varargin));
[i_var,nvar] = varlist_indices(varargin,M_.endo_names);
for j=1:nvar
% mfys = strmatch(varargin{j},lgy_,'exact');
y0(:,j)=[0; oo_.irfs.([ varargin{j} '_' exo ])']+ys_(i_var(j));
if isfield(oo_,'irfs')
irf_fields=fieldnames(oo_.irfs);
else
error('get_irf: No IRFs detected in oo_')
end
exo_matches=find(endsWith(irf_fields,['_' exoname]));
if isempty(exo_matches)
error('get_irf: No IRFs for shock %s detected in oo_',exoname)
else
if nargin>1
endo_cell={};
i_var=[];
for var_iter=1:length(varargin)
temp=startsWith(irf_fields(exo_matches),varargin{var_iter});
if isempty(temp)
fprintf('get_irf: No IRF for variable %s detected in oo_',varargin{var_iter})
else
endo_cell=[endo_cell,varargin{var_iter}];
i_var=[i_var,strmatch(varargin(var_iter),M_.endo_names,'exact')];
end
end
else
endo_cell={};
i_var=[];
for var_iter=1:length(exo_matches)
endo_cell=[endo_cell,irf_fields{var_iter}(1:end-length(exoname)-1)];
i_var=[i_var,strmatch(endo_cell(end),M_.endo_names,'exact')];
end
end
end
ys_ = [oo_.steady_state];
nvars=length(endo_cell);
y0=zeros(length(oo_.irfs.([ endo_cell{1} '_' exoname ]))+1,nvars);
for j=1:nvars
y0(:,j)=[0; oo_.irfs.([ endo_cell{j} '_' exoname ])']+ys_(i_var(j));
end

View File

@ -1,5 +1,5 @@
function [ts, oo_] = extended_path(initialconditions, samplesize, exogenousvariables, options_, M_, oo_)
% [ts, oo_] = extended_path(initialconditions, samplesize, exogenousvariables, options_, M_, oo_)
function [ts,oo_] = extended_path(initialconditions, samplesize, exogenousvariables, options_, M_, oo_)
% [ts,oo_] = extended_path(initialconditions, samplesize, exogenousvariables, options_, M_, oo_)
% Stochastic simulation of a non linear DSGE model using the Extended Path method (Fair and Taylor 1983). A time
% series of size T is obtained by solving T perfect foresight models.
%
@ -13,7 +13,7 @@ function [ts, oo_] = extended_path(initialconditions, samplesize, exogenousvaria
%
% OUTPUTS
% o ts [dseries] m*samplesize array, the simulations.
% o results [cell]
% o results [struct] results structure
%
% ALGORITHM
%
@ -106,9 +106,4 @@ if any(isnan(endogenous_variables_paths(:)))
end
ts = dseries(transpose(endogenous_variables_paths), initial_period, M_.endo_names);
oo_.endo_simul = transpose(ts.data);
assignin('base', 'Simulated_time_series', ts);
if ~nargout || nargout<2
assignin('base', 'oo_', oo_);
end
oo_.endo_simul = transpose(ts.data);

View File

@ -1,4 +1,4 @@
function oo_=perfect_foresight_solver(M_, options_, oo_, no_error_if_learnt_in_is_present, marginal_linearization_previous_raw_sims)
function [oo_, ts]=perfect_foresight_solver(M_, options_, oo_, no_error_if_learnt_in_is_present, marginal_linearization_previous_raw_sims)
% Computes deterministic simulations
%
% INPUTS
@ -16,6 +16,7 @@ function oo_=perfect_foresight_solver(M_, options_, oo_, no_error_if_learnt_in_i
%
% OUTPUTS
% oo_ [structure] storing the results
% ts [dseries] final simulation paths
%
% ALGORITHM
%
@ -273,8 +274,6 @@ if ~isempty(per_block_status)
oo_.deterministic_simulation.block = per_block_status;
end
dyn2vec(M_, oo_, options_);
if isfield(oo_, 'initval_series') && ~isempty(oo_.initval_series)
initial_period = oo_.initval_series.dates(1)+(M_.orig_maximum_lag-1);
elseif ~isdates(options_.initial_period) && isnan(options_.initial_period)
@ -290,8 +289,6 @@ if isfield(oo_, 'initval_series') && ~isempty(oo_.initval_series)
ts = merge(oo_.initval_series{names{:}}, ts);
end
assignin('base', 'Simulated_time_series', ts);
oo_.gui.ran_perfect_foresight = oo_.deterministic_simulation.status;

View File

@ -0,0 +1,25 @@
function send_exogenous_variables_to_workspace()
% send_exogenous_variables_to_workspace()
% Saves all the endogenous variables in matlab's workspace.
% Copyright © 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/>.
global M_ oo_
for idx = 1:M_.exo_nbr
assignin('base', M_.exo_names{idx}, oo_.exo_simul(:,idx))
end

View File

@ -0,0 +1,27 @@
function send_irfs_to_workspace()
% Saves all the IRFs in MATLAB's workspace.
% Copyright © 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/>.
global oo_
if isfield(oo_,'irfs')
irf_fields=fieldnames(oo_.irfs);
for irf_iter = 1:size(irf_fields,1)
assignin('base',irf_fields{irf_iter},oo_.irfs.(irf_fields{irf_iter})');
end
end

View File

@ -197,9 +197,6 @@ if options_.periods > 0 && ~PI_PCL_solver
end
end
[oo_.endo_simul, oo_.exo_simul] = simult(y0,oo_.dr,M_,options_);
if ~options_.minimal_workspace
dyn2vec(M_, oo_, options_);
end
end
if ~options_.nomoments
@ -267,8 +264,6 @@ if options_.irf
mylistTeX = [];
end
for j = 1:nvar
assignin('base',[M_.endo_names{i_var(j)} '_' M_.exo_names{i}],...
y(i_var(j),:)');
oo_.irfs.([M_.endo_names{i_var(j)} '_' M_.exo_names{i}]) = y(i_var(j),:);
if max(abs(y(i_var(j),:))) >= options_.impulse_responses.plot_threshold
irfs = cat(1,irfs,y(i_var(j),:));

@ -1 +1 @@
Subproject commit 0f397f40afd096b062010a374464aae1567ef623
Subproject commit 638c49d96ee491a66ba775ffc79a1d6afbe8acfc

View File

@ -62,6 +62,7 @@ var e_m; stderr 0.005;
end;
stoch_simul(order=1,periods=200, irf=0,nomoments,noprint);
send_endogenous_variables_to_workspace;
save('my_data.mat','gp_obs','gy_obs');
estimated_params;

View File

@ -46,7 +46,7 @@ verbatim;
% Information arriving in period 1 (temp shock now)
oo_.exo_simul(2,1) = 1.2;
perfect_foresight_solver(true);
oo_=perfect_foresight_solver(M_, options_, oo_, true);
% Information arriving in period 2 (temp shock now + permanent shock in future)
oo_.exo_simul(3,1) = 1.3;
@ -59,7 +59,7 @@ saved_endo = oo_.endo_simul(:, 1);
saved_exo = oo_.exo_simul(1, :);
oo_.endo_simul = oo_.endo_simul(:, 2:end);
oo_.exo_simul = oo_.exo_simul(2:end, :);
perfect_foresight_solver(true);
oo_=perfect_foresight_solver(M_, options_, oo_, true);
oo_.endo_simul = [ saved_endo oo_.endo_simul ];
oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
@ -74,7 +74,7 @@ saved_endo = oo_.endo_simul(:, 1:2);
saved_exo = oo_.exo_simul(1:2, :);
oo_.endo_simul = oo_.endo_simul(:, 3:end);
oo_.exo_simul = oo_.exo_simul(3:end, :);
perfect_foresight_solver(true);
oo_=perfect_foresight_solver(M_, options_, oo_, true);
oo_.endo_simul = [ saved_endo oo_.endo_simul ];
oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
@ -90,7 +90,7 @@ saved_endo = oo_.endo_simul(:, 1:5);
saved_exo = oo_.exo_simul(1:5, :);
oo_.endo_simul = oo_.endo_simul(:, 6:end);
oo_.exo_simul = oo_.exo_simul(6:end, :);
perfect_foresight_solver(true);
oo_=perfect_foresight_solver(M_, options_, oo_,true);
oo_.endo_simul = [ saved_endo oo_.endo_simul ];
oo_.exo_simul = [ saved_exo; oo_.exo_simul ];

View File

@ -45,7 +45,7 @@ verbatim;
% Information arriving in period 1 (temp shock now)
oo_.exo_simul(2,1) = 1.2;
perfect_foresight_solver(true);
oo_=perfect_foresight_solver(M_, options_, oo_, true);
% Information arriving in period 2 (temp shock now + permanent shock in future)
oo_.exo_simul(3,1) = 1.3;
@ -57,7 +57,7 @@ saved_endo = oo_.endo_simul(:, 1);
saved_exo = oo_.exo_simul(1, :);
oo_.endo_simul = oo_.endo_simul(:, 2:end);
oo_.exo_simul = oo_.exo_simul(2:end, :);
perfect_foresight_solver(true);
oo_=perfect_foresight_solver(M_, options_, oo_, true);
oo_.endo_simul = [ saved_endo oo_.endo_simul ];
oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
@ -71,7 +71,7 @@ saved_endo = oo_.endo_simul(:, 1:2);
saved_exo = oo_.exo_simul(1:2, :);
oo_.endo_simul = oo_.endo_simul(:, 3:end);
oo_.exo_simul = oo_.exo_simul(3:end, :);
perfect_foresight_solver(true);
oo_=perfect_foresight_solver(M_, options_, oo_, true);
oo_.endo_simul = [ saved_endo oo_.endo_simul ];
oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
@ -87,7 +87,7 @@ saved_endo = oo_.endo_simul(:, 1:5);
saved_exo = oo_.exo_simul(1:5, :);
oo_.endo_simul = oo_.endo_simul(:, 6:end);
oo_.exo_simul = oo_.exo_simul(6:end, :);
perfect_foresight_solver(true);
oo_=perfect_foresight_solver(M_, options_, oo_, true);
oo_.endo_simul = [ saved_endo oo_.endo_simul ];
oo_.exo_simul = [ saved_exo; oo_.exo_simul ];

View File

@ -95,7 +95,7 @@ verbatim;
% Information arriving in period 1 (temp shock now and tomorrow)
oo_.exo_simul(2:3,1) = 1.2;
perfect_foresight_solver(true);
oo_=perfect_foresight_solver(M_, options_, oo_, true);
% Information arriving in period 2 (temp shock now + permanent shock in future)
oo_.exo_simul(3,1) = 1.3;
@ -108,7 +108,7 @@ saved_endo = oo_.endo_simul(:, 1);
saved_exo = oo_.exo_simul(1, :);
oo_.endo_simul = oo_.endo_simul(:, 2:end);
oo_.exo_simul = oo_.exo_simul(2:end, :);
perfect_foresight_solver(true);
oo_=perfect_foresight_solver(M_, options_, oo_, true);
oo_.endo_simul = [ saved_endo oo_.endo_simul ];
oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
@ -125,7 +125,7 @@ saved_endo = oo_.endo_simul(:, 1:2);
saved_exo = oo_.exo_simul(1:2, :);
oo_.endo_simul = oo_.endo_simul(:, 3:end);
oo_.exo_simul = oo_.exo_simul(3:end, :);
perfect_foresight_solver(true);
oo_=perfect_foresight_solver(M_, options_, oo_, true);
oo_.endo_simul = [ saved_endo oo_.endo_simul ];
oo_.exo_simul = [ saved_exo; oo_.exo_simul ];
@ -141,7 +141,7 @@ saved_endo = oo_.endo_simul(:, 1:5);
saved_exo = oo_.exo_simul(1:5, :);
oo_.endo_simul = oo_.endo_simul(:, 6:end);
oo_.exo_simul = oo_.exo_simul(6:end, :);
perfect_foresight_solver(true);
oo_=perfect_foresight_solver(M_, options_, oo_, true);
oo_.endo_simul = [ saved_endo oo_.endo_simul ];
oo_.exo_simul = [ saved_exo; oo_.exo_simul ];

View File

@ -31,7 +31,8 @@ perfect_foresight_solver;
if ~oo_.deterministic_simulation.status
error('Perfect foresight simulation failed')
end
send_endogenous_variables_to_workspace;
if max(abs(y-[1; exp(cumprod([1; rho*ones(9, 1)]))]))>options_.dynatol.x
if max(abs(y'-[1; exp(cumprod([1; rho*ones(9, 1)]))]))>options_.dynatol.x
error('Wrong solution!')
end

View File

@ -79,6 +79,7 @@ end;
% Simulate data
stoch_simul(order=@{orderApp},pruning,nodisplay,nomoments,periods=250);
send_endogenous_variables_to_workspace;
save('RBC_MoM_data_@{orderApp}.mat', options_.varobs{:} );
pause(1);

View File

@ -35,6 +35,7 @@ varobs n c iv;
% Simulate data
stoch_simul(order=@{orderApp},pruning,nodisplay,nomoments,periods=250,TeX);
send_endogenous_variables_to_workspace;
save('RBC_MoM_data_@{orderApp}.mat', options_.varobs{:} );
pause(1);

View File

@ -33,6 +33,16 @@ end;
stoch_simul(periods=2000,irf=0);
verbatim;
w=oo_.endo_simul(strmatch('w',M_.endo_names,'exact'),:)';
x=oo_.endo_simul(strmatch('x',M_.endo_names,'exact'),:)';
y=oo_.endo_simul(strmatch('y',M_.endo_names,'exact'),:)';
z=oo_.endo_simul(strmatch('z',M_.endo_names,'exact'),:)';
dw=oo_.endo_simul(strmatch('dw',M_.endo_names,'exact'),:)';
dx=oo_.endo_simul(strmatch('dx',M_.endo_names,'exact'),:)';
dy=oo_.endo_simul(strmatch('dy',M_.endo_names,'exact'),:)';
end;
plot([w x y z]);
save data_algo.mat w x y z dw dx dy;

View File

@ -85,5 +85,6 @@ shocks;
end;
stoch_simul(order=1,irf=20,periods=500);
send_endogenous_variables_to_workspace;
save data_Pinf_Pstar.mat v1 v2 v3;

View File

@ -58,7 +58,7 @@ perfect_foresight_solver(lmmcp, maxit=200, no_homotopy);
if ~oo_.deterministic_simulation.status
error('Convergence not obtained')
end
send_endogenous_variables_to_workspace;
n = 40;
figure(2);

View File

@ -130,6 +130,7 @@ if max(max(abs(struct2array(forecasts.cond.Mean)-struct2array(conditional_foreca
end
stoch_simul(loglinear,order=1,periods=100000);
send_endogenous_variables_to_workspace;
if abs(mean(y)-0.0776)>0.02
error('Simulations are wrong')
end

View File

@ -73,15 +73,16 @@ total_var_filtered=diag(oo_.var);
oo_filtered_all_shocks=oo_;
stoch_simul(order=1,nofunctions,hp_filter=0,periods=2500000,nomoments);
send_endogenous_variables_to_workspace;
options_.nomoments=0;
oo_unfiltered_all_shocks=oo_;
[junk, y_filtered]=sample_hp_filter(y,1600);
[junk, c_filtered]=sample_hp_filter(c,1600);
[junk, k_filtered]=sample_hp_filter(k,1600);
[junk, a_filtered]=sample_hp_filter(a,1600);
[junk, h_filtered]=sample_hp_filter(h,1600);
[junk, b_filtered]=sample_hp_filter(b,1600);
[junk, y_filtered]=sample_hp_filter(y',1600);
[junk, c_filtered]=sample_hp_filter(c',1600);
[junk, k_filtered]=sample_hp_filter(k',1600);
[junk, a_filtered]=sample_hp_filter(a',1600);
[junk, h_filtered]=sample_hp_filter(h',1600);
[junk, b_filtered]=sample_hp_filter(b',1600);
verbatim;
total_std_all_shocks_filtered_sim=std([y_filtered c_filtered k_filtered a_filtered h_filtered b_filtered]);
@ -108,14 +109,15 @@ total_var_filtered_one_shock=diag(oo_.var);
oo_filtered_one_shock=oo_;
stoch_simul(order=1,nofunctions,hp_filter=0,periods=2500000,nomoments);
send_endogenous_variables_to_workspace;
oo_unfiltered_one_shock=oo_;
[junk, y_filtered]=sample_hp_filter(y,1600);
[junk, c_filtered]=sample_hp_filter(c,1600);
[junk, k_filtered]=sample_hp_filter(k,1600);
[junk, a_filtered]=sample_hp_filter(a,1600);
[junk, h_filtered]=sample_hp_filter(h,1600);
[junk, b_filtered]=sample_hp_filter(b,1600);
[junk, y_filtered]=sample_hp_filter(y',1600);
[junk, c_filtered]=sample_hp_filter(c',1600);
[junk, k_filtered]=sample_hp_filter(k',1600);
[junk, a_filtered]=sample_hp_filter(a',1600);
[junk, h_filtered]=sample_hp_filter(h',1600);
[junk, b_filtered]=sample_hp_filter(b',1600);
verbatim;
total_std_one_shock_filtered_sim=std([y_filtered c_filtered k_filtered a_filtered h_filtered b_filtered]);

View File

@ -20,6 +20,7 @@ options_.SpectralDensity.trigger=1;
options_.bandpass.indicator=0;
stoch_simul(order=1,nofunctions,hp_filter=0,irf=0,periods=1000000,filtered_theoretical_moments_grid=2048);
send_endogenous_variables_to_workspace;
white_noise_sample=white_noise;

View File

@ -12,7 +12,7 @@ var ca = 0.01^2;
end;
stoch_simul(order=3,periods=200, irf=0);
send_endogenous_variables_to_workspace;
save('my_data.mat','q','ca');
estimation(datafile='my_data.mat',order=2,mode_compute=0,mh_replic=0,filter_algorithm=sis,nonlinear_filter_initialization=2

View File

@ -12,6 +12,7 @@ var ca = 0.01^2;
end;
stoch_simul(order=3,periods=200, irf=0);
send_endogenous_variables_to_workspace;
save('my_data_MCMC.mat','ca','b');

View File

@ -10,6 +10,7 @@ var nnu = 0.03^2;
end;
stoch_simul(order=3,periods=200, irf=0, nomoments, nofunctions);
send_endogenous_variables_to_workspace;
save('my_data.mat','q','ca');

View File

@ -11,6 +11,7 @@ var q = 0.01^2;
end;
stoch_simul(order=3,periods=200, irf=0);
send_endogenous_variables_to_workspace;
save('my_data.mat','q','ca');