Exemple #1
0
    def __init__(self,
                 a,
                 ms=None,
                 Ps=None,
                 Us=None,
                 Ss=None,
                 xs=None,
                 seed=None):
        """Mixture of Gaussians

        Creates a MoG with a valid combination of parameters or an already given
        list of Gaussian variables.

        Parameters
        ----------
        a : list or np.array, 1d
            Mixing coefficients
        ms : list, length n_components
            Means
        Ps : list, length n_components
            Precisions
        Us : list, length n_components
            Precision factors such that U'U = P
        Ss : list, length n_components
            Covariances
        xs : list, length n_components
            List of gaussian variables
        seed : int or None
            If provided, random number generator will be seeded
        """
        self.__div__ = self.__truediv__
        self.__idiv__ = self.__itruediv__

        if ms is not None:
            super().__init__(a=np.asarray(a),
                             ncomp=len(ms),
                             ndim=np.asarray(ms[0]).ndim,
                             seed=seed)

            if Ps is not None:
                self.xs = [
                    Gaussian(m=m, P=P, seed=self.gen_newseed())
                    for m, P in zip(ms, Ps)
                ]

            elif Us is not None:
                self.xs = [
                    Gaussian(m=m, U=U, seed=self.gen_newseed())
                    for m, U in zip(ms, Us)
                ]

            elif Ss is not None:
                self.xs = [
                    Gaussian(m=m, S=S, seed=self.gen_newseed())
                    for m, S in zip(ms, Ss)
                ]

            else:
                raise ValueError('Precision information missing')

        elif xs is not None:
            super().__init__(a=np.asarray(a),
                             ncomp=len(xs),
                             ndim=xs[0].ndim,
                             seed=seed)
            self.xs = xs

        else:
            raise ValueError('Mean information missing')
Exemple #2
0
 def project_to_gaussian(self):
     """Returns a gaussian with the same mean and precision as the mog"""
     m, S = self.calc_mean_and_cov()
     return Gaussian(m=m, S=S, seed=self.seed)
    def __init__(self,
                 a,
                 ms=None,
                 Ps=None,
                 Us=None,
                 Ss=None,
                 xs=None,
                 seed=None,
                 beta=0.99):
        """Mixture of Ellipsoidals

        Creates a MoE with a valid combination of parameters or an already given
        list of Gaussian variables.

        Parameters
        ----------
        a : list or np.array, 1d
            Mixing coefficients
        ms : list, length n_components
            Means
        Ps : list, length n_components
            Precisions
        Us : list, length n_components
            Precision factors such that U'U = P
        Ss : list, length n_components
            Covariances
        xs : list, length n_components
            List of gaussian variables
        seed : int or None
            If provided, random number generator will be seeded
        beta : float
            Mass to preserve when sampling
        """
        if ms is not None:
            super().__init__(a=np.asarray(a),
                             ncomp=len(ms),
                             ndim=np.asarray(ms[0]).ndim,
                             seed=seed)

            if Ps is not None:
                self.xs = [
                    Gaussian(m=m, P=P, seed=self.gen_newseed())
                    for m, P in zip(ms, Ps)
                ]

            elif Us is not None:
                self.xs = [
                    Gaussian(m=m, U=U, seed=self.gen_newseed())
                    for m, U in zip(ms, Us)
                ]

            elif Ss is not None:
                self.xs = [
                    Gaussian(m=m, S=S, seed=self.gen_newseed())
                    for m, S in zip(ms, Ss)
                ]

            else:
                raise ValueError('Precision information missing')

        elif xs is not None:
            super().__init__(a=np.asarray(a),
                             ncomp=len(xs),
                             ndim=xs[0].ndim,
                             seed=seed)
            self.xs = xs

        else:
            raise ValueError('Mean information missing')

        self.threshold = 2 * gammaincinv(0.5 * self.ndim, beta)