Package gPy :: Module Models :: Class _AbsM
[hide private]
[frames] | no frames]

Class _AbsM

source code


Abstract model class

Defined by a hypergraph and specification of values for the variables. Represents the set of all probability distributions which have a factored representation whose hypergraph is the specifed one.

Since the hypergraph is not required to be simple it is not useful to actually create objects of this class, hence it is an abstract class. The most general model class is that of log-linear models (LLM objects, not yet implemented) where the associated hypergraph is required to be simple.

Instance Methods [hide private]
 
__init__(self, factors=(), domain=None, new_domain_variables=None, must_be_new=False, check=False)
Initialise a hierarchical model
source code
A Parameters.Factor object (for simple) or a list of such objects (non-simple)
__getitem__(self, hyperedge)
Return the factor(s) (not a copy) whose variables are hyperedge
source code
Iterator
__iter__(self)
Return an iterator over the factors in the model
source code
Int
__len__(self)
Return the number of factors in the model
source code
 
__repr__(self)
repr(x)
source code
 
__setitem__(self, hyperedge, factor)
Set the factor whose variables are hyperedge to factor
source code
String
__str__(self)
Print each factor separately
source code
Same as self
__div__(self, other)
Return the result of dividing a hierarchical model by a scalar
source code
Same as self
__idiv__(self, other)
Divide a hierarchical model by a scalar
source code
Same as self
__imul__(self, other)
Multiply a hierarchical model by a factor, scalar or another FR
source code
Same as self
__mul__(self, other)
Return the result of multiplying a hierarchical model by a factor, scalar or another FR
source code
 
add_ident(self, hyperedge)
Add an ident factor for a hyperedge to a hierarchical model
source code
Same as self
condition(self, condition, keep_class=False)
Alter a distribution by effecting the restriction on variables given by condition
source code
Same type as self
copy(self, copy_domain=False)
Return a deep copy of a hierarchical model
source code
Parameters.CPT
cpt(self, child, parents=())
Return a conditional probability table for specified child and parents
source code
 
eliminate_variable(self, variable, trace=False)
Alter a factored representation by summing out a variable
source code
Parameters.Factor object
factor(self, hyperedge)
Return the factor produced by multiplying all factors with variables hyperedge
source code
List
factors(self)
Return a list of the factors in the model
source code
List
factors_containing_variable(self, variable)
Return a list of factors containing variable
source code
List
items(self)
Return sequence of factor.variables(),factor pairs for each factor in the model.
source code
 
gui_display(self, parent, colours=None)
Display a GUI widget for displaying a model
source code
Hypergraph
hypergraph(self)
Return (a copy of) the hypergraph associated with the model
source code
 
inc_from_rawdata(self, rawdata)
Increment self with counts directly from rawdata
source code
Graphs.UGraph
interaction_graph(self)
Return the interaction graph for a model
source code
Boolean
is_simple(self)
Whether the model is simple
source code
DFR
make_decomposable(self, elimination_ordering=None)
Return a decomposable model using an elimination ordering
source code
 
marginal(self, variables)
Alter a model to represent the marginal distibution on variables
source code
 
marginal_factor(self, variables)
Return the marginal distibution on variables as a Parameters.Factor
source code
Class of self
marginalise_away(self, variables, naive=True)
Alter a factored representation by summing out variables
source code
 
makeDN(self, allow_dummies=False)
Make a dependency network where the CPT for each variable is its distribution conditional on its Markov blanket
source code
Set
markov_blanket(self, variable)
Return the Markov blanket for variable
source code
 
markov_blanket_cpt(self, variable, allow_dummies=False)
Create a CPT for the distribution of variable conditional on its Markov blanket
source code
 
num_factors_containing_variable(self, variable)
Return the number of distincet factors containing a given variable
source code
 
remove(self, hyperedge)
Remove a factor or factors from a hierarchical model
source code
List
red(self)
Reduce the model, returning any distinct redundant hyperedges
source code
Boolean
reduced(self)
Test whether a model is reduced
source code
 
simplify(self)
Simplify the model
source code
Parameters.Factor object
some_factor(self, hyperedge)
Return an arbitrary factor with variables hyperedge
source code
Same as self
sumout(self, variables)
Sum out (marginalise away) variables using maximum cardinality
source code
Class of self
variable_elimination(self, variables, naive=True)
Alter a factored representation by summing out variables
source code
None or Float
variable_elimination_trace(self, variables)
As variable_elimination but also return the corresponding Graphs.DiForest object
source code
Float
z(self)
Return the sum of values associated with each full joint instantiation
source code
 
