Irf function: not extracting values of dr, or other inputs

Hi,

Please see the function irf below: it takes dr as input, but in my workspace dr does not exist separately (it is found under oo_.dr). This has required me to define dr and its subfiles manually (you can see these additions in the code). However, this is not realistic, as some of the subfiles are large matrices (such as oo_.dr_kstate, dr.ghx, dr.ghu - all called in simult_.m file, which is called in irf.m).

ScienceDirect_files_06Jun2024_08-59-06.459 copy.zip (1.4 MB)
Is there anyway to add a line so that it takes dr as a structure array, and can automatically recognise sub files with its dot indexing?
I have the same issue with M_, needing to define each field name otherwise it does not take it from the workspace.

issue in irf.m and also in simult_.m (it will be become clear through the error message).

function y = irf(M_, options_, dr, e1, long, drop, replic, iorder)

options_ = struct();  % IEB: addded to initialize options_ as a structure
options_.loglinear = false;  % IEB: added to re-assign its value
options_.logged_steady_state = 0;  % IEB: added to re-assign its value
M_ = struct();  % IEB: added to initialize M_ as a structure
M_.maximum_lag = 1;  % IEB: added to re-assign value
M_.endo_nbr = 31; % IEB: added to re-assign value
M_.exo_nbr = 3;  % IEB: added to re-assign value
M_.hessian_eq_zero = 0; % IEB: added to re-assign value
M_.Sigma_e = diag(0.0001 * ones(3,1)); % IEB: added to re-assign value
dr = struct(); % IEB: added to initialize M_ as a structure
dr.ys = zeros(M_.endo_nbr, 1);  % IEB: added to set default dr.ys
replic = 1000; %IEB: set number of simulations to 1000

if options_.loglinear && ~options_.logged_steady_state
   dr.ys = log_variable(1:M_.endo_nbr,dr.ys,M_);
    options_.logged_steady_state=1;
end


if M_.maximum_lag >= 1
    temps = repmat(dr.ys,1,M_.maximum_lag);
else
    temps = zeros(M_.endo_nbr, 1); % Dummy values for purely forward models
end
y       = 0;


iorder = 2; %IEB: added this to set approximation to second order
local_order = iorder;
if local_order~=1 && M_.hessian_eq_zero
    local_order = 1;
end

if local_order == 1
    y1 = repmat(dr.ys,1,long);
    ex2 = zeros(long,M_.exo_nbr);
    ex2(1,:) = e1';
    y2 = simult_(M_,options_,temps,dr,ex2,local_order);
    y = y2(:,M_.maximum_lag+1:end)-y1;
else
    % eliminate shocks with 0 variance
    i_exo_var = setdiff(1:M_.exo_nbr,find(diag(M_.Sigma_e) == 0 ));
    nxs = length(i_exo_var);
    ex1 = zeros(long+drop,M_.exo_nbr);
    chol_S = chol(M_.Sigma_e(i_exo_var,i_exo_var));
    for j = 1: replic
        ex1(:,i_exo_var) = randn(long+drop,nxs)*chol_S;
        ex2 = ex1;
        ex2(drop+1,:) = ex2(drop+1,:)+e1';
        y1 = simult_(M_,options_,temps,dr,ex1,local_order);
        y2 = simult_(M_,options_,temps,dr,ex2,local_order);
        y = y+(y2(:,M_.maximum_lag+drop+1:end)-y1(:,M_.maximum_lag+drop+1:end));
    end
    y=y/replic;
end

attached is the zip file.

Many thanks in advance!

I attached another zip file, this has the original issue of irf.m not reading indexed options_ , M_, dr, replic or iorder. I tried to manually define these, but it became too messy to keep doing this (especially in subsequent simult_.m file).

When I did manually define themM_, options_, dr, replic, iorder, I got the following warning message: 'input argument might be unused. Consider replacing argument with ~ instead.

It’s clear I tried to solve something, but added more issues. So here is the attached file before I define the input arguments…

I’m grateful to anyone who might know how to address this!
ScienceDirect_files_06Jun2024_08-59-06.459 copy 2.zip (1.4 MB)

I implemented some changes. But given that you posted an already modified version, it’s hard to tell what the desired effect should be:
DW2013.zip (347.7 KB)

thank you for your response. It is helpful to see.