jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def test_train(self, params='wmc'):
        g = mixture.GMM(n_components=self.n_components,
                        covariance_type=self.covariance_type)
        with ignore_warnings(category=DeprecationWarning):
            g.weights_ = self.weights
            g.means_ = self.means
            g.covars_ = 20 * self.covars[self.covariance_type]

        # Create a training set by sampling from the predefined distribution.
        with ignore_warnings(category=DeprecationWarning):
            X = g.sample(n_samples=100)
            g = self.model(n_components=self.n_components,
                           covariance_type=self.covariance_type,
                           random_state=rng, min_covar=1e-1,
                           n_iter=1, init_params=params)
            g.fit(X)

        # Do one training iteration at a time so we can keep track of
        # the log likelihood to make sure that it increases after each
        # iteration.
        trainll = []
        with ignore_warnings(category=DeprecationWarning):
            for _ in range(5):
                g.params = params
                g.init_params = ''
                g.fit(X)
                trainll.append(self.score(g, X))
            g.n_iter = 10
            g.init_params = ''
            g.params = params
            g.fit(X)  # finish fitting

        # Note that the log likelihood will sometimes decrease by a
        # very small amount after it has more or less converged due to
        # the addition of min_covar to the covariance (to prevent
        # underflow).  This is why the threshold is set to -0.5
        # instead of 0.
        with ignore_warnings(category=DeprecationWarning):
            delta_min = np.diff(trainll).min()
        self.assertTrue(
            delta_min > self.threshold,
            "The min nll increase is %f which is lower than the admissible"
            " threshold of %f, for model %s. The likelihoods are %s."
            % (delta_min, self.threshold, self.covariance_type, trainll))
//  [python]
def test_train(self, params='wmc'):
        g = mixture.GaussianMixture(n_components=self.n_components,
                        covariance_type=self.covariance_type)
        with ignore_warnings(category=DeprecationWarning):
            g.weights_ = self.weights
            g.means_ = self.means
            g.covariances_ = 20 * self.covars[self.covariance_type]

        # Create a training set by sampling from the predefined distribution.
        with ignore_warnings(category=DeprecationWarning):
            X = g.sample(n_samples=100)[0]
            g = self.model(n_components=self.n_components,
                           covariance_type=self.covariance_type,
                           random_state=rng, min_covar=1e-1,
                           n_init=1, init_params=params)
            g.fit(X)

        # Do one training iteration at a time so we can keep track of
        # the log likelihood to make sure that it increases after each
        # iteration.
        trainll = []
        with ignore_warnings(category=DeprecationWarning):
            for _ in range(5):
                g.fit(X)
                trainll.append(self.score(g, X))
            g.n_init = 10
            g.fit(X)  # finish fitting

        # Note that the log likelihood will sometimes decrease by a
        # very small amount after it has more or less converged due to
        # the addition of min_covar to the covariance (to prevent
        # underflow).  This is why the threshold is set to -0.5
        # instead of 0.
        with ignore_warnings(category=DeprecationWarning):
            delta_min = np.diff(trainll).min()
        self.assertTrue(
            delta_min > self.threshold,
            "The min nll increase is %f which is lower than the admissible"
            " threshold of %f, for model %s. The likelihoods are %s."
            % (delta_min, self.threshold, self.covariance_type, trainll))
//  [python]
def trading(nr_groups, is_data, oos_data, grouping_type='kmeans', feature_type = 1):
    is_delta = get_feature(is_data, feature_type)
    oos_delta = get_feature(oos_data, feature_type)
    
    ### using machine learning algorithm to group the data
    if grouping_type == 'kmeans':
        #### using k-mean to categorize
        model = KMeans(nr_groups)
        fitted = model.fit(np.array(is_delta))
        is_delta['group'] = fitted.labels_
        oos_delta['group'] = fitted.predict(np.array(oos_delta))
    if grouping_type == 'gmm':
        ### Using GMM to calibrate
        model = GMM(nr_groups)
        is_delta['group'] = model.fit_predict(np.array(is_delta))
        oos_delta['group'] = model.predict(np.array(oos_delta))
    
    is_stats = risk_measure(is_data['ret_cc'], is_delta['group'], nr_groups)
    ### calculate the returns
    oos_ret = pd.DataFrame()
    is_ret = pd.DataFrame()
    
    strategy_is_ret = is_data['ret_cc'] * 0
    strategy_oos_ret = oos_data['ret_cc'] * 0
    
    for i in range(nr_groups) :
        oos_ret[str(i)] = oos_data['ret_cc'] * ((oos_delta['group'] == i)*1).shift(1)
        is_ret[str(i)] = is_data['ret_cc'] * ((is_delta['group'] == i)*1).shift(1)
     
        if is_stats['sharpe'][i] > 0.5:
            strategy_is_ret = strategy_is_ret + is_ret[str(i)]
            strategy_oos_ret = strategy_oos_ret + oos_ret[str(i)]
        #if is_stats['sharpe'][i] < -0.5:
        #    strategy_is_ret = strategy_is_ret - is_ret[str(i)]
        #    strategy_oos_ret = strategy_oos_ret - oos_ret[str(i)]        
            
    return  is_ret, oos_ret, is_stats, strategy_is_ret, strategy_oos_ret
