-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathheatmap.m
245 lines (235 loc) · 6 KB
/
heatmap.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
function p=gkde2(x,p)
% function p=gkde2(x,N,h,xylim,alpha)
% GKDE2 Bivariate Gaussian Kernel Density Estimation
%
% Usage:
% p = gkdeb(d) returns an estmate of the probability density function (pdf)
% and the cumulative distribution function (cdf) of the given
% random data d in p, where p.pdf and p.cdf are the pdf and cdf
% vectors estimated at locations of (p.x, p.y), respectively
% and p.h is the bandwidth used for the estimation.
% p = gkdeb(d,p) specifies optional parameters for the estimation:
% p.h - 1x2 vector for bandwidth, [hx, hy]
% p.xy - Nx2 matrix for locations to make estimation
% p.xylim - 1x4 location limits as [xmin ymin xmax ymax]
% p.alpha - a vector of probabilities to calculate inverse cdfs.
%
% Without output, gkdeb(d) and gkdeb(d,p) will disply the pdf and cdf plots.
%
% See also: hist, histc, ksdensity, ecdf, cdfplot, ecdfhist
% Example 1: two-peak normal distribution
%{
gkde2([randn(500,2);randn(500,1)+5 randn(500,1)]);
%}
% Example 2: four-peak normal distribution
%{
gkde2([randn(100,1), (randn(100,1)-10)*2;
randn(100,1)+10, randn(100,1);
randn(100,1)+10, (randn(100,1)-10)*2;
randn(100,2)]);
%}
% Example 3: uniform distribution
%{
clear p
p.xylim=[0 0 1 1];
gkde2(rand(500,2),p);
%}
% Example 4: dumbbell density
%{
X1=[randn(2000,1)-2 randn(2000,1)+2];
X2=randn(2000,2)*inv([0.8 -0.72;-0.72 0.8]);
X3=[randn(2000,1)+2 randn(2000,1)-2];
p=gkde2(((X1+X3)*4+X2*3)/11);
figure
contour(p.x,p.y,p.pdf,20);
figure
contour(p.x,p.y,p.cdf,20);
%}
% V2.1 by Yi Cao at Cranfield University on 10th August 2013
%
% Check input and output
error(nargchk(1,2,nargin));
error(nargoutchk(0,1,nargout));
[nr, nc] = size(x);
if nr ~= 2 && nc ~= 2
error('Bivariate data required!');
end
if nc>nr
x=x';
end
n=length(x);
% Default parameters
if nargin<2
N=50;
% rule of thumb bandwidth suggested by Bowman and Azzalini (1997) p.31
h=median(abs(x-repmat(median(x),n,1)))/0.6745*(1/n)^(1/6);
xylim=[-inf -inf inf inf];
xmax=max(x);
xmin=min(x);
xmax=min([xmax+3*h;xylim(3:4)]);
xmin=max([xmin-3*h;xylim(1:2)]);
x1=linspace(xmin(1),xmax(1),N);
x2=linspace(xmin(2),xmax(2),N);
[px,py]=meshgrid(x1,x2);
X=[px(:) py(:)];
p.x=px;
p.y=py;
p.h=h;
p.xylim=xylim;
dxdz=ones(N*N,1);
p.N=N;
else % check parameters with default setting
[p,x,dxdz,X]=checkp(x,p);
N=p.N;
xmin = min(X);
xmax = max(X);
end
dxdz=dxdz/n;
p.pdf=zeros(N,N);
p.cdf=zeros(N,N);
N2=N*N;
% Bivariant Gaussian kernel function
kerf=@(t)exp(-sum(t.*t,2)/2)/(2*pi);
% cdf estimation based on A. Genz, Statistics and Computing 14:251260, 2004
ckerf=@(t)prod(erfc(-t ./ sqrt(2))/2,2);
for k=1:N2
h=min([min([X(k,:)-xmin;p.h]);min([xmax-X(k,:);p.h])]);
if prod(h)<sqrt(eps)
p.pdf(k)=0;
r = mod(k-1,N)+1;
c = ceil(k/N);
p.cdf(k)=max(max(p.cdf(1:r,1:c)));
continue
end
z=(X(k+zeros(n,1),:)-x)./h(ones(n,1),:);
p.pdf(k)=sum(kerf(z))/prod(h);
p.cdf(k)=sum(ckerf(z));
end
% p.cdf=cumsum(p.pdf);
% p.cdf=cumsum(p.cdf,2)*prod((xmax-xmin)/N);
p.pdf(:)=p.pdf(:).*dxdz(:);
p.cdf=p.cdf/n;
p.cdf(p.cdf>1)=1;
p.cdf(p.cdf<0)=0;
% calculate icdf
if isfield(p,'alpha')
C=contour(p.x,p.y,p.cdf,[p.alpha p.alpha]);
p.icdfx = min(C(1,2:end));
p.icdfy = min(C(2,2:end));
end
% Plot results
if ~nargout
xLim=[min(p.x(:)) max(p.x(:))];
yLim=[min(p.y(:)) max(p.y(:))];
set(gca,'XLim',xLim,'YLim',yLim,'ZLim',[0 1.1])
contour(p.x,p.y,p.pdf);
hold on;
plot(x(:,1),x(:,2), 'LineWidth', 1);
hold on;
xlabel('x');
ylabel('y')
zlabel('p(x)')
title('Contour of Probability Density Function');
end
function [p,x,dxdz,z]=checkp(x,p)
%check structure p
if ~isstruct(p)
error('p is not a structure.');
end
if ~isfield(p,'xylim')
p.xylim=[-Inf -Inf Inf Inf];
end
if ~isfield(p,'N')
p.N=50;
end
px.N=p.N;
py.N=p.N;
if isfield(p,'h')
px.h=p.h(1);
py.h=p.h(2);
end
% px = p;
% py = p;
if isfield(p,'x')
px.x = p.x(:);
end
if isfield(p,'y')
py.x=p.y(:);
end
px.lB = p.xylim(1);
py.lB = p.xylim(2);
px.uB = p.xylim(3);
py.uB = p.xylim(4);
[px,X,dXdz,zx] = bounded(x(:,1),px);
[py,Y,dYdz,zy] = bounded(x(:,2),py);
if isfield(p,'x') && isfield(p,'y')
p.x = reshape(zx,p.N,[]);
p.y = reshape(zy,p.N,[]);
z = [px.x py.x];
dxdz=dXdz.*dYdz;
else
[p.x,p.y]=meshgrid(zx,zy);
[zx,zy]=meshgrid(px.x,py.x);
z = [zx(:) zy(:)];
dxdz=dYdz(:)*dXdz(:)';
end
p.h = [px.h py.h];
x = [X Y];
function [p,x,dxdz,z]=bounded(x,p)
n=length(x);
if p.lB>-Inf || p.uB<Inf
if p.lB==-Inf
% function for (dx_bound)/(dx_unbounded)
dx=@(t)1./(p.uB-t);
% function for converting x_bounded to x_unbounded
y=@(t)-log(p.uB-t);
% function for converting x_unbounded to x_bounded
zf=@(t)(p.uB-exp(-t));
elseif p.uB==Inf
dx=@(t)1./(t-p.lB);
y=@(t)log(t-p.lB);
zf=@(t)exp(t)+p.lB;
else
dx=@(t)(p.uB-p.lB)./(t-p.lB)./(p.uB-t);
y=@(t)log((t-p.lB)./(p.uB-t));
zf=@(t)(exp(t)*p.uB+p.lB)./(exp(t)+1);
end
x=y(x);
n=numel(x);
if ~isfield(p,'h')
p.h=median(abs(x-median(x)))/0.6745*(4/3/n)^0.2;
end
h=p.h;
if ~isfield(p,'x')
N=p.N;
xmax=max(x)+3*h;
xmin=min(x)-3*h;
p.x=linspace(xmin,xmax,N);
z=zf(p.x);
else
z=p.x;
p.x=y(p.x);
end
dxdz=dx(z);
else
if ~isfield(p,'h')
p.h=median(abs(x-median(x)))/0.6745*(4/3/n)^0.2;
end
error(varchk(eps, inf, p.h, 'Bandwidth, p.h is not positive.'));
if ~isfield(p,'x')
N=p.N;
xmax=max(x);
xmin=min(x);
xmax=min(xmax+3*p.h,p.uB);
xmin=max(xmin-3*p.h,p.lB);
p.x=linspace(xmin,xmax,N);
end
dxdz=ones(size(p.x(:)));
z=p.x;
end
function msg=varchk(low,high,n,msg)
% check if variable n is not between low and high, returns msg, otherwise
% empty matrix
if all(n>=low) && all(n<=high)
msg=[];
end