forked from yumilceh/igmm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
igmm_cond.py
60 lines (43 loc) · 2.12 KB
/
igmm_cond.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
"""
Created on June,2017
@author: Andreas Gerken
This class extends the gmm class by Juan Manuel Acevedo Valle by the ability
to sample conditionally from the gaussians. pypr is used for this.
"""
from igmm import IGMM
from igmm import DynamicParameter
import pypr.clustering.gmm as pypr_gmm
import numpy as np
class IGMM_COND(IGMM):
def cond_dist(self, Y):
""" Returns the conditional distribution with some fixed dimensions.
Keyword arguments:
Y -- A numpy vector of the same length as the input data samples with either
values or np.nan. A two dimensional input array could be
np.array([3, np.nan]). The gmm would be sampled with a fixed first
dimension of 3.
"""
return pypr_gmm.cond_dist(np.array(Y), self.means_, self.covariances_, self.weights_)
def sample_cond_dist(self, Y, n_samples):
""" Returns conditional samples from the gaussian mixture model.
Keyword arguments:
Y -- A numpy vector of the same length as the input data samples with either
values or np.nan. A two dimensional input array could be
np.array([3, np.nan]). The gmm would be sampled with a fixed first
dimension of 3.
n_samples -- Number of requested samples
"""
# get the conditional distribution
(con_means, con_covariances, con_weights) = self.cond_dist(Y)
#sample from the conditional distribution
samples = pypr_gmm.sample_gaussian_mixture(con_means, con_covariances, con_weights, n_samples)
# find the columns where the nans are
nan_cols = np.where(np.isnan(Y))[0]
# extend the input to the length of the samples
full_samples = np.tile(Y, (n_samples, 1))
#copy the sample columns
full_samples[:,nan_cols] = samples
return full_samples
def get_score(self, data):
""" returns the log likelihood for the recent model given the data, either as normal likelihood or as log likelihood (default)"""
return pypr_gmm.gm_log_likelihood(data, self.means_, self.covariances_, self.weights_)