-
Notifications
You must be signed in to change notification settings - Fork 1
/
kernel_methods.py
208 lines (171 loc) · 5.64 KB
/
kernel_methods.py
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
from itertools import combinations
import numpy as np
from numba import njit
@njit
def discordant_pairs(a, b):
n = len(a)
assert len(b) == n
discordant = 0
a_inv = np.argsort(a)
b_inv = np.argsort(b)
for i in range(n):
for j in range(i):
if (a_inv[i] < a_inv[j] and b_inv[i] > b_inv[j]) or (
a_inv[i] > a_inv[j] and b_inv[i] < b_inv[j]):
discordant += 1
return discordant
def test_discordant_pairs():
d = 20
a = np.random.permutation(d)
b = np.flip(a)
dis = discordant_pairs(a, b)
norm_dis = dis / ((d * (d - 1)) / 2)
assert norm_dis == 1.0
dis = discordant_pairs(a, a)
norm_dis = dis / (d * (d - 1) / 2)
assert norm_dis == 0.0
class KTKernel:
def __call__(self, a, b):
n = len(a)
return 1.0 - 4 * discordant_pairs(a, b) / (n * (n - 1))
def expected_value(self, d):
return 0.0
class MallowsKernel:
def __init__(self, l=5):
self.l = l
def __call__(self, a, b):
d = len(a)
norm = (d * (d - 1)) / 2
return np.exp(-self.l * discordant_pairs(a, b) / norm)
def expected_value(self, d):
mgf_expected = 1.0
norm = (d * (d - 1)) / 2
for j in range(1, d + 1):
mgf_expected *= (1 - np.exp(j * (-self.l / norm))) / (j * (1 - np.exp(-self.l / norm)))
return mgf_expected
class SpearmanKernel:
def __call__(self, a, b):
return a.dot(b)
def expected_value(self, d):
return (d * (d - 1) ** 2) / 4
def kt_inverse_feature_map(v, n_features):
inv_p = np.zeros(n_features, dtype=np.int64)
for i, (j, k) in enumerate(combinations(range(n_features), 2)):
if v[i] >= 0:
inv_p[j] += 1
else:
inv_p[k] += 1
p = sorted(np.arange(0, n_features), key=lambda x: inv_p[x])
return p
@njit
def kt_feature_map(p, n_features):
p_inv = np.zeros_like(p)
for i in range(len(p)):
p_inv[p[i]] = i
v = np.ones(n_features * (n_features - 1) // 2)
i = 0
for j in range(n_features):
for k in range(j + 1, n_features):
if p_inv[j] > p_inv[k]:
v[i] = 1
else:
v[i] = -1
i += 1
return v / np.sqrt(len(v))
def test_kt_feature_map():
trials = 10
n_features = [3, 10, 20]
for n_f in n_features:
for _ in range(trials):
p = np.random.permutation(n_f)
v = kt_feature_map(p, n_f)
x = kt_inverse_feature_map(v, n_f)
assert np.all(x == p), (x, p)
def kernel_argmax(samples, max_trials, n_features, kernel):
p = np.zeros((max_trials, n_features), dtype=np.int64)
scores = np.full(max_trials, kernel.expected_value(n_features))
n = samples.shape[0]
for i in range(max_trials):
p[i] = np.random.permutation(n_features)
for s in samples:
scores[i] -= 1 / (n + 1) * kernel(p[i], s)
best_trial = np.argmax(scores)
return p[best_trial]
def kernel_herding(n_samples, n_features, kernel, max_trials):
p = np.zeros((n_samples, n_features), dtype=np.int64)
for i in range(n_samples):
p[i] = kernel_argmax(p[:i], max_trials, n_features, kernel)
return p
def compute_bayesian_weights(p, kernel):
n = p.shape[0]
K = np.zeros((n, n))
for i in range(n):
for j in range(n):
if i <= j:
k = kernel(p[i], p[j])
K[i, j] = k
K[j, i] = k
z = np.full(n, 0.0)
w = np.linalg.lstsq(K, z, rcond=None)[0]
return w
def sbq_variance(K):
z = np.full(K.shape[0], 0.5)
w = np.linalg.lstsq(K, z, rcond=None)[0]
return 0.5 - w.dot(z)
def update_kernel_matrix(K, p, new_p, kernel):
K_new = K.copy()
K_new[len(p), len(p)] = 1.0
for i in range(len(p)):
k = kernel(new_p, p[i])
K_new[i, len(p)] = k
K_new[len(p), i] = k
return K_new
def sequential_bayesian_quadrature(n_points, n_features, kernel, num_trials):
# start with a random permutation
p = [np.random.permutation(n_features)]
K = np.zeros((n_points, n_points))
K[0, 0] = 1.0
for i in range(1, n_points):
trial_points = []
trial_points_var = []
for j in range(num_trials):
# Create a trial kernel matrix
trial_perm = np.random.permutation(n_features)
trial_K = update_kernel_matrix(K, p, trial_perm, kernel)
trial_points_var.append(sbq_variance(trial_K[:i + 1, :i + 1]))
trial_points.append((trial_perm, trial_K))
best = np.argmin(trial_points_var)
p.append(trial_points[best][0])
K = trial_points[best][1]
z = np.full(n_points, kernel.expected_value(n_features))
w = np.linalg.lstsq(K, z, rcond=None)[0]
w /= w.sum()
return np.array(p), w
def discrepancy(p, w, kernel):
n = p.shape[0]
d = p.shape[1]
if w is None:
w = np.full(n, 1 / n)
disc = kernel.expected_value(d)
for i in range(n):
disc -= 2 * w[i] * kernel.expected_value(d)
for i in range(n):
for j in range(n):
disc += w[i] * w[j] * kernel(p[i], p[j])
return np.sqrt(disc)
def plot_mallows_lambda():
import matplotlib.pyplot as plt
plt.style.use("seaborn")
plt.rc('font', family='serif')
x = np.linspace(0, 1, 100)
lambdas = [0.5, 5.0, 50]
plt.figure(figsize=(4 * 1.3, 3 * 1.3))
for l in lambdas:
y = np.exp(- x * l)
plt.plot(x, y, label="$\lambda={}$".format(l))
plt.legend()
plt.xlabel("$n_{dis}(\sigma,\sigma')/\\binom{d}{2}$")
plt.ylabel("$K_M(\sigma,\sigma')$")
plt.tight_layout()
plt.savefig('figures/kernel/mallows_lambda.png')
plt.show()