Package teamwork :: Package math :: Module probability :: Class Distribution
[hide private]
[frames] | no frames]

Class Distribution

source code

object --+    
         |    
      dict --+
             |
            Distribution

A probability distribution

The possible domain values are any objects


Warning: If you make the domain values mutable types, try not to change the values while they are inside the distribution. If you must change a domain value, it is better to first delete the old value, change it, and then re-insert it.

Instance Methods [hide private]
new empty dictionary

__init__(self, args=None)
x.__init__(...) initializes x; see x.__class__.__doc__ for signature
source code
 
__getitem__(self, element)
x[y]
source code
 
__setitem__(self, element, value)
x[i]=y
source code
 
__delitem__(self, element)
del x[y]
source code
None
clear(self)
Remove all items from D.
source code
 
replace(self, old, new)
Replaces on element in the sample space with another.
source code
list
domain(self)
Returns: the sample space of this probability distribution
source code
(value,float)[]
items(self)
Returns: a list of tuples of value,probability pairs
source code
dict:teamwork.math.Keys.Key→boolean
domainKeys(self)
Returns: all keys contained in the domain values
source code
 
normalize(self)
Normalizes the distribution so that the sum of values = 1
source code
 
marginalize(self, key)
Marginalizes the distribution to remove the given key (not in place! returns the new distribution)
source code
 
getMarginal(self, key)
Marginalizes the distribution over all but the given key
source code
 
join(self, key, value, debug=False)
Returns the joint distribution that includes the given key
source code
 
expectation(self)
Returns the expected value of this distribution
source code
 
prune(self)
Removes any zero-probability entries from this distribution
source code
 
fill(self, keys, value=0.0)
Fills in any missing rows/columns in the domain matrices with a default value
source code
 
freeze(self)
Locks in the dimensions and keys of all domain values
source code
 
unfreeze(self)
Unlocks in the dimensions and keys of all domain values
source code
 
instantiate(self, table)
Substitutes values for any abstract references, using the given substitution table
source code
 
instantiateKeys(self, table)
Substitutes values for any abstract references, using the given substitution table
source code
 
compose(self, other, operator, replace=False, debug=False)
Composes this distribution with the other given, using the given op
source code
 
__add__(self, other) source code
 
__neg__(self) source code
 
__sub__(self, other) source code
 
__mul__(self, other) source code
 
__div__(self, other) source code
Distribution
conditional(self, other, value={})
Computes a conditional probability, given this joint probability P(AB), the marginal probability P(B), and the value of B being conditioned on
source code
Distribution[]
reachable(self, estimators, observations, horizon)
Computes any reachable distributions from this one
source code
 
sample(self)
Returns: a single element from the sample space, chosen randomly according to this distribution.
source code
 
__float__(self)
Supports float conversion of distributions by returning EV.
source code
 
__str__(self)
Returns a pretty string representation of this distribution
source code
 
simpleText(self, numbers=True, all=False) source code
 
__xml__(self)
Returns: An XML Document object representing this distribution
source code
 
__copy__(self) source code
 
__deepcopy__(self, memo) source code
 
parse(self, element, valueClass=None)
Extracts the distribution from the given XML element
source code

Inherited from dict: __cmp__, __contains__, __eq__, __ge__, __getattribute__, __gt__, __hash__, __iter__, __le__, __len__, __lt__, __ne__, __new__, __repr__, copy, fromkeys, get, has_key, iteritems, iterkeys, itervalues, keys, pop, popitem, setdefault, update, values

Inherited from object: __delattr__, __reduce__, __reduce_ex__, __setattr__

Class Variables [hide private]
float epsilon = 0.0001
the granularity for float comparisons
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, args=None)
(Constructor)

source code 

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Returns:
new empty dictionary

Overrides: object.__init__
(inherited documentation)

__getitem__(self, element)
(Indexing operator)

source code 

x[y]

Overrides: dict.__getitem__
(inherited documentation)

__setitem__(self, element, value)
(Index assignment operator)

source code 

x[i]=y

Parameters:
  • element - the domain element
  • value (float) - the probability to associate with the given key
Overrides: dict.__setitem__

Warning: raises an AssertionError if setting to an invalid prob value

__delitem__(self, element)
(Index deletion operator)

source code 

del x[y]

Overrides: dict.__delitem__
(inherited documentation)

clear(self)

source code 

Remove all items from D.

Returns: None
Overrides: dict.clear
(inherited documentation)

replace(self, old, new)

source code 

Replaces on element in the sample space with another. Raises an exception if the original element does not exist, and an exception if the new element already exists (i.e., does not do a merge)

domain(self)

source code 
Returns: list
the sample space of this probability distribution

