-
Notifications
You must be signed in to change notification settings - Fork 10
/
plotMarginal.m
127 lines (104 loc) · 3.86 KB
/
plotMarginal.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
function hline = plotMarginal(result,dim,plotOptions)
% plots the marginal for a single dimension
%function hline = plotMarginal(result,dim,plotOptions)
% result should be a result struct from the main psignifit routine
% dim is the parameter to plot
% 1=threshold,2=width,3=lambda,4=gamma,5=eta
% plotOPtions a struct with additional options for the plot
% This always plots into gca
% convert strings to dimension number
if ischar(dim)
dim = strToDim(dim);
end
assert(length(result.marginals{dim})>1,'The parameter you wanted to plot was fixed in the analysis!');
if ~exist('plotOptions','var'), plotOptions = struct; end
if ~isfield(plotOptions,'lineColor'), plotOptions.lineColor = [0,105/255,170/255];end
if ~isfield(plotOptions,'lineWidth'), plotOptions.lineWidth = 2; end
if ~isfield(plotOptions,'xLabel'), plotOptions.xLabel = []; end
if ~isfield(plotOptions,'yLabel'), plotOptions.yLabel = 'Marginal Density'; end
if ~isfield(plotOptions,'labelSize'), plotOptions.labelSize = 15; end
if ~isfield(plotOptions,'tufteAxis'), plotOptions.tufteAxis = false; end
if ~isfield(plotOptions,'prior'), plotOptions.prior = true; end
if ~isfield(plotOptions,'priorColor'), plotOptions.priorColor = [.7,.7,.7]; end
if ~isfield(plotOptions,'CIpatch'), plotOptions.CIpatch = true; end
if ~isfield(plotOptions,'plotPE'), plotOptions.plotPE = true; end
if isfield(plotOptions,'h')
h = plotOptions.h;
else
h = gca;
end
assert(ishandle(h),'Invalid axes handle provided to plot in.')
axes(h);
if ~exist('dim','var'), dim = 1; end
if isempty(plotOptions.xLabel)
switch dim
case 1
plotOptions.xLabel='Threshold';
case 2
plotOptions.xLabel='Width';
case 3
plotOptions.xLabel='\lambda';
case 4
plotOptions.xLabel='\gamma';
case 5
plotOptions.xLabel='\eta';
end
end
if exist('h','var') && ~isempty(h)
axes(h);
else
h=gca;
axes(h);
end
x = result.marginalsX{dim};
marginal = result.marginals{dim};
CI = result.conf_Intervals(dim,:);
Fit = result.Fit(dim);
holdState = ishold(h);
if ~holdState
cla(h);
end
hold on
% patch for confidence region
if plotOptions.CIpatch
xCI = [CI(1);x(x>=CI(1) & x<=CI(2));CI(2);CI(2);CI(1)];
yCI = [interp1(x,marginal,CI(1)); ...
marginal(x>=CI(1) & x<=CI(2)); ...
interp1(x,marginal,CI(2));0;0];
patch(xCI,yCI,.5*plotOptions.lineColor+.5*[1,1,1],'EdgeColor',.5*plotOptions.lineColor+.5*[1,1,1]);
end
% plot prior
if plotOptions.prior
% plot prior
xprior = linspace(min(x),max(x),1000);
plot(xprior,result.options.priors{dim}(xprior),'--','Color',plotOptions.priorColor);
end
%posterior
hline = plot(x,marginal,'LineWidth',plotOptions.lineWidth,'Color',plotOptions.lineColor);
% point estimate
if plotOptions.plotPE
plot([Fit;Fit],[0;interp1(x,marginal,Fit)],'k');
end
if plotOptions.tufteAxis
tufteaxis(unique([min(x),CI(1),Fit,CI(2),max(x)]),[0,max(marginal)]);
else
xlim([min(x),max(x)]);
ylim([0,1.1*max(marginal)]);
end
hlabel = xlabel(plotOptions.xLabel,'FontSize',plotOptions.labelSize);
set(hlabel,'Visible','on')
if plotOptions.tufteAxis
set(hlabel,'Position',get(hlabel,'Position') - [0 .05 0]);
end
hlabel = ylabel(plotOptions.yLabel,'FontSize',plotOptions.labelSize);
set(hlabel,'Visible','on')
if plotOptions.tufteAxis
set(hlabel,'Position',get(hlabel,'Position') - [.05 0 0])
else
set(gca,'TickDir','out')
box off
end
%% toggle back hold state
if ~holdState
hold off
end