//  [python]
def trading(nr_groups, is_data, oos_data, grouping_type='kmeans', feature_type = 1):
    is_delta = get_feature(is_data, feature_type)
    oos_delta = get_feature(oos_data, feature_type)
    
    ### using machine learning algorithm to group the data
    if grouping_type == 'kmeans':
        #### using k-mean to categorize
        model = KMeans(nr_groups)
        fitted = model.fit(np.array(is_delta))
        is_delta['group'] = fitted.labels_
        oos_delta['group'] = fitted.predict(np.array(oos_delta))
    if grouping_type == 'gmm':
        ### Using GMM to calibrate
        model = GaussianMixture(nr_groups)
        is_delta['group'] = model.fit_predict(np.array(is_delta))
        oos_delta['group'] = model.predict(np.array(oos_delta))
    
    is_stats = risk_measure(is_data['ret_cc'], is_delta['group'], nr_groups)
    ### calculate the returns
    oos_ret = pd.DataFrame()
    is_ret = pd.DataFrame()
    
    strategy_is_ret = is_data['ret_cc'] * 0
    strategy_oos_ret = oos_data['ret_cc'] * 0
    
    for i in range(nr_groups) :
        oos_ret[str(i)] = oos_data['ret_cc'] * ((oos_delta['group'] == i)*1).shift(1)
        is_ret[str(i)] = is_data['ret_cc'] * ((is_delta['group'] == i)*1).shift(1)
     
        if is_stats['sharpe'][i] > 0.5:
            strategy_is_ret = strategy_is_ret + is_ret[str(i)]
            strategy_oos_ret = strategy_oos_ret + oos_ret[str(i)]
        #if is_stats['sharpe'][i] < -0.5:
        #    strategy_is_ret = strategy_is_ret - is_ret[str(i)]
        #    strategy_oos_ret = strategy_oos_ret - oos_ret[str(i)]        
            
    return  is_ret, oos_ret, is_stats, strategy_is_ret, strategy_oos_ret
//  [python]
def test_verbose_second_level():
    # Create sample data
    X = rng.randn(30, 5)
    X[:10] += 2
    g = mixture.GMM(n_components=2, n_init=2, verbose=2)

    old_stdout = sys.stdout
    sys.stdout = StringIO()
    try:
        g.fit(X)
    finally:
        sys.stdout = old_stdout
//  [python]
def test_verbose_second_level():
    # Create sample data
    X = rng.randn(30, 5)
    X[:10] += 2
    g = mixture.GaussianMixture(n_components=2, n_init=2, verbose=2)

    old_stdout = sys.stdout
    sys.stdout = StringIO()
    try:
        g.fit(X)
    finally:
        sys.stdout = old_stdout
//  [python]
def test_aic():
    # Test the aic and bic criteria
    n_samples, n_dim, n_components = 50, 3, 2
    X = rng.randn(n_samples, n_dim)
    SGH = 0.5 * (X.var() + np.log(2 * np.pi))  # standard gaussian entropy

    for cv_type in ['full', 'tied', 'diag', 'spherical']:
        g = mixture.GMM(n_components=n_components, covariance_type=cv_type,
                        random_state=rng, min_covar=1e-7)
        g.fit(X)
        aic = 2 * n_samples * SGH * n_dim + 2 * g._n_parameters()
        bic = (2 * n_samples * SGH * n_dim +
               np.log(n_samples) * g._n_parameters())
        bound = n_dim * 3. / np.sqrt(n_samples)
        assert_true(np.abs(g.aic(X) - aic) / n_samples < bound)
        assert_true(np.abs(g.bic(X) - bic) / n_samples < bound)
//  [python]
def test_aic():
    # Test the aic and bic criteria
    n_samples, n_dim, n_components = 50, 3, 2
    X = rng.randn(n_samples, n_dim)
    SGH = 0.5 * (X.var() + np.log(2 * np.pi))  # standard gaussian entropy

    for cv_type in ['full', 'tied', 'diag', 'spherical']:
        g = mixture.GaussianMixture(n_components=n_components, covariance_type=cv_type,
                        random_state=rng, min_covar=1e-7)
        g.fit(X)
        aic = 2 * n_samples * SGH * n_dim + 2 * g._n_parameters()
        bic = (2 * n_samples * SGH * n_dim +
               np.log(n_samples) * g._n_parameters())
        bound = n_dim * 3. / np.sqrt(n_samples)
        assert_true(np.abs(g.aic(X) - aic) / n_samples < bound)
        assert_true(np.abs(g.bic(X) - bic) / n_samples < bound)
//  [python]
def fit_new(self, x, label):
        self.y.append(label)
        gmm = GMM(self.gmm_order)
        gmm.fit(x)
        self.gmms.append(gmm)
//  [python]
def fit_new(self, x, label):
        self.y.append(label)
        gmm = GaussianMixture(self.gmm_order)
        gmm.fit(x)
        self.gmms.append(gmm)
//  [python]
def cluster_rows(data,
                 data_slice,
                 n_clusters=2,
                 cluster_method='GMM',
                 n_iters=100,
                 n_restarts=3,
                 cluster_penalty=1.0,
                 rand_gen=None,
                 sklearn_args=None):
    """
    A wrapper to abstract from the implemented clustering method

    cluster_method = GMM | DPGMM | HOEM
    """

    clustering = None

    #
    # slicing the data
    sliced_data = data[data_slice.instance_ids, :][:, data_slice.feature_ids]

    if cluster_method == 'GMM':
        #
        # retrieving other properties
        cov_type = sklearn_args['covariance_type'] \
            if 'covariance_type' in sklearn_args else 'diag'
        #
        # creating the cluster from sklearn
        gmm_c = sklearn.mixture.GMM(n_components=n_clusters,
                                    covariance_type=cov_type,
                                    random_state=rand_gen,
                                    n_iter=n_iters,
                                    n_init=n_restarts)

        #
        # fitting to training set
        fit_start_t = perf_counter()
        gmm_c.fit(sliced_data)
        fit_end_t = perf_counter()

        #
        # getting the cluster assignment
        pred_start_t = perf_counter()
        clustering = gmm_c.predict(sliced_data)
        pred_end_t = perf_counter()

    elif cluster_method == 'DPGMM':
        #
        # retrieving other properties
        cov_type = sklearn_args['covariance_type'] \
            if 'covariance_type' in sklearn_args else 'diag'
        verbose = sklearn_args['verbose']\
            if 'verbose' in sklearn_args else False

        dpgmm_c = sklearn.mixture.DPGMM(n_components=n_clusters,
                                        covariance_type=cov_type,
                                        random_state=rand_gen,
                                        n_iter=n_iters,
                                        alpha=cluster_penalty,
                                        verbose=verbose)

        #
        # fitting to training set
        fit_start_t = perf_counter()
        dpgmm_c.fit(sliced_data)
        fit_end_t = perf_counter()

        #
        # getting the cluster assignment
        pred_start_t = perf_counter()
        clustering = dpgmm_c.predict(sliced_data)
        pred_end_t = perf_counter()

    elif cluster_method == 'HOEM':
        raise NotImplementedError('Hard Online EM is not implemented yet')
    else:
        raise Exception('Clustering method not valid')

    logging.info('Clustering done in %f secs', (fit_end_t - fit_start_t))

    #
    # translating the cluster assignment to
    # a list of clusters (set of instances)

    return retrieve_clustering(clustering, data_slice.instance_ids)
