v4: fixing initvalf

git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@1795 ac1d8469-bf42-47a9-8791-bf33cf982152
time-shift
michel 2008-04-14 07:22:10 +00:00
parent 9330ff1786
commit 74fb4a04c5
11 changed files with 405 additions and 136 deletions

View File

@ -0,0 +1,229 @@
function [data,rawdata]=dynare_estimation_init(var_list_)
% function dynare_estimation_init(var_list_)
% preforms initialization tasks before estimation or
% global sensitivity analysis
%
% INPUTS
% var_list_: selected endogenous variables vector
%
% OUTPUTS
% data: data after required transformation
% rawdat: data as in the data file
%
% SPECIAL REQUIREMENTS
% none
%
% part of DYNARE, copyright Dynare Team (2003-2007)
% Gnu Public License.
global M_ options_ oo_ estim_params_
global bayestopt_ dsge_prior_weight
options_.varlist = var_list_;
options_.lgyidx2varobs = zeros(size(M_.endo_names,1),1);
for i = 1:size(M_.endo_names,1)
tmp = strmatch(deblank(M_.endo_names(i,:)),options_.varobs,'exact');
if ~isempty(tmp)
options_.lgyidx2varobs(i,1) = tmp;
end
end
if ~isempty(strmatch('dsge_prior_weight',M_.param_names))
options_.bvar_dsge = 1;
end
if options_.order > 1
options_.order = 1;
end
if options_.prefilter == 1
options_.noconstant = 1;
end
if options_.filtered_vars ~= 0 & options_.filter_step_ahead == 0
options_.filter_step_ahead = 1;
end
if options_.filter_step_ahead ~= 0
options_.nk = max(options_.filter_step_ahead);
else
options_.nk = 0;
end
%% Add something to the parser ++>
% The user should be able to choose another name
% for the directory...
M_.dname = M_.fname;
pnames = [' ';'beta ';'gamm ';'norm ';'invg ';'unif ';'invg2'];
n_varobs = size(options_.varobs,1);
if ~isempty(estim_params_)
[xparam1,estim_params_,bayestopt_,lb,ub] = set_prior(estim_params_);
if any(bayestopt_.pshape > 0)
if options_.mode_compute
plot_priors
end
else
options_.mh_replic = 0;
end
% set prior bounds and check initial value of the parameters
bounds = prior_bounds(bayestopt_);
bounds(:,1)=max(bounds(:,1),lb);
bounds(:,2)=min(bounds(:,2),ub);
if any(xparam1 < bounds(:,1)) | any(xparam1 > bounds(:,2))
find(xparam1 < bounds(:,1))
find(xparam1 > bounds(:,2))
error('Initial parameter values are outside parameter bounds')
end
lb = bounds(:,1);
ub = bounds(:,2);
bayestopt_.lb = lb;
bayestopt_.ub = ub;
else
xparam1 = [];
bayestopt_.lb = [];
bayestopt_.ub = [];
bayestopt_.jscale = [];
bayestopt_.pshape = [];
bayestopt_.p1 = [];
bayestopt_.p2 = [];
bayestopt_.p3 = [];
bayestopt_.p4 = [];
estim_params_.nvx = 0;
estim_params_.nvn = 0;
estim_params_.ncx = 0;
estim_params_.ncn = 0;
estim_params_.np = 0;
end
nvx = estim_params_.nvx;
nvn = estim_params_.nvn;
ncx = estim_params_.ncx;
ncn = estim_params_.ncn;
np = estim_params_.np ;
nx = nvx+nvn+ncx+ncn+np;
if ~isfield(options_,'trend_coeffs')
bayestopt_.with_trend = 0;
else
bayestopt_.with_trend = 1;
bayestopt_.trend_coeff = {};
trend_coeffs = options_.trend_coeffs;
nt = length(trend_coeffs);
for i=1:n_varobs
if i > length(trend_coeffs)
bayestopt_.trend_coeff{i} = '0';
else
bayestopt_.trend_coeff{i} = trend_coeffs{i};
end
end
end
bayestopt_.penalty = 1e8; % penalty
dr = set_state_space([]);
nstatic = dr.nstatic;
npred = dr.npred;
nspred = dr.nspred;
if isempty(options_.varobs)
error('ESTIMATION: VAROBS is missing')
end
%% Setting resticted state space (observed + predetermined variables)
k = [];
k1 = [];
for i=1:n_varobs
k = [k strmatch(deblank(options_.varobs(i,:)),M_.endo_names(dr.order_var,:),'exact')];
k1 = [k1 strmatch(deblank(options_.varobs(i,:)),M_.endo_names, 'exact')];
end
% union of observed and state variables
k2 = union(k',[dr.nstatic+1:dr.nstatic+dr.npred]');
% set restrict_state to postion of observed + state variables
% in expanded state vector
bayestopt_.restrict_var_list = k2;
% set mf1 to positions of observed variables in restricted state vector
% for likelihood computation
[junk,bayestopt_.mf1] = ismember(k,k2);
% set mf2 to positions of observed variables in expanded state vector
% for filtering and smoothing
bayestopt_.mf2 = k;
bayestopt_.mfys = k1;
[junk,ic] = intersect(k2,nstatic+(1:npred)');
bayestopt_.restrict_columns = [ic; length(k2)+(1:nspred-npred)'];
aux = dr.transition_auxiliary_variables;
aux(:,2) = aux(:,2) + sum(k2 <= nstatic);
k = find(aux(:,2) > npred);
aux(k,2) = aux(k,2) + sum(k2 > nstatic+npred);
bayestopt_.restrict_aux = aux;
%% Initialization with unit-root variables
if ~isempty(options_.unit_root_vars)
n_ur = size(options_.unit_root_vars,1);
i_ur = zeros(n_ur,1);
for i=1:n_ur
i1 = strmatch(deblank(options_.unit_root_vars(i,:)),M_.endo_names(dr.order_var,:),'exact');
if isempty(i1)
error('Undeclared variable in unit_root_vars statement')
end
i_ur(i) = i1;
end
[junk,bayestopt_.var_list_stationary] = ...
setdiff((1:M_.endo_nbr)',i_ur);
[junk,bayestopt_.restrict_var_list_stationary] = ...
setdiff(bayestopt_.restrict_var_list,i_ur);
[junk,bayestopt_.restrict_var_list_nonstationary] = ...
intersect(bayestopt_.restrict_var_list,i_ur);
if M_.maximum_lag > 1
l1 = flipud([cumsum(M_.lead_lag_incidence(1:M_.maximum_lag-1,dr.order_var),1);ones(1,M_.endo_nbr)]);
l2 = l1(:,bayestopt_.restrict_var_list);
il2 = find(l2' > 0);
l2(il2) = (1:length(il2))';
bayestopt_.restrict_var_list_stationary = ...
nonzeros(l2(:,bayestopt_.restrict_var_list_stationary));
bayestopt_.restrict_var_list_nonstationary = ...
nonzeros(l2(:,bayestopt_.restrict_var_list_nonstationary));
end
options_.lik_init = 3;
end % if ~isempty(options_.unit_root_vars)
if isempty(options_.datafile)
error('ESTIMATION: datafile option is missing')
end
%% If jscale isn't specified for an estimated parameter, use
%% global option options_.jscale, set to 0.2, by default
k = find(isnan(bayestopt_.jscale));
bayestopt_.jscale(k) = options_.mh_jscale;
%% Read and demean data
rawdata = read_variables(options_.datafile,options_.varobs,[],options_.xls_sheet,options_.xls_range);
options_ = set_default_option(options_,'nobs',size(rawdata,1)-options_.first_obs+1);
gend = options_.nobs;
rawdata = rawdata(options_.first_obs:options_.first_obs+gend-1,:);
if options_.loglinear == 1 & ~options_.logdata
rawdata = log(rawdata);
end
if options_.prefilter == 1
bayestopt_.mean_varobs = mean(rawdata,1);
data = transpose(rawdata-ones(gend,1)*bayestopt_.mean_varobs);
else
data = transpose(rawdata);
end
if ~isreal(rawdata)
error(['There are complex values in the data. Probably a wrong' ...
' transformation'])
end

Binary file not shown.

View File

@ -15,12 +15,19 @@ function x0=dynare_sensitivity(options_gsa)
% M. Ratto, Global Sensitivity Analysis for Macroeconomic models, MIMEO, 2006.
%
global M_ options_ oo_ bayestopt_
global M_ options_ oo_ bayestopt_ estim_params_
fname_ = M_.fname;
M_.dname = fname_;
lgy_ = M_.endo_names;
x0=[];
options_.datafile = options_gsa.datafile;
options_.mode_compute = 0;
[data,rawdata]=dynare_estimation_init([]);
% computes a first linear solution to set up various variables
dynare_resolve;
options_gsa = set_default_option(options_gsa,'identification',0);
if options_gsa.identification,
options_gsa.redform=0;
@ -53,19 +60,20 @@ if options_gsa.redform,
options_gsa.ppost=0;
end
if ~(exist('Sampling_Function_2','file')==6 | exist('Sampling_Function_2','file')==2),
dynare_root = strrep(which('dynare.m'),'dynare.m','');
gsa_path = [dynare_root 'gsa'];
if exist(gsa_path)
addpath(gsa_path,path)
else
disp('Download pre-parsed mapping routines at:')
disp('http://eemc.jrc.ec.europa.eu/softwareDYNARE-Dowload.htm')
disp(' ' )
error('GSA routines missing!')
end
end
if options_gsa.morris,
if ~(exist('Sampling_Function_2','file')==6 | exist('Sampling_Function_2','file')==2),
dynare_root = strrep(which('dynare.m'),'dynare.m','');
gsa_path = [dynare_root 'gsa'];
if exist(gsa_path)
addpath(gsa_path,path)
else
disp('Download pre-parsed mapping routines at:')
disp('http://eemc.jrc.ec.europa.eu/softwareDYNARE-Dowload.htm')
disp(' ' )
error('Mapping routines missing!')
end
end
options_gsa.pprior=1;
options_gsa.ppost=0;
%options_gsa.stab=1;

121
matlab/initvalf.m Normal file
View File

@ -0,0 +1,121 @@
function initvalf(fname)
% function initvalf(fname,varargin)
% reads an initial path from the 'fname' file for exogenous and endogenous variables
%
% INPUTS
% fname: name of the function
% // period: period
% // varargin: list of arguments following period
%
% OUTPUTS
% none
%
% SPECIAL REQUIREMENTS
% none
%
% part of DYNARE, copyright Dynare Team (2003-2007)
% Gnu Public License.
global M_ oo_ options_
global y_start_date ex_start_date
series = 1;
if exist(fname) == 2
eval(fname);
elseif exist([fname '.xls']) == 2
[data,names_v]=xlsread([fname '.xls']);
series = 0;
elseif exist([fname '.mat']) == 2
load(fname);
end
% $$$ if length(period) == 2
% $$$ period = dy_date(period(1),period(2));
% $$$ end
% $$$
% $$$ if period - max(M_.maximum_lag,M_.maximum_lag) < 0
% $$$ error(['INITVALF_: not enough data points in database for number of' ...
% $$$ ' lags. Start later!'])
% $$$ end
% $$$
% $$$ if nargin > 2
% $$$ if strcmp(upper(varargin{1}),'SERIES')
% $$$ series = 1 ;
% $$$ elseif strcmp(upper(varargin{1}),'MAT')
% $$$ series = 0 ;
% $$$ else
% $$$ error(['INITVALF: unknown option ' varargin{1}])
% $$$ end
% $$$ else
% $$$ series = 0 ;
% $$$ end
% $$$
% $$$ y1 = floor((period-M_.maximum_lag)/M_.freq);
% $$$ p1 = period-M_.maximum_lag-M_.freq*y1;
% $$$ y_start_date(2) = M_.start_date(2) + p1-1;
% $$$ if y_start_date(2) > M_.freq
% $$$ y_start_date(2) = y_start_date(2) - M_.freq;
% $$$ y1 = y1 + 1;
% $$$ end
% $$$ y_start_date(1) = M_.start_date(1)+y1;
% $$$
% $$$ y1 = floor((period-M_.maximum_lag)/M_.freq);
% $$$ p1 = period-M_.maximum_lag-M_.freq*y1;
% $$$ ex_start_date(2) = M_.start_date(2) + p1-1;
% $$$ if y_start_date(2) > M_.freq
% $$$ ex_start_date(2) = ex_start_date(2) - M_.freq;
% $$$ y1 = y1 + 1;
% $$$ end
% $$$ ex_start_date(1) = M_.start_date(1)+y1;
% $$$
% $$$ clear y1, p1;
options_.initval_file = 1;
oo_.endo_simul = [];
oo_.exo_simul = [];
for i=1:size(M_.endo_names,1)
if series == 1
% x = eval([M_.endo_names(i,:) '(period-M_.maximum_lag:period+options_.periods+M_.maximum_lead-1);']);
x = eval(M_.endo_names(i,:));
oo_.endo_simul = [oo_.endo_simul; x'];
else
k = strmatch(upper(M_.endo_names(i,:)),names_v,'exact');
if isempty(k)
error(['INITVALF: ' M_.endo_names(i,:) ' not found'])
end
x = data(:,k);
% oo_.endo_simul = [oo_.endo_simul; x(period-M_.maximum_lag:period+options_.periods+M_.maximum_lead-1)'];
oo_.endo_simul = [oo_.endo_simul; x'];
end
end
for i=1:size(M_.exo_names,1)
if series == 1
% x = eval([M_.exo_names(i,:) '(period-M_.maximum_lag:period+options_.periods+M_.maximum_lead-1);']);
x = eval(M_.exo_names(i,:) );
oo_.exo_simul = [oo_.exo_simul x];
else
k = strmatch(upper(M_.exo_names(i,:)),names_v,'exact');
if isempty(k)
error(['INITVALF: ' M_.exo_names(i,:) ' not found'])
end
x = data(:,k);
% oo_.exo_simul = [oo_.exo_simul x(period-M_.maximum_lag:period+options_.periods+M_.maximum_lead-1)];
oo_.exo_simul = [oo_.exo_simul x];
end
end

View File

@ -1,113 +0,0 @@
function initvalf_(fname,period,varargin)
% function initvalf_(fname,period,varargin)
% reads an initial path from the 'fname' file for exogenous and endogenous variables
%
% INPUTS
% fname: name of the function
% period: period
% varargin: list of arguments following period
%
% OUTPUTS
% none
%
% SPECIAL REQUIREMENTS
% none
%
% part of DYNARE, copyright Dynare Team (2003-2007)
% Gnu Public License.
global M_ oo_ options_
global y_start_date ex_start_date
if ~isempty(strfind(upper(fname),'.XLS'))
[data,names_v]=xlsread(fname);
else
load(fname);
end
if length(period) == 2
period = dy_date(period(1),period(2));
end
if period - max(M_.maximum_lag,M_.maximum_lag) < 0
error(['INITVALF_: not enough data points in database for number of' ...
' lags. Start later!'])
end
if nargin > 2
if strcmp(upper(varargin{1}),'SERIES')
series = 1 ;
elseif strcmp(upper(varargin{1}),'MAT')
series = 0 ;
else
error(['INITVALF: unknown option ' varargin{1}])
end
else
series = 0 ;
end
y1 = floor((period-M_.maximum_lag)/M_.freq);
p1 = period-M_.maximum_lag-M_.freq*y1;
y_start_date(2) = M_.start_date(2) + p1-1;
if y_start_date(2) > M_.freq
y_start_date(2) = y_start_date(2) - M_.freq;
y1 = y1 + 1;
end
y_start_date(1) = M_.start_date(1)+y1;
y1 = floor((period-M_.maximum_lag)/M_.freq);
p1 = period-M_.maximum_lag-M_.freq*y1;
ex_start_date(2) = M_.start_date(2) + p1-1;
if y_start_date(2) > M_.freq
ex_start_date(2) = ex_start_date(2) - M_.freq;
y1 = y1 + 1;
end
ex_start_date(1) = M_.start_date(1)+y1;
clear y1, p1;
options_.initval_file = 1;
oo_.endo_simul = [];
oo_.exo_simul = [];
for i=1:size(M_.endo_names,1)
if series == 1
x = eval([M_.endo_names(i,:) '(period-M_.maximum_lag:period+options_.periods+M_.maximum_lead-1);']);
oo_.endo_simul = [oo_.endo_simul; x'];
else
k = strmatch(upper(M_.endo_names(i,:)),names_v,'exact');
if isempty(k)
error(['INITVALF: ' M_.endo_names(i,:) ' not found'])
end
x = data(:,k);
oo_.endo_simul = [oo_.endo_simul; x(period-M_.maximum_lag:period+options_.periods+M_.maximum_lead-1)'];
end
end
for i=1:size(M_.exo_name,1)
if series == 1
x = eval([M_.exo_name(i,:) '(period-M_.maximum_lag:period+options_.periods+M_.maximum_lead-1);']);
oo_.exo_simul = [oo_.exo_simul x];
else
k = strmatch(upper(M_.exo_name(i,:)),names_v,'exact');
if isempty(k)
error(['INITVALF: ' M_.exo_name(i,:) ' not found'])
end
x = data(:,k);
oo_.exo_simul = [oo_.exo_simul x(period-M_.maximum_lag:period+options_.periods+M_.maximum_lead-1)];
end
end

View File

@ -92,7 +92,7 @@ class ParsingDriver;
%token FORECAST
%token GAMMA_PDF GAUSSIAN_ELIMINATION GCC_COMPILER GMRES GRAPH
%token HISTVAL HP_FILTER HP_NGRID
%token INITVAL
%token INITVAL INITVAL_FILE
%token <string_val> INT_NUMBER
%token INV_GAMMA_PDF IRF
%token KALMAN_ALGO KALMAN_TOL
@ -152,6 +152,7 @@ statement : declaration
| markowitz
| model
| initval
| initval_file
| endval
| histval
| init_param
@ -363,11 +364,10 @@ comma_expression : expression
initval : INITVAL ';' initval_list END
{ driver.end_initval(); }
| INITVAL '(' initval_option ')' ';' initval_list END
{ driver.end_initval(); }
;
initval_option : FILENAME EQUAL NAME { driver.init_val_filename($3); };
initval_file : INITVAL_FILE '(' FILENAME EQUAL NAME ')' ';'
{ driver.initval_file($5); }
;
endval : ENDVAL ';' initval_list END { driver.end_endval(); };

View File

@ -129,6 +129,7 @@ int sigma_e = 0;
<INITIAL>bvar_density {BEGIN DYNARE_STATEMENT; return token::BVAR_DENSITY; }
<INITIAL>bvar_forecast {BEGIN DYNARE_STATEMENT; return token::BVAR_FORECAST; }
<INITIAL>dynare_sensitivity {BEGIN DYNARE_STATEMENT; return token::DYNARE_SENSITIVITY;}
<INITIAL>initval_file {BEGIN DYNARE_STATEMENT; return token::INITVAL_FILE;}
/* End of a Dynare statement */
<DYNARE_STATEMENT>; {
@ -207,6 +208,7 @@ int sigma_e = 0;
<DYNARE_STATEMENT>constant {return token::CONSTANT;}
<DYNARE_STATEMENT>noconstant {return token::NOCONSTANT;}
<DYNARE_STATEMENT>covar {return token::COVAR;}
<DYNARE_STATEMENT>filename {return token::FILENAME;}
<DYNARE_STATEMENT>bvar_prior_tau { return token::BVAR_PRIOR_TAU; }
<DYNARE_STATEMENT>bvar_prior_decay { return token::BVAR_PRIOR_DECAY; }
@ -234,7 +236,6 @@ int sigma_e = 0;
<DYNARE_BLOCK>periods {return token::PERIODS;}
<DYNARE_BLOCK>cutoff {return token::CUTOFF;}
<DYNARE_BLOCK>markowitz {return token::MARKOWITZ;}
<DYNARE_BLOCK>filename {return token::FILENAME;}
<DYNARE_BLOCK>gamma_pdf {return token::GAMMA_PDF;}
<DYNARE_BLOCK>beta_pdf {return token::BETA_PDF;}
<DYNARE_BLOCK>normal_pdf {return token::NORMAL_PDF;}

View File

@ -153,6 +153,21 @@ HistValStatement::writeOutput(ostream &output, const string &basename) const
}
}
InitvalFileStatement::InitvalFileStatement(const string* const filename_arg):
filename(*filename_arg)
{
}
void InitvalFileStatement::writeOutput(ostream &output, const string &basename) const
{
output << interfaces::comment() << endl
<< interfaces::comment() << "INITVAL_FILE statement" << endl
<< interfaces::comment() << endl
<< "options_.intival_file = 1;" << endl;
output << "initvalf('" << filename << "');" << endl;
}
HomotopyStatement::HomotopyStatement(const homotopy_values_type &homotopy_values_arg,
const SymbolTable &symbol_table_arg) :
homotopy_values(homotopy_values_arg),

View File

@ -305,10 +305,9 @@ ParsingDriver::init_val(string *name, NodeID rhs)
}
void
ParsingDriver::init_val_filename(string *filename)
ParsingDriver::initval_file(string *filename)
{
options_list.num_options["initval_file"] = "1";
options_list.string_options["initval_filename"] = *filename;
mod_file->addStatement(new InitvalFileStatement(filename));
delete filename;
}

View File

@ -92,6 +92,15 @@ public:
virtual void writeOutput(ostream &output, const string &basename) const;
};
class InitvalFileStatement : public Statement
{
private:
string filename;
public:
InitvalFileStatement(const string* const filename);
virtual void writeOutput(ostream &output, const string &basename) const;
};
class HomotopyStatement : public Statement
{
public:

View File

@ -174,7 +174,7 @@ public:
//! Sets the compiler type used in conjunction with SPARCE_DLL
void init_compiler(int compiler_type);
//! Sets the FILENAME for the initial value in initval
void init_val_filename(string *filename);
void initval_file(string *filename);
//! Declares an endogenous variable
void declare_endogenous(string *name, string *tex_name = new string);
//! Declares an exogenous variable