From 306137bd2b490187803ed3a376f8e93d3a947f04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?= Date: Sat, 17 Sep 2011 23:58:20 +0200 Subject: [PATCH] Reverted commit 3bf482ae5c3fb0252363d59a6b345b74a6a97e18. Added missing routine nanmean (computes the mean of the columns of a matrix with some NaNs). --- matlab/utilities/dataset/initialize_dataset.m | 10 +-- matlab/utilities/general/nandemean.m | 24 ++++--- matlab/utilities/general/nanmean.m | 62 +++++++++++++++++++ 3 files changed, 81 insertions(+), 15 deletions(-) create mode 100644 matlab/utilities/general/nanmean.m diff --git a/matlab/utilities/dataset/initialize_dataset.m b/matlab/utilities/dataset/initialize_dataset.m index c77f5e11f..483cd5a23 100644 --- a/matlab/utilities/dataset/initialize_dataset.m +++ b/matlab/utilities/dataset/initialize_dataset.m @@ -3,7 +3,7 @@ function dataset_ = initialize_dataset(datafile,varobs,first,nobs,transformation % Copyright (C) 2011 Dynare Team % stephane DOT adjemian AT univ DASH lemans DOT fr -% +% % This file is part of Dynare. % % Dynare is free software: you can redistribute it and/or modify @@ -54,7 +54,7 @@ else dataset_.rawdata = arrayfun(transformation,rawdata); end -% Test if the observations are real numbers. +% Test if the observations are real numbers. if ~isreal(dataset_.rawdata) error('Estimation:: There are complex values in the data! Probably a wrong (log) transformation...') end @@ -66,16 +66,16 @@ if dataset_.missing.state dataset_.missing.aindex = i; dataset_.missing.vindex = j; dataset_.missing.number_of_observations = n; - dataset_.missing.no_more_missing_observations = s; + dataset_.missing.no_more_missing_observations = s; else dataset_.missing.aindex = []; dataset_.missing.vindex = []; dataset_.missing.number_of_observations = []; - dataset_.missing.no_more_missing_observations = []; + dataset_.missing.no_more_missing_observations = []; end % Compute the empirical mean of the observed variables.. -%dataset_.descriptive.mean = nanmean(dataset_.rawdata); +dataset_.descriptive.mean = nanmean(dataset_.rawdata); % Prefilter the data if needed. if prefilter == 1 diff --git a/matlab/utilities/general/nandemean.m b/matlab/utilities/general/nandemean.m index d7561516f..55afdb0f9 100644 --- a/matlab/utilities/general/nandemean.m +++ b/matlab/utilities/general/nandemean.m @@ -1,35 +1,37 @@ function c = nandemean(x) % Removes the mean of each column of a matrix with some NaNs. - + %@info: %! @deftypefn {Function File} {@var{c} =} nandemean (@var{x}) %! @anchor{nandemean} +%! @sp 1 %! This function removes the mean of each column of a matrix with some NaNs. -%! +%! @sp 2 %! @strong{Inputs} %! @table @var %! @item x %! Matlab matrix (T-by-N). %! @end table -%! +%! @sp 2 %! @strong{Outputs} %! @table @var %! @item c %! Matlab matrix (T-by-N). The demeaned x matrix. %! @end table -%! -%! @strong{This function is called by:} -%! @ref{compute_cova}, @ref{compute_acov}, @ref{compute_std}. -%! +%! @sp 2 +%! @strong{This function is called by:} +%! @sp 1 +%! @ref{compute_cova}, @ref{compute_acov}, @ref{compute_std} +%! @sp 2 %! @strong{This function calls:} -%! @ref{ndim}, +%! @sp 1 +%! @ref{ndim} %! %! @end deftypefn %@eod: % Copyright (C) 2011 Dynare Team -% stephane DOT adjemian AT ens DOT fr -% +% % This file is part of Dynare. % % Dynare is free software: you can redistribute it and/or modify @@ -45,6 +47,8 @@ function c = nandemean(x) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see . +% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr + if ndim(x)==1 c = x-nanmean(x); elseif ndim(x)==2 diff --git a/matlab/utilities/general/nanmean.m b/matlab/utilities/general/nanmean.m new file mode 100644 index 000000000..3d4515018 --- /dev/null +++ b/matlab/utilities/general/nanmean.m @@ -0,0 +1,62 @@ +function y = nanmean(x) +% Computes the mean of each column of a matrix with some NaNs. + +%@info: +%! @deftypefn {Function File} {@var{y} =} nanmean (@var{x}) +%! @anchor{nandemean} +%! @sp 1 +%! Computes the mean of each column of a matrix with some NaNs. +%! @sp 2 +%! @strong{Inputs} +%! @table @ @var +%! @item x +%! Matlab matrix (T-by-N). +%! @end table +%! @sp 2 +%! @strong{Outputs} +%! @table @ @var +%! @item y +%! Matlab matrix (T-by-N). The demeaned x matrix. +%! @end table +%! @sp 2 +%! @strong{This function is called by:} +%! @sp 1 +%! @ref{compute_cova}, @ref{compute_acov}, @ref{compute_std}, @ref{nandemean} +%! @sp 2 +%! @strong{This function calls:} +%! @sp 1 +%! @ref{ndim}, +%! +%! @end deftypefn +%@eod: + +% Copyright (C) 2011 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 . + +% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr + +switch ndim(x) + case 1 + y = mean(x(find(~isnan(x)))); + case 2 + y = NaN(1,size(x,2)); + for i = 1:size(x,2) + y(i) = mean(x(find(~isnan(x(:,i))),i)); + end + otherwise + error('descriptive_statistics::nanmean:: This function is not implemented for arrays with dimension greater than two!') +end \ No newline at end of file