//  [python]
def cluster_rows(data,
                 data_slice,
                 n_clusters=2,
                 cluster_method='GMM',
                 n_iters=100,
                 n_restarts=3,
                 cluster_penalty=1.0,
                 rand_gen=None,
                 sklearn_args=None):
    """
    A wrapper to abstract from the implemented clustering method

    cluster_method = GMM | DPGMM | HOEM
    """

    clustering = None

    #
    # slicing the data
    sliced_data = data[data_slice.instance_ids, :][:, data_slice.feature_ids]

    if cluster_method == 'GMM':
        #
        # retrieving other properties
        cov_type = sklearn_args['covariance_type'] \
            if 'covariance_type' in sklearn_args else 'diag'
        #
        # creating the cluster from sklearn
        gmm_c = sklearn.mixture.GaussianMixture(n_components=n_clusters,
                                    covariance_type=cov_type,
                                    random_state=rand_gen,
                                    n_init=n_restarts, max_iter=n_iters)

        #
        # fitting to training set
        fit_start_t = perf_counter()
        gmm_c.fit(sliced_data)
        fit_end_t = perf_counter()

        #
        # getting the cluster assignment
        pred_start_t = perf_counter()
        clustering = gmm_c.predict(sliced_data)
        pred_end_t = perf_counter()

    elif cluster_method == 'DPGMM':
        #
        # retrieving other properties
        cov_type = sklearn_args['covariance_type'] \
            if 'covariance_type' in sklearn_args else 'diag'
        verbose = sklearn_args['verbose']\
            if 'verbose' in sklearn_args else False

        dpgmm_c = sklearn.mixture.DPGMM(n_components=n_clusters,
                                        covariance_type=cov_type,
                                        random_state=rand_gen,
                                        n_iter=n_iters,
                                        alpha=cluster_penalty,
                                        verbose=verbose)

        #
        # fitting to training set
        fit_start_t = perf_counter()
        dpgmm_c.fit(sliced_data)
        fit_end_t = perf_counter()

        #
        # getting the cluster assignment
        pred_start_t = perf_counter()
        clustering = dpgmm_c.predict(sliced_data)
        pred_end_t = perf_counter()

    elif cluster_method == 'HOEM':
        raise NotImplementedError('Hard Online EM is not implemented yet')
    else:
        raise Exception('Clustering method not valid')

    logging.info('Clustering done in %f secs', (fit_end_t - fit_start_t))

    #
    # translating the cluster assignment to
    # a list of clusters (set of instances)

    return retrieve_clustering(clustering, data_slice.instance_ids)
//  [python]
X_train = X[:700]
X_test = X[700:]
scores = []
n_components = np.arange(1, 21)
for i in n_components:
    gmm = GMM(n_components=i).fit(X_train)
    scores.append(gmm.bic(X))
//  [python]
X_train = X[:700]
X_test = X[700:]
scores = []
n_components = np.arange(1, 21)
for i in n_components:
    gmm = GaussianMixture(n_components=i).fit(X_train)
    scores.append(gmm.bic(X))
//  [python]
def check_positive_definite_covars(covariance_type):
    r"""Test that covariance matrices do not become non positive definite

    Due to the accumulation of round-off errors, the computation of the
    covariance  matrices during the learning phase could lead to non-positive
    definite covariance matrices. Namely the use of the formula:

    .. math:: C = (\sum_i w_i  x_i x_i^T) - \mu \mu^T

    instead of:

    .. math:: C = \sum_i w_i (x_i - \mu)(x_i - \mu)^T

    while mathematically equivalent, was observed a ``LinAlgError`` exception,
    when computing a ``GMM`` with full covariance matrices and fixed mean.

    This function ensures that some later optimization will not introduce the
    problem again.
    """
    rng = np.random.RandomState(1)
    # we build a dataset with 2 2d component. The components are unbalanced
    # (respective weights 0.9 and 0.1)
    X = rng.randn(100, 2)
    X[-10:] += (3, 3)  # Shift the 10 last points

    gmm = mixture.GMM(2, params="wc", covariance_type=covariance_type,
                      min_covar=1e-3)

    # This is a non-regression test for issue #2640. The following call used
    # to trigger:
    # numpy.linalg.linalg.LinAlgError: 2-th leading minor not positive definite
    gmm.fit(X)

    if covariance_type == "diag" or covariance_type == "spherical":
        assert_greater(gmm.covars_.min(), 0)
    else:
        if covariance_type == "tied":
            covs = [gmm.covars_]
        else:
            covs = gmm.covars_

        for c in covs:
            assert_greater(np.linalg.det(c), 0)
//  [python]
def check_positive_definite_covars(covariance_type):
    r"""Test that covariance matrices do not become non positive definite

    Due to the accumulation of round-off errors, the computation of the
    covariance  matrices during the learning phase could lead to non-positive
    definite covariance matrices. Namely the use of the formula:

    .. math:: C = (\sum_i w_i  x_i x_i^T) - \mu \mu^T

    instead of:

    .. math:: C = \sum_i w_i (x_i - \mu)(x_i - \mu)^T

    while mathematically equivalent, was observed a ``LinAlgError`` exception,
    when computing a ``GMM`` with full covariance matrices and fixed mean.

    This function ensures that some later optimization will not introduce the
    problem again.
    """
    rng = np.random.RandomState(1)
    # we build a dataset with 2 2d component. The components are unbalanced
    # (respective weights 0.9 and 0.1)
    X = rng.randn(100, 2)
    X[-10:] += (3, 3)  # Shift the 10 last points

    gmm = mixture.GaussianMixture(2, params="wc", covariance_type=covariance_type,
                      min_covar=1e-3)

    # This is a non-regression test for issue #2640. The following call used
    # to trigger:
    # numpy.linalg.linalg.LinAlgError: 2-th leading minor not positive definite
    gmm.fit(X)

    if covariance_type == "diag" or covariance_type == "spherical":
        assert_greater(gmm.covariances_.min(), 0)
    else:
        if covariance_type == "tied":
            covs = [gmm.covariances_]
        else:
            covs = gmm.covariances_

        for c in covs:
            assert_greater(np.linalg.det(c), 0)
