-
Notifications
You must be signed in to change notification settings - Fork 190
/
EPSR.m
31 lines (27 loc) · 805 Bytes
/
EPSR.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function [Rhat, m, s] = EPSR(samples)
% EPSR "estimated potential scale reduction" statistic due to Gelman and Rubin.
%
% Inputs
% samples(i,j) for sample i, chain j
%
% Outputs
% Rhat = measure of scale reduction - value below 1.1 means converged:
% m = mean(samples)
% s = std(samples)
[n m] = size(samples);
meanPerChain = mean(samples,1); % each column of samples is a chain
meanOverall = mean(meanPerChain);
% Rhat only works if more than one chain is specified.
if m > 1
% between sequence variace
B = (n/(m-1))*sum( (meanPerChain-meanOverall).^2);
% within sequence variance
varPerChain = var(samples);
W = (1/m)*sum(varPerChain);
vhat = ((n-1)/n)*W + (1/n)*B;
Rhat = sqrt(vhat/(W+eps));
else
Rhat = nan;
end
m = meanOverall;
s = std(samples(:));