zero(self)
Set all values in all factors to zero
source code
 
_add_factor(self, factor)
Multiply a hierarchical model by a factor
source code
 
_add_factor_to_dict(self, hyperedge, factor)
Add a factor to the _factors dictionary
source code
 
_check_condition(self, condition) source code
 
_put_factor_to_dict(self, hyperedge, factor)
Put a factor in the _factors dictionary
source code

Inherited from Variables.SubDomain: __add__, __iadd__, __isub__, __rdiv__, __rmul__, __sub__, drop_variable, drop_variables, inst2index, insts, insts_indices, marginalise_onto, table_size, uses_default_domain, variables, varvalues

Inherited from Variables.Domain: add_domain_variable, add_domain_variables, add_domain_variables_from_rawdata, change_domain_variable, change_domain_variables, common_domain, known_variable, numvals, values

Inherited from object: __delattr__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __setattr__

Instance Variables [hide private]
Dictionary _factors
A mapping from each distinct hyperedge in the associated hypergraph to a list of factors having the variables of the hyperedge.
Hypergraph object _hypergraph
The hypergraph associated with the model.

Inherited from Variables.SubDomain (private): _variables

Inherited from Variables.Domain (private): _domain, _instd, _numvals

Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, factors=(), domain=None, new_domain_variables=None, must_be_new=False, check=False)
(Constructor)

source code 

Initialise a hierarchical model

Each factor has its domain expanded to be the domain of the model, if necessary.

Parameters:
  • factors (Sequence, each element of which is a Parameters.Factor or Variables.SubDomain object. (Alternatively an object of class FR or one of its subclasses.)) - The factors in the hierarchical model. (Alternatively an existing object of class FR (or its subclasses), in which case self has identical attributes.)
  • domain (Variables.Domain or None) - A domain for the model. If None and all factors have the same domain then this domain is used. If None and all factors do not have the same domain then the internal default domain is used.
  • new_domain_variables (Dict or None) - A dictionary containing a mapping from any new variables to their values. domain is updated with these values
  • must_be_new (Boolean) - Whether domain variables in new_domain_variables have to be new
  • check (Boolean) - Whether to check that all variables exist in domain
Raises:
  • VariableError - If a variable in new_domain_variables already exists with values different from its values in new_domain_variables; Or if must_be_new is set and the variable already exists. Or if check is set and a variable in variables is not in the domain
Overrides: object.__init__

__getitem__(self, hyperedge)
(Indexing operator)

source code 

Return the factor(s) (not a copy) whose variables are hyperedge

Parameters:
  • hyperedge (Iterable) - The variables of the sought factor
Returns: A Parameters.Factor object (for simple) or a list of such objects (non-simple)
The factor(s)
Raises:
  • KeyError - If there is no factor with these variables

__iter__(self)

source code 

Return an iterator over the factors in the model

To allow for factor in model: ... constructions.

Returns: Iterator
An iterator over the factors in the model

__len__(self)
(Length operator)

source code 

Return the number of factors in the model

Returns: Int
The number of factors in the model

__repr__(self)
(Representation operator)

source code 

repr(x)

Overrides: object.__repr__
(inherited documentation)

__setitem__(self, hyperedge, factor)
(Index assignment operator)

source code 

Set the factor whose variables are hyperedge to factor

Parameters:
  • hyperedge (Iterable) - The variables of the factor
  • factor (Parameters.Factor) - Factor which will replace existing factor(s)
Raises:
  • KeyError - If there is no factor with these variables

__str__(self)
(Informal representation operator)

source code 

Print each factor separately

Use lexicographical ordering on factor variables

Returns: String
Pretty representation of a FR
Overrides: object.__str__

__div__(self, other)

source code 

Return the result of dividing a hierarchical model by a scalar

A randomly chosen factor is divided by the scalar

The returned value shares the same domain as self. To avoid this make a deep copy and do in-place division.

Parameters:
  • other (float or int) - The scalar
Returns: Same as self
Result of factor division
Raises:
  • VariableError - If self and other use a variable with different values in each one's domain.
Overrides: Variables.SubDomain.__div__

__idiv__(self, other)

source code 

Divide a hierarchical model by a scalar

A randomly chosen factor is divided by the scalar

Parameters:
  • other (float or int) - The scalar
Returns: Same as self
self after being divided
Raises:
  • VariableError - If self and other use a variable with different values in each one's domain.