//  [python]
def gmm_cause(n, k=4, p1=2, p2=2):
    """Init a root cause with a Gaussian Mixture Model."""
    g = GMM(k)
    g.means_ = p1 * np.random.randn(k, 1)
    g.covars_ = np.power(abs(p2 * np.random.randn(k, 1) + 1), 2)
    g.weights_ = abs(np.random.rand(k, 1))
    g.weights_ = g.weights_ / sum(g.weights_)
    return np.random.uniform(-1, 1, n)
//  [python]
def gmm_cause(n, k=4, p1=2, p2=2):
    """Init a root cause with a Gaussian Mixture Model."""
    g = GaussianMixture(k)
    g.means_ = p1 * np.random.randn(k, 1)
    g.covars_ = np.power(abs(p2 * np.random.randn(k, 1) + 1), 2)
    g.weights_ = abs(np.random.rand(k, 1))
    g.weights_ = g.weights_ / sum(g.weights_)
    return np.random.uniform(-1, 1, n)
//  [python]
def getPeaksGMM(self, nodeID, itemname='rawScore'):

        X = []
        for i in range(len(self.nodes[nodeID][itemname])):
            X = X + [i] * int(self.nodes[nodeID][itemname][i])

        if len(X) < 10:
            return [], [], []

        print("GMM size of X", len(X))

        X = np.asarray(X).reshape(-1, 1)

        m = [GMM(i + 1).fit(X) for i in range(3)]
        bic = [m[i].bic(X) for i in range(3)]

        nPeak = bic.index(min(bic))

        peaks = [int(m[nPeak].means_[i]) for i in range(nPeak + 1)]
        covars = m[nPeak].covars_
        weight = m[nPeak].weights_

        print(peaks)
        print(covars)
        print(weight)

        return peaks, covars, weight
//  [python]
def getPeaksGMM(self, nodeID, itemname='rawScore'):

        X = []
        for i in range(len(self.nodes[nodeID][itemname])):
            X = X + [i] * int(self.nodes[nodeID][itemname][i])

        if len(X) < 10:
            return [], [], []

        print("GMM size of X", len(X))

        X = np.asarray(X).reshape(-1, 1)

        m = [GaussianMixture(i + 1).fit(X) for i in range(3)]
        bic = [m[i].bic(X) for i in range(3)]

        nPeak = bic.index(min(bic))

        peaks = [int(m[nPeak].means_[i]) for i in range(nPeak + 1)]
        covars = m[nPeak].covariances_
        weight = m[nPeak].weights_

        print(peaks)
        print(covars)
        print(weight)

        return peaks, covars, weight
//  [python]
np.random.seed(1)

gmm = GMM(3, n_iter=1)
//  [python]
np.random.seed(1)

gmm = GaussianMixture(3, n_iter=1)
//  [python]
def test_n_parameters():
    # Test that the right number of parameters is estimated
    n_samples, n_dim, n_components = 7, 5, 2
    X = rng.randn(n_samples, n_dim)
    n_params = {'spherical': 13, 'diag': 21, 'tied': 26, 'full': 41}
    for cv_type in ['full', 'tied', 'diag', 'spherical']:
        g = mixture.GMM(n_components=n_components, covariance_type=cv_type,
                        random_state=rng, min_covar=1e-7, n_iter=1)
        g.fit(X)
        assert_true(g._n_parameters() == n_params[cv_type])
//  [python]
def test_n_parameters():
    # Test that the right number of parameters is estimated
    n_samples, n_dim, n_components = 7, 5, 2
    X = rng.randn(n_samples, n_dim)
    n_params = {'spherical': 13, 'diag': 21, 'tied': 26, 'full': 41}
    for cv_type in ['full', 'tied', 'diag', 'spherical']:
        g = mixture.GaussianMixture(n_components=n_components, covariance_type=cv_type,
                        random_state=rng, min_covar=1e-7, max_iter=1)
        g.fit(X)
        assert_true(g._n_parameters() == n_params[cv_type])
//  [python]
def cause(n,k,p1,p2):
  g = GMM(k)
  g.means_   = p1*np.random.randn(k,1)
  g.covars_  = np.power(abs(p2*np.random.randn(k,1)+1),2)
  g.weights_ = abs(np.random.rand(k,1))
  g.weights_ = g.weights_/sum(g.weights_)
  return scale(g.sample(n))
//  [python]
def cause(n,k,p1,p2):
  g = GaussianMixture(k)
  g.means_   = p1*np.random.randn(k,1)
  g.covars_  = np.power(abs(p2*np.random.randn(k,1)+1),2)
  g.weights_ = abs(np.random.rand(k,1))
  g.weights_ = g.weights_/sum(g.weights_)
  return scale(g.sample(n))
//  [python]
def plot_gmm(n_components, dataframe):
    plt.figure(figsize=(12, 12))

    for index, covariance_type in enumerate(['spherical', 'tied', 'diag', 'full']):
        gmm = GMM(n_components=n_components, covariance_type=covariance_type).fit(dataframe)
        pred = gmm.predict(dataframe)
        ax = plt.subplot(2, 2, index + 1)
        ax.axis('equal')
        make_ellipses(gmm, ax)
        colors = np.vectorize(color_mapping.get)(pred)
        plt.scatter(dataframe.iloc[:, 0], dataframe.iloc[:, 1], color=colors)
        plt.title(covariance_type)
