-
Notifications
You must be signed in to change notification settings - Fork 1
/
computeNMI.m
55 lines (45 loc) · 1.81 KB
/
computeNMI.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %
% This is a demo for the PTA and PTGP algorithms. If you find the %
% code useful for your research,please cite the paper below. %
% %
% Dong Huang, Jian-Huang Lai, and Chang-Dong Wang. Robust ensemble %
% clustering using probability trajectories, IEEE Transactions on %
% Knowledge and Data Engineering, 2016, 28(5), pp.1312-1326. %
% %
% The code has been tested in Matlab R2014a and Matlab R2015a on a %
% workstation with Windows Server 2008 R2 64-bit. %
% %
% https://www.researchgate.net/publication/284259332 %
% %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function allScores = computeNMI(results,gt)
allScores = zeros(size(results,2),1);
for i = 1:size(results,2)
if min(results(:,i))>0
allScores(i) = NMImax(results(:,i), gt);
end
end
function NMImax = NMImax(x, y)
assert(numel(x) == numel(y));
n = numel(x);
x = reshape(x,1,n);
y = reshape(y,1,n);
l = min(min(x),min(y));
x = x-l+1;
y = y-l+1;
k = max(max(x),max(y));
idx = 1:n;
Mx = sparse(idx,x,1,n,k,n);
My = sparse(idx,y,1,n,k,n);
Pxy = nonzeros(Mx'*My/n); %joint distribution of x and y
Hxy = -dot(Pxy,log(Pxy+eps));
Px = mean(Mx,1);
Py = mean(My,1);
% entropy of Py and Px
Hx = -dot(Px,log(Px+eps));
Hy = -dot(Py,log(Py+eps));
% mutual information
MI = Hx + Hy - Hxy;
% maximum normalized mutual information
NMImax = MI/max(Hx,Hy);