-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutual_proximity.m
215 lines (171 loc) · 5.66 KB
/
mutual_proximity.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
function Dmp = mutual_proximity(D, type)
% Applies Mutual Proximity (MP) [1] on a distance matrix. The return value is
% converted to a distance matrix again. The resulting distance matrix
% should show lower hubness.
%
% This file is part of the HUB TOOLBOX available at
% http://ofai.at/research/impml/projects/hubology.html
% https://github.com/OFAI/hub-toolbox-matlab/
% (c) 2013, Dominik Schnitzer <[email protected]>
% (c) 2016, Roman Feldbauer <[email protected]>
%
% Usage:
% Dmp = mutual_proximity(D, type) - Applies MP on the distance matrix 'D'
% using the selected variant ('type'). The transformed distance matrix
% is returned.
%
% Possible types:
% 'empiric': Uses the Empirical distribution to perform Mutual Proximity.
% 'gauss': (requires the Statistics Toolbox (the mvncdf() function)
% Assumes that the distances are Gaussian distributed.
% 'gaussi': Assumes that the distances are independently Gaussian
% distributed. (fastest Variante)
% 'gammai': Assumes that the distances follow a Gamma distribution and
% are independently distributed.
%
% [1] Local and global scaling reduce hubs in space,
% Schnitzer, Flexer, Schedl, Widmer, Journal of Machine Learning Research 2012
if (nargin < 2)
mp_func = @mp_empiric;
fprintf('No Mutual Proximity type given. Using: ''empiric''\n');
fprintf('For fast results use: ''gaussi''\n');
else
if (strcmp(type, 'empiric') == 1)
mp_func = @mp_empiric;
elseif (strcmp(type, 'gauss') == 1)
mp_func = @mp_gauss;
elseif (strcmp(type, 'gaussi') == 1)
mp_func = @mp_gaussi;
elseif (strcmp(type, 'gammai') == 1)
mp_func = @mp_gammai;
else
fprintf(2, ['\nValid Mutual Proximity type missing!\n'...
'Use: Dmp = mutual_proximity(D, ''empiric''|'...
'''gauss''|''gaussi''|''gammai'');\n\n']);
Dmp = [];
return;
end
end
Dmp = mp_func(D);
end
function Dmp = mp_empiric(D)
n = size(D, 1);
Dmp_list = cell(size(D, 1), 1);
for i = 1:(n-1)
% select only finite distances for MP
j_idx = i+1:n;
j_len = length(j_idx);
dI = repmat(D(i, :), j_len, 1);
dJ = D(j_idx, :);
d = repmat(D(j_idx, i), 1, n);
sIJ_intersect = sum((dI > d) & (dJ > d), 2);
sIJ_overlap = 1 - (sIJ_intersect / (n - 1));
Dmp_list{i} = sIJ_overlap;
end
Dmp = zeros(size(D), class(D));
for i = 1:(n-1)
j_idx = i+1:n;
Dmp(i, j_idx) = Dmp_list{i}';
Dmp(j_idx, i) = Dmp_list{i};
end
end
function Dmp = mp_gaussi(D)
D(1:length(D)+1:numel(D)) = nan;
mu = nanmean(D);
sd = nanstd(D);
D(1:length(D)+1:numel(D)) = 0;
Dmp = zeros(size(D), class(D));
n = length(D);
for i=1:n
j_idx = i+1:n;
j_len = length(j_idx);
p1 = 1 - local_normcdf(D(i, j_idx), ...
repmat(mu(i), 1, j_len), repmat(sd(i), 1, j_len));
p2 = 1 - local_normcdf(D(j_idx, i)', ...
mu(j_idx), sd(j_idx));
Dmp(i, j_idx) = 1 - p1.*p2;
Dmp(j_idx, i) = Dmp(i, j_idx);
% Old Non-Vectorized Code (slow)
% for j=i+1:n
%
% p1 = (1 - local_normcdf(D(i, j), mu(i), sd(i)));
% p2 = (1 - local_normcdf(D(j, i), mu(j), sd(j)));
%
% Dmp(j, i) = 1 - p1*p2;
% Dmp(i, j) = Dmp(j, i);
%
% end
end
end
function Dmp = mp_gauss(D)
mu = mean(D);
sd = std(D);
% Ignore this warning for now
warning off MATLAB:quadgk:MinStepSize
epsmat = [100000*eps 0; 0 100000*eps];
Dmp = zeros(size(D), class(D));
n = size(D, 1);
for i=1:n
for j=i+1:n
c = cov(D([i j], :)');
x = [D(i, j) D(j, i)];
m = [mu(i) mu(j)];
p1 = local_normcdf(D(j, i), mu(i), sd(i));
p2 = local_normcdf(D(j, i), mu(j), sd(j));
try
p12 = mvncdf(x, m, c);
catch err
if (strcmp(err.identifier,'stats:mvncdf:BadMatrixSigma'))
c = c + epsmat;
p12 = mvncdf(x, m, c);
end
end
Dmp(j, i) = p1 + p2 - p12;
Dmp(i, j) = Dmp(j, i);
end
end
end
function Dmp = mp_gammai(D)
D(1:length(D)+1:numel(D)) = nan;
mu = nanmean(D);
va = nanvar(D);
A = (mu.^2)./va;
B = va ./ mu;
D(1:length(D)+1:numel(D)) = 0;
Dmp = zeros(size(D), class(D));
n = size(D, 1);
for i=1:n
j_idx = i+1:n;
j_len = length(j_idx);
p1 = 1 - local_gamcdf(D(i, j_idx), ...
repmat(A(i), 1, j_len), repmat(B(i), 1, j_len));
p2 = 1 - local_gamcdf(D(j_idx, i)', ...
A(j_idx), B(j_idx));
Dmp(i, j_idx) = 1 - p1.*p2;
Dmp(j_idx, i) = Dmp(i, j_idx);
% Old Non-Vectorized Code (slow)
% for j=i+1:length(distm)
%
% a2 = (mu(j)^2)/va(j);
% b2 = va(j)/mu(j);
%
% p1 = (1 - local_gamcdf(D(j, i), a1, b1));
% p2 = (1 - local_gamcdf(D(j, i), a2, b2));
% Dmp(j, i) = 1 - p1*p2;
%
% Dmp(i, j) = Dmp(j, i);
%
% end
end
end
function p = local_normcdf(x, mu, sd)
z = (x-mu) ./ sd;
p = 0.5 * erfc(-z ./ sqrt(2));
end
function p = local_gamcdf(x, a, b)
a(a < 0) = NaN;
b(b <= 0) = NaN;
x(x < 0) = 0;
z = x ./ b;
p = gammainc(z, a);
end