Overrides: Variables.SubDomain.__idiv__

__imul__(self, other)

source code 

Multiply a hierarchical model by a factor, scalar or another FR

Parameters:
  • other (Factor of FR object) - The factor or FR object to be multiplied in
Returns: Same as self
self after being multiplied
Raises:
  • VariableError - If self and other use a variable with different values in each one's domain.
Overrides: Variables.SubDomain.__imul__

__mul__(self, other)

source code 

Return the result of multiplying a hierarchical model by a factor, scalar or another FR

The returned value shares the same domain as self. To avoid this make a deep copy and do in-place multiplication.

Parameters:
  • other (Factor of FR object) - The factor or FR object to be multiplied in
Returns: Same as self
Result of factor multiplication
Raises:
  • VariableError - If self and other use a variable with different values in each one's domain.
Overrides: Variables.SubDomain.__mul__

add_ident(self, hyperedge)

source code 

Add an ident factor for a hyperedge to a hierarchical model

Typically hyperedge will come from a Hypergraph model.

An ident factor maps all instantiations to 1.0

Parameters:
  • hyperedge (Iterable) - The variables for the factor

condition(self, condition, keep_class=False)

source code 

Alter a distribution by effecting the restriction on variables given by condition

This alters the model's domain. Make a copy with copy_domain=True if the original domain will be needed

Parameters:
  • condition (Dict) - Dictionary of the form {var1:values1,var2:values2..} Each value of this dictionary must be an iterable
  • keep_class (Boolean) - TODO
Returns: Same as self
The conditioned model
Raises:
  • KeyError - If a variable is used that is not in the model
  • ValueError - If a value is used that is not a possible value of the variable it is attached to

copy(self, copy_domain=False)

source code 

Return a deep copy of a hierarchical model

Parameters:
  • copy_domain (Boolean) - If true self's domain is copied, otherwise the copy shares self's domain
Returns: Same type as self
A copy of self
Overrides: Variables.Domain.copy

cpt(self, child, parents=())

source code 

Return a conditional probability table for specified child and parents

Return the probability distribution of child conditional on parents

Parameters:
  • child (String) - Child of the CPT
  • parents (Sequence) - Parents of child in the CPT
Returns: Parameters.CPT
The specified CPT

eliminate_variable(self, variable, trace=False)

source code 

Alter a factored representation by summing out a variable

Removes one or more factors.

Parameters:
  • variable (String) - The variable to eliminate
Raises:
  • KeyError - If variable is not in the model

factor(self, hyperedge)

source code 

Return the factor produced by multiplying all factors with variables hyperedge

Parameters:
  • hyperedge (Iterable) - Set of variables
Returns: Parameters.Factor object
Product of all factors with variables hyperedge
Raises:
  • KeyError - if no factor has hyperedge as variables.

factors(self)

source code 

Return a list of the factors in the model

Returns: List
A list of the factors in the model

factors_containing_variable(self, variable)

source code 

Return a list of factors containing variable

If non-simple, then a list of lists of factors is returned

Parameters:
  • variable (Immutable (usually string)) - A variable
Returns: List
A list of factors containing variable

items(self)

source code 

Return sequence of factor.variables(),factor pairs for each factor in the model.

Returns: List
Sequence of factor.variables(),factor pairs for each factor in the model

gui_display(self, parent, colours=None)

source code 

Display a GUI widget for displaying a model

Parameters:
  • parent (Some suitable Tk object.) - A widget into which the GUI is placed.
  • colours (Dictionary) - Mapping from hyperedges to colours

hypergraph(self)

source code 

Return (a copy of) the hypergraph associated with the model

Returns: Hypergraph
(A copy of) the hypergraph associated with the model

inc_from_rawdata(self, rawdata)

source code 

Increment self with counts directly from rawdata

OK for parameter fitting not for structure learning

Parameters:
  • rawdata (Tuple) - A tuple like that returned by IO.read_csv.
Raises:
  • IndexError - If self has a variable missing from rawdata

interaction_graph(self)

source code 

Return the interaction graph for a model

The interaction graph contains an edge for any pair of variables which are members of a common factor.

Returns: Graphs.UGraph
The interaction graph

is_simple(self)

source code 

Whether the model is simple

Returns: Boolean
Whether the model is simple

make_decomposable(self, elimination_ordering=None)

source code 

Return a decomposable model using an elimination ordering

If no elimination_ordering is given Hypergraphs.ReducedHypergraph.maximum_cardinality_search is used to provide one.

