From 1c070f536af0e5b8c78a3c13943cabf9fe3dcbde Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Tue, 5 Jul 2016 19:41:24 +0200 Subject: [PATCH] Add headers to various functions --- matlab/mcp_func.m | 29 +++++ .../perfect_foresight_mcp_problem.m | 120 +++++++++++------- .../perfect_foresight_problem.m | 41 ++++-- .../perfect_foresight_solver_core.m | 10 ++ .../private/initialize_stacked_problem.m | 30 ++++- matlab/perfect-foresight-models/sim1.m | 9 +- .../solve_stacked_problem.m | 17 ++- 7 files changed, 194 insertions(+), 62 deletions(-) diff --git a/matlab/mcp_func.m b/matlab/mcp_func.m index 486764857..424b9203f 100644 --- a/matlab/mcp_func.m +++ b/matlab/mcp_func.m @@ -1,4 +1,33 @@ function [res,fjac,domer] = mcp_func(x,jacflag) +% function [res,fjac,domer] = mcp_func(x,jacflag) +% wrapper function for mixed complementarity problem when using PATH +% +% INPUTS +% - x [double] N*T array, paths for the endogenous variables (initial guess). +% - jacflag [scalar] indicator whether Jacobian is requested +% +% OUTPUTS +% - res [double] (N*T)*1 array, residuals of the stacked problem +% - fjac [double] (N*T)*(N*T) array, Jacobian of the stacked problem +% - domer [scalar] errorflag that is 1 if solution is not real + +% Copyright (C) 2016 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 . + global mcp_data if jacflag diff --git a/matlab/perfect-foresight-models/perfect_foresight_mcp_problem.m b/matlab/perfect-foresight-models/perfect_foresight_mcp_problem.m index 18fcff1c3..d4b419308 100644 --- a/matlab/perfect-foresight-models/perfect_foresight_mcp_problem.m +++ b/matlab/perfect-foresight-models/perfect_foresight_mcp_problem.m @@ -3,23 +3,49 @@ function [residuals,JJacobian] = perfect_foresight_mcp_problem(y, dynamic_functi maximum_lag, T, ny, i_cols, ... i_cols_J1, i_cols_1, i_cols_T, ... i_cols_j,nnzJ,eq_index) -% function [residuals,JJacobian] = perfect_foresight_mcp_problem(x, model_dynamic, Y0, YT,exo_simul, -% params, steady_state, maximum_lag, periods, ny, i_cols,i_cols_J1, i_cols_1, -% i_cols_T, i_cols_j, nnzA) -% computes the residuals and th Jacobian matrix -% for a perfect foresight problem over T periods. +% function [residuals,JJacobian] = perfect_foresight_mcp_problem(y, dynamic_function, Y0, YT, ... +% exo_simul, params, steady_state, ... +% maximum_lag, T, ny, i_cols, ... +% i_cols_J1, i_cols_1, i_cols_T, ... +% i_cols_j,nnzJ,eq_index) +% Computes the residuals and the Jacobian matrix for a perfect foresight problem over T periods +% in a mixed complementarity problem context % % INPUTS -% ... +% y [double] N*1 array, terminal conditions for the endogenous variables +% dynamic_function [handle] function handle to _dynamic-file +% Y0 [double] N*1 array, initial conditions for the endogenous variables +% YT [double] N*1 array, terminal conditions for the endogenous variables +% exo_simul [double] nperiods*M_.exo_nbr matrix of exogenous variables (in declaration order) +% for all simulation periods +% params [double] nparams*1 array, parameter values +% steady_state [double] endo_nbr*1 vector of steady state values +% maximum_lag [scalar] maximum lag present in the model +% T [scalar] number of simulation periods +% ny [scalar] number of endogenous variables +% i_cols [double] indices of variables appearing in M.lead_lag_incidence +% and that need to be passed to _dynamic-file +% i_cols_J1 [double] indices of contemporaneous and forward looking variables +% appearing in M.lead_lag_incidence +% i_cols_1 [double] indices of contemporaneous and forward looking variables in +% M.lead_lag_incidence in dynamic Jacobian (relevant in first period) +% i_cols_T [double] columns of dynamic Jacobian related to contemporaneous and backward-looking +% variables (relevant in last period) +% i_cols_j [double] indices of variables in M.lead_lag_incidence +% in dynamic Jacobian (relevant in intermediate periods) +% nnzJ [scalar] number of non-zero elements in Jacobian +% eq_index [double] N*1 array, index vector describing residual mapping resulting +% from complementarity setup % OUTPUTS -% ... +% residuals [double] (N*T)*1 array, residuals of the stacked problem +% JJacobian [double] (N*T)*(N*T) array, Jacobian of the stacked problem % ALGORITHM -% ... +% None % % SPECIAL REQUIREMENTS % None. -% Copyright (C) 1996-2015 Dynare Team +% Copyright (C) 1996-2016 Dynare Team % % This file is part of Dynare. % @@ -37,46 +63,46 @@ function [residuals,JJacobian] = perfect_foresight_mcp_problem(y, dynamic_functi % along with Dynare. If not, see . - YY = [Y0; y; YT]; - - residuals = zeros(T*ny,1); - if nargout == 2 - iJacobian = cell(T,1); - end +YY = [Y0; y; YT]; - i_rows = 1:ny; - offset = 0; - i_cols_J = i_cols; +residuals = zeros(T*ny,1); +if nargout == 2 + iJacobian = cell(T,1); +end - for it = 2:(T+1) - if nargout == 1 - res = dynamic_function(YY(i_cols),exo_simul, params, ... - steady_state,it); - residuals(i_rows) = res(eq_index); - elseif nargout == 2 - [res,jacobian] = dynamic_function(YY(i_cols),exo_simul, params, ... - steady_state,it); - residuals(i_rows) = res(eq_index); - if it == 2 - [rows,cols,vals] = find(jacobian(eq_index,i_cols_1)); - iJacobian{1} = [offset+rows, i_cols_J1(cols), vals]; - elseif it == T + 1 - [rows,cols,vals] = find(jacobian(eq_index,i_cols_T)); - iJacobian{T} = [offset+rows, i_cols_J(i_cols_T(cols)), vals]; - else - [rows,cols,vals] = find(jacobian(eq_index,i_cols_j)); - iJacobian{it-1} = [offset+rows, i_cols_J(cols), vals]; - i_cols_J = i_cols_J + ny; - end - offset = offset + ny; +i_rows = 1:ny; +offset = 0; +i_cols_J = i_cols; + +for it = 2:(T+1) + if nargout == 1 + res = dynamic_function(YY(i_cols),exo_simul, params, ... + steady_state,it); + residuals(i_rows) = res(eq_index); + elseif nargout == 2 + [res,jacobian] = dynamic_function(YY(i_cols),exo_simul, params, ... + steady_state,it); + residuals(i_rows) = res(eq_index); + if it == 2 + [rows,cols,vals] = find(jacobian(eq_index,i_cols_1)); + iJacobian{1} = [offset+rows, i_cols_J1(cols), vals]; + elseif it == T + 1 + [rows,cols,vals] = find(jacobian(eq_index,i_cols_T)); + iJacobian{T} = [offset+rows, i_cols_J(i_cols_T(cols)), vals]; + else + [rows,cols,vals] = find(jacobian(eq_index,i_cols_j)); + iJacobian{it-1} = [offset+rows, i_cols_J(cols), vals]; + i_cols_J = i_cols_J + ny; end - - i_rows = i_rows + ny; - i_cols = i_cols + ny; + offset = offset + ny; end + + i_rows = i_rows + ny; + i_cols = i_cols + ny; +end - if nargout == 2 - iJacobian = cat(1,iJacobian{:}); - JJacobian = sparse(iJacobian(:,1),iJacobian(:,2),iJacobian(:,3),T* ... - ny,T*ny); - end \ No newline at end of file +if nargout == 2 + iJacobian = cat(1,iJacobian{:}); + JJacobian = sparse(iJacobian(:,1),iJacobian(:,2),iJacobian(:,3),T* ... + ny,T*ny); +end \ No newline at end of file diff --git a/matlab/perfect-foresight-models/perfect_foresight_problem.m b/matlab/perfect-foresight-models/perfect_foresight_problem.m index adce18df5..956119094 100644 --- a/matlab/perfect-foresight-models/perfect_foresight_problem.m +++ b/matlab/perfect-foresight-models/perfect_foresight_problem.m @@ -3,23 +3,46 @@ function [residuals,JJacobian] = perfect_foresight_problem(y, dynamic_function, maximum_lag, T, ny, i_cols, ... i_cols_J1, i_cols_1, i_cols_T, ... i_cols_j,nnzJ) -% function [residuals,JJacobian] = perfect_foresight_problem(x, model_dynamic, Y0, YT,exo_simul, -% params, steady_state, maximum_lag, periods, ny, i_cols,i_cols_J1, i_cols_1, -% i_cols_T, i_cols_j, nnzA) -% computes the residuals and th Jacobian matrix -% for a perfect foresight problem over T periods. +% function [residuals,JJacobian] = perfect_foresight_problem(y, dynamic_function, Y0, YT, ... +% exo_simul, params, steady_state, ... +% maximum_lag, T, ny, i_cols, ... +% i_cols_J1, i_cols_1, i_cols_T, ... +% i_cols_j,nnzJ) +% computes the residuals and the Jacobian matrix for a perfect foresight problem over T periods. % % INPUTS -% ... +% y [double] N*1 array, terminal conditions for the endogenous variables +% dynamic_function [handle] function handle to _dynamic-file +% Y0 [double] N*1 array, initial conditions for the endogenous variables +% YT [double] N*1 array, terminal conditions for the endogenous variables +% exo_simul [double] nperiods*M_.exo_nbr matrix of exogenous variables (in declaration order) +% for all simulation periods +% params [double] nparams*1 array, parameter values +% steady_state [double] endo_nbr*1 vector of steady state values +% maximum_lag [scalar] maximum lag present in the model +% T [scalar] number of simulation periods +% ny [scalar] number of endogenous variables +% i_cols [double] indices of variables appearing in M.lead_lag_incidence +% and that need to be passed to _dynamic-file +% i_cols_J1 [double] indices of contemporaneous and forward looking variables +% appearing in M.lead_lag_incidence +% i_cols_1 [double] indices of contemporaneous and forward looking variables in +% M.lead_lag_incidence in dynamic Jacobian (relevant in first period) +% i_cols_T [double] columns of dynamic Jacobian related to contemporaneous and backward-looking +% variables (relevant in last period) +% i_cols_j [double] indices of variables in M.lead_lag_incidence +% in dynamic Jacobian (relevant in intermediate periods) +% nnzJ [scalar] number of non-zero elements in Jacobian % OUTPUTS -% ... +% residuals [double] (N*T)*1 array, residuals of the stacked problem +% JJacobian [double] (N*T)*(N*T) array, Jacobian of the stacked problem % ALGORITHM -% ... +% None % % SPECIAL REQUIREMENTS % None. -% Copyright (C) 1996-2015 Dynare Team +% Copyright (C) 1996-2016 Dynare Team % % This file is part of Dynare. % diff --git a/matlab/perfect-foresight-models/perfect_foresight_solver_core.m b/matlab/perfect-foresight-models/perfect_foresight_solver_core.m index d0b403c2b..da267b7da 100644 --- a/matlab/perfect-foresight-models/perfect_foresight_solver_core.m +++ b/matlab/perfect-foresight-models/perfect_foresight_solver_core.m @@ -1,5 +1,15 @@ function [oo_, maxerror] = perfect_foresight_solver_core(M_, options_, oo_) %function [oo_, maxerror] = perfect_foresight_solver_core(M_, options_, oo_) +% Core function calling solvers for perfect foresight model +% +% INPUTS +% - M_ [struct] contains a description of the model. +% - options_ [struct] contains various options. +% - oo_ [struct] contains results +% +% OUTPUTS +% - oo_ [struct] contains results +% - maxerror [double] contains the maximum absolute error % Copyright (C) 2015-2016 Dynare Team % diff --git a/matlab/perfect-foresight-models/private/initialize_stacked_problem.m b/matlab/perfect-foresight-models/private/initialize_stacked_problem.m index b56fec828..64beba34e 100644 --- a/matlab/perfect-foresight-models/private/initialize_stacked_problem.m +++ b/matlab/perfect-foresight-models/private/initialize_stacked_problem.m @@ -1,7 +1,33 @@ function [options, y0, yT, z, i_cols, i_cols_J1, i_cols_T, i_cols_j, i_cols_1, ... - dynamicmodel] = initialize_stack_solve_algo_7(endogenousvariables, options, M, steadystate_y) + dynamicmodel] = initialize_stacked_problem(endogenousvariables, options, M, steadystate_y) +% function [options, y0, yT, z, i_cols, i_cols_J1, i_cols_T, i_cols_j, i_cols_1, ... +% dynamicmodel] = initialize_stacked_problem(endogenousvariables, options, M, steadystate_y) +% Sets up the stacked perfect foresight problem for use with dynare_solve.m +% +% INPUTS +% - endogenousvariables [double] N*T array, paths for the endogenous variables (initial guess). +% - options [struct] contains various options. +% - M [struct] contains a description of the model. +% - steadystate_y [double] N*1 array, steady state for the endogenous variables. +% OUTPUTS +% - options [struct] contains various options. +% - y0 [double] N*1 array, initial conditions for the endogenous variables +% - yT [double] N*1 array, terminal conditions for the endogenous variables +% - z [double] T*M array, paths for the exogenous variables. +% - i_cols [double] indices of variables appearing in M.lead_lag_incidence +% and that need to be passed to _dynamic-file +% - i_cols_J1 [double] indices of contemporaneous and forward looking variables +% appearing in M.lead_lag_incidence +% - i_cols_T [double] columns of dynamic Jacobian related to +% contemporaneous and backward-looking +% variables (relevant in last period) +% - i_cols_j [double] indices of variables in M.lead_lag_incidence +% in dynamic Jacobian (relevant in intermediate periods) +% - i_cols_1 [double] indices of contemporaneous and forward looking variables in +% M.lead_lag_incidence in dynamic Jacobian (relevant in first period) +% - dynamicmodel [handle] function handle to _dynamic-file -% Copyright (C) 2015 Dynare Team +% Copyright (C) 2015-16 Dynare Team % % This file is part of Dynare. % diff --git a/matlab/perfect-foresight-models/sim1.m b/matlab/perfect-foresight-models/sim1.m index 55c17bb70..05ef5b186 100644 --- a/matlab/perfect-foresight-models/sim1.m +++ b/matlab/perfect-foresight-models/sim1.m @@ -3,9 +3,14 @@ function [endogenousvariables, info] = sim1(endogenousvariables, exogenousvariab % Performs deterministic simulations with lead or lag on one period. Uses sparse matrices. % % INPUTS -% ... +% - endogenousvariables [double] N*T array, paths for the endogenous variables (initial guess). +% - exogenousvariables [double] T*M array, paths for the exogenous variables. +% - steadystate [double] N*1 array, steady state for the endogenous variables. +% - M [struct] contains a description of the model. +% - options [struct] contains various options. % OUTPUTS -% ... +% - endogenousvariables [double] N*T array, paths for the endogenous variables (solution of the perfect foresight model). +% - info [struct] contains informations about the results. % ALGORITHM % ... % diff --git a/matlab/perfect-foresight-models/solve_stacked_problem.m b/matlab/perfect-foresight-models/solve_stacked_problem.m index 7b1ab780e..7a1835ba6 100644 --- a/matlab/perfect-foresight-models/solve_stacked_problem.m +++ b/matlab/perfect-foresight-models/solve_stacked_problem.m @@ -1,6 +1,19 @@ function [endogenousvariables, info] = solve_stacked_problem(endogenousvariables, exogenousvariables, steadystate, M, options); +% [endogenousvariables, info] = solve_stacked_problem(endogenousvariables, exogenousvariables, steadystate, M, options); +% Solves the perfect foresight model using dynare_solve +% +% INPUTS +% - endogenousvariables [double] N*T array, paths for the endogenous variables (initial guess). +% - exogenousvariables [double] T*M array, paths for the exogenous variables. +% - steadystate [double] N*1 array, steady state for the endogenous variables. +% - M [struct] contains a description of the model. +% - options [struct] contains various options. +% +% OUTPUTS +% - endogenousvariables [double] N*T array, paths for the endogenous variables (solution of the perfect foresight model). +% - info [struct] contains informations about the results. -% Copyright (C) 2015 Dynare Team +% Copyright (C) 2015-16 Dynare Team % % This file is part of Dynare. % @@ -20,7 +33,7 @@ function [endogenousvariables, info] = solve_stacked_problem(endogenousvariables [options, y0, yT, z, i_cols, i_cols_J1, i_cols_T, i_cols_j, i_cols_1, dynamicmodel] = ... initialize_stacked_problem(endogenousvariables, options, M, steadystate); -if (options.solve_algo == 10 || options.solve_algo == 11) +if (options.solve_algo == 10 || options.solve_algo == 11)% mixed complementarity problem [lb,ub,eq_index] = get_complementarity_conditions(M,options.ramsey_policy); if options.linear_approximation lb = lb - steadystate_y;