Use a structure for the dataset. Bug fixes.

time-shift
Stéphane Adjemian (Scylla) 2011-09-17 15:38:49 +02:00
parent e3bf749826
commit e9764d0538
6 changed files with 185 additions and 189 deletions

View File

@ -1,39 +0,0 @@
function [data_index,number_of_observations,no_more_missing_observations] = describe_missing_data(data,gend,nvarobs)
% Copyright (C) 2008-2009 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/>.
[variable_index,observation_index] = find(~isnan(data));
data_index = cell(1,gend);
missing_observations_counter = NaN(gend,1);
for obs=1:gend
idx = find(observation_index==obs);
tmp = variable_index(idx);
missing_observations_counter(obs,1) = nvarobs-length(tmp);
data_index(obs) = { tmp(:) };
end
missing_observations_counter = cumsum(missing_observations_counter);
number_of_observations = length(variable_index);
if ~missing_observations_counter
no_more_missing_observations = 0;
else
tmp = find(missing_observations_counter>=(gend*nvarobs-number_of_observations));
no_more_missing_observations = tmp(1);
end

View File

@ -55,6 +55,8 @@ addpath([dynareroot '/particle/'])
addpath([dynareroot '/gsa/'])
addpath([dynareroot '/utilities/doc/'])
addpath([dynareroot '/utilities/tests/'])
addpath([dynareroot '/utilities/dataset/'])
addpath([dynareroot '/utilities/general/'])
% For functions that exist only under some Octave versions
% or some MATLAB versions, and for which we provide some replacement functions

View File

@ -1,4 +1,4 @@
function dynare_estimation(var_list,varargin)
function dynare_estimation(var_list,dname)
% function dynare_estimation(var_list)
% runs the estimation of the model
%
@ -47,19 +47,20 @@ end
nnobs = length(nobs);
horizon = options_.forecast;
if nnobs > 1
if nargin > 1
dname = vargin{1};
else
if nargin<2 || ~exist(dname) || isempty(dname)
dname = M_.fname;
end
end
M_.dname = dname;
if nnobs > 1
for i=1:nnobs
options_.nobs = nobs(i);
dynare_estimation_1(var_list,[dname '_' int2str(nobs(i))]);
oo_recursive_{nobs(i)} = oo_;
end
else
dynare_estimation_1(var_list,varargin{:});
dynare_estimation_1(var_list,dname);
end
if nnobs > 1 && horizon > 0

View File

@ -31,21 +31,20 @@ function dynare_estimation_1(var_list_,dname)
global M_ options_ oo_ estim_params_ bayestopt_
if nargin > 1
[data,rawdata,xparam1] = dynare_estimation_init(var_list_,dname);
else
[data,rawdata,xparam1] = dynare_estimation_init(var_list_);
end
[dataset_,xparam1, M_, options_, oo_, estim_params_,bayestopt_, fake] = dynare_estimation_init(var_list_, dname, [], M_, options_, oo_, estim_params_, bayestopt_);
%% Set various options.
data = dataset_.data;
rawdata = dataset_.rawdata;
% Set various options.
options_ = set_default_option(options_,'mh_nblck',2);
options_ = set_default_option(options_,'nodiagnostic',0);
%% Set number of observations
% Set number of observations
gend = options_.nobs;
%% Set the number of observed variables.
% Set the number of observed variables.
n_varobs = size(options_.varobs,1);
%% Get the number of parameters to be estimated.
% Get the number of parameters to be estimated.
nvx = estim_params_.nvx; % Variance of the structural innovations (number of parameters).
nvn = estim_params_.nvn; % Variance of the measurement innovations (number of parameters).
ncx = estim_params_.ncx; % Covariance of the structural innovations (number of parameters).
@ -87,7 +86,7 @@ if options_.steadystate_flag% if the *_steadystate.m file is provided.
end
oo_.steady_state = ys;
else% if the steady state file is not provided.
[dd,info] = resol(oo_.steady_state,0);
[dd,info,M_,options_,oo_] = resol(0,M_,options_,oo_);
oo_.steady_state = dd.ys; clear('dd');
end
if all(abs(oo_.steady_state(bayestopt_.mfys))<1e-9)
@ -116,8 +115,11 @@ if options_.dsge_var
end
end
[data_index,number_of_observations,no_more_missing_observations] = describe_missing_data(data,gend,n_varobs);
missing_value = ~(number_of_observations == gend*n_varobs);
% [data_index,number_of_observations,no_more_missing_observations] = describe_missing_data(data,gend,n_varobs);
missing_value = dataset_.missing.state; %~(number_of_observations == gend*n_varobs);
data_index = [];
number_of_observations = gend*n_varobs;
no_more_missing_observations = [];
initial_estimation_checks(xparam1,gend,data,data_index,number_of_observations,no_more_missing_observations);