Returned object shares many attributes with self. Use a copy of self, if it still needed as an independent object.

Parameters:
  • elimination_ordering (Sequence) - Order in which to eliminate variables
Returns: DFR
A decomposable model

marginal(self, variables)

source code 

Alter a model to represent the marginal distibution on variables

Marginal only represented up to normalisation

If self is a BN and variables is an ancestral set then self remains a BN otherwise it becomes a general hierarchical model.

marginal_factor(self, variables)

source code 

Return the marginal distibution on variables as a Parameters.Factor

Marginal is properly normalised

marginalise_away(self, variables, naive=True)

source code 

Alter a factored representation by summing out variables

TODO: If we just want a marginal, no need to bother altering self

Parameters:
  • variables (Sequence type) - Ordered variables to eliminate
  • naive (Boolean) - If True the variables are summed out in the order given by variables. If False conditional independence and the number of factors a variable is in is used to construct a more intelligent order.
Returns: Class of self
The altered self

markov_blanket(self, variable)

source code 

Return the Markov blanket for variable

Parameters:
  • variable (Immutable) - Variable in the model
Returns: Set
Markov blanket for variable
Raises:
  • KeyError - If variable is not in the model

markov_blanket_cpt(self, variable, allow_dummies=False)

source code 

Create a CPT for the distribution of variable conditional on its Markov blanket

Parameters:
  • variable (Immutable) - variable for which the conditional distribution is required

remove(self, hyperedge)

source code 

Remove a factor or factors from a hierarchical model

All factors with variables hyperedge will be removed

Parameters:
  • hyperedge (Iterable) - The variables of the factor
Raises:
  • KeyError - If no factor with these variables exists

red(self)

source code 

Reduce the model, returning any distinct redundant hyperedges

Any factor all of whose variables are contained in another is removed

Returns: List
Redundant hyperedges

reduced(self)

source code 

Test whether a model is reduced

Returns: Boolean
Whether a model is reduced

simplify(self)

source code 

Simplify the model

so that there are no factors with the same variable sets

some_factor(self, hyperedge)

source code 

Return an arbitrary factor with variables hyperedge

Parameters:
  • hyperedge (Iterable) - Variable set
Returns: Parameters.Factor object
Factor
Raises:
  • KeyError - If no factor with hyperedge as varaibles exists

sumout(self, variables)

source code 

Sum out (marginalise away) variables using maximum cardinality

variables may be altered

Parameters:
  • variables (Iterable) - Variables to sum out
Returns: Same as self
The marginal model
Overrides: Variables.SubDomain.sumout

variable_elimination(self, variables, naive=True)

source code 

Alter a factored representation by summing out variables

TODO: If we just want a marginal, no need to bother altering self

Parameters:
  • variables (Sequence type) - Ordered variables to eliminate
  • naive (Boolean) - If True the variables are summed out in the order given by variables. If False conditional independence and the number of factors a variable is in is used to construct a more intelligent order.
Returns: Class of self
The altered self

variable_elimination_trace(self, variables)

source code 

As variable_elimination but also return the corresponding Graphs.DiForest object

If all variables are summed out then the relevant scalar is returned

Parameters:
  • variables (Sequence type) - Ordered variables to eliminate
Returns: None or Float
None usually. The relevant scalar if all variables are summed out

z(self)

source code 

Return the sum of values associated with each full joint instantiation

This is the partition function. Result is generally not 1.0 since models may represent distributions only up to normalisation. An expensive operation.

Returns: Float
The sum of values associated with each full joint instantiation

To Do: Use sensible ordering of variables to eliminate

_add_factor(self, factor)

source code 

Multiply a hierarchical model by a factor

Parameters:
  • factor (Factor) - The factor to be multiplied in

_add_factor_to_dict(self, hyperedge, factor)

source code 

Add a factor to the _factors dictionary

Parameters:
  • hyperedge (Frozenset) - The hyperedge for the factor
  • factor (Factor) - The factor to be added

_put_factor_to_dict(self, hyperedge, factor)

source code 

Put a factor in the _factors dictionary

Overwriting any previous entry

Parameters:
  • hyperedge (Frozenset) - The hyperedge for the factor
  • factor (Factor) - The factor to be put

Instance Variable Details [hide private]

_hypergraph

The hypergraph associated with the model. There is exactly one hyperedge for each factor. The hyperedge for a given factor is just the set of that factor's variables (implemented as a frozenset). If the hypergraph is non-simple, then hyperedges can be repeated.
Type:
Hypergraph object