-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrand_index.m
79 lines (71 loc) · 1.94 KB
/
rand_index.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
function ri = rand_index(p1, p2)
%RAND_INDEX Computes the rand index between two partitions.
% RAND_INDEX(p1, p2) computes the rand index between partitions p1 and
% p2.
%
% RAND_INDEX(p1, p2, 'adjusted'); computes the adjusted rand index
% between partitions p1 and p2. The adjustment accounts for chance
% correlation.
% Parse the input and throw errors
adj = 0;
if nargin == 0
end
if nargin > 3
error('Too many input arguments');
end
adj = 1;
if length(p1)~=length(p2)
error('Both partitions must contain the same number of points.');
end
% Preliminary computations and cleansing of the partitions
N = length(p1);
[~, ~, p1] = unique(p1);
N1 = max(p1);
[~, ~, p2] = unique(p2);
N2 = max(p2);
% Create the matching matrix
for i=1:1:N1
for j=1:1:N2
G1 = find(p1==i);
G2 = find(p2==j);
n(i,j) = length(intersect(G1,G2));
end
end
% If required, calculate the basic rand index
if adj==0
ss = sum(sum(n.^2));
ss1 = sum(sum(n,1).^2);
ss2 =sum(sum(n,2).^2);
ri = (nchoosek2(N,2) + ss - 0.5*ss1 - 0.5*ss2)/nchoosek2(N,2);
end
% Otherwise, calculate the adjusted rand index
if adj==1
ssm = 0;
sm1 = 0;
sm2 = 0;
for i=1:1:N1
for j=1:1:N2
ssm = ssm + nchoosek2(n(i,j),2);
end
end
temp = sum(n,2);
for i=1:1:N1
sm1 = sm1 + nchoosek2(temp(i),2);
end
temp = sum(n,1);
for i=1:1:N2
sm2 = sm2 + nchoosek2(temp(i),2);
end
NN = ssm - sm1*sm2/nchoosek2(N,2);
DD = (sm1 + sm2)/2 - sm1*sm2/nchoosek2(N,2);
ri = NN/DD;
end
% Special definition of n choose k
function c = nchoosek2(a,b)
if a>1
c = nchoosek(a,b);
else
c = 0;
end
end
end