Beispiel #1
0
 def vector(self):
     """
     :return: if this distribution contains only a single vector, return that vector; otherwise, throw exception
     :rtype: KeyedVector
     """
     vector = KeyedVector()
     for substate, distribution in self.distributions.items():
         assert len(
             distribution
         ) == 1, 'Cannot return vector from uncertain distribution'
         vector.update(distribution.domain()[0])
     return vector
Beispiel #2
0
 def __str__(self):
     certain = [
         dist for dist in self.distributions.values() if len(dist) == 1
     ]
     vector = KeyedVector()
     for dist in certain:
         vector.update({
             key: value
             for key, value in dist.first().items() if key != keys.CONSTANT
         })
     return '%s\n%s' % (vector.sortedString(), '\n---\n'.join([
         str(dist) for dist in self.distributions.values() if len(dist) > 1
     ]))
Beispiel #3
0
 def select(self, maximize=False, incremental=False):
     """
     Reduce distribution to a single element, sampled according to the given distribution
     :param incremental: if C{True}, then select each key value in series (rather than picking out a joint vector all at once, default is C{False})
     :return: the probability of the selection made
     """
     if incremental:
         prob = KeyedVector()
     else:
         prob = 1
     for distribution in self.distributions.values():
         if incremental:
             prob.update(distribution.select(maximize, incremental))
         else:
             prob *= distribution.select(maximize, incremental)
     return prob