Move source code of gensylv and dynare_simul_ MEX to the same place as other MEX

time-shift
Sébastien Villemot 2019-04-15 15:24:10 +02:00
parent 95584aa4f8
commit 2169341639
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
9 changed files with 51 additions and 257 deletions

View File

@ -1,174 +0,0 @@
%
% SYNOPSIS
%
% r = dynare_simul(name, shocks)
% r = dynare_simul(name, prefix, shocks)
% r = dynare_simul(name, shocks, start)
% r = dynare_simul(name, prefix, shocks, start)
%
% name name of MAT-file produced by dynare++
% prefix prefix of variables in the MAT-file
% shocks matrix of shocks
% start zero period value
%
% Note that this file requires the dynare_simul_ DLL to be in the path.
% This DLL is distributed with Dynare, under the mex/matlab or mex/octave
% subdirectory.
%
% SEMANTICS
%
% The command reads a decision rule from the MAT-file having the given
% prefix. Then it starts simulating the decision rule with zero time value
% equal to the given start. It uses the given shocks for the simulation. If
% the start is not given, the state about which the decision rule is
% centralized is taken (called fix point, or stochastic steady state, take
% your pick).
%
% prefix Use the prefix with which you called dynare++, the default
% prefix in dynare++ is 'dyn'.
% shocks Number of rows must be a number of exogenous shocks,
% number of columns gives the number of simulated
% periods. NaNs and Infs in the matrix are substitued by
% draws from the normal distribution using the covariance
% matrix given in the model file.
% start Vector of endogenous variables in the ordering given by
% <prefix>_vars.
%
% Seed for random generator is derived from calling rand(1,1). Therefore,
% seeding can be controlled with rand('state') and rand('state',some_seed).
%
% EXAMPLES
%
% All examples suppose that the prefix is 'dyn' and that your_model.mat
% has been loaded into Matlab.
%
% 1. response to permanent negative shock to the third exo var EPS3 for
% 100 periods
%
% shocks = zeros(4,100); % 4 exogenous variables in the model
% shocks(dyn_i_EPS3,:) = -0.1; % the permanent shock to EPS3
% r = dynare_simul('your_model.mat',shocks);
%
% 2. one stochastic simulation for 100 periods
%
% shocks = zeros(4,100)./0; % put NaNs everywhere
% r = dynare_simul('your_model.mat',shocks);
%
% 3. one stochastic simulation starting at 75% undercapitalized economy
%
% shocks = zeros(4,100)./0; % put NaNs everywhere
% ystart = dyn_ss; % get copy of DR fix point
% ystart(dyn_i_K) = 0.75*dyn_ss(dyn_i_K); % scale down the capital
% r = dynare_simul('your_model.mat',shocks,ystart);
%
%
% SEE ALSO
%
% "DSGE Models with Dynare++. A Tutorial.", Ondra Kamenik, 2005
% Copyright (C) 2005-2011, Ondra Kamenik
function r = dynare_simul(varargin)
if exist('dynare_simul_') ~= 3
error('Can''t find dynare_simul_ DLL in the path. The simplest way to add it is to run Dynare once in this session.')
end
% get the file name and load data
fname = varargin{1};
eval(['load ' fname]);
% set prefix, shocks, ystart
if ischar(varargin{2})
prefix = varargin{2};
if length(varargin) == 3
shocks = varargin{3};
ystart = NaN;
elseif length(varargin) == 4
shocks = varargin{3};
ystart = varargin{4};
else
error('Wrong number of parameters.');
end
else
prefix = 'dyn';
if length(varargin) == 2
shocks = varargin{2};
ystart = NaN;
elseif length(varargin) == 3
shocks = varargin{2};
ystart = varargin{3};
else
error('Wrong number of parameters.');
end
end
% load all needed variables but prefix_g_*
if (exist([prefix '_nstat']))
nstat = eval([prefix '_nstat']);
else
error(['Could not find variable ' prefix '_nstat in workspace']);
end
if (exist([prefix '_npred']))
npred = eval([prefix '_npred']);
else
error(['Could not find variable ' prefix '_npred in workspace']);
end
if (exist([prefix '_nboth']))
nboth = eval([prefix '_nboth']);
else
error(['Could not find variable ' prefix '_nboth in workspace']);
end
if (exist([prefix '_nforw']))
nforw = eval([prefix '_nforw']);
else
error(['Could not find variable ' prefix '_nforw in workspace']);
end
if (exist([prefix '_ss']))
ss = eval([prefix '_ss']);
else
error(['Could not find variable ' prefix '_ss in workspace']);
end
if (exist([prefix '_vcov_exo']))
vcov_exo = eval([prefix '_vcov_exo']);
else
error(['Could not find variable ' prefix '_vcov_exo in workspace']);
end
nexog = size(vcov_exo,1);
if isnan(ystart)
ystart = ss;
end
% newer version of dynare++ doesn't return prefix_g_0, we make it here if
% it does not exist in workspace
g_zero = [prefix '_g_0'];
if (~ exist(g_zero))
eval([ g_zero '= zeros(nstat+npred+nboth+nforw,1);']);
end
% make derstr a string of comma seperated existing prefix_g_*
derstr = [',' g_zero];
order = 1;
cont = 1;
while cont == 1
g_ord = [prefix '_g_' num2str(order)];
if (exist(g_ord))
derstr = [derstr ',' g_ord];
order = order + 1;
else
cont = 0;
end
end
% set seed
seed = ceil(10000*rand(1,1));
% call dynare_simul_
command = ['[err,r]=dynare_simul_(' num2str(order-1) ',nstat,npred,nboth,nforw,' ...
'nexog,ystart,shocks,vcov_exo,seed,ss' derstr ');'];
eval(command);
if err
error('Simulation failed')
end

