MCPcopy Create free account
hub / github.com/CyberPoint/libpgm / TableCPDFactorization

Class TableCPDFactorization

libpgm/tablecpdfactorization.py:35–435  ·  view source on GitHub ↗

This class represents a factorized Bayesian network with discrete CPD tables. It contains the attributes *bn*, *originalfactorlist*, and *factorlist*, and the methods *refresh*, *sumproductve*, *sumproducteliminatevar*, *condprobve*, *specificquery*, and *gibbssample*.

Source from the content-addressed store, hash-verified

33import copy
34
35class TableCPDFactorization():
36 '''
37 This class represents a factorized Bayesian network with discrete CPD tables. It contains the attributes *bn*, *originalfactorlist*, and *factorlist*, and the methods *refresh*, *sumproductve*, *sumproducteliminatevar*, *condprobve*, *specificquery*, and *gibbssample*.
38
39 '''
40
41 def __init__(self, bn):
42 '''
43 This class is constructed with a :doc:`DiscreteBayesianNetwork <discretebayesiannetwork>` instance as argument. First, it takes the input itself and stores it in the *bn* attribute. Then, it transforms the information of each of these nodes from standard discrete CPD form into a :doc:`TableCPDFactor <tablecpdfactor>` isntance and stores the instances in an array in the attribute *originalfactorlist*. Finally, it makes a copy of this list to work with and stores it in *factorlist*.
44
45 ''&#x27;
46 assert hasattr(bn, "V") and hasattr(bn, "E") and hasattr(bn, "Vdata"), \
47 "Input must be a DiscreteBayesianNetwork instance."
48
49 self.bn = bn
50 '''The Bayesian network used as argument at instantiation.'''
51 self.originalfactorlist = []
52 '''A list of :doc:`TableCPDFactor <tablecpdfactor>` instances, one per node.'''
53 for vertex in bn.V:
54 factor = TableCPDFactor(vertex, bn)
55 self.originalfactorlist.append(factor)
56 self.factorlist = copy.deepcopy(self.originalfactorlist)
57 '''A working copy of *originalfactorlist*.'''
58
59 assert self.factorlist, "Factor list not properly loaded, check for an incomplete class instance as input."
60
61 def refresh(self):
62 ''&#x27;
63 Refresh the *factorlist* attribute to equate with *originalfactorlist*. This is in effect a reset of the system, erasing any changes to *factorlist* that the program has executed.
64
65 ''&#x27;
66 self.factorlist = copy.deepcopy(self.originalfactorlist)
67
68 def sumproducteliminatevar(self, vertex):
69 ''&#x27;
70 Multiply the all the factors in *factorlist* that have *vertex* in their scope, then sum out *vertex* from the resulting product factor. Replace all factors that were multiplied together with the resulting summed-out product.
71
72 Arguments:
73 1. *vertex* - The name of the variable to eliminate.
74
75 Attributes modified:
76 1. *factorlist* -- Modified to reflect the eliminated variable.
77
78 For more information on this algorithm cf. Koller et al. 298
79
80 ''&#x27;
81 factors2 = []
82 factors1 = []
83 for factor in self.factorlist:
84 try:
85 factor.scope.index(vertex)
86 factors1.append(factor)
87 except ValueError:
88 factors2.append(factor)
89
90 # multiply factors1 array together
91 for i in range(1, len(factors1)):
92 factors1[0].multiplyfactor(factors1[i])

Callers 4

specificqueryMethod · 0.90
setUpMethod · 0.90
setUpMethod · 0.90
examples.pyFile · 0.90

Calls

no outgoing calls

Tested by 2

setUpMethod · 0.72
setUpMethod · 0.72