Exemplo n.º 1
0
 def test(self):
     
     obs = np.concatenate((np.random.randn(10000, 1), 10 + np.random.randn(30000, 1))).T
     
     mu, Sigma, weights = mixgauss_init(M=2, data=obs, cov_type='diag', method='kmeans')
     
     assert np.all(np.abs(mu-np.array([[10, 0]])) < 1e-1) or np.all(np.abs(mu-np.array([[0, 10]])) < 1e-1)
     assert np.abs(Sigma[:,:,0]-1)<1e-1
     assert np.abs(Sigma[:,:,1]-1)<1e-1
     assert np.all(np.abs(weights-np.array([[0.75], [0.25]])) < 0.2) or np.all(np.abs(weights-np.array([[0.25], [0.75]])) < 0.2)
Exemplo n.º 2
0
    def fit(self, obs):
        obs = self._convertObs(obs)

        O = obs[0].shape[0]
        M = self.n_mix
        Q = self.n_components

        if 's' in self.init_params:
            self.startprob_, _ = normalise(self.startprob_)

        if 't' in self.init_params:
            self.transmat_, _ = mk_stochastic(self.transmat_)

        if 'm' in self.init_params or 'c' in self.init_params:
            mu0, Sigma0, weights0 = mixgauss_init(
                Q * M, obs, cov_type=self._covariance_type)

            if 'm' in self.init_params:
                self.means_ = np.transpose(np.reshape(mu0, (O, M, Q)),
                                           (0, 2, 1))

            if 'c' in self.init_params:
                self.covars_ = np.transpose(np.reshape(Sigma0, (O, O, M, Q)),
                                            (0, 1, 3, 2))

        mixmat0, _ = mk_stochastic(np.random.rand(Q, M))

        self.LL, prior1, transmat1, mu1, Sigma1, mixmat1 = mhmm_em(
            data=obs,
            prior=self.startprob_,
            transmat=self.transmat_,
            mu=self.means_,
            Sigma=self.covars_,
            mixmat=mixmat0,
            max_iter=self.n_iter,
            thresh=self.thresh,
            cov_type=self._covariance_type,
            adj_trans='t' in self.params,
            adj_mix='w' in self.params,
            adj_mu='m' in self.params,
            adj_Sigma='c' in self.params)

        self.startprob_ = prior1
        self.transmat_ = transmat1
        self.means_ = mu1
        self.covars_ = Sigma1
        self.weights_ = mixmat1
Exemplo n.º 3
0
    def test(self):
        
        mean0 = [0, 10]
        cov = [[.1, 0],
               [0, .1]]
        
        obs = np.random.multivariate_normal(mean0, cov, 10000)
        
        mean1 = [5, 15]
        cov = [[.1, 0],
               [0, .1]]
        
        obs = np.vstack((obs, np.random.multivariate_normal(mean1, cov, 30000)))

        mu, Sigma, weights = mixgauss_init(M=2, data=obs.T, cov_type='diag', method='kmeans')
        
        assert np.all(np.abs(mu[:,0]-mean0) < 1e-1) or np.all(np.abs(mu[:,1]-mean0) < 1e-1)
Exemplo n.º 4
0
    def test(self):

        obs = np.concatenate(
            (np.random.randn(10000, 1), 10 + np.random.randn(30000, 1))).T

        mu, Sigma, weights = mixgauss_init(M=2,
                                           data=obs,
                                           cov_type='diag',
                                           method='kmeans')

        assert np.all(np.abs(mu - np.array([[10, 0]])) < 1e-1) or np.all(
            np.abs(mu - np.array([[0, 10]])) < 1e-1)
        assert np.abs(Sigma[:, :, 0] - 1) < 1e-1
        assert np.abs(Sigma[:, :, 1] - 1) < 1e-1
        assert np.all(
            np.abs(weights - np.array([[0.75], [0.25]])) < 0.2) or np.all(
                np.abs(weights - np.array([[0.25], [0.75]])) < 0.2)
Exemplo n.º 5
0
    def fit(self, obs):
        obs = self._convertObs(obs)

        O = obs[0].shape[0]
        M = self.n_mix
        Q = self.n_components

        if "s" in self.init_params:
            self.startprob_, _ = normalise(self.startprob_)

        if "t" in self.init_params:
            self.transmat_, _ = mk_stochastic(self.transmat_)

        if "m" in self.init_params or "c" in self.init_params:
            mu0, Sigma0, weights0 = mixgauss_init(Q * M, obs, cov_type=self._covariance_type)

            if "m" in self.init_params:
                self.means_ = np.transpose(np.reshape(mu0, (O, M, Q)), (0, 2, 1))

            if "c" in self.init_params:
                self.covars_ = np.transpose(np.reshape(Sigma0, (O, O, M, Q)), (0, 1, 3, 2))

        mixmat0, _ = mk_stochastic(np.random.rand(Q, M))

        self.LL, prior1, transmat1, mu1, Sigma1, mixmat1 = mhmm_em(
            data=obs,
            prior=self.startprob_,
            transmat=self.transmat_,
            mu=self.means_,
            Sigma=self.covars_,
            mixmat=mixmat0,
            max_iter=self.n_iter,
            thresh=self.thresh,
            cov_type=self._covariance_type,
            adj_trans="t" in self.params,
            adj_mix="w" in self.params,
            adj_mu="m" in self.params,
            adj_Sigma="c" in self.params,
        )

        self.startprob_ = prior1
        self.transmat_ = transmat1
        self.means_ = mu1
        self.covars_ = Sigma1
        self.weights_ = mixmat1
