Make naming in GetPosteriorMeanVariance.m more expressive

time-shift
Johannes Pfeifer 2016-10-03 22:36:25 +02:00
parent b2984f43c9
commit d5233336b5
1 changed files with 17 additions and 18 deletions

View File

@ -1,6 +1,6 @@
function [mean,variance] = GetPosteriorMeanVariance(M,drop)
% Copyright (C) 2012, 2013 Dynare Team
% Copyright (C) 2012-2016 Dynare Team
%
% This file is part of Dynare.
%
@ -26,29 +26,28 @@ function [mean,variance] = GetPosteriorMeanVariance(M,drop)
NbrBlocks = record.Nblck;
mean = 0;
variance = 0;
z = [];
nkept = 0;
NbrKeptDraws = 0;
for i=1:NbrBlocks
n = 0;
NbrDrawsCurrentBlock = 0;
for j=1:NbrFiles
o = load([BaseName '_mh' int2str(j) '_blck' int2str(i)]);
m = size(o.x2,1);
if n + m <= drop*NbrDraws
n = n + m;
o = load([BaseName '_mh' int2str(j) '_blck' int2str(i),'.mat']);
NbrDrawsCurrentFile = size(o.x2,1);
if NbrDrawsCurrentBlock + NbrDrawsCurrentFile <= drop*NbrDraws
NbrDrawsCurrentBlock = NbrDrawsCurrentBlock + NbrDrawsCurrentFile;
continue
elseif n < drop*NbrDraws
k = ceil(drop*NbrDraws - n + 1);
x2 = o.x2(k:end,:);
elseif NbrDrawsCurrentBlock < drop*NbrDraws
FirstDraw = ceil(drop*NbrDraws - NbrDrawsCurrentBlock + 1);
x2 = o.x2(FirstDraw:end,:);
else
x2 = o.x2;
end
z =[z; x2];
p = size(x2,1);
mean = (nkept*mean + sum(x2)')/(nkept+p);
x = bsxfun(@minus,x2,mean');
variance = (nkept*variance + x'*x)/(nkept+p);
n = n + m;
nkept = nkept + p;
NbrKeptDrawsCurrentFile = size(x2,1);
%recursively compute mean and variance
mean = (NbrKeptDraws*mean + sum(x2)')/(NbrKeptDraws+NbrKeptDrawsCurrentFile);
x2Demeaned = bsxfun(@minus,x2,mean');
variance = (NbrKeptDraws*variance + x2Demeaned'*x2Demeaned)/(NbrKeptDraws+NbrKeptDrawsCurrentFile);
NbrDrawsCurrentBlock = NbrDrawsCurrentBlock + NbrDrawsCurrentFile;
NbrKeptDraws = NbrKeptDraws + NbrKeptDrawsCurrentFile;
end
end