View File

@ -164,7 +164,7 @@ else% If estim_params_ is empty...
estim_params_.np = 0;
end
%% Is there a linear trend in the measurement equation?
% Is there a linear trend in the measurement equation?
if ~isfield(options_,'trend_coeffs') % No!
bayestopt_.with_trend = 0;
else% Yes!
@ -181,22 +181,22 @@ else% Yes!
end
end
%% Set the "size" of penalty.
% Set the "size" of penalty.
bayestopt_.penalty = 1e8;
%% Get informations about the variables of the model.
% Get informations about the variables of the model.
dr = set_state_space(oo_.dr,M_);
oo_.dr = dr;
nstatic = dr.nstatic; % Number of static variables.
npred = dr.npred; % Number of predetermined variables.
nspred = dr.nspred; % Number of predetermined variables in the state equation.
%% Test if observed variables are declared.
% Test if observed variables are declared.
if isempty(options_.varobs)
error('VAROBS is missing')
end
%% Setting resticted state space (observed + predetermined variables)
% Setting resticted state space (observed + predetermined variables)
var_obs_index = [];
k1 = [];
for i=1:n_varobs
@ -204,6 +204,21 @@ for i=1:n_varobs
k1 = [k1 strmatch(deblank(options_.varobs(i,:)),M_.endo_names, 'exact')];
end
% Define union of observed and state variables
k2 = union(var_obs_index',[dr.nstatic+1:dr.nstatic+dr.npred]', 'rows');
% Set restrict_state to postion of observed + state variables in expanded state vector.
oo_.dr.restrict_var_list = k2;
% set mf0 to positions of state variables in restricted state vector for likelihood computation.
[junk,bayestopt_.mf0] = ismember([dr.nstatic+1:dr.nstatic+dr.npred]',k2);
% Set mf1 to positions of observed variables in restricted state vector for likelihood computation.
[junk,bayestopt_.mf1] = ismember(var_obs_index,k2);
% Set mf2 to positions of observed variables in expanded state vector for filtering and smoothing.
bayestopt_.mf2 = var_obs_index;
bayestopt_.mfys = k1;
[junk,ic] = intersect(k2,nstatic+(1:npred)');
oo_.dr.restrict_columns = [ic; length(k2)+(1:nspred-npred)'];
k3 = [];
k3p = [];
if options_.selected_variables_only
@ -262,7 +277,7 @@ else
end;
%% Initialization with unit-root variables.
% 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);
@ -292,7 +307,7 @@ if ~isempty(options_.unit_root_vars)
options_.lik_init = 3;
end % if ~isempty(options_.unit_root_vars)
%% Test if the data file is declared.
% Test if the data file is declared.
if isempty(options_.datafile)
if gsa_flag
data = [];
@ -304,48 +319,61 @@ if isempty(options_.datafile)
end
end
%% If jscale isn't specified for an estimated parameter, use global option options_.jscale, set to 0.2, by default.
% 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;
%% Load and transform data.
rawdata = read_variables(options_.datafile,options_.varobs,[],options_.xls_sheet,options_.xls_range);
% Set the number of observations (nobs) and build a subsample between first_obs and nobs.
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,:);
% Take the log of the variables if needed
if options_.loglinear % If the model is log-linearized...
if ~options_.logdata % and if the data are not in logs, then...
rawdata = log(rawdata);
end
% Load and transform data.
transformation = [];
if options_.loglinear && ~options_.logdata
transformation = @log;
end
% Test if the observations are real numbers.
if ~isreal(rawdata)
error('There are complex values in the data! Probably a wrong transformation')
end
% Test for missing observations.
options_.missing_data = any(any(isnan(rawdata)));
% Prefilter the data if needed.
if options_.prefilter == 1
if options_.missing_data
bayestopt_.mean_varobs = zeros(n_varobs,1);
for variable=1:n_varobs
rdx = find(~isnan(rawdata(:,variable)));
m = mean(rawdata(rdx,variable));
rawdata(rdx,variable) = rawdata(rdx,variable)-m;
bayestopt_.mean_varobs(variable) = m;
end
else
bayestopt_.mean_varobs = mean(rawdata,1)';
rawdata = rawdata-repmat(bayestopt_.mean_varobs',gend,1);
end
end
% Transpose the dataset array.
data = transpose(rawdata);
xls.sheet = options_.xls_sheet;
xls.range = options_.xls_range;
if nargout>3
%% Compute the steady state:
if ~isfield(options_,'nobs')
options_.nobs = [];
end
dataset_ = initialize_dataset(options_.datafile,options_.varobs,options_.first_obs,options_.nobs,transformation,options_.prefilter,xls);
% $$$ rawdata = read_variables(options_.datafile,options_.varobs,[],options_.xls_sheet,options_.xls_range);
% $$$ % Set the number of observations (nobs) and build a subsample between first_obs and nobs.
% $$$ 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,:);
% $$$ % Take the log of the variables if needed
% $$$ if options_.loglinear % If the model is log-linearized...
% $$$ if ~options_.logdata % and if the data are not in logs, then...
% $$$ rawdata = log(rawdata);
% $$$ end
% $$$ end
% $$$ % Test if the observations are real numbers.
% $$$ if ~isreal(rawdata)
% $$$ error('There are complex values in the data! Probably a wrong transformation')
% $$$ end
% $$$ % Test for missing observations.
% $$$ options_.missing_data = any(any(isnan(rawdata)));
% $$$ % Prefilter the data if needed.
% $$$ if options_.prefilter == 1
% $$$ if options_.missing_data
% $$$ bayestopt_.mean_varobs = zeros(n_varobs,1);
% $$$ for variable=1:n_varobs
% $$$ rdx = find(~isnan(rawdata(:,variable)));
% $$$ m = mean(rawdata(rdx,variable));
% $$$ rawdata(rdx,variable) = rawdata(rdx,variable)-m;
% $$$ bayestopt_.mean_varobs(variable) = m;
% $$$ end
% $$$ else
% $$$ bayestopt_.mean_varobs = mean(rawdata,1)';
% $$$ rawdata = rawdata-repmat(bayestopt_.mean_varobs',gend,1);
% $$$ end
% $$$ end
% $$$ % Transpose the dataset array.
% $$$ data = transpose(rawdata);
if nargout>7
% Compute the steady state:
if options_.steadystate_flag% if the *_steadystate.m file is provided.
[ys,tchek] = feval([M_.fname '_steadystate'],...
[zeros(M_.exo_nbr,1);...
@ -364,7 +392,7 @@ if nargout>3
end
oo_.steady_state = ys;
else% if the steady state file is not provided.
[dd,info] = resol(oo_.steady_state,0);
[dd,info,M_,options_,oo_] = resol(0,M_,options_,oo_);
oo_.steady_state = dd.ys; clear('dd');
end
if all(abs(oo_.steady_state(bayestopt_.mfys))<1e-9)
@ -373,15 +401,16 @@ if nargout>3
options_.noconstant = 0;
end
[data_index,number_of_observations,no_more_missing_observations] = describe_missing_data(data,gend,n_varobs);
missing_value = ~(number_of_observations == gend*n_varobs);
% initial_estimation_checks(xparam1,gend,data,data_index,number_of_observations,no_more_missing_observations);
data_info.gend = gend;
data_info.data = data;
data_info.data_index = data_index;
data_info.number_of_observations = number_of_observations;
data_info.no_more_missing_observations = no_more_missing_observations;
data_info.missing_value = missing_value;
fake = [];
% $$$ [data_index,number_of_observations,no_more_missing_observations] = describe_missing_data(data,gend,n_varobs);
% $$$ missing_value = ~(number_of_observations == gend*n_varobs);
% $$$
% $$$ % initial_estimation_checks(xparam1,gend,data,data_index,number_of_observations,no_more_missing_observations);
% $$$
% $$$ data_info.gend = gend;
% $$$ data_info.data = data;
% $$$ data_info.data_index = data_index;
% $$$ data_info.number_of_observations = number_of_observations;
% $$$ data_info.no_more_missing_observations = no_more_missing_observations;
% $$$ data_info.missing_value = missing_value;
end

View File

@ -40,7 +40,8 @@ function [A,B,ys,info] = dynare_resolve(mode)
global oo_ M_ options_
[oo_.dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_);
[dr,info,M_,options_,oo_] = resol(0,M_,options_,oo_);
oo_.dr = dr;
if info(1) > 0
A = [];