//  [python]
def plot_gmm(n_components, dataframe):
    plt.figure(figsize=(12, 12))

    for index, covariance_type in enumerate(['spherical', 'tied', 'diag', 'full']):
        gmm = GaussianMixture(n_components=n_components, covariance_type=covariance_type).fit(dataframe)
        pred = gmm.predict(dataframe)
        ax = plt.subplot(2, 2, index + 1)
        ax.axis('equal')
        make_ellipses(gmm, ax)
        colors = np.vectorize(color_mapping.get)(pred)
        plt.scatter(dataframe.iloc[:, 0], dataframe.iloc[:, 1], color=colors)
        plt.title(covariance_type)
//  [python]
def plot_cond_model(xdgmm, cond_xdgmm, y):
    plt.clf()
    setup_text_plots(fontsize=16, usetex=True)
    fig = plt.figure(figsize=(12, 9))

    ax1 = fig.add_subplot(111)
    for i in range(xdgmm.n_components):
        draw_ellipse(xdgmm.mu[i], xdgmm.V[i], scales=[2], ax=ax1,
                     ec='k', fc='gray', alpha=0.2)

    ax1.plot([-2,15],[y,y],color='blue',linewidth=2)
    ax1.set_xlim(-1, 13)
    ax1.set_ylim(-6, 16)
    ax1.set_xlabel('$x$', fontsize = 18)
    ax1.set_ylabel('$y$', fontsize = 18)

    ax2 = ax1.twinx()
    x = np.array([np.linspace(-2,14,1000)]).T

    gmm=skl_GMM(n_components = cond_xdgmm.n_components,
                covariance_type = 'full')
    gmm.means_ = cond_xdgmm.mu
    gmm.weights_ = cond_xdgmm.weights
    gmm.covars_ = cond_xdgmm.V

    logprob, responsibilities = gmm.score_samples(x)

    pdf = np.exp(logprob)
    ax2.plot(x, pdf, color='red', linewidth = 2,
             label='Cond. dist. of $x$ given $y='+str(y)+'\pm 0.05$')
    ax2.legend()
    ax2.set_ylabel('Probability', fontsize= 18 )
    ax2.set_ylim(0, 0.52)
    ax1.set_xlim(-1, 13)
    plt.show()
//  [python]
def plot_cond_model(xdgmm, cond_xdgmm, y):
    plt.clf()
    setup_text_plots(fontsize=16, usetex=True)
    fig = plt.figure(figsize=(12, 9))

    ax1 = fig.add_subplot(111)
    for i in range(xdgmm.n_components):
        draw_ellipse(xdgmm.mu[i], xdgmm.V[i], scales=[2], ax=ax1,
                     ec='k', fc='gray', alpha=0.2)

    ax1.plot([-2,15],[y,y],color='blue',linewidth=2)
    ax1.set_xlim(-1, 13)
    ax1.set_ylim(-6, 16)
    ax1.set_xlabel('$x$', fontsize = 18)
    ax1.set_ylabel('$y$', fontsize = 18)

    ax2 = ax1.twinx()
    x = np.array([np.linspace(-2,14,1000)]).T

    gmm=sklearn.mixture.GaussianMixture(n_components = cond_xdgmm.n_components,
                covariance_type = 'full')
    gmm.means_ = cond_xdgmm.mu
    gmm.weights_ = cond_xdgmm.weights
    gmm.covars_ = cond_xdgmm.V

    logprob, responsibilities = gmm.score_samples(x)

    pdf = np.exp(logprob)
    ax2.plot(x, pdf, color='red', linewidth = 2,
             label='Cond. dist. of $x$ given $y='+str(y)+'\pm 0.05$')
    ax2.legend()
    ax2.set_ylabel('Probability', fontsize= 18 )
    ax2.set_ylim(0, 0.52)
    ax1.set_xlim(-1, 13)
    plt.show()
//  [python]
def gmm(k, X, run_times=5):
    gm = GMM(k, n_init=run_times)
    gm.fit(X)
    zh = gm.predict(X)
    return zh
//  [python]
def gmm(k, X, run_times=5):
    gm = GaussianMixture(k, n_init=run_times)
    gm.fit(X)
    zh = gm.predict(X)
    return zh
//  [python]
def _initialize(self, obs, init_count):
        """Perform initialization step before entering the EM algorithm.

        Parameters
        ----------
        obs : array_like, shape (`n`, `ni`, `nfeatures`)
            List of observation sequences, where `n` is the number of sequences, `ni` is
            the length of the i_th observation, and each observation has `nfeatures` features.
        init_count : int
            Restart counter

        """
        if self.emission is None or self.startprob is None or self.transmat is None:
            if init_count == 0:
                stacked_obs = self._stack_obs(obs)

                gmm = GMM(n_components=self.ncomponents, covariance_type='full')
                gmm.fit(stacked_obs)

                if self.transmat is None or self.startprob is None:
                    pz = gmm.predict_proba(stacked_obs)
                    self._init_with_mix_model(obs, pz)

                if self.emission is None:
                    self.emission = conditional_normal(gmm.means_, gmm.covars_, algorithm='map')
                    # regularize MLE
                    for i in range(self.emission.ncomponents):
                        self.emission.cov[i] += np.eye(self.emission.dim)
            else:
                stacked_obs = self._stack_obs(obs)
                mean = np.zeros((self.ncomponents, self.nfeatures))
                cov = np.zeros((self.ncomponents, self.nfeatures, self.nfeatures))
                for i in range(self.ncomponents):
                    xx = stacked_obs + np.random.randn(stacked_obs.shape[0], stacked_obs.shape[1])
                    mean[i] = np.mean(xx, 0)
                    cov[i, :, :] = np.cov(xx, rowvar=0)

                if self.emission is None:
                    self.emission = conditional_normal(mean, cov, algorithm='map')
                else:
                    self.emission.mean = mean
                    self.emission.cov = cov
                self._rand_init()

        if self.emission_prior:
            self.emission.prior = self.emission_prior
