Commit e9764d05 authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Use a structure for the dataset. Bug fixes.

parent e3bf7498
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
\ No newline at end of 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
......
function dynare_estimation(var_list,varargin)
function dynare_estimation(var_list,dname)
% function dynare_estimation(var_list)
% runs the estimation of the model
%
%
% INPUTS
% var_list: selected endogenous variables vector
%
%
% OUTPUTS
% none
%
......@@ -47,19 +47,20 @@ end
nnobs = length(nobs);
horizon = options_.forecast;
if nargin<2 || ~exist(dname) || isempty(dname)
dname = M_.fname;
end
M_.dname = dname;
if nnobs > 1
if nargin > 1
dname = vargin{1};
else
dname = M_.fname;
end
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
......@@ -69,12 +70,12 @@ if nnobs > 1 && horizon > 0
rawdata = rawdata(options_.first_obs:options_.first_obs+gend-1,:);
% Take the log of the variables if needed
if options_.loglinear && ~options_.logdata % and if the data are not in logs, then...
rawdata = log(rawdata);
rawdata = log(rawdata);
end
endo_names = M_.endo_names;
n_varobs = size(options_.varobs,1);
if isempty(var_list)
var_list = endo_names;
nvar = size(endo_names,1);
......@@ -89,7 +90,7 @@ if nnobs > 1 && horizon > 0
else
error(['Estimation: ' var_list(i,:) ' isn''t an endogenous' ...
'variable'])
end
end
end
end
......@@ -100,8 +101,8 @@ if nnobs > 1 && horizon > 0
end
if ~isempty(iobs)
IdObs(j,1) = iobs;
end
end
end
end
k = 3+min(nobs(end)-nobs(1)+horizon, ...
size(rawdata,1)-nobs(1));
......@@ -125,7 +126,7 @@ if nnobs > 1 && horizon > 0
offsetx = 3;
else
offsetx = 0;
end
end
vname = deblank(var_list(i,:));
maxlag = M_.maximum_lag;
for j=1:nnobs
......
This diff is collapsed.
......@@ -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
xls.sheet = options_.xls_sheet;
xls.range = options_.xls_range;
if ~isfield(options_,'nobs')
options_.nobs = [];
end
% Transpose the dataset array.
data = transpose(rawdata);
if nargout>3
%% Compute the steady state:
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
......@@ -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 = [];
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment