-
Notifications
You must be signed in to change notification settings - Fork 24
/
ecornerplot.m
198 lines (173 loc) · 6.17 KB
/
ecornerplot.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
function H=ecornerplot(m,varargin)
%% Corner plot with allowance for effective sample size
%
% ecornerplot(m,[parameter,value])
%
% INPUTS:
% m: a matrix of values that should be plotted in the corner plot.
%
%
% When m is a 3d matrix (ndims(m)==3), then it is assumed to have the form
% MxWxT as output from GWMCMC, where M is the number of parameters, W is
% the number of walkers, and T is the number of steps in each markov chain.
%
%
% NAMED PARAMETERS:
% range: Restrict visual limits to central [99.5] percentile.
% names: A cell of strings with labels for each parameter in m.
% ks: enable kernel smoothing density instead of histograms [false]
% support: a 2xM matrix with upper and lower limits.
% ess: effective sample size. [default: auto-determine ess using EACORR.]
% - used to adjust bandwidth estimates in kernel density estimates.
% scatter: show scatter plot instead of 2d-kernel density estimate [true if #points<2000].
% fullmatrix: display upper corner of plotmatrix. [false]
% color: A color-theme for the plot. [.5 .5 .5].
% grid: show grid. [false].
%
%
% Notes: The 2d kernel density contours are plotted as the highest density
% contours at intervals of 10%:20%:90%
%
% EXAMPLE:
% mu = [1 -1 1]; C= [.9 .4 .1; .4 .3 .2; .1 .2 1];
% m=mvnrnd(mu,C,6000);
% m(:,3)=exp(m(:,3)/2);
% ecornerplot(m,'support',[nan nan;nan nan; 0 nan]','ks',true);
%
% Aslak Grinsted 2015
if nargin==0
close all
m=randn(10000,4);%m(:,2)=m(:,2)+100000;
ecornerplot(m,'ks',true,'fullmatrix',false,'grid',true);
error('test mode... ')
return
end
p = inputParser;
p.addOptional('range',99.5,@isnumeric);
p.addOptional('names',{},@iscellstr);
p.addOptional('ks',false,@islogical); %TODO: allow definition of support?
p.addOptional('support',[]);
p.addOptional('grid',false,@islogical);
p.addOptional('scatter',nan,@islogical);
p.addOptional('fullmatrix',false,@islogical);
p.addOptional('color',[1 1 1]*.5,@(x)all(size(x)==[1 3]))
p.addOptional('ess',[]);
% p.addOptional('truth',[fa],@isnumeric);
p.parse(varargin{:});
p=p.Results;
if (size(m,1)<size(m,2))&&(ismatrix(m)), m=m'; end; %Consider this behaviour further....
if isempty(p.ess)
[~,~,p.ess]=eacorr(m);
p.ess=mean(p.ess);
end
if ndims(m)==3
m=m(:,:)';
end
M=size(m,2);
Np=size(m,1);
if p.ess>Np
error('Effective Sample Size (ess) must be smaller than number of samples')
end
if M>20
error('Too many dimensions. You probably don''t want to make that many subplots. ')
end
if isnan(p.scatter)
p.scatter=Np<2000;
end
p.range=prctile(m,[50+[-1 1]*p.range/2 0 100]); %first 2
rng=p.range(4,:)-p.range(3,:);
if isempty(p.support),p.support=nan(2,M);end
ix=isnan(p.support(1,:)); p.support(1,ix)=p.range(3,ix)-rng(ix)/4;
ix=isnan(p.support(2,:)); p.support(2,ix)=p.range(4,ix)+rng(ix)/4;
for ii=length(p.names)+1:M
p.names{ii}=sprintf('m_{%.0f}',ii);
end
% for ii=size(p.truth,2)+1:M
% p.truth(ii,:)=nan;
% end
if p.grid
p.grid='on';
else
p.grid='off';
end
clf
H=nan(M);
for r=1:M
for c=1:max(r,M*p.fullmatrix)
H(r,c)=subaxis(M,M,c,r,'s',0.01,'mb',0.12,'mt',0.05,'ml',0.12,'mr',0.0);
if c==r
if p.ks
[F,X,bw]=ksdensity(m(:,r),'support',p.support(:,r)); %TODO: use ESS
if p.ess<Np
[F,X,bw]=ksdensity(m(:,r),'width',bw*(Np/p.ess)^.2,'support',p.support(:,r)); %(the power 1/5 comes from examining the bandwidth calculation in ksdensity)
end
X=X([1,1:end,end]);F=[0,F,0];
else
[F,X]=histcounts(m(:,r),'Normalization','pdf');
X=X(ceil(0.5:0.5:end));
F=[0,F(ceil(0.5:0.5:end)),0];
end
fill(X,F,p.color,'edgecolor','none')
set(gca,'ytick',[],'YLim',[0 max(F)*1.1])
set(gca,'XGrid',p.grid)
else
if p.scatter
plot(m(:,c),m(:,r),'.','color',p.color)
else
% [N,C]=hist3(m(:,[c r]),[0 0]+ceil(sqrt(Np)/5));
% imagesc(C{1},C{2},N)
% caxis([0 max(N(:))]);
% axis xy
try
[~,N,X,Y]=kde2d(m(:,[c r]),2^9,p.support(1,[c r]),p.support(2,[c r]),p.ess);
% ns=sort(N(:));
% cint=interp1q(cumsum(ns)/sum(ns),ns,[0.05 0.17 0.50 0.83 0.95]');
hold on
%N=N/max(N(:));
%contourf(X,Y,N,(0.1:.2:1)','edgecolor',p.color); %TODO: try to make it HDI like???
N=N/sum(N(:));
NS=sort(N(:));
levels = interp1q(cumsum(NS),NS,(0.1:.2:1)')'; %HDI LEVELS
contourf(X,Y,N,levels,'edgecolor',p.color);
caxis([0,max(NS)])
catch
end
% pcolor(X,Y,N); %
% shading interp
end
set(gca,'XGrid',p.grid,'YGrid',p.grid)
if diff(p.range(1:2,r))>0, set(gca,'Ylim',p.range(1:2,r)); end
end
if r==M, xlabel(['^{ }' p.names{c} '_{ }']);end
if (c==1)&(r>1-p.fullmatrix), ylabel(['^{ }' p.names{r} '_{ }']);end
if diff(p.range(1:2,c))>0, set(gca,'Xlim',p.range(1:2,c)'); end
end
end
h=H(:,2:end);h(isnan(h))=[];
set(h,'YTickLabel',[])
h=H(1:M-1,:);h(isnan(h))=[];
set(h,'XTickLabel',[])
colormap(bsxfun(@minus,[1 1 1],linspace(0,.7,300)'));
%LINK the axes for zooming:
hlink={};
drawnow
lh=cellfun(@(x)double(x),get(H(~isnan(H)),'Xlabel'));
set(lh,'units','normalized')
set(lh,'position',min(cell2mat(get(lh,'position'))));
hlink{end+1}=linkprop(lh,'position');
lh=cellfun(@(x)double(x),get(H(~isnan(H)),'Ylabel'));
set(lh,'units','normalized');
set(lh,'position',min(cell2mat(get(lh,'position'))));
hlink{end+1}=linkprop(lh,'position');
for ii=1:M
h=H(:,ii); h(isnan(h))=[];
set(h,'XLimMode','manual')
hlink{end+1}=linkprop(h,'XLim');
h=H(ii,1:ii-1); h(isnan(h))=[];
set(h,'YLimMode','manual')
hlink{end+1}=linkprop(h,{'YLim','YTick'});
end
setappdata(gcf,'aplotmatrix_linkprop_handles',hlink)
if nargout==0
clearvars H
end