diff --git a/matlab/+mom/data_moments.m b/matlab/+mom/get_data_moments.m similarity index 92% rename from matlab/+mom/data_moments.m rename to matlab/+mom/get_data_moments.m index 140262108..0a2ced1c4 100644 --- a/matlab/+mom/data_moments.m +++ b/matlab/+mom/get_data_moments.m @@ -1,5 +1,5 @@ -function [dataMoments, m_data] = data_moments(data, oo_, matched_moments_, options_mom_) -% [dataMoments, m_data] = data_moments(data, oo_, matched_moments_, options_mom_) +function [dataMoments, m_data] = get_data_moments(data, oo_, matched_moments_, options_mom_) +% [dataMoments, m_data] = get_data_moments(data, oo_, matched_moments_, options_mom_) % This function computes the user-selected empirical moments from data % ========================================================================= % INPUTS @@ -13,10 +13,10 @@ function [dataMoments, m_data] = data_moments(data, oo_, matched_moments_, optio % o m_data [T x numMom] selected empirical moments at each point in time % ------------------------------------------------------------------------- % This function is called by -% o mom.run.m -% o mom.objective_function.m +% o mom.run +% o mom.objective_function % ========================================================================= -% Copyright © 2020-2021 Dynare Team +% Copyright © 2020-2023 Dynare Team % % This file is part of Dynare. % @@ -66,7 +66,4 @@ for jm = 1:options_mom_.mom.mom_nbr end m_data_tmp(isnan(m_data_tmp)) = dataMoments(jm,1); m_data(:,jm) = m_data_tmp; -end - - -end %function end \ No newline at end of file +end \ No newline at end of file diff --git a/matlab/+mom/objective_function.m b/matlab/+mom/objective_function.m index f0b7f814c..419c7112b 100644 --- a/matlab/+mom/objective_function.m +++ b/matlab/+mom/objective_function.m @@ -247,7 +247,7 @@ if strcmp(options_mom_.mom.mom_method,'SMM') if options_mom_.prefilter y_sim = bsxfun(@minus, y_sim, mean(y_sim,1)); end - oo_.mom.model_moments = mom.data_moments(y_sim, oo_, M_.matched_moments, options_mom_); + oo_.mom.model_moments = mom.get_data_moments(y_sim, oo_, M_.matched_moments, options_mom_); end diff --git a/matlab/+mom/run.m b/matlab/+mom/run.m index 777b43fcf..838cec6ed 100644 --- a/matlab/+mom/run.m +++ b/matlab/+mom/run.m @@ -414,33 +414,33 @@ end % ------------------------------------------------------------------------- -% Step 4: Checks and transformations for data +% datafile: checks and transformations % ------------------------------------------------------------------------- - -% Check if datafile has same name as mod file -[~,name,~] = fileparts(options_mom_.datafile); -if strcmp(name,M_.fname) - error('method_of_moments: Data-file and mod-file are not allowed to have the same name. Please change the name of the data file.') -end - % Build dataset -dataset_ = makedataset(options_mom_); - -% set options for old interface from the ones for new interface -if ~isempty(dataset_) - options_mom_.nobs = dataset_.nobs; +if strcmp(options_mom_.mom.mom_method,'GMM') || strcmp(options_mom_.mom.mom_method,'SMM') + % Check if datafile has same name as mod file + [~,name,~] = fileparts(options_mom_.datafile); + if strcmp(name,M_.fname) + error('method_of_moments: ''datafile'' and mod file are not allowed to have the same name; change the name of the ''datafile''!') + end + dataset_ = makedataset(options_mom_); + % set options for old interface from the ones for new interface + if ~isempty(dataset_) + options_mom_.nobs = dataset_.nobs; + end + % Check length of data for estimation of second moments + if options_mom_.ar > options_mom_.nobs+1 + error('method_of_moments: Dataset is too short to compute higher than first moments!'); + end + % Provide info on data moments handling + fprintf('Computing data moments. Note that NaN values in the moments (due to leads and lags or missing data) are replaced by the mean of the corresponding moment.\n'); + % Get data moments for the method of moments + [oo_.mom.data_moments, oo_.mom.m_data] = mom.get_data_moments(dataset_.data, oo_, M_.matched_moments, options_mom_); + if ~isreal(dataset_.data) + error('method_of_moments: The data moments contain complex values!') + end end -% Check length of data for estimation of second moments -if options_mom_.ar > options_mom_.nobs+1 - error('method_of_moments: Data set is too short to compute second moments'); -end - -% Provide info on data moments handling -fprintf('Computing data moments. Note that NaN values in the moments (due to leads and lags or missing data) are replaced by the mean of the corresponding moment\n'); - -% Get data moments for the method of moments -[oo_.mom.data_moments, oo_.mom.m_data] = mom.data_moments(dataset_.data, oo_, M_.matched_moments, options_mom_); % Get shock series for SMM and set variance correction factor if strcmp(options_mom_.mom.mom_method,'SMM')