View File

@ -1,6 +1,6 @@
SUBDIRS = cc testing
EXTRA_DIST = sylvester.tex change_log.html matlab
EXTRA_DIST = sylvester.tex change_log.html
if HAVE_PDFLATEX
pdf-local: sylvester.pdf

View File

@ -1,76 +0,0 @@
%
% GENSYLV solves the following matrix equation:
% A*X + [0 B]*X*kron(C,..,C) = D,
% where
% A ...... regular (n,n) matrix,
% [0 B] .. (n,n) matrix with a few first
% columns equal to zeros
% B ...... rectangular (n, nz) matrix with nz<=n
% C ...... regular (m,m) matrix, whose spectrum is
% within (-1, 1)
% kron(C,..,C)
% ...... Kronecker power of C of order 'order'
% D ..... rectangular (n, m^order) matrix.
%
% [err, X] = gensylv(order, A, B, C, D)
% returns err a scalar where 1 indicates failure, 0 indicates success
% and X as the solution, doesn't perform any checks
%
% [err, X, par] = gensylv(order, A, B, C, D)
% solves the system, and performs checks. 'par' is struct
% containing information about solution and error norms
% returned by the check. This is a list of the struct
% members, some of them may be missing in actual returned
% value:
% method = method used for solution recursive/iterative
% convergence_tol = convergence tolerance for iter. method
% max_num_iter = max number of steps for iter. method
% bs_norm = Bavely Stewart log10 norm for diagonalization
% converged = convergence status for iterative method
% iter_last_norm = residual norm of the last step of iterations
% num_iter = number of iterations performed
% f_err1 = norm 1 of abs. error C-V*F*inv(V)
% f_errI = norm Inf of abs. error C-V*F*inv(V)
% viv_err1 = norm 1 of abs. error I-V*inv(V)
% viv_errI = norm Inf of abs. error I-V*inv(V)
% ivv_err1 = norm 1 of abs. error I-inv(V)*V
% ivv_errI = norm Inf of abs. error I-inv(V)*V
% f_blocks = number of diagonal blocks of F
% f_largest = size of largest diagonal block in F
% f_zeros = number of off diagonal zeros in F
% f_offdiag = number of all offdiagonal elements in F
% rcondA1 = reciprocal cond 1 number of A
% rcondAI = reciprocal cond Inf number of A
% eig_min = minimum eigenvalue of vectorized system
% mat_err1 = rel. matrix 1 norm of A*X-[0 B]*X*kron(C,..,C)-D
% mat_errI = rel. matrix Inf norm of --"--
% mat_errF = rel. matrix Frobenius norm of --"--
% vec_err1 = rel. vector 1 norm of --"--
% vec_errI = rel. vector Inf norm of --"--
% cpu_time = CPU time needed for solution in CPU seconds
%
% $Header: /var/lib/cvs/dynare_cpp/sylv/matlab/gensylv.m,v 1.1.1.1 2004/06/04 13:01:13 kamenik Exp $
% Tag $Name: $
function [err, X, varargout] = gensylv(order, A, B, C, D)
% in Windows, ensure that aa_gensylv.dll is loaded, this prevents
% clearing the function and a successive Matlab crash
if strcmp('PCWIN', computer)
if ~ libisloaded('aa_gensylv')
loadlibrary('aa_gensylv', 'dummy');
end
end
% launch aa_gensylv
if nargout == 2
X = aa_gensylv(order, A, B, C, D);
elseif nargout == 3
[X, par] = aa_gensylv(order, A, B, C, D);
varargout(1) = {par};
else
error('Must have 2 or 3 output arguments.');
end
err = 0;

View File

@ -268,6 +268,12 @@ Copyright: 2010 Yannick Kalantzis
2019 Dynare Team
License: GPL-3+
Files: mex/sources/dynare_simul_/dynare_simul_.cc
mex/sources/gensylv/gensylv.cc
Copyright: 2005-2011 Ondra Kamenik
2019 Dynare Team
License: GPL-3+
Files: mex/sources/sobol/sobol.hh mex/sources/sobol/initialize_v_array.hh
mex/sources/sobol/initialize_v_array.inc
Copyright: 2009 John Burkardt

View File

@ -7,10 +7,10 @@ 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
nodist_dynare_simul__SOURCES = dynare_simul_.cc
BUILT_SOURCES = $(nodist_dynare_simul__SOURCES)
CLEANFILES = $(nodist_dynare_simul__SOURCES)
%.cc: $(top_srcdir)/../../../dynare++/extern/matlab/%.cc
%.cc: $(top_srcdir)/../../sources/dynare_simul_/%.cc
$(LN_S) -f $< $@

View File

@ -9,5 +9,5 @@ nodist_gensylv_SOURCES = gensylv.cc
BUILT_SOURCES = $(nodist_gensylv_SOURCES)
CLEANFILES = $(nodist_gensylv_SOURCES)
%.cc: $(top_srcdir)/../../../dynare++/sylv/matlab/%.cc
%.cc: $(top_srcdir)/../../sources/gensylv/%.cc
$(LN_S) -f $< $@

View File

@ -11,7 +11,9 @@ EXTRA_DIST = \
ms-sbvar \
block_kalman_filter \
sobol \
local_state_space_iterations
local_state_space_iterations \
gensylv \
dynare_simul_
clean-local:
rm -rf `find mex/sources -name *.o`

View File

@ -1,4 +1,22 @@
// Copyright (C) 2005-2011, Ondra Kamenik
/*
* Copyright (C) 2005-2011 Ondra Kamenik
* Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
*/
// This is the mexFunction providing interface to
// DecisionRule<>::simulate(). It takes the following input

View File

@ -1,4 +1,22 @@
// Copyright (C) 2005-2011, Ondra Kamenik
/*
* Copyright (C) 2005-2011 Ondra Kamenik
* Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
*/
#include "dynmex.h"
#include "mex.h"