Exemplo n.º 1
0
 def test_map(self):
     g = Gaussian(mean=Gaussian(np.zeros(2), np.ones(2)), var=np.ones(2))
     g.map(np.array([[1., 0.], [0., 1.], [-1., 0.], [0., -1.]]))
     self.assertTrue((g.mean == 0).all())
     g1 = Gaussian(mean=Gaussian(np.zeros(2), np.ones(2)), var=np.ones(2))
     g1.bayes(np.array([[1., 0.], [0., 1.], [-1., 0.], [0., -1.]]))
     self.assertTrue((g.mean == g1.mean.mean).all())
Exemplo n.º 2
0
 def __init__(self, initial_proba, transition_proba, means, covs):
     """
     construct hidden markov model with Gaussian emission model 用高斯模型构建HMM
     Parameters
     ----------
     initial_proba : (n_hidden,) np.ndarray or None
         probability of initial states 初始状态概率
     transition_proba : (n_hidden, n_hidden) np.ndarray or None
         transition probability matrix 转移概率矩阵, 是隐状态之间的转换
         (i, j) component denotes the transition probability from i-th to j-th hidden state
     means : (n_hidden, ndim) np.ndarray
         mean of each gaussian component 每一部分的系数(概率)?或者每一高斯部分的均值
     covs : (n_hidden, ndim, ndim) np.ndarray
         covariance matrix of each gaussian component 每一高斯部分的方差矩阵
     Attributes
     ----------
     ndim : int
         dimensionality of observation space 观测空间的维数
     n_hidden : int
         number of hidden states 隐藏状态的数量
     """
     assert initial_proba.size == transition_proba.shape[
         0] == transition_proba.shape[1] == means.shape[0] == covs.shape[0]
     assert means.shape[1] == covs.shape[1] == covs.shape[2]
     super().__init__(initial_proba, transition_proba)
     self.ndim = means.shape[1]
     self.means = means
     self.covs = covs
     self.precisions = np.linalg.inv(self.covs)
     self.gaussians = [Gaussian(m, cov) for m, cov in zip(means, covs)]
Exemplo n.º 3
0
 def test_init(self):
     g = Gaussian(mean=np.ones(2))
     self.assertTrue((g.mean == 1).all())
     self.assertTrue(g.var is None)
     g = Gaussian(mean=np.ones(3), var=np.ones(3) * 2)
     self.assertTrue(np.allclose(g.precision, np.ones(3) * 0.5))
Exemplo n.º 4
0
 def test_draw(self):
     g = Gaussian(mean=np.ones((2, 4)), var=np.ones((2, 4)) * 2)
     sample = g.draw(10000)
     self.assertTrue(np.allclose(np.mean(sample, 0), g.mean, 1e-1, 1e-1))
     self.assertTrue(np.allclose(np.var(sample, 0), g.var, 1e-1, 1e-1))
Exemplo n.º 5
0
 def test_pdf(self):
     g = Gaussian(mean=np.zeros(1), var=np.ones(1))
     self.assertEqual(g.pdf(np.zeros((1, 1))), (2 * np.pi)**-0.5)
     self.assertEqual(g.pdf(np.ones((1, 1))),
                      (2 * np.pi)**-0.5 * np.exp(-0.5))
Exemplo n.º 6
0
 def test_bayes(self):
     g = Gaussian(mean=Gaussian(np.zeros(2), np.ones(2)), var=np.ones(2))
     g.bayes(np.array([[1., 0.], [0., 1.], [-1., 0.], [0., -1.]]))
     self.assertTrue((g.mean.var == np.ones(2) * 0.2).all())
Exemplo n.º 7
0
 def test_ml(self):
     g = Gaussian()
     g.ml(np.array([[1., 0.], [0., 1.], [-1., 0.], [0., -1.]]))
     self.assertTrue((g.mean == 0).all())
     self.assertTrue((g.var == 0.5 * np.ones(2)).all())
Exemplo n.º 8
0
 def test_var(self):
     g = Gaussian(mean=np.ones((2, 3)), var=np.ones((2, 3)) * 2)
     self.assertTrue((g.var == np.ones((2, 3)) * 2).all())
Exemplo n.º 9
0
 def test_mean(self):
     g = Gaussian(mean=np.ones(3))
     self.assertTrue((g.mean == 1).all())
Exemplo n.º 10
0
 def test_repr(self):
     g = Gaussian(mean=np.ones(2), var=np.ones(2) * 2)
     self.assertEqual(repr(g),
                      "Gaussian(\nmean=\n[ 1.  1.],\nvar=\n[ 2.  2.]\n)")
Exemplo n.º 11
0
 def test_draw(self):
     g = Gaussian(mean=np.ones(2), var=np.eye(2) * 2)
     sample = g.draw(10000)
     self.assertTrue(np.allclose(np.mean(sample, 0), g.mean, 1e-1, 1e-1))
     self.assertTrue(np.allclose(np.cov(sample.T), g.var, 1e-1, 1e-1))