-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpyMM.py
1302 lines (1080 loc) · 47.4 KB
/
pyMM.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Probabilistic principal components analysis (PPCA)
A generative latent linear variable model.
PPCA assumes that the observed data is generated by linearly transforming a
number of latent variables and then adding spherical Gaussian noise. The
latent variables are drawn from a standard Gaussian distribution.
This implementation is based on David Barber's Matlab implementation:
http://web4.cs.ucl.ac.uk/staff/D.Barber/pmwiki/pmwiki.php?n=Main.Software
This implementation uses the EM algorithm to handle missing data.
"""
# Author: Charlie Nash <[email protected]>
import numpy as np
import scipy as sp
import numpy.random as rd
from random import seed
from scipy.stats import multivariate_normal
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA, FactorAnalysis
from sklearn.preprocessing import Imputer
class GMM():
"""Probabilistic principal components analysis (PPCA).
A generative latent linear variable model.
PPCA assumes that the observed data is generated by linearly transforming a
number of latent variables and then adding spherical Gaussian noise. The
latent variables are drawn from a standard Gaussian distribution.
The parameters of the model are the transformation matrix (principal
components) the mean, and the noise variance.
PPCA performs maximum likelihood or MAP estimation of the model parameters
using the expectation-maximisation algorithm (EM).
Attributes
----------
latentDim : int
Dimensionality of latent space. The number of variables that are
transformed by the principal components to the data space.
components : array, [latentDim, nFeatures]
Transformation matrix parameter.
bias: array, [nFeatures]
Bias parameter.
noiseVariance : float
Noise variance parameter. Variance of noise that is added to linearly
transformed latent variables to generate data.
standardize : bool, optional
When True, the mean is subtracted from the data, and each feature is
divided by it's standard deviation so that the mean and variance of
the transformed features are 0 and 1 respectively.
componentPrior : float >= 0
Gaussian component matrix hyperparameter. If > 0 then a Gaussian prior
is applied to each column of the component matrix with covariance
componentPrior^-1 * noiseVariance. This has the effect
of regularising the component matrix.
tol : float
Stopping tolerance for EM algorithm
maxIter : int
Maximum number of iterations for EM algorithm
Notes
-----
TODO
Examples
--------
TODO
"""
def __init__(self, n_components, covariance_type='full', tol=1e-3,
max_iter=1000, random_state=0, verbose=True, robust=False):
self.n_components = n_components
self.covariance_type = covariance_type
self.tol = tol
self.max_iter = max_iter
self.random_state = random_state
self.verbose = verbose
self.robust = robust
self.isFitted = False
def _e_step(self, X, params):
if self.missing_data:
return self._e_step_miss(X, params)
else:
return self._e_step_no_miss(X, params)
def _e_step_no_miss(self, X, params):
""" E-Step of the EM-algorithm.
The E-step takes the existing parameters, for the components, bias
and noise variance and computes sufficient statistics for the M-Step
by taking the expectation of latent variables conditional on the
visible variables. Also returns the likelihood for the data X and
projections into latent space of the data.
Args
----
X : array, [nExamples, nFeatures]
Matrix of training data, where nExamples is the number of
examples and nFeatures is the number of features.
W : array, [dataDim, latentDim]
Component matrix data. Maps latent points to data space.
b : array, [dataDim,]
Data bias.
sigmaSq : float
Noise variance parameter.
Returns
-------
ss : dict
proj :
ll :
"""
# Get params
mu_list = params['mu_list']
components = params['components']
n_examples, data_dim = X.shape
# Compute responsibilities
log_r = np.zeros([n_examples, self.n_components])
# Get Sigma from params
Sigma_list = self._params_to_Sigma(params)
for k, mu, Sigma in zip(range(self.n_components), mu_list,
Sigma_list):
try:
log_r[:, k] = multivariate_normal.logpdf(X, mu, Sigma)
except np.linalg.linalg.LinAlgError:
if self.robust:
Sigma_robust = Sigma + 1e-8*np.eye(self.data_dim)
log_r[:, k] = multivariate_normal.logpdf(X, mu,
Sigma_robust)
else:
error_msg = ('Covariance matrix ill-conditioned. Use ' +
'robust=True to pre-condition covariance ' +
'matrices or choose fewer mixture ' +
'components')
raise np.linalg.linalg.LinAlgError(error_msg)
log_r = log_r + np.log(components)
log_r_sum = sp.misc.logsumexp(log_r, axis=1)
responsibilities = np.exp(log_r - log_r_sum[:, np.newaxis])
x_list = [X for k in range(self.n_components)]
xx_list = [X[:, :, np.newaxis] * X[:, np.newaxis, :] for k in
range(self.n_components)]
# Store sufficient statistics in dictionary
ss = {'responsibilities': responsibilities,
'x_list': x_list,
'xx_list': xx_list}
# Compute log-likelihood of each example
sample_ll = log_r_sum
return ss, sample_ll
def _e_step_miss(self, X, params):
""" E-Step of the EM-algorithm.
The E-step takes the existing parameters, for the components, bias
and noise variance and computes sufficient statistics for the M-Step
by taking the expectation of latent variables conditional on the
visible variables. Also returns the likelihood for the data X and
projections into latent space of the data.
Args
----
X : array, [nExamples, nFeatures]
Matrix of training data, where nExamples is the number of
examples and nFeatures is the number of features.
W : array, [dataDim, latentDim]
Component matrix data. Maps latent points to data space.
b : array, [dataDim,]
Data bias.
sigmaSq : float
Noise variance parameter.
Returns
-------
ss : dict
proj :
ll :
"""
# Get current params
mu_list = params['mu_list']
components = params['components']
# Get Sigma from params
Sigma_list = self._params_to_Sigma(params)
observed_list = [np.array(np.where(~np.isnan(row))).flatten() for
row in X]
n_examples, data_dim = np.shape(X)
# Loop over data points computing responsibilities
r = np.zeros([n_examples, self.n_components])
for n in range(n_examples):
id_obs = observed_list[n]
row = X[n, :]
row_obs = row[id_obs]
for k, mu, Sigma in zip(range(self.n_components), mu_list,
Sigma_list):
mu_obs = mu[id_obs]
Sigma_obs = Sigma[np.ix_(id_obs, id_obs)]
r[n, k] = multivariate_normal.pdf(row_obs[np.newaxis, :],
mu_obs, Sigma_obs)
r = r * components
r_sum = r.sum(axis=1)
responsibilities = r / r_sum[:, np.newaxis]
x_list = []
xx_list = []
for k, mu, Sigma in zip(range(self.n_components), mu_list, Sigma_list):
x_tot = np.zeros([n_examples, data_dim])
xx_tot = np.zeros([n_examples, data_dim, data_dim])
for n in range(n_examples):
id_obs = observed_list[n]
id_miss = np.setdiff1d(np.arange(data_dim), id_obs)
n_miss = len(id_miss)
row = X[n, :]
row_obs = row[id_obs]
# Simplify for case with no missing data
if n_miss == 0:
x_tot[n] = row_obs
xx_tot[n] = np.outer(row_obs, row_obs)
# Get missing / present parameters
mu_obs = mu[id_obs]
mu_miss = mu[id_miss]
Sigma_obs = Sigma[np.ix_(id_obs, id_obs)]
Sigma_miss = Sigma[np.ix_(id_miss, id_miss)]
Sigma_obs_miss = Sigma[np.ix_(id_obs, id_miss)]
Sigma_miss_obs = Sigma[np.ix_(id_miss, id_obs)]
# Get conditional distribution p(x_miss | x_vis, params_k)
mean_cond = (mu_miss + Sigma_miss_obs.dot(
np.linalg.inv(Sigma_obs)).dot(row_obs - mu_obs))
Sigma_cond = (Sigma_miss -
Sigma_miss_obs.dot(np.linalg.inv(Sigma_obs))
.dot(Sigma_obs_miss))
# Get sufficient statistics E[x] and E[xx^t]
x = np.empty(data_dim)
x[id_obs] = row_obs
x[id_miss] = mean_cond
x_tot[n] = x
xx = np.empty([data_dim, data_dim])
xx[np.ix_(id_obs, id_obs)] = np.outer(row_obs, row_obs)
xx[np.ix_(id_obs, id_miss)] = np.outer(row_obs, mean_cond)
xx[np.ix_(id_miss, id_obs)] = np.outer(mean_cond, row_obs)
xx[np.ix_(id_miss, id_miss)] = (np.outer(mean_cond, mean_cond)
+ Sigma_cond)
xx_tot[n] = xx
x_list.append(x_tot)
xx_list.append(xx_tot)
# Store sufficient statistics in dictionary
ss = {'responsibilities': responsibilities,
'x_list': x_list,
'xx_list': xx_list}
# Compute log-likelihood of each example
sample_ll = np.log(r_sum)
return ss, sample_ll
def _m_step(self, ss, params):
""" M-Step of the EM-algorithm.
The M-step takes the sufficient statistics computed in the E-step, and
maximizes the expected complete data log-likelihood with respect to the
parameters.
Args
----
ss : dict
Returns
-------
params : dict
"""
resp = ss['responsibilities']
x_list = ss['x_list']
xx_list = ss['xx_list']
# Update components param
components = np.mean(resp, axis=0)
# Update mean / Sigma params
mu_list = []
Sigma_list = []
for r, x, xx in zip(resp.T, x_list, xx_list):
mu = np.sum(x*r[:, np.newaxis], axis=0) / r.sum()
mu_list.append(mu)
Sigma = (np.sum((xx*r[:, np.newaxis, np.newaxis]), axis=0) /
r.sum() - np.outer(mu, mu))
Sigma_list.append(Sigma)
# Store params in dictionary
params = {'Sigma_list': Sigma_list,
'mu_list': mu_list,
'components': components}
return params
def _params_to_Sigma(self, params):
return params['Sigma_list']
def _init_params(self, X, init_method='kmeans'):
seed(self.random_state)
n_examples = X.shape[0]
if init_method == 'kmeans':
kmeans = KMeans(self.n_components)
if self.missing_data:
imputer = Imputer()
X = imputer.fit_transform(X)
kmeans.fit(X)
mu_list = [k for k in kmeans.cluster_centers_]
Sigma_list = []
for k in range(self.n_components):
X_k = X[kmeans.labels_ == k, :]
if X_k.shape[0] < 5*self.data_dim:
Sigma_list.append(np.eye(self.data_dim))
else:
Sigma_list.append(np.cov(X_k.T))
components = np.array([np.sum(kmeans.labels_ == k) / n_examples
for k in range(self.n_components)])
params_init = {'mu_list': mu_list,
'Sigma_list': Sigma_list,
'components': components}
return params_init
def fit(self, X, params_init=None, init_method='kmeans'):
""" Fit the model using EM with data X.
Args
----
X : array, [nExamples, nFeatures]
Matrix of training data, where nExamples is the number of
examples and nFeatures is the number of features.
"""
n_examples, data_dim = np.shape(X)
self.data_dim = data_dim
if np.isnan(X).any():
self.missing_data = True
else:
self.missing_data = False
if params_init is None:
params = self._init_params(X, init_method)
else:
params = params_init
# Check for missing values and remove if whole row is missing
X = X[~np.isnan(X).all(axis=1), :]
oldL = -np.inf
for i in range(self.max_iter):
# E-Step
ss, sample_ll = self._e_step(X, params)
# Evaluate likelihood
ll = sample_ll.mean()
if self.verbose:
print("Iter {:d} NLL: {:.3f} Change: {:.3f}".format(i,
-ll, -(ll-oldL)), flush=True)
# Break if change in likelihood is small
if np.abs(ll - oldL) < self.tol:
break
oldL = ll
# M-step
params = self._m_step(ss, params)
else:
if self.verbose:
print("PPCA did not converge within the specified" +
" tolerance. You might want to increase the number of" +
" iterations.")
# Update Object attributes
self.params = params
self.trainNll = ll
self.isFitted = True
def sample(self, n_samples=1):
"""Sample from fitted model.
Sample from fitted model by first sampling from latent space
(spherical Gaussian) then transforming into data space using learned
parameters. Noise can then be added optionally.
Parameters
----------
nSamples : int
Number of samples to generate
noisy : bool
Option to add noise to samples (default = True)
Returns
-------
dataSamples : array [nSamples, dataDim]
Collection of samples in data space.
"""
if not self.isFitted:
print("Model is not yet fitted. First use fit to learn the " +
"model params.")
else:
components = self.params['components']
mu_list = self.params['mu_list']
Sigma_list = self._params_to_Sigma(self.params)
components_cumsum = np.cumsum(components)
samples = np.zeros([n_samples, self.data_dim])
for n in range(n_samples):
r = np.random.rand(1)
z = np.argmin(r > components_cumsum)
samples[n] = rd.multivariate_normal(mu_list[z], Sigma_list[z])
return samples
def score_samples(self, X):
if not self.isFitted:
print("Model is not yet fitted. First use fit to learn the " +
"model params.")
else:
# Apply one step of E-step to get the sample log-likelihoods
return self._e_step(X, self.params)[1]
def score(self, X):
"""Compute the average log-likelihood of data matrix X
Parameters
----------
X: array, shape (n_samples, n_features)
The data
Returns
-------
meanLl: array, shape (n_samples,)
Log-likelihood of each sample under the current model
"""
if not self.isFitted:
print("Model is not yet fitted. First use fit to learn the " +
"model params.")
else:
# Apply one step of E-step to get the sample log-likelihoods
sample_ll = self.score_samples(X)
# Divide by number of examples to get average log likelihood
return sample_ll.mean()
class SphericalGMM(GMM):
@staticmethod
def _convert_gmm_params(params):
sigma_sq_list = [np.mean(np.diag(cov)) for cov in
params['Sigma_list']]
params_conv = {i: params[i] for i in params if i != 'Sigma_list'}
params_conv['sigma_sq_list'] = sigma_sq_list
return params_conv
def _init_params(self, X, init_method='kmeans'):
params_init_gmm = super(SphericalGMM, self)._init_params(X,
init_method)
return self._convert_gmm_params(params_init_gmm)
def _m_step(self, ss, params):
params_gmm = super(SphericalGMM, self)._m_step(ss, params)
return self._convert_gmm_params(params_gmm)
def _params_to_Sigma(self, params):
return [sigma_sq*np.eye(self.data_dim) for sigma_sq in
params['sigma_sq_list']]
class DiagonalGMM(SphericalGMM):
@staticmethod
def _convert_gmm_params(params):
Psi_list = [np.diag(np.diag(cov)) for cov in params['Sigma_list']]
params_conv = {i: params[i] for i in params if i != 'Sigma_list'}
params_conv['Psi_list'] = Psi_list
return params_conv
def _params_to_Sigma(self, params):
return params['Psi_list']
class MPPCA(GMM):
"""Mixtures of probabilistic principal components analysis (PPCA) models.
A generative latent variable model.
PPCA assumes that the observed data is generated by first generating latent
variables z from a Gaussian distribution p(z), then linearly transforming
these variables with a weights matrix W, and then finally adding spherical
Gaussian noise. PPCA can be viewed as a Gaussian model with a low-rank
approximation to the covariance matrix. It can be useful in the case
where there are many dimensions, but not many examples. Here a
full-covariance model needs to estimate many parameters, and will have a
tendency to overfit, whereas a PPCA model can have considerably fewer
parameters, and therefore is less likely to overfit.
The parameters of the model are the transformation matrix W , the mean mu,
and the noise variance sigma_sq.
The mixture of PPCA models (MPPCA) additionally assumes that the data can
come from a number of PPCA components, with each component being selected
from a disrete probability distribution. Thus the parameters are W_k, mu_k
and sigma_sq_k for each component k, and component probabilities alpha_k
for each component.
MPPCA performs maximum likelihood or MAP estimation of the model
parameters using the expectation-maximisation algorithm (EM algorithm).
Attributes
----------
latent_dim : int
Dimensionality of latent space. The number of variables that are
transformed by the weight matrix to the data space.
n_components : array, [latentDim, nFeatures]
Transformation matrix parameter.
bias: array, [nFeatures]
Bias parameter.
noiseVariance : float
Noise variance parameter. Variance of noise that is added to linearly
transformed latent variables to generate data.
standardize : bool, optional
When True, the mean is subtracted from the data, and each feature is
divided by it's standard deviation so that the mean and variance of
the transformed features are 0 and 1 respectively.
componentPrior : float >= 0
Gaussian component matrix hyperparameter. If > 0 then a Gaussian prior
is applied to each column of the component matrix with covariance
componentPrior^-1 * noiseVariance. This has the effect
of regularising the component matrix.
tol : float
Stopping tolerance for EM algorithm
maxIter : int
Maximum number of iterations for EM algorithm
"""
def __init__(self, n_components, latent_dim, tol=1e-3, max_iter=1000,
random_state=0, verbose=True, robust=False):
super(MPPCA, self).__init__(n_components=n_components, tol=tol,
max_iter=max_iter,
random_state=random_state,
verbose=verbose, robust=robust)
self.latent_dim = latent_dim
def _init_params(self, X, init_method='kmeans'):
seed(self.random_state)
n_examples = X.shape[0]
if init_method == 'kmeans':
kmeans = KMeans(self.n_components)
if self.missing_data:
imputer = Imputer()
X = imputer.fit_transform(X)
kmeans.fit(X)
mu_list = [k for k in kmeans.cluster_centers_]
W_list = []
sigma_sq_list = []
for k in range(self.n_components):
data_k = X[kmeans.labels_ == k, :]
pca = PCA(n_components=self.latent_dim)
pca.fit(data_k)
W_list.append(pca.components_.T)
sigma_sq_list.append(0.1)
components = np.array([np.sum(kmeans.labels_ == k) / n_examples
for k in range(self.n_components)])
params_init = {'mu_list': mu_list,
'W_list': W_list,
'sigma_sq_list': sigma_sq_list,
'components': components}
return params_init
def _e_step_no_miss(self, X, params):
""" E-Step of the EM-algorithm.
The E-step takes the existing parameters, for the components, bias
and noise variance and computes sufficient statistics for the M-Step
by taking the expectation of latent variables conditional on the
visible variables. Also returns the likelihood for the data X and
projections into latent space of the data.
Args
----
X : array, [nExamples, nFeatures]
Matrix of training data, where nExamples is the number of
examples and nFeatures is the number of features.
W : array, [dataDim, latentDim]
Component matrix data. Maps latent points to data space.
b : array, [dataDim,]
Data bias.
sigmaSq : float
Noise variance parameter.
Returns
-------
ss : dict
proj :
ll :
"""
# Get params
mu_list = params['mu_list']
components = params['components']
W_list = params['W_list']
sigma_sq_list = params['sigma_sq_list']
n_examples, data_dim = X.shape
# Compute responsibilities
r = np.zeros([n_examples, self.n_components])
# Get Sigma from params
Sigma_list = self._params_to_Sigma(params)
for k, mu, Sigma in zip(range(self.n_components), mu_list, Sigma_list):
r[:, k] = multivariate_normal.pdf(X, mu, Sigma)
r = r * components
r_sum = r.sum(axis=1)
responsibilities = r / r_sum[:, np.newaxis]
# Get sufficient statistics E[z] and E[zz^t] for each component
z_list = []
zz_list = []
xz_list = []
xx_list = []
for mu, W, sigma_sq in zip(mu_list, W_list, sigma_sq_list):
dev = X - mu
F_inv = (np.linalg.inv(W.T.dot(W) +
sigma_sq*np.eye(self.latent_dim)))
z = dev.dot(W).dot(F_inv)
z_list.append(z)
zz = sigma_sq*F_inv + z[:, :, np.newaxis] * z[:, np.newaxis, :]
zz_list.append(zz)
xx = dev[:, :, np.newaxis] * dev[:, np.newaxis, :]
xx_list.append(xx)
xz = dev[:, :, np.newaxis] * z[:, np.newaxis, :]
xz_list.append(xz)
# Store sufficient statistics in dictionary
ss = {'responsibilities': responsibilities,
'x_list': [X for k in range(self.n_components)],
'xx_list': xx_list,
'xz_list': xz_list,
'z_list': z_list,
'zz_list': zz_list}
# Compute log-likelihood
sample_ll = np.log(r_sum)
return ss, sample_ll
def _e_step_miss(self, X, params):
""" E-Step of the EM-algorithm.
The E-step takes the existing parameters, for the components, bias
and noise variance and computes sufficient statistics for the M-Step
by taking the expectation of latent variables conditional on the
visible variables. Also returns the likelihood for the data X and
projections into latent space of the data.
Args
----
X : array, [nExamples, nFeatures]
Matrix of training data, where nExamples is the number of
examples and nFeatures is the number of features.
W : array, [dataDim, latentDim]
Component matrix data. Maps latent points to data space.
b : array, [dataDim,]
Data bias.
sigmaSq : float
Noise variance parameter.
Returns
-------
ss : dict
proj :
ll :
"""
# Get current params
mu_list = params['mu_list']
components = params['components']
sigma_sq_list = params['sigma_sq_list']
W_list = params['W_list']
# Get Sigma from params
Sigma_list = self._params_to_Sigma(params)
observed_list = [np.array(np.where(~np.isnan(row))).flatten() for
row in X]
n_examples, data_dim = np.shape(X)
# Loop over data points computing responsibilities
r = np.zeros([n_examples, self.n_components])
for n in range(n_examples):
id_obs = observed_list[n]
row = X[n, :]
row_obs = row[id_obs]
for k, mu, Sigma in zip(range(self.n_components), mu_list,
Sigma_list):
mu_obs = mu[id_obs]
Sigma_obs = Sigma[np.ix_(id_obs, id_obs)]
r[n, k] = multivariate_normal.pdf(row_obs[np.newaxis, :],
mu_obs, Sigma_obs)
r = r * components
r_sum = r.sum(axis=1)
responsibilities = r / r_sum[:, np.newaxis]
x_list = []
xx_list = []
z_list = []
zz_list = []
xz_list = []
for k, mu, W, sigma_sq in zip(range(self.n_components), mu_list,
W_list, sigma_sq_list):
x_tot = np.zeros([n_examples, data_dim])
xx_tot = np.zeros([n_examples, data_dim, data_dim])
z_tot = np.zeros([n_examples, self.latent_dim])
zz_tot = np.zeros([n_examples, self.latent_dim, self.latent_dim])
xz_tot = np.zeros([n_examples, self.data_dim, self.latent_dim])
for n in range(n_examples):
id_obs = observed_list[n]
id_miss = np.setdiff1d(np.arange(data_dim), id_obs)
n_miss = len(id_miss)
row = X[n, :]
row_obs = row[id_obs]
# # Simplify for case with no missing data
# if n_miss == 0:
# x_tot[n] = row_obs
# xx_tot[n] = np.outer(row_obs, row_obs)
# Get missing and visible points
W_obs = W[id_obs, :]
W_miss = W[id_miss, :]
mu_obs = mu[id_obs]
mu_miss = mu[id_miss]
row_min_mu = row_obs - mu_obs
# Get conditional distribution of p(z | x_vis, params)
F_inv = (np.linalg.inv(W_obs.T.dot(W_obs) +
sigma_sq*np.eye(self.latent_dim)))
cov_z_cond = sigma_sq*F_inv
mean_z_cond = F_inv.dot(W_obs.T).dot(row_obs - mu_obs)
# Get conditional distribution of p(x_miss | z, params)
mean_x_miss = W_miss.dot(mean_z_cond) + mu_miss
# Append sufficient statistics
z_tot[n] = mean_z_cond
zz_tot[n] = cov_z_cond + np.outer(mean_z_cond, mean_z_cond)
x_tot[n, id_obs] = row_obs
x_tot[n, id_miss] = mean_x_miss
xz_tot[n, id_miss, :] = W_miss.dot(zz_tot[n])
xz_tot[n, id_obs, :] = np.outer(row_min_mu, mean_z_cond)
xx = np.empty([data_dim, data_dim])
xx[np.ix_(id_obs, id_obs)] = np.outer(row_min_mu, row_min_mu)
xx[np.ix_(id_obs, id_miss)] = np.outer(row_min_mu,
mean_x_miss - mu_miss)
xx[np.ix_(id_miss, id_obs)] = np.outer(mean_x_miss - mu_miss,
row_min_mu)
xx[np.ix_(id_miss, id_miss)] = (W_miss.dot(zz_tot[n]).
dot(W_miss.T) +
sigma_sq*np.eye(n_miss))
xx_tot[n] = xx
x_list.append(x_tot)
xx_list.append(xx_tot)
z_list.append(z_tot)
zz_list.append(zz_tot)
xz_list.append(xz_tot)
# Store sufficient statistics in dictionary
ss = {'responsibilities': responsibilities,
'x_list': x_list,
'xx_list': xx_list,
'xz_list': xz_list,
'z_list': z_list,
'zz_list': zz_list}
# Compute log-likelihood
sample_ll = np.log(r_sum)
return ss, sample_ll
def _m_step(self, ss, params):
""" M-Step of the EM-algorithm.
The M-step takes the sufficient statistics computed in the E-step, and
maximizes the expected complete data log-likelihood with respect to the
parameters.
Args
----
ss : dict
Returns
-------
params : dict
"""
resp = ss['responsibilities']
x_list = ss['x_list']
z_list = ss['z_list']
zz_list = ss['zz_list']
xz_list = ss['xz_list']
xx_list = ss['xx_list']
W_list_old = params['W_list']
# Update components param
components = np.mean(resp, axis=0)
# Update mean / Sigma params
mu_list = []
W_list = []
sigma_sq_list = []
for r, W, x, z, zz, xz, xx in zip(resp.T, W_list_old, x_list, z_list,
zz_list, xz_list, xx_list):
resid = x - z.dot(W.T)
mu = np.sum(resid*r[:, np.newaxis], axis=0) / r.sum()
mu_list.append(mu)
W1 = np.sum(xz*r[:, np.newaxis, np.newaxis], axis=0)
W2 = np.linalg.inv(np.sum(zz*r[:, np.newaxis, np.newaxis],
axis=0))
W = W1.dot(W2)
W_list.append(W)
s1 = np.trace(xx, axis1=1, axis2=2)
s2 = -2*np.trace(xz.dot(W.T), axis1=1, axis2=2)
s3 = np.trace(zz*W.T.dot(W), axis1=1, axis2=2)
sigma_sq = np.sum(r*(s1 + s2 + s3)) / (self.data_dim * r.sum())
sigma_sq_list.append(sigma_sq)
# Store params in dictionary
params = {'W_list': W_list,
'sigma_sq_list': sigma_sq_list,
'mu_list': mu_list,
'components': components}
return params
def _params_to_Sigma(self, params):
W_list = params['W_list']
sigma_sq_list = params['sigma_sq_list']
Sigma_list = [W.dot(W.T) + sigma_sq*np.eye(self.data_dim)
for W, sigma_sq in zip(W_list, sigma_sq_list)]
return Sigma_list
class MFA(GMM):
def __init__(self, n_components, latent_dim, tol=1e-3, max_iter=1000,
random_state=0, verbose=True, robust=False, small=1e-4):
super(MFA, self).__init__(n_components=n_components, tol=tol,
max_iter=max_iter,
random_state=random_state,
verbose=verbose, robust=robust)
self.latent_dim = latent_dim
self.SMALL = small
def _init_params(self, X, init_method='kmeans'):
seed(self.random_state)
n_examples = X.shape[0]
if init_method == 'kmeans':
kmeans = KMeans(self.n_components)
if self.missing_data:
imputer = Imputer()
X = imputer.fit_transform(X)
kmeans.fit(X)
mu_list = [k for k in kmeans.cluster_centers_]
W_list = []
Psi_list = []
for k in range(self.n_components):
X_k = X[kmeans.labels_ == k, :]
if X_k.shape[0] < 500*self.data_dim:
W_list.append(np.random.randn(self.data_dim,
self.latent_dim))
Psi_list.append(10*np.eye(self.data_dim))
else:
fa = FactorAnalysis(n_components=self.latent_dim)
fa.fit(X_k)
W_list.append(fa.components_.T)
Psi_list.append(np.diag(fa.noise_variance_))
components = np.array([np.sum(kmeans.labels_ == k) / n_examples
for k in range(self.n_components)])
params_init = {'mu_list': mu_list,
'W_list': W_list,
'Psi_list': Psi_list,
'components': components}
return params_init
def _e_step_no_miss(self, X, params):
""" E-Step of the EM-algorithm.
The E-step takes the existing parameters, for the components, bias
and noise variance and computes sufficient statistics for the M-Step
by taking the expectation of latent variables conditional on the
visible variables. Also returns the likelihood for the data X and
projections into latent space of the data.
Args
----
X : array, [nExamples, nFeatures]
Matrix of training data, where nExamples is the number of
examples and nFeatures is the number of features.
W : array, [dataDim, latentDim]
Component matrix data. Maps latent points to data space.
b : array, [dataDim,]
Data bias.
sigmaSq : float
Noise variance parameter.
Returns
-------
ss : dict
proj :
ll :
"""
# Get params
mu_list = params['mu_list']
components = params['components']
W_list = params['W_list']
Psi_list = params['Psi_list']
n_examples, data_dim = X.shape
# Get Sigma from params
Sigma_list = self._params_to_Sigma(params)
# Compute responsibilities
log_r = np.zeros([n_examples, self.n_components])
for k, mu, Sigma, W, Psi in zip(range(self.n_components), mu_list,
Sigma_list, W_list, Psi_list):
# print('Sigma = {}'.format(Sigma))
# print('Psi: {}'.format(Psi))
# print('WW.T: {}'.format(W.dot(W.T)))
try:
log_r[:, k] = multivariate_normal.logpdf(X, mu, Sigma)
# except ValueError:
# print('ValueError: Sigma = {}'.format(Sigma))
# print('Eigenvalues: {}'.format(np.linalg.eigvals(Sigma)))
# print('Psi: {}'.format(Psi))
# print('WW.T: {}'.format(W.dot(W.T)))
except np.linalg.linalg.LinAlgError:
if self.robust:
# print(Sigma)
Sigma_robust = Sigma + self.SMALL*np.eye(self.data_dim)
log_r[:, k] = multivariate_normal.logpdf(X, mu,
Sigma_robust)
else:
error_msg = ('Covariance matrix ill-conditioned. Use ' +