forked from iricchi/Brain_GLMM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnmi.m
33 lines (27 loc) · 815 Bytes
/
nmi.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
function v = nmi(x, y)
% Compute nomalized mutual information I(x,y)/sqrt(H(x)*H(y)).
% Written by Michael Chen (sth4nth@gmail.com).
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,log2(Pxy+eps));
Px = mean(Mx,1);
Py = mean(My,1);
% entropy of Py and Px
Hx = -dot(Px,log2(Px+eps));
Hy = -dot(Py,log2(Py+eps));
% mutual information
MI = Hx + Hy - Hxy;
% normalized mutual information
% v = sqrt((MI/Hx)*(MI/Hy)) ;
v = MI/((Hx+Hy)/2);
end