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 test(self):

        mu = np.zeros((1, 3, 1))
        mu[0, :, 0] = [1, 2, 3]
        Sigma = np.zeros((1, 1, 3, 1))
        Sigma[0, 0, :, 0] = 0.01

        obs, hidden = mhmm_sample(T=4,
                                  numex=1000,
                                  initial_prob=np.array([1, 0, 0]),
                                  transmat=np.matrix([[0, 1, 0], [0, 0, 1],
                                                      [0, 0, 1.]]),
                                  mu=mu,
                                  Sigma=Sigma)

        assert np.all(
            np.abs(
                np.mean(np.asmatrix(obs), axis=1) -
                np.array([[1], [2], [3], [3]])) < 1e-1)
Example #3
0
 def test(self):
     
     mu = np.zeros((1,3,1))
     mu[0,:,0] = [1, 2, 3]
     Sigma = np.zeros((1, 1, 3, 1))
     Sigma[0,0,:,0] = 0.01
     
     obs, hidden = mhmm_sample(T=4, 
                               numex=1000,
                               initial_prob=np.array([1, 0, 0]),
                               transmat=np.matrix([[0, 1, 0],
                                                   [0, 0, 1],
                                                   [0, 0, 1.]]),
                               mu=mu, 
                               Sigma=Sigma)
     
     assert np.all(np.abs(np.mean(np.asmatrix(obs), axis=1)-np.array([[1],
                                                                      [2],
                                                                      [3],
                                                                      [3]])) < 1e-1)
Example #4
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 #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((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 #6
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 #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)
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('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)