Drop the dynare_simul_ MEX in favour of the new Fortran k_order_simul MEX
parent
74d070c216
commit
06fd42c19b
|
@ -319,8 +319,7 @@ Copyright: 2010 Yannick Kalantzis
|
||||||
2019 Dynare Team
|
2019 Dynare Team
|
||||||
License: GPL-3+
|
License: GPL-3+
|
||||||
|
|
||||||
Files: mex/sources/dynare_simul_/dynare_simul_.cc
|
Files: mex/sources/gensylv/gensylv.cc
|
||||||
mex/sources/gensylv/gensylv.cc
|
|
||||||
Copyright: 2005-2011 Ondra Kamenik
|
Copyright: 2005-2011 Ondra Kamenik
|
||||||
2019 Dynare Team
|
2019 Dynare Team
|
||||||
License: GPL-3+
|
License: GPL-3+
|
||||||
|
|
|
@ -17,7 +17,7 @@ function y_=simult_(M_,options_,y0,dr,ex_,iorder)
|
||||||
% SPECIAL REQUIREMENTS
|
% SPECIAL REQUIREMENTS
|
||||||
% none
|
% none
|
||||||
|
|
||||||
% Copyright (C) 2001-2020 Dynare Team
|
% Copyright (C) 2001-2021 Dynare Team
|
||||||
%
|
%
|
||||||
% This file is part of Dynare.
|
% This file is part of Dynare.
|
||||||
%
|
%
|
||||||
|
@ -58,9 +58,8 @@ if options_.k_order_solver && ~options_.pruning % Call dynare++ routines.
|
||||||
'consistent with the one used for computing the decision rules'])
|
'consistent with the one used for computing the decision rules'])
|
||||||
end
|
end
|
||||||
y_start=y_(:,1); %store first period required for output
|
y_start=y_(:,1); %store first period required for output
|
||||||
y_ = dynare_simul_(iorder,M_.nstatic,M_.npred,M_.nboth,M_.nfwrd,exo_nbr, ...
|
y_ = k_order_simul(iorder,M_.nstatic,M_.npred,M_.nboth,M_.nfwrd,exo_nbr, ...
|
||||||
y_start(dr.order_var,:),ex_',M_.Sigma_e,options_.DynareRandomStreams.seed, ...
|
y_start(dr.order_var,:),ex_',dr.ys(dr.order_var),dr);
|
||||||
dr.ys(dr.order_var),dr);
|
|
||||||
y_(dr.order_var,:) = y_;
|
y_(dr.order_var,:) = y_;
|
||||||
y_=[y_start y_];
|
y_=[y_start y_];
|
||||||
else
|
else
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
mex_PROGRAMS = dynare_simul_
|
|
||||||
|
|
||||||
dynare_simul__CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/../../../dynare++/sylv/cc -I$(top_srcdir)/../../../dynare++/tl/cc -I$(top_srcdir)/../../../dynare++/kord -I$(top_srcdir)/../../../dynare++/utils/cc $(CPPFLAGS_MATIO)
|
|
||||||
|
|
||||||
dynare_simul__CXXFLAGS = $(AM_CXXFLAGS) $(THREAD_CXXFLAGS)
|
|
||||||
|
|
||||||
dynare_simul__LDFLAGS = $(AM_LDFLAGS) $(LDFLAGS_MATIO)
|
|
||||||
dynare_simul__LDADD = ../libdynare++/libdynare++.a $(LIBADD_MATIO)
|
|
||||||
|
|
||||||
nodist_dynare_simul__SOURCES = dynare_simul_.cc
|
|
||||||
|
|
||||||
BUILT_SOURCES = $(nodist_dynare_simul__SOURCES)
|
|
||||||
CLEANFILES = $(nodist_dynare_simul__SOURCES)
|
|
||||||
|
|
||||||
%.cc: $(top_srcdir)/../../sources/dynare_simul_/%.cc
|
|
||||||
$(LN_S) -f $< $@
|
|
|
@ -2,9 +2,9 @@ ACLOCAL_AMFLAGS = -I ../../../m4
|
||||||
|
|
||||||
SUBDIRS = mjdgges kronecker bytecode block_kalman_filter sobol perfect_foresight_problem num_procs block_trust_region disclyap_fast
|
SUBDIRS = mjdgges kronecker bytecode block_kalman_filter sobol perfect_foresight_problem num_procs block_trust_region disclyap_fast
|
||||||
|
|
||||||
# libdynare++ must come before gensylv, k_order_perturbation, dynare_simul_
|
# libdynare++ must come before gensylv and k_order_perturbation
|
||||||
if ENABLE_MEX_DYNAREPLUSPLUS
|
if ENABLE_MEX_DYNAREPLUSPLUS
|
||||||
SUBDIRS += libdynare++ gensylv libkorder dynare_simul_ k_order_perturbation k_order_welfare local_state_space_iterations libkordersim folded_to_unfolded_dr local_state_space_iteration_fortran k_order_simul k_order_mean
|
SUBDIRS += libdynare++ gensylv libkorder k_order_perturbation k_order_welfare local_state_space_iterations libkordersim folded_to_unfolded_dr local_state_space_iteration_fortran k_order_simul k_order_mean
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if ENABLE_MEX_MS_SBVAR
|
if ENABLE_MEX_MS_SBVAR
|
||||||
|
|
|
@ -155,7 +155,6 @@ AC_CONFIG_FILES([Makefile
|
||||||
libkorder/Makefile
|
libkorder/Makefile
|
||||||
k_order_perturbation/Makefile
|
k_order_perturbation/Makefile
|
||||||
k_order_welfare/Makefile
|
k_order_welfare/Makefile
|
||||||
dynare_simul_/Makefile
|
|
||||||
kalman_steady_state/Makefile
|
kalman_steady_state/Makefile
|
||||||
ms_sbvar/Makefile
|
ms_sbvar/Makefile
|
||||||
block_kalman_filter/Makefile
|
block_kalman_filter/Makefile
|
||||||
|
|
|
@ -1,2 +0,0 @@
|
||||||
include ../mex.am
|
|
||||||
include ../../dynare_simul_.am
|
|
|
@ -2,9 +2,9 @@ ACLOCAL_AMFLAGS = -I ../../../m4
|
||||||
|
|
||||||
SUBDIRS = mjdgges kronecker bytecode block_kalman_filter sobol perfect_foresight_problem num_procs block_trust_region disclyap_fast
|
SUBDIRS = mjdgges kronecker bytecode block_kalman_filter sobol perfect_foresight_problem num_procs block_trust_region disclyap_fast
|
||||||
|
|
||||||
# libdynare++ must come before gensylv, k_order_perturbation, dynare_simul_
|
# libdynare++ must come before gensylv and k_order_perturbation
|
||||||
if ENABLE_MEX_DYNAREPLUSPLUS
|
if ENABLE_MEX_DYNAREPLUSPLUS
|
||||||
SUBDIRS += libdynare++ gensylv libkorder dynare_simul_ k_order_perturbation k_order_welfare local_state_space_iterations libkordersim folded_to_unfolded_dr local_state_space_iteration_fortran k_order_simul
|
SUBDIRS += libdynare++ gensylv libkorder k_order_perturbation k_order_welfare local_state_space_iterations libkordersim folded_to_unfolded_dr local_state_space_iteration_fortran k_order_simul
|
||||||
endif
|
endif
|
||||||
|
|
||||||
if ENABLE_MEX_MS_SBVAR
|
if ENABLE_MEX_MS_SBVAR
|
||||||
|
|
|
@ -156,7 +156,6 @@ AC_CONFIG_FILES([Makefile
|
||||||
libkorder/Makefile
|
libkorder/Makefile
|
||||||
k_order_perturbation/Makefile
|
k_order_perturbation/Makefile
|
||||||
k_order_welfare/Makefile
|
k_order_welfare/Makefile
|
||||||
dynare_simul_/Makefile
|
|
||||||
kalman_steady_state/Makefile
|
kalman_steady_state/Makefile
|
||||||
ms_sbvar/Makefile
|
ms_sbvar/Makefile
|
||||||
block_kalman_filter/Makefile
|
block_kalman_filter/Makefile
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
EXEEXT = .mex
|
|
||||||
include ../mex.am
|
|
||||||
include ../../dynare_simul_.am
|
|
|
@ -1,142 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright © 2005-2011 Ondra Kamenik
|
|
||||||
* Copyright © 2019-2020 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/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
// This is the mexFunction providing interface to
|
|
||||||
// DecisionRule<>::simulate(). It takes the following input
|
|
||||||
// parameters:
|
|
||||||
// order the order of approximation, needs order+1 derivatives
|
|
||||||
// nstat
|
|
||||||
// npred
|
|
||||||
// nboth
|
|
||||||
// nforw
|
|
||||||
// nexog
|
|
||||||
// ystart starting value (full vector of endogenous)
|
|
||||||
// shocks matrix of shocks (nexog x number of period)
|
|
||||||
// vcov covariance matrix of shocks (nexog x nexog)
|
|
||||||
// seed integer seed
|
|
||||||
// ysteady full vector of decision rule's steady
|
|
||||||
// dr structure containing matrices of derivatives (g_0, g_1,…)
|
|
||||||
|
|
||||||
// output:
|
|
||||||
// res simulated results
|
|
||||||
|
|
||||||
#include "dynmex.h"
|
|
||||||
#include "mex.h"
|
|
||||||
|
|
||||||
#include "decision_rule.hh"
|
|
||||||
#include "fs_tensor.hh"
|
|
||||||
#include "SylvException.hh"
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
extern "C" {
|
|
||||||
void
|
|
||||||
mexFunction(int nlhs, mxArray *plhs[],
|
|
||||||
int nhrs, const mxArray *prhs[])
|
|
||||||
{
|
|
||||||
if (nhrs != 12 || nlhs != 1)
|
|
||||||
mexErrMsgTxt("dynare_simul_ must have at exactly 12 input parameters and 1 output argument.");
|
|
||||||
|
|
||||||
int order = static_cast<int>(mxGetScalar(prhs[0]));
|
|
||||||
int nstat = static_cast<int>(mxGetScalar(prhs[1]));
|
|
||||||
int npred = static_cast<int>(mxGetScalar(prhs[2]));
|
|
||||||
int nboth = static_cast<int>(mxGetScalar(prhs[3]));
|
|
||||||
int nforw = static_cast<int>(mxGetScalar(prhs[4]));
|
|
||||||
int nexog = static_cast<int>(mxGetScalar(prhs[5]));
|
|
||||||
|
|
||||||
const mxArray *const ystart = prhs[6];
|
|
||||||
const mxArray *const shocks = prhs[7];
|
|
||||||
const mxArray *const vcov = prhs[8];
|
|
||||||
int seed = static_cast<int>(mxGetScalar(prhs[9]));
|
|
||||||
const mxArray *const ysteady = prhs[10];
|
|
||||||
const mxArray *const dr = prhs[11];
|
|
||||||
const mwSize *const ystart_dim = mxGetDimensions(ystart);
|
|
||||||
const mwSize *const shocks_dim = mxGetDimensions(shocks);
|
|
||||||
const mwSize *const vcov_dim = mxGetDimensions(vcov);
|
|
||||||
const mwSize *const ysteady_dim = mxGetDimensions(ysteady);
|
|
||||||
|
|
||||||
int ny = nstat + npred + nboth + nforw;
|
|
||||||
if (ny != static_cast<int>(ystart_dim[0]))
|
|
||||||
mexErrMsgTxt("ystart has wrong number of rows.\n");
|
|
||||||
if (1 != ystart_dim[1])
|
|
||||||
mexErrMsgTxt("ystart has wrong number of cols.\n");
|
|
||||||
int nper = shocks_dim[1];
|
|
||||||
if (nexog != static_cast<int>(shocks_dim[0]))
|
|
||||||
mexErrMsgTxt("shocks has a wrong number of rows.\n");
|
|
||||||
if (nexog != static_cast<int>(vcov_dim[0]))
|
|
||||||
mexErrMsgTxt("vcov has a wrong number of rows.\n");
|
|
||||||
if (nexog != static_cast<int>(vcov_dim[1]))
|
|
||||||
mexErrMsgTxt("vcov has a wrong number of cols.\n");
|
|
||||||
if (ny != static_cast<int>(ysteady_dim[0]))
|
|
||||||
mexErrMsgTxt("ysteady has wrong number of rows.\n");
|
|
||||||
if (1 != ysteady_dim[1])
|
|
||||||
mexErrMsgTxt("ysteady has wrong number of cols.\n");
|
|
||||||
|
|
||||||
plhs[0] = mxCreateDoubleMatrix(ny, nper, mxREAL);
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
// initialize tensor library
|
|
||||||
TLStatic::init(order, npred+nboth+nexog);
|
|
||||||
|
|
||||||
// form the polynomial
|
|
||||||
UTensorPolynomial pol(ny, npred+nboth+nexog);
|
|
||||||
for (int dim = 0; dim <= order; dim++)
|
|
||||||
{
|
|
||||||
const mxArray *gk_m = mxGetField(dr, 0, ("g_" + std::to_string(dim)).c_str());
|
|
||||||
if (!gk_m)
|
|
||||||
mexErrMsgTxt(("Can't find field `g_" + std::to_string(dim) + "' in structured passed as last argument").c_str());
|
|
||||||
ConstTwoDMatrix gk(gk_m);
|
|
||||||
FFSTensor ft(ny, npred+nboth+nexog, dim);
|
|
||||||
if (ft.ncols() != gk.ncols())
|
|
||||||
mexErrMsgTxt(("Wrong number of columns for folded tensor: got " + std::to_string(gk.ncols()) + " but i want " + std::to_string(ft.ncols()) + '\n').c_str());
|
|
||||||
if (ft.nrows() != gk.nrows())
|
|
||||||
mexErrMsgTxt(("Wrong number of rows for folded tensor: got " + std::to_string(gk.nrows()) + " but i want " + std::to_string(ft.nrows()) + '\n').c_str());
|
|
||||||
ft.zeros();
|
|
||||||
ft.add(1.0, gk);
|
|
||||||
pol.insert(std::make_unique<UFSTensor>(ft));
|
|
||||||
}
|
|
||||||
// form the decision rule
|
|
||||||
UnfoldDecisionRule dr(pol, PartitionY(nstat, npred, nboth, nforw),
|
|
||||||
nexog, ConstVector{ysteady});
|
|
||||||
// form the shock realization
|
|
||||||
ConstTwoDMatrix shocks_mat(nexog, nper, ConstVector{shocks});
|
|
||||||
ConstTwoDMatrix vcov_mat(nexog, nexog, ConstVector{vcov});
|
|
||||||
GenShockRealization sr(vcov_mat, shocks_mat, seed);
|
|
||||||
// simulate and copy the results
|
|
||||||
TwoDMatrix res_mat{dr.simulate(DecisionRule::emethod::horner, nper,
|
|
||||||
ConstVector{ystart}, sr)};
|
|
||||||
TwoDMatrix res_tmp_mat{plhs[0]};
|
|
||||||
res_tmp_mat = const_cast<const TwoDMatrix &>(res_mat);
|
|
||||||
}
|
|
||||||
catch (const KordException &e)
|
|
||||||
{
|
|
||||||
mexErrMsgTxt("Caught Kord exception.");
|
|
||||||
}
|
|
||||||
catch (const TLException &e)
|
|
||||||
{
|
|
||||||
mexErrMsgTxt("Caught TL exception.");
|
|
||||||
}
|
|
||||||
catch (SylvException &e)
|
|
||||||
{
|
|
||||||
mexErrMsgTxt("Caught Sylv exception.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
|
@ -119,54 +119,7 @@ for T = 1:size(oo_.endo_simul,2)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if abs(y_-oo_.endo_simul(1,T)) > 1e-14
|
if abs(y_-oo_.endo_simul(1,T)) > 1e-14
|
||||||
error(['Error in dynare_simul_ DLL'])
|
error(['Error in k_order_simul DLL'])
|
||||||
end
|
end
|
||||||
xlag = oo_.endo_simul(2,T);
|
xlag = oo_.endo_simul(2,T);
|
||||||
end
|
end
|
||||||
|
|
||||||
% Verify that the simulated time series is correct with the Fortran routine k_order_simul
|
|
||||||
|
|
||||||
order = options_.order;
|
|
||||||
nstat = M_.nstatic;
|
|
||||||
npred = M_.npred;
|
|
||||||
nboth = M_.nboth;
|
|
||||||
nfwrd = M_.nfwrd;
|
|
||||||
nexog = M_.exo_nbr;
|
|
||||||
ystart = oo_.dr.ys(oo_.dr.order_var,1);
|
|
||||||
ex_ = oo_.exo_simul';
|
|
||||||
ysteady = oo_.dr.ys(oo_.dr.order_var);
|
|
||||||
dr = oo_.dr;
|
|
||||||
|
|
||||||
vcov = M_.Sigma_e;
|
|
||||||
seed = options_.DynareRandomStreams;
|
|
||||||
|
|
||||||
%do 1 call to get k_order_simul into memory (dynare_simul_ was already called within stoch_simul)
|
|
||||||
fortran_endo_simul = k_order_simul(order, nstat, npred, nboth, nfwrd, nexog, ystart, ex_, ysteady, dr);
|
|
||||||
|
|
||||||
tElapsed=NaN(2,1000);
|
|
||||||
for ii=1:1000
|
|
||||||
|
|
||||||
tStart1 = tic; fortran_endo_simul = k_order_simul(order, nstat, npred, nboth, nfwrd, nexog, ystart, ex_, ysteady, dr); tElapsed(1,ii) = toc(tStart1);
|
|
||||||
tStart2 = tic; dynare_endo_simul = dynare_simul_(order, nstat, npred, nboth, nfwrd, nexog, ystart,ex_,vcov,seed, ysteady, dr); tElapsed(2,ii) = toc(tStart2);
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
if max(max(abs(oo_.endo_simul-fortran_endo_simul(oo_.dr.order_var,:)))) > 1e-10
|
|
||||||
error('Error in k_order_simul: inaccurate simulation');
|
|
||||||
end;
|
|
||||||
|
|
||||||
if max(max(abs(dynare_endo_simul-fortran_endo_simul))) > 1e-10
|
|
||||||
error('Error in k_order_simul: inaccurate simulation');
|
|
||||||
end;
|
|
||||||
|
|
||||||
tElapsed1=mean(tElapsed(1,:))
|
|
||||||
tElapsed2=mean(tElapsed(2,:))
|
|
||||||
if tElapsed1<tElapsed2
|
|
||||||
skipline()
|
|
||||||
dprintf('k_order_simul is %5.2f times faster than dynare_simul_', tElapsed2/tElapsed1)
|
|
||||||
skipline()
|
|
||||||
else
|
|
||||||
skipline()
|
|
||||||
dprintf('k_order_simul is %5.2f times slower than dynare_simul_', tElapsed1/tElapsed2)
|
|
||||||
skipline()
|
|
||||||
end
|
|
||||||
|
|
Loading…
Reference in New Issue