//  [python]
def _initialize(self, obs, init_count):
        """Perform initialization step before entering the EM algorithm.

        Parameters
        ----------
        obs : array_like, shape (`n`, `ni`, `nfeatures`)
            List of observation sequences, where `n` is the number of sequences, `ni` is
            the length of the i_th observation, and each observation has `nfeatures` features.
        init_count : int
            Restart counter

        """
        if self.emission is None or self.startprob is None or self.transmat is None:
            if init_count == 0:
                stacked_obs = self._stack_obs(obs)

                gmm = GaussianMixture(n_components=self.ncomponents, covariance_type='full')
                gmm.fit(stacked_obs)

                if self.transmat is None or self.startprob is None:
                    pz = gmm.predict_proba(stacked_obs)
                    self._init_with_mix_model(obs, pz)

                if self.emission is None:
                    self.emission = conditional_normal(gmm.means_, gmm.covariances_, algorithm='map')
                    # regularize MLE
                    for i in range(self.emission.ncomponents):
                        self.emission.cov[i] += np.eye(self.emission.dim)
            else:
                stacked_obs = self._stack_obs(obs)
                mean = np.zeros((self.ncomponents, self.nfeatures))
                cov = np.zeros((self.ncomponents, self.nfeatures, self.nfeatures))
                for i in range(self.ncomponents):
                    xx = stacked_obs + np.random.randn(stacked_obs.shape[0], stacked_obs.shape[1])
                    mean[i] = np.mean(xx, 0)
                    cov[i, :, :] = np.cov(xx, rowvar=0)

                if self.emission is None:
                    self.emission = conditional_normal(mean, cov, algorithm='map')
                else:
                    self.emission.mean = mean
                    self.emission.cov = cov
                self._rand_init()

        if self.emission_prior:
            self.emission.prior = self.emission_prior
//  [python]
def _initialize(self, obs, init_count):
        """Perform initialization step before entering the EM algorithm.

        Parameters
        ----------
        obs : array_like, shape (`n`, `ni`, `nfeatures`)
            List of observation sequences, where `n` is the number of sequences, `ni` is
            the length of the i_th observation, and each observation has `nfeatures` features.
        init_count : int
            Restart counter

        """
        if self.emission is None or self.startprob is None or self.transmat is None:
            stacked_obs = self._stack_obs(obs)

            if init_count == 0:

                gmm = GMM(n_components=self.ncomponents, covariance_type='full')
                gmm.fit(stacked_obs)

                if self.emission is None:
                    cov = np.eye(self.nfeatures) + gmm.covars_
                    # noinspection PyTypeChecker
                    m = np.tile(gmm.weights_, self.ncomponents)
                    m = normalize(m + np.random.rand(m.shape[0], m.shape[1]), 0)
                    self.emission = conditional_mix_normal(gmm.means_, cov, m, self.emission_prior)
            else:
                stacked_obs = self._stack_obs(obs)
                mean = np.zeros((self.ncomponents, self.nfeatures))
                cov = np.zeros((self.ncomponents, self.nfeatures, self.nfeatures))
                for i in range(self.ncomponents):
                    xx = stacked_obs + np.random.randn(stacked_obs.shape[0], stacked_obs.shape[1])
                    mean[i] = np.mean(xx, 0)
                    cov[i, :, :] = np.cov(xx, rowvar=0)

                m = normalize(np.random.rand(self.nmix, self.ncomponents), 0)

                if self.emission is None:
                    self.emission = conditional_mix_normal(mean, cov, m, self.emission_prior)
                else:
                    self.emission.mean = mean
                    self.emission.cov = cov
                    self.m = m
            self._rand_init()

        if self.emission_prior:
            self.emission.prior = self.emission_prior
//  [python]
def _initialize(self, obs, init_count):
        """Perform initialization step before entering the EM algorithm.

        Parameters
        ----------
        obs : array_like, shape (`n`, `ni`, `nfeatures`)
            List of observation sequences, where `n` is the number of sequences, `ni` is
            the length of the i_th observation, and each observation has `nfeatures` features.
        init_count : int
            Restart counter

        """
        if self.emission is None or self.startprob is None or self.transmat is None:
            stacked_obs = self._stack_obs(obs)

            if init_count == 0:

                gmm = GaussianMixture(n_components=self.ncomponents, covariance_type='full')
                gmm.fit(stacked_obs)

                if self.emission is None:
                    cov = np.eye(self.nfeatures) + gmm.covariances_
                    # noinspection PyTypeChecker
                    m = np.tile(gmm.weights_, self.ncomponents)
                    m = normalize(m + np.random.rand(m.shape[0], m.shape[1]), 0)
                    self.emission = conditional_mix_normal(gmm.means_, cov, m, self.emission_prior)
            else:
                stacked_obs = self._stack_obs(obs)
                mean = np.zeros((self.ncomponents, self.nfeatures))
                cov = np.zeros((self.ncomponents, self.nfeatures, self.nfeatures))
                for i in range(self.ncomponents):
                    xx = stacked_obs + np.random.randn(stacked_obs.shape[0], stacked_obs.shape[1])
                    mean[i] = np.mean(xx, 0)
                    cov[i, :, :] = np.cov(xx, rowvar=0)

                m = normalize(np.random.rand(self.nmix, self.ncomponents), 0)

                if self.emission is None:
                    self.emission = conditional_mix_normal(mean, cov, m, self.emission_prior)
                else:
                    self.emission.mean = mean
                    self.emission.cov = cov
                    self.m = m
            self._rand_init()

        if self.emission_prior:
            self.emission.prior = self.emission_prior
//  [python]
def InitSearch(npts,guess):
    Z = GMM(n_components=1,covariance_type="full")
    Z.means_ = np.zeros((1,6*npts))
    Z.covars_ = np.zeros((1,6*npts,6*npts))
    Z.covars_[0] = np.eye(6*npts)
    for i in range(npts):
        p = (i+1) * guess / float(npts+1) * 10
        ii = 6*i
        Z.means_[0,ii:ii+3] = [x for x in p]
        #print p
    #raw_input()

    return Z
//  [python]
def InitSearch(npts,guess):
    Z = GaussianMixture(n_components=1,covariance_type="full")
    Z.means_ = np.zeros((1,6*npts))
    Z.covars_ = np.zeros((1,6*npts,6*npts))
    Z.covars_[0] = np.eye(6*npts)
    for i in range(npts):
        p = (i+1) * guess / float(npts+1) * 10
        ii = 6*i
        Z.means_[0,ii:ii+3] = [x for x in p]
        #print p
    #raw_input()

    return Z