items(self)

source code 
Returns: (value,float)[]
a list of tuples of value,probability pairs
Overrides: dict.items

domainKeys(self)

source code 
Returns: dict:teamwork.math.Keys.Key→boolean
all keys contained in the domain values

normalize(self)

source code 

Normalizes the distribution so that the sum of values = 1

Note: Not sure if this is really necessary

marginalize(self, key)

source code 

Marginalizes the distribution to remove the given key (not in place! returns the new distribution)

Parameters:
  • key - the key to marginalize over
Returns:
a new Distribution object representing the marginal distribution

Note: no exception is raised if the key is not present

getMarginal(self, key)

source code 

Marginalizes the distribution over all but the given key

Parameters:
  • key - the key to compute the marginal distribution over
Returns:
a new Distribution object representing the marginal

join(self, key, value, debug=False)

source code 

Returns the joint distribution that includes the given key

Parameters:
  • key - any hashable instance
  • value - if a Distribution, the marginal distribution for the given key; otherwise, the marginal distribution is assumed to be P(key=value)=1
Returns:
the joint distribution combining the current distribution with the specified marginal over the given key

Warning: this method assumes that this Distribution has domain values that are dict instances (i.e., for each domain element e, it can set e[key]=value)...in other words, there should probably be a subclass.

expectation(self)

source code 

Returns the expected value of this distribution

Warning: As a side effect, the distribution will be normalized

prune(self)

source code 

Removes any zero-probability entries from this distribution

Returns:
the pruned distribution (not a copy)

fill(self, keys, value=0.0)

source code 

Fills in any missing rows/columns in the domain matrices with a default value

Parameters:
  • keys (teamwork.math.Keys.Key[]) - the new slots that should be filled
  • value - the default value (default is 0.)

Note: essentially calls appropriate fill method for any domain objects

instantiate(self, table)

source code 

Substitutes values for any abstract references, using the given substitution table

Parameters:
  • table (dictionary) - dictionary of key-value pairs, where the value will be substituted for any appearance of the given key in a field of this teamwork.math.Keys.Key object

instantiateKeys(self, table)

source code 

Substitutes values for any abstract references, using the given substitution table

Parameters:
  • table (dictionary) - dictionary of key-value pairs, where the value will be substituted for any appearance of the given key in a field of this teamwork.math.Keys.Key object

compose(self, other, operator, replace=False, debug=False)

source code 

Composes this distribution with the other given, using the given op

Parameters:
  • other - a Distribution object, or an object of the same class as the keys in this Distribution object
  • operator - a binary operator applicable to the class of keys in this Distribution object
  • replace - if this flag is true, the result modifies this distribution itself
Returns:
the composed distribution

__add__(self, other)
(Addition operator)

source code 

Note: Also supports + operator between Distribution object and objects of the same class as its keys

__sub__(self, other)
(Subtraction operator)

source code 

Note: Also supports - operator between Distribution object and objects of the same class as its keys

__mul__(self, other)

source code 

Note: Also supports * operator between Distribution object and objects of the same class as its keys

conditional(self, other, value={})

source code 

Computes a conditional probability, given this joint probability P(AB), the marginal probability P(B), and the value of B being conditioned on

Parameters:
  • other (Distribution) - the marginal probability, P(B)
  • value (KeyedVector (if omitted, it's assumed that both P(AB) and P(B) have already been conditioned on the desired value)) - the value of B
Returns: Distribution
P(A|B=value) where self is P(AB)

reachable(self, estimators, observations, horizon)

source code 

Computes any reachable distributions from this one

Parameters:
  • estimators (dict[]) - any possible conditional probability distributions, expressed as dictionaries, each containing numerator and denominator fields
  • observations (KeyedVector[]) - any possible observations
  • horizon - the maximum length of observation sequences to consider (if less than 1, then only the current distribution is reachable)
Returns: Distribution[]
all the reachable distributions

sample(self)

source code 
Returns:
a single element from the sample space, chosen randomly according to this distribution.

__float__(self)

source code 

Supports float conversion of distributions by returning EV. Invoked by calling float(self)

__str__(self)
(Informal representation operator)

source code 

Returns a pretty string representation of this distribution

Overrides: object.__str__

simpleText(self, numbers=True, all=False)

source code 
Parameters:
  • numbers - if True, returns a number-free representation of this distribution

__xml__(self)

source code 
Returns:
An XML Document object representing this distribution

parse(self, element, valueClass=None)

source code 

Extracts the distribution from the given XML element

Parameters:
  • element (Element) - The XML Element object specifying the distribution
  • valueClass - The class used to generate the domain values for this distribution
Returns:
This Distribution object