From b4204f8b9ecbfe63b660bb019169e2f352730f8c Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Thu, 11 Jan 2018 22:08:03 +0100 Subject: [PATCH] Make sure that mean and covariance of data are correctly computed with only one observation Need to always compute mean along first dimension (cherry picked from commit 2d371b1997f5fa07fcbbf47e5923d7817d07c6b9) --- matlab/utilities/dataset/makedataset.m | 2 +- matlab/utilities/dataset/nancovariance.m | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/matlab/utilities/dataset/makedataset.m b/matlab/utilities/dataset/makedataset.m index c5c244b54..a26a074c5 100644 --- a/matlab/utilities/dataset/makedataset.m +++ b/matlab/utilities/dataset/makedataset.m @@ -267,7 +267,7 @@ else end % Compute the empirical mean of the observed variables. -DatasetInfo.descriptive.mean = nanmean(DynareDataset.data); +DatasetInfo.descriptive.mean = nanmean(DynareDataset.data,1); % Compute the empirical covariance matrix of the observed variables. DatasetInfo.descriptive.covariance = nancovariance(DynareDataset.data); diff --git a/matlab/utilities/dataset/nancovariance.m b/matlab/utilities/dataset/nancovariance.m index 8e2e2b227..09f2a38af 100644 --- a/matlab/utilities/dataset/nancovariance.m +++ b/matlab/utilities/dataset/nancovariance.m @@ -51,7 +51,7 @@ function CovarianceMatrix = nancovariance(data) CovarianceMatrix = zeros(size(data,2)); if isanynan(data) - data = bsxfun(@minus,data,nanmean(data)); + data = bsxfun(@minus,data,nanmean(data,1)); for i=1:size(data,2) for j=i:size(data,2) CovarianceMatrix(i,j) = nanmean(data(:,i).*data(:,j)); @@ -61,7 +61,7 @@ if isanynan(data) end end else - data = bsxfun(@minus,data,mean(data)); + data = bsxfun(@minus,data,mean(data,1)); CovarianceMatrix = (transpose(data)*data)/size(data,1); end -- GitLab