-
Notifications
You must be signed in to change notification settings - Fork 4
/
mixture.py
292 lines (224 loc) · 9.08 KB
/
mixture.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import numpy as np
import pandas as pd
import tqdm
import scipy.stats as stats
import sklearn.cluster as cluster
import cvxpy as cp
class MultiGauss:
def init_responsibilities(X, weights,n_components):
# initialize weights with KMeans
n_samples, _ = X.shape
labels = cluster.KMeans(n_clusters=n_components, n_init=10).fit_predict(
X, sample_weight=weights
)
resp = np.zeros((n_samples, n_components))
resp[np.arange(n_samples), labels] = 1
return resp
def fit(self, X, w):
self.mean = np.average(X, weights=w, axis=0)
self.cov = np.nan_to_num(np.cov(X.T, aweights=w), 0)
np.fill_diagonal(self.cov, self.cov.diagonal() + 1e-6)
self._n_parameters = len(self.mean) + len(self.cov.flatten())
return self
def pdf(self, X):
return np.nan_to_num(
stats.multivariate_normal.pdf(
X, mean=self.mean, cov=self.cov
)
)
class Gauss:
def fit(self, X, w):
self.mean = np.average(X, weights=w, axis=0)
self.std = np.sqrt(np.cov(X, aweights=w))
self.std += 1e-6
self._n_parameters = 2
return self
def pdf(self, X):
return np.nan_to_num(
stats.norm.pdf(
X, loc=self.mean, scale=self.std
)
)
class VonMises:
def init_responsibilities(alpha, weights,n_components):
# initialize weights with KMeans
n_samples, _ = alpha.shape
X = np.concatenate([np.sin(alpha),np.cos(alpha)],axis=1)
labels = cluster.KMeans(n_clusters=n_components, n_init=10).fit_predict(
X, sample_weight=weights
)
resp = np.zeros((n_samples, n_components))
resp[np.arange(n_samples), labels] = 1
return resp
def fit(self, alpha, w):
sin = np.average(np.sin(alpha), weights=w, axis=0)
cos = np.average(np.cos(alpha), weights=w, axis=0)
self.loc = np.arctan2(sin, cos)
self.R = np.sqrt(sin ** 2 + cos ** 2) # mean resultant length
# self.R = min(self.R,0.99)
maxR = np.empty_like(self.R)
maxR[0] = 0.99
self.R = min(self.R, maxR)
self.kappa = (
self.R * (2 - self.R ** 2) / (1 - self.R ** 2)
) # approximation for kappa
self._n_parameters = 2
return self
def pdf(self, alpha):
return np.nan_to_num(
stats.vonmises.pdf(alpha, kappa=self.kappa, loc=self.loc).flatten()
)
class CategoricalModel:
def __init__(self, tol=1e-6):
self.tol = tol
def fit(self, X, weights=None):
if weights:
X = X[weights > self.tol]
self.categories = set(X)
return self
def predict_proba(self, X, weights=None):
p = pd.DataFrame()
if weights is None:
weights = np.zeros(len(X)) + 1
for c in self.categories:
p[str(c)] = ((X == c) & (weights > self.tol)).apply(float)
return p
class MixtureModel:
def __init__(self, n_components, distribution=MultiGauss, max_iter=100, tol=1e-6):
self.n_components = n_components
self.distribution = distribution
self.max_iter = max_iter
self.tol = tol
def fit(self, X, weights=None, verbose=False):
# handle sparsity
if weights is None:
weights = np.zeros(len(X)) + 1
pos_weights_idx = weights > self.tol
X = X[pos_weights_idx]
weights = weights[pos_weights_idx]
self.weight_total = weights.sum()
self.loglikelihood = -np.inf
self.submodels = list(self.distribution() for _i in range(self.n_components))
if len(X) < self.n_components:
return None
responsibilities = self.distribution.init_responsibilities(X, weights,self.n_components)
# learn models on initial weights
self.priors = responsibilities.sum(axis=0) / responsibilities.sum()
# invalid model if less clusters found than given components
if any(self.priors < self.tol):
return None
for i in range(self.n_components):
self.submodels[i].fit(X, weights * responsibilities[:, i])
iterations = (
range(self.max_iter) if not verbose else tqdm.tqdm(range(self.max_iter))
)
for self._n_iter in iterations:
# Expectation
for i in range(self.n_components):
responsibilities[:, i] = self.priors[i] * self.submodels[i].pdf(X)
# enough improvement or not?
new_loglikelihood = (weights * np.log(responsibilities.sum(axis=1))).sum()
if new_loglikelihood > self.loglikelihood + self.tol:
self.loglikelihood = new_loglikelihood
# self.responsibilities = responsibilities
# self.weights = weights
else:
break
# normalize responsibilities such that each data point occurs with P=1
responsibilities /= responsibilities.sum(axis=1)[:, np.newaxis]
# Maximalization
self.priors = responsibilities.sum(axis=0) / responsibilities.sum()
for i in range(self.n_components):
self.submodels[i].fit(X, weights * responsibilities[:, i])
if np.isinf(self.loglikelihood):
return None
return self
def predict_proba(self, X, weights=None):
p = np.zeros((len(X), self.n_components))
# handle sparsity
if weights is None:
weights = np.zeros(len(X)) + 1
pos_weights_idx = weights > self.tol
X = X[pos_weights_idx]
weights = weights[pos_weights_idx]
pdfs = np.vstack([m.pdf(X) for m in self.submodels]).T
resp = self.priors * pdfs
probs = resp / resp.sum(axis=1)[:, np.newaxis]
p[pos_weights_idx, :] = (weights * probs.T).T
return p
def params(self):
return list(m.__dict__ for m in self.submodels)
def _n_parameters(self):
return (
sum(m._n_parameters for m in self.submodels)
- self.submodels[0]._n_parameters
)
def ilp_select_models_max(models, max_components, verbose=False):
x = cp.Variable(len(models), boolean=True)
c = np.array(list(m.loglikelihood for m in models))
n_components = np.array(list(m.n_components for m in models))
objective = cp.Maximize(cp.sum(c * x))
constraints = []
constraints += [n_components * x <= max_components]
for name in set(m.name for m in models):
name_idx = np.array(list(int(m.name == name) for m in models))
constraints += [name_idx * x == 1]
prob = cp.Problem(objective, constraints)
prob.solve(verbose=verbose)
idx, = np.where(x.value > 0.3)
return list(models[i] for i in idx)
def ilp_select_models_bic_triangle(models, verbose=False):
x = cp.Variable(len(models), boolean=True)
c = np.array(list(m.loglikelihood for m in models))
n_parameters = np.array(list(m.n_components for m in models))
dataweights = {}
for m in models:
if m.name not in dataweights:
dataweights[m.name] = m.weight_total
n_data = sum(dataweights.values())
n = cp.sum(n_parameters * x)
para = n + (cp.square(n) + n) / 2
objective = cp.Minimize(np.log(n_data) * para - 2 * cp.sum(c * x))
constraints = []
for name in set(m.name for m in models):
name_idx = np.array(list(int(m.name == name) for m in models))
constraints += [name_idx * x == 1]
prob = cp.Problem(objective, constraints)
prob.solve(verbose=verbose)
idx, = np.where(x.value > 0.3)
return list(models[i] for i in idx)
def ilp_select_models_bic(models, verbose=False):
x = cp.Variable(len(models), boolean=True)
c = np.array(list(m.loglikelihood for m in models))
n_parameters = np.array(list(m._n_parameters() for m in models))
dataweights = {}
for m in models:
if m.name not in dataweights:
dataweights[m.name] = m.weight_total
n_data = sum(dataweights.values())
objective = cp.Minimize(
np.log(n_data) * cp.sum(n_parameters * x) - 2 * cp.sum(c * x)
)
constraints = []
for name in set(m.name for m in models):
name_idx = np.array(list(int(m.name == name) for m in models))
constraints += [name_idx * x == 1]
prob = cp.Problem(objective, constraints)
prob.solve(verbose=verbose)
idx, = np.where(x.value > 0.3)
return list(models[i] for i in idx)
def select_models_solo_bic(models):
for m in models:
m.solo_bic = np.log(m.weight_total) * m._n_parameters() - 2 * m.loglikelihood
ms = []
for name in set(m.name for m in models):
bestm = min([m for m in models if m.name == name], key=lambda m: m.solo_bic)
ms.append(bestm)
return ms
def probabilities(models, X, W):
weights = []
for model in models:
probs = model.predict_proba(X, W[model.name].values)
nextlevel_columns = list(f"{model.name}_{i}" for i in range(model.n_components))
weights.append(pd.DataFrame(probs, columns=nextlevel_columns))
return pd.concat(weights, axis=1)