Example #1
0
class TestZMGauss(object):
  def setUp(self):
    self.Sigma = np.eye(4)
    self.distr = ZMGaussDistr(Sigma=self.Sigma.copy())
    
  def test_dimension(self):
    assert self.distr.D == self.Sigma.shape[0]
    
  def test_cholSigma(self):
    chol = self.distr.cholSigma()
    assert np.allclose(np.dot(chol, chol.T), self.distr.Sigma)
    
  def test_logdetSigma(self):
    logdetSigma = self.distr.logdetSigma()
    assert np.allclose( np.log(np.linalg.det(self.Sigma)), logdetSigma)
  
  def test_get_log_norm_const(self):
    logZ = self.distr.get_log_norm_const()
    logdetSigma = np.log(np.linalg.det(self.Sigma))
    mylogZ = 0.5*self.Sigma.shape[0]*np.log(2*np.pi) + 0.5 * logdetSigma
    
  def test_dist_mahalanobis(self, N=10):
    X = np.random.randn(N, self.distr.D)
    Dist = self.distr.dist_mahalanobis(X)
    invSigma = np.linalg.inv(self.Sigma)
    MyDist = np.zeros(N)
    for ii in range(N):
      x = X[ii]
      MyDist[ii] = np.dot(x.T, np.dot(invSigma, x))
      #if error, we print it out
      print MyDist[ii], Dist[ii]
    assert np.allclose(MyDist, Dist)
Example #2
0
 def set_global_params(self, hmodel=None, Sigma=None, **kwargs):
     ''' Set global parameters to provided values
 '''
     if hmodel is not None:
         self.comp = [copy.deepcopy(c) for c in hmodel.obsModel.comp]
         self.K = hmodel.obsModel.K
     if Sigma is not None:
         self.K = Sigma.shape[0]
         self.comp = [None for k in range(self.K)]
         for k in range(self.K):
             self.comp[k] = ZMGaussDistr(Sigma=Sigma[k])
Example #3
0
 def CreateWithAllComps(cls, oDict, obsPrior, compDictList):
     if 'min_covar' in oDict:
         mc = oDict['min_covar']
         self = cls(oDict['inferType'], obsPrior=obsPrior, min_covar=mc)
     else:
         self = cls(oDict['inferType'], obsPrior=obsPrior)
     self.K = len(compDictList)
     self.comp = [None for k in range(self.K)]
     if self.inferType == 'EM':
         for k in xrange(self.K):
             self.comp[k] = ZMGaussDistr(**compDictList[k])
             self.D = self.comp[k].D
     elif self.inferType.count('VB') > 0:
         for k in xrange(self.K):
             self.comp[k] = WishartDistr(**compDictList[k])
             self.D = self.comp[k].D
     return self
Example #4
0
 def update_obs_params_EM(self, SS, **kwargs):
     for k in xrange(self.K):
         covMat = SS.xxT[k] / SS.N[k]
         covMat += self.min_covar * np.eye(self.D)
         self.comp[k] = ZMGaussDistr(Sigma=covMat)
Example #5
0
 def setUp(self):
   self.Sigma = np.eye(4)
   self.distr = ZMGaussDistr(Sigma=self.Sigma.copy())
Example #6
0
 def setUp(self):
   R = np.random.rand(5,5)
   self.Sigma = np.dot(R, R.T) + 0.02*np.eye(5)
   self.distr = ZMGaussDistr(Sigma=self.Sigma)
Example #7
0
 def setUp(self):
   self.Sigma = np.asarray([[42.0]])
   self.distr = ZMGaussDistr(Sigma=self.Sigma)