Example #1
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((1, 3, 2))
        self.mu[0, :, 0] = [1, 2, 3]
        self.mu[0, :, 1] = [4, 5, 6]
        self.Sigma = np.zeros((1, 1, 3, 2))
        self.Sigma[0, 0, :, 0] = 0.01
        self.Sigma[0, 0, :, 1] = 0.01
        self.mixmat = np.array([[.5, .5], [.5, .5], [.5, .5]])

        self.obs, self.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))

        self.mu0 = np.zeros((1, 3, 2))
        self.mu0[0, :, 0] = [1.5, 2.5, 3.5]
        self.mu0[0, :, 1] = [4.5, 5.5, 6.5]

        self.Sigma0 = np.zeros((1, 1, 3, 2))
        self.Sigma0[0, 0, :, 0] = 1.0
        self.Sigma0[0, 0, :, 1] = 1.0

        self.mixmat0 = np.array([[.3, .7], [.3, .7], [.3, .7]])
Example #2
0
 def testMatrix(self):
     T = np.asmatrix(np.random.rand(3,3))
     M, S = mk_stochastic(T)
     
     assert np.all(np.abs(np.sum(M,1)-np.matrix([[1],
                                           [1],
                                           [1]])) < 1e-3)
Example #3
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
Example #4
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
Example #5
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((1,3,2))
     self.mu[0,:,0] = [1, 2, 3]
     self.mu[0,:,1] = [4, 5, 6]
     self.Sigma = np.zeros((1, 1, 3, 2))
     self.Sigma[0,0,:,0] = 0.01
     self.Sigma[0,0,:,1] = 0.01
     self.mixmat = np.array([[.5,.5],
                         [.5,.5],
                         [.5,.5]])
     
     self.obs, self.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))
     
     self.mu0 = np.zeros((1,3,2))
     self.mu0[0,:,0] = [1.5, 2.5, 3.5]
     self.mu0[0,:,1] = [4.5, 5.5, 6.5]
     
     self.Sigma0 = np.zeros((1, 1, 3, 2))
     self.Sigma0[0,0,:,0] = 1.0
     self.Sigma0[0,0,:,1] = 1.0
     
     self.mixmat0 = np.array([[.3,.7],
                         [.3,.7],
                         [.3,.7]])
Example #6
0
    def testTensor(self):
        T = np.random.rand(3, 3, 3)
        M, S = mk_stochastic(T)

        assert np.all(np.abs(np.sum(M, 2) - np.ones((3, 3))) < 1e-3)
Example #7
0
    def testMatrix(self):
        T = np.asmatrix(np.random.rand(3, 3))
        M, S = mk_stochastic(T)

        assert np.all(np.abs(np.sum(M, 1) - np.matrix([[1], [1], [1]])) < 1e-3)
Example #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)
Example #9
0
    def testVector(self):
        T = np.random.rand(1, 3)[0, :]

        M, S = mk_stochastic(T)

        assert np.abs(sum(M) - 1) < 1e-3
Example #10
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('MhmmEM2DTest.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))

            self.mu0 = np.zeros((2, 3, 2))
            self.mu0[:, :, 0] = np.array([[1.5, 2.5, 3.5], [1.5, 2.5, 3.5]])
            self.mu0[:, :, 1] = np.array([[4.5, 5.5, 6.5], [4.5, 5.5, 6.5]])

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

            self.mixmat0 = np.array([[.2, .8], [.2, .8], [.2, .8]])

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

            with open('MhmmEM2DTest.cache', 'wb') as f:
                dump(cache, f)
            savemat('MhmmEM2DTest.mat', cache)
Example #11
0
 def testTensor(self):
     T = np.random.rand(3,3,3)
     M, S = mk_stochastic(T)
     
     assert np.all(np.abs(np.sum(M, 2)-np.ones((3,3)))<1e-3)
Example #12
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)
Example #13
0
 def testVector(self):
     T = np.random.rand(1,3)[0,:]
     
     M, S = mk_stochastic(T)
     
     assert np.abs(sum(M)-1) < 1e-3
Example #14
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('MhmmEM2DTest.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))
            
            self.mu0 = np.zeros((2,3,2))
            self.mu0[:,:,0] = np.array([[1.5, 2.5, 3.5],
                                  [1.5, 2.5, 3.5]])
            self.mu0[:,:,1] = np.array([[4.5, 5.5, 6.5],
                                  [4.5, 5.5, 6.5]])
            
            self.Sigma0 = np.zeros((2, 2, 3, 2))
            for i in range(3):
                self.Sigma0[:,:,i,0] = np.diag(np.ones((2,))*1.0)
                self.Sigma0[:,:,i,1] = np.diag(np.ones((2,))*1.0)
            
            self.mixmat0= np.array([[.2,.8],
                                [.2,.8],
                                [.2,.8]])
        
            cache =  {'obs':self.obs,
                      'prior0':self.prior0,
                      'transmat0':self.transmat0,
                      'mu0':self.mu0,
                      'Sigma0':self.Sigma0,
                      'mixmat0':self.mixmat0}
            
            with open('MhmmEM2DTest.cache', 'wb') as f:
                dump(cache, f)
            savemat('MhmmEM2DTest.mat', cache)