//  [python]
def traine(x):
    file_paths=open("development_set_enroll.txt" ,'w')
    i=1
    while i<6:
        file_paths.write(x+str(i)+".wav\n")
        i+=1
    file_paths.close()
     
    #path to training data
    source   = r"training_data/"
    #path where training speakers will be saved
    dest = r"models/"
    train_file = "development_set_enroll.txt"        
    file_paths = open(train_file,'r')
    
    count = 1
    # Extracting features for each speaker (5 files per speakers)
    features = np.asarray(())
    for path in file_paths:    
        path = path.strip()
        # read the audio
        rate,sig = wav.read(source+path)
        mfcc_feat=mfeatures.extract_features(sig,rate)
        # extract MFCC 
        
        if features.size == 0:
            features = mfcc_feat
        else:
            features = np.vstack((features, mfcc_feat))
        # when features of 5 files of speaker are concatenated, then do model training
        if count == 5:    
            gmm = GMM(n_components = 16, n_iter = 200, covariance_type='diag',n_init = 10)
            gmm.fit(features)
            
            # dumping the trained gaussian model
            picklefile = path.split("-")[0]+".gmm"
            pickle.dump(gmm,open(dest + picklefile,'wb'))
            features = np.asarray(())
            count = 0
        count = count + 1
//  [python]
def traine(x):
    file_paths=open("development_set_enroll.txt" ,'w')
    i=1
    while i<6:
        file_paths.write(x+str(i)+".wav\n")
        i+=1
    file_paths.close()
     
    #path to training data
    source   = r"training_data/"
    #path where training speakers will be saved
    dest = r"models/"
    train_file = "development_set_enroll.txt"        
    file_paths = open(train_file,'r')
    
    count = 1
    # Extracting features for each speaker (5 files per speakers)
    features = np.asarray(())
    for path in file_paths:    
        path = path.strip()
        # read the audio
        rate,sig = wav.read(source+path)
        mfcc_feat=mfeatures.extract_features(sig,rate)
        # extract MFCC 
        
        if features.size == 0:
            features = mfcc_feat
        else:
            features = np.vstack((features, mfcc_feat))
        # when features of 5 files of speaker are concatenated, then do model training
        if count == 5:    
            gmm = GaussianMixture(n_components = 16, n_iter = 200, covariance_type='diag',n_init = 10)
            gmm.fit(features)
            
            # dumping the trained gaussian model
            picklefile = path.split("-")[0]+".gmm"
            pickle.dump(gmm,open(dest + picklefile,'wb'))
            features = np.asarray(())
            count = 0
        count = count + 1
//  [python]
def train(self,features,iterMax=100):
        # construct a GMM classifier
        gmm = mixture.GMM(n_components=self.codebookSize,n_iter=iterMax)
        # train the classifier 
        self.classifier = gmm.fit(features)
//  [python]
def train(self,features,iterMax=100):
        # construct a GMM classifier
        gmm = mixture.GaussianMixture(n_components=self.codebookSize,n_iter=iterMax)
        # train the classifier 
        self.classifier = gmm.fit(features)
//  [python]
def pg_means(points, significance=0.01, projections=24):
    """Find a consensus location with the PG-means algorithm.

    points: Array of points with dimension (N, 2).
    significance: Optional. Significance level for increasing the Gaussian
        count.
    projections: Optional. How many projections to try before accepting.
    -> (x, y), boolean of whether PG-means succeeded.
    """
    k = 1

    last_gmm = None
    while True:
        # Fit a Gaussian mixture model with k components.
        gmm = sklearn.mixture.GMM(n_components=k, covariance_type='full')
        try:
            gmm.fit(points)
        except ValueError:
            if last_gmm is None:
                return maybe_mean(points), False

            return last_gmm.means_[last_gmm.weights_.argmax()], False
        last_gmm = gmm

        for _ in range(projections):
            # Project the data to one dimension.
            projection_vector = numpy.random.random(size=(2,))
            projected_points = numpy.dot(points, projection_vector)
            # Project the model to one dimension. We need the CDF in one
            # dimension, so we'll sample some data points and project them.
            n_samples = 1000
            samples = numpy.dot(gmm.sample(n_samples), projection_vector)
            samples.sort()

            def cdf(x):
                for sample, y in zip(samples,
                                     numpy.arange(n_samples) / n_samples):
                    if sample >= x:
                        break
                return y

            _, p_value = scipy.stats.kstest(projected_points,
                                            numpy.vectorize(cdf))
            if p_value < significance:
                # Reject the null hypothesis.
                break
        else:
            # Null hypothesis was not broken.
            return gmm.means_[gmm.weights_.argmax()], True

        k += 1
//  [python]
def pg_means(points, significance=0.01, projections=24):
    """Find a consensus location with the PG-means algorithm.

    points: Array of points with dimension (N, 2).
    significance: Optional. Significance level for increasing the Gaussian
        count.
    projections: Optional. How many projections to try before accepting.
    -> (x, y), boolean of whether PG-means succeeded.
    """
    k = 1

    last_gmm = None
    while True:
        # Fit a Gaussian mixture model with k components.
        gmm = sklearn.mixture.GaussianMixture(n_components=k, covariance_type='full')
        try:
            gmm.fit(points)
        except ValueError:
            if last_gmm is None:
                return maybe_mean(points), False

            return last_gmm.means_[last_gmm.weights_.argmax()], False
        last_gmm = gmm

        for _ in range(projections):
            # Project the data to one dimension.
            projection_vector = numpy.random.random(size=(2,))
            projected_points = numpy.dot(points, projection_vector)
            # Project the model to one dimension. We need the CDF in one
            # dimension, so we'll sample some data points and project them.
            n_samples = 1000
            samples = numpy.dot(gmm.sample(n_samples)[0], projection_vector)
            samples.sort()

            def cdf(x):
                for sample, y in zip(samples,
                                     numpy.arange(n_samples) / n_samples):
                    if sample >= x:
                        break
                return y

            _, p_value = scipy.stats.kstest(projected_points,
                                            numpy.vectorize(cdf))
            if p_value < significance:
                # Reject the null hypothesis.
                break
        else:
            # Null hypothesis was not broken.
            return gmm.means_[gmm.weights_.argmax()], True

        k += 1