Exemplo n.º 6
0
    def test(self):

        mean0 = [0, 10]
        cov = [[.1, 0], [0, .1]]

        obs = np.random.multivariate_normal(mean0, cov, 10000)

        mean1 = [5, 15]
        cov = [[.1, 0], [0, .1]]

        obs = np.vstack((obs, np.random.multivariate_normal(mean1, cov,
                                                            30000)))

        mu, Sigma, weights = mixgauss_init(M=2,
                                           data=obs.T,
                                           cov_type='diag',
                                           method='kmeans')

        assert np.all(np.abs(mu[:, 0] - mean0) < 1e-1) or np.all(
            np.abs(mu[:, 1] - mean0) < 1e-1)
Exemplo n.º 7
0
    def setUp(self):

        self.prior = np.array([1, 0, 0])
        self.transmat = np.matrix([[0, 1, 0], [0, 0, 1], [0, 0, 1]])

        self.mu = np.zeros((2, 3, 2))
        self.mu[:, :, 0] = np.array([[1, 2, 3], [1, 2, 3]])
        self.mu[:, :, 1] = np.array([[4, 5, 6], [4, 5, 6]])

        self.Sigma = np.zeros((2, 2, 3, 2))
        for i in range(3):
            self.Sigma[:, :, i, 0] = np.diag(np.ones((2, )) * 0.01)
            self.Sigma[:, :, i, 1] = np.diag(np.ones((2, )) * 0.01)

        self.mixmat = np.array([[.5, .5], [.5, .5], [.5, .5]])

        try:
            with open('MhmmEM2DTestGaussInit.cache', 'rb') as f:
                cache = load(f)

            self.obs = cache['obs']
            self.prior0 = cache['prior0']
            self.transmat0 = cache['transmat0']
            self.mu0 = cache['mu0']
            self.Sigma0 = cache['Sigma0']
            self.mixmat0 = cache['mixmat0']

        except:

            self.obs, hidden = mhmm_sample(T=4,
                                           numex=100,
                                           initial_prob=self.prior,
                                           transmat=self.transmat,
                                           mu=self.mu,
                                           Sigma=self.Sigma,
                                           mixmat=self.mixmat)

            self.prior0, _ = mk_stochastic(np.random.rand(3))
            self.transmat0, _ = mk_stochastic(np.random.rand(3, 3))

            O = self.obs.shape[0]
            M = 2
            Q = 3

            mu0, Sigma0, weights0 = mixgauss_init(Q * M,
                                                  self.obs,
                                                  cov_type='diag')

            self.mu0 = np.transpose(np.reshape(mu0, (O, M, Q)), (0, 2, 1))
            self.Sigma0 = np.transpose(np.reshape(Sigma0, (O, O, M, Q)),
                                       (0, 1, 3, 2))

            self.mixmat0, _ = mk_stochastic(np.random.rand(Q, M))

            cache = {
                'obs': self.obs,
                'prior0': self.prior0,
                'transmat0': self.transmat0,
                'mu0': self.mu0,
                'Sigma0': self.Sigma0,
                'mixmat0': self.mixmat0
            }

            with open('MhmmEM2DTestGaussInit.cache', 'wb') as f:
                dump(cache, f)
            savemat('MhmmEM2DTestGaussInit.mat', cache)
Exemplo n.º 8
0
    def setUp(self):
        
        self.prior = np.array([1, 0, 0])
        self.transmat = np.matrix([[0, 1, 0],
                              [0, 0, 1],
                              [0, 0, 1]])
        
        self.mu = np.zeros((2,3,2))
        self.mu[:,:,0] = np.array([[1, 2, 3],
                              [1, 2, 3]])
        self.mu[:,:,1] = np.array([[4, 5, 6],
                              [4, 5, 6]])
        
        self.Sigma = np.zeros((2, 2, 3, 2))
        for i in range(3):
            self.Sigma[:,:,i,0] = np.diag(np.ones((2,))*0.01)
            self.Sigma[:,:,i,1] = np.diag(np.ones((2,))*0.01)
        
        self.mixmat = np.array([[.5,.5],
                            [.5,.5],
                            [.5,.5]])
        
        try:
            with open('MhmmEM2DTestGaussInit.cache', 'rb') as f:
                cache = load(f)
            
            self.obs = cache['obs']
            self.prior0 = cache['prior0']
            self.transmat0 = cache['transmat0']
            self.mu0 = cache['mu0']
            self.Sigma0 = cache['Sigma0']
            self.mixmat0 = cache['mixmat0']
            
        except:
        
            self.obs, hidden = mhmm_sample(T=4, 
                                      numex=100,
                                      initial_prob=self.prior,
                                      transmat=self.transmat,
                                      mu=self.mu, 
                                      Sigma=self.Sigma,
                                      mixmat = self.mixmat)
        

            self.prior0, _ = mk_stochastic(np.random.rand(3))
            self.transmat0, _ = mk_stochastic(np.random.rand(3,3))
            
            O = self.obs.shape[0]
            M = 2
            Q = 3
            
            mu0, Sigma0, weights0 = mixgauss_init(Q*M, self.obs, cov_type='diag')
            
            self.mu0 = np.transpose(np.reshape(mu0, (O, M, Q)), (0,2,1))
            self.Sigma0 = np.transpose(np.reshape(Sigma0, (O, O, M, Q)), (0, 1, 3, 2))
            
            self.mixmat0, _ = mk_stochastic(np.random.rand(Q,M))
            
            cache =  {'obs':self.obs,
                      'prior0':self.prior0,
                      'transmat0':self.transmat0,
                      'mu0':self.mu0,
                      'Sigma0':self.Sigma0,
                      'mixmat0':self.mixmat0}
            
            with open('MhmmEM2DTestGaussInit.cache', 'wb') as f:
                dump(cache, f)
            savemat('MhmmEM2DTestGaussInit.mat', cache)