//  [python]
def find_centers_gmm(data, gridpoints, n_components=2):
    r""" Provided 1D data and a grid of points, return the indices of the points closest to
    the centers of an assumed n-modal distribution behind "data"

    data : 1D data ndarray, of shape (N, 1)
    gridpoints: 1D gridpoints

    returns: idxs, igmm
    idxs: 1D ndarray
            INDICES of gridpoints that are closest to the centers of the gaussians

    igmm: gaussian mixture model (sklearn type)

    tested = true
    """
    igmm = _GMM(n_components=n_components)
    igmm.fit(data)
    return _np.abs(gridpoints-_np.sort(igmm.means_, 0)).argmin(1), igmm
//  [python]
def find_centers_gmm(data, gridpoints, n_components=2):
    r""" Provided 1D data and a grid of points, return the indices of the points closest to
    the centers of an assumed n-modal distribution behind "data"

    data : 1D data ndarray, of shape (N, 1)
    gridpoints: 1D gridpoints

    returns: idxs, igmm
    idxs: 1D ndarray
            INDICES of gridpoints that are closest to the centers of the gaussians

    igmm: gaussian mixture model (sklearn type)

    tested = true
    """
    igmm = GaussianMixture(n_components=n_components)
    igmm.fit(data)
    return _np.abs(gridpoints-_np.sort(igmm.means_, 0)).argmin(1), igmm
//  [python]
def gmm(k, X):
    gmm = GMM(n_components=k)
    gmm.fit(X)
    zh = gmm.predict(X)
    return zh
//  [python]
def gmm(k, X):
    gmm = GaussianMixture(n_components=k)
    gmm.fit(X)
    zh = gmm.predict(X)
    return zh
//  [python]
def setup_class(cls):
        cls.true_gmm = skGMM(n_components=3)
        cls.true_gmm.weights_ = np.array([0.45, 0.45, 0.1])
        cls.true_gmm.means_ = np.array([[1.0], [2.0], [3.0]])
        cls.true_gmm.covars_ = np.array([[0.25], [0.05], [0.01]])
        cls.true_gmm.converged_ = False

        cls.gmm = GMM(n_components=3, n_init=3)
        cls.gmm.fit(cls.true_gmm.sample(10000).astype(np.float32))
        cls.reindex = np.argsort(cls.gmm.means_, axis=0).ravel()
//  [python]
def setup_class(cls):
        cls.true_gmm = skGMM(n_components=3)
        cls.true_gmm.weights_ = np.array([0.45, 0.45, 0.1])
        cls.true_gmm.means_ = np.array([[1.0], [2.0], [3.0]])
        cls.true_gmm.covars_ = np.array([[0.25], [0.05], [0.01]])
        cls.true_gmm.converged_ = False

        cls.gmm = GaussianMixture(n_components=3, n_init=3)
        cls.gmm.fit(cls.true_gmm.sample(10000).astype(np.float32))
        cls.reindex = np.argsort(cls.gmm.means_, axis=0).ravel()
//  [python]
def _gmm_from_memberships(data, memberships, covariance_type):
    clusters = set(memberships)
    n_clusters = len(clusters)
    gmm = GMM(n_components=n_clusters, params='m')
    gmm.weights_ = np.ones([n_clusters])/n_clusters
    gmm.means_ = np.zeros([n_clusters, data.shape[1]]) 
    if covariance_type == 'diag':
        gmm.covars_ = np.zeros([n_clusters, data.shape[1]])
    if covariance_type == 'spherical':
        gmm.covars_ = np.zeros([n_clusters])
    if covariance_type == 'full':
        gmm.covars_ = np.zeros([n_clusters, data.shape[1], data.shape[1]])

    for cluster in clusters:
        cluster = int(cluster)
        indices = (memberships == cluster)
        gmm.means_[cluster, :] = data[indices, :].mean(axis=0)
        if covariance_type in ['diag', 'spherical']:
            #TODO Fix covariance calculation, for now, return cov=1
            #D = np.diag(np.cov(data[indices, :].T))
            D = np.ones([data.shape[1]])
            if covariance_type == 'spherical':
                gmm.covars_[cluster] = D.mean()
            else:
                gmm.covars_[cluster] = D
        if covariance_type == 'full':
            cov_estimator = OAS()
            cov_estimator.fit(data[indices, :])
            gmm.covars_[cluster] = cov_estimator.covariance_
    return gmm
//  [python]
def _gmm_from_memberships(data, memberships, covariance_type):
    clusters = set(memberships)
    n_clusters = len(clusters)
    gmm = GaussianMixture(n_components=n_clusters, params='m')
    gmm.weights_ = np.ones([n_clusters])/n_clusters
    gmm.means_ = np.zeros([n_clusters, data.shape[1]]) 
    if covariance_type == 'diag':
        gmm.covariances_ = np.zeros([n_clusters, data.shape[1]])
    if covariance_type == 'spherical':
        gmm.covariances_ = np.zeros([n_clusters])
    if covariance_type == 'full':
        gmm.covariances_ = np.zeros([n_clusters, data.shape[1], data.shape[1]])

    for cluster in clusters:
        cluster = int(cluster)
        indices = (memberships == cluster)
        gmm.means_[cluster, :] = data[indices, :].mean(axis=0)
        if covariance_type in ['diag', 'spherical']:
            #TODO Fix covariance calculation, for now, return cov=1
            #D = np.diag(np.cov(data[indices, :].T))
            D = np.ones([data.shape[1]])
            if covariance_type == 'spherical':
                gmm.covariances_[cluster] = D.mean()
            else:
                gmm.covariances_[cluster] = D
        if covariance_type == 'full':
            cov_estimator = OAS()
            cov_estimator.fit(data[indices, :])
            gmm.covariances_[cluster] = cov_estimator.covariance_
    return gmm