The POTENTIAL class represents the potential attached to a clique. Unlike the CPD class, the PO- TENTIAL class has methods allowing arithmetical operation to be performed between POTENTIAL objects.
6.6.1 Attributes
1 A clique over which the potential is dened, the domain of the potential. 2 Every node in the potential's domain has a size.
3 A multi-dimensional array holding the values of the potential.
We store the domain as a sorted list domain, and the potential values array as as a multi-dimensional array T, with the number dimensions equal to the length of the domain list. We then associate each dimension of the array with the node it indexes in the the ordered list. For instance, the rst domain of the table T is associated with the node in rst position in the domain list. As an example, consider the potential shown in Table 2.4, and its sorted domain list {x2, x3}. The rst dimension,
the rows of Table 2.4, corresponds to the dierent values the rst node in the domain list, the node x2, can realize. The second dimension, the columns of Table 2.4, corresponds to the dierent values
the second node in the domain list, the node x3, can realize.
We also store the sizes of the nodes that are in the domain of the potential, in the list attribute node_sizes. Therefore, node_sizes[i] is the size of the node stored in domain[i]. Thus, if a potential has domain= [x1, x3, x4]and node_sizes = [3, 7, 2], then the node x3 has the size 7, and the node x4
has the size 2.
6.6.2 Methods
The following paragraphs detail each of the methods within the POTENTIAL class. POTENTIAL.init: The initialization method for the class.
To initialize a POTENTIAL object we need the following parameters:
1 list: domain. A list of values indexing the nodes in this potentials domain.
2 list: sizes. A list of integers expressing the size of each node in the domain of this potential. If size[i]=k, and domain[i]=j, then the node xj can take on k possible values.
3 array: T. A multi-dimensional array of oats representing the values of this potential. If it is not specied, we initialize it to the required dimensions and ll it with 1's.
The algorithm for the POTENTIAL init method is shown below.
POTENTIAL. i n i t ( domain , s i z e s , T)
STEP 1 : Save the r e q u i r e d input parameters as a t t r i b u t e s . s e l f . domain <− domain
s e l f . s i z e s <− s i z e s
STEP 2 : Determine whether the parameter T has been
s p e c i f i e d . I f i t has been s p e c i f i e d then save i t as an a t t r i b u t e , and i f i t has not then c r e a t e a t a b l e with the dimensions s p e c i f i e d in the parameter sizes
and save i t as an a t t r i b u t e . i f T has been s p e c i f i e d
s e l f .T <− T e l s e
s e l f .T <− An array of 1 ' s with the dimensions s p e c i f i e d in the a t t r i b u t e self.sizes . For i n s t a n c e i f self.sizes = [ 2 , 7 ] , then self.T w i l l be a two
dimensional t a b l e with 2 rows and 7 columns , and a l l e n t r i e s w i l l be 1 .
end
POTENTIAL.arithmetic: This method implements the adding/subtracting/dividing/multiply- ing of potentials together.
To perform arithmetic on two POTENTIAL objects we need the following parameters:
1 POTENTIAL: pot. The other potential object which we are arithmetically combining with this one. The domain of pot must be a subset of the domain of this potential.
2 char: op. A character indicating which arithmetic operation to perform. The options are: '+' for addition, '-' for subtraction, '*' for multiplication and '/' for division. The default is '+'. The algorithm for the POTENTIAL arithmetic method is shown below.
POTENTIAL. a r i t h m e t i c ( pot , op='+')
STEP 1 : Re p l i c a t e the dimensions of the input p o t e n t i a l ' s t a b l e in such away that i t s domain matches that of t h i s p o t e n t i a l .
pos <− A l i s t of the i n d i c e s in self.domain where the e n t r i e s in pot.domain appear . For instance , i f
self.domain = [ 1 , 4 , 5 ] , and pot.domain = [ 1 , 5 ] , then pos = [ 0 , 2 ] .
sz <− A l i s t of ones with the same length as self.sizes .
sz <− S u b s t i t u t e the values in sz indexed by the values in pos with the values in the l i s t pot.sizes . For example ,
i f sz = [ 1 , 1 , 1 , 1 ] , and pos = [ 0 , 2 ] , and pot.sizes = [ 4 , 3 ] then sz w i l l become
sz = [ 4 , 1 , 3 , 1 ] .
Ts <− The t a b l e pot.T reshaped to the dimensions s p e c i f i e d in sz .
N <− The length of the l i s t self.sizes f o r i <− 0 to N−1
i f i i s not in pos
Ts <− R e p l ic a t e the i ' th dimension of Ts the number of times s p e c i f i e d in self.sizes[i] . So i f i =3, and
self.sizes[3]=4 then r e p l i c a t e the 3 rd dimension of Ts 4 times .
end end
STEP 2 : Now that the input p o t e n t i a l has the same domain and s i z e as t h i s p o t e n t i a l , determine which mathematical operation to perform and execute i t .
i f op == '+ ' s e l f .T <− s e l f .T + Ts e l s e i f op == ' −' s e l f .T <− s e l f .T − Ts e l s e i f op == ' ∗ ' s e l f .T <− s e l f .T ∗ Ts e l s e i f op == '/ ' s e l f .T <− s e l f .T / Ts end
POTENTIAL.enter_evidence: This method introduces evidence into a potential.
To modify a POTENTIAL object to reect observed evidence we need the following parameter: 1 list: evidence. A list of observed evidence with the length equal to the number of nodes in
the POTENTIALS domain. The value of evidence[i] is either None, to represent that the node xP OT EN T IAL.domain[i] is unobserved, or an integer value k to represent the observation
xP OT EN T IAL.domain[i] = k.
The algorithm for the POTENTIAL enter_evidence method is shown below.
POTENTIAL. enter_evidence ( evidence )
appropriate s l i c e s of i t s dimensions . N <− The length of the l i s t evidence .
f o r i <− 0 to N−1
i f evidence[i] i s not None
s e l f .T <− Replace dimension i in self.T with the s l i c e indexed by evidence[i] in dimension i . For example i f i =2, and evidence[i] = 5 , and self.T has 3 dimensions , then
self.T <− self.T [ : , 5 , : ] . end
end
STEP 2 : Update the l i s t of node s i z e s to r e f l e c t that the nodes which have been observed have s i z e 1 .
odom_ndx <− A l i s t of the i n d i c e s of a l l the nodes in evidence which are not None . For example
i f evidence = [ None , 3 , 5 , None ] , then odom_ndx = [ 1 , 2 ] .
s e l f . s i z e s <− Replace every entry indexed by the l i s t odom_ndx with the value 1 . For instance ,
i f self.sizes = [ 2 , 4 , 7 , 3 ] , and
odom_ndx = [ 1 , 2 ] , then self.sizes would become [ 2 , 1 , 1 , 3 ] .
POTENTIAL.marginalize_pot: Marginalizes a potential over a domain. To marginalize a POTENTIAL object we need the following parameters:
1 list: onto A list of integers indexing the marginal nodes. This list is therefore the domain of the marginalized return potential. This list of integers must be a subset of this potential's current domain.
2 boolean: maximize. This value is FALSE if we wish to marginalize this potential onto the domain specied in the parameter onto, and is TRUE if we wish to maximize this potential onto the domain specied in the parameter onto. The default value is FALSE.
This method returns the following values:
1 POTENTIAL: small_pot. The potential after marginalization or maximization. This return potential has the domain specied in the parameter onto.
2 key indexed list: maximizers. This structure is used to determine the backtracking information in the max-sum algorithm. It has the structure
1 maximizer[key] = [dependants, argmax]. 2 key: Is the index of a node.
3 dependants: Is a list of nodes that gave rise to the maximum value of the node xkey.
4 argmax: The values of the nodes indexed in the list dependants which gave rise to the maximum value of the node xkey.
The algorithm for the POTENTIAL marginalize_pot method is shown below. POTENTIAL. marginalize_pot ( onto , maximize=False )
STEP 1 : Determine the v a r i a b l e nodes within the p o t e n t i a l that must be marginalized out .
sum_over <− The s e t d i f f e r e n c e between the l i s t self.domain and the parameter l i s t onto . Therefore , t h i s
l i s t indexes the nodes which need to be marginalized /maximized out of the p o t e n t i a l . STEP 2 : Make a copy of t h i s p o t e n t i a l s t a b l e to m ar g i n a l iz e
or maximize f o r the new p o t e n t i a l . small_T <− A copy of self.T .
STEP 3 : I f we are to sum the p o t e n t i a l , jump to STEP
3 . 1 , and i f we wish to maximize , jump to STEP 3 . 2 . i f maximize i s FALSE
STEP 3 . 1 : Sum every dimension in small_T that corresponds to a node indexed in sum_over .
N <− The length of the l i s t sum_over . f o r i <− 0 to N−1
pos <− The p o s i t i o n of the node sum_over[i] in the self.domain l i s t , which corresponds to the dimension in the array self.T that we need to m ar g i n a li z e over . I f sum_over[i] = 2 , and self.domain = [ 0 , 2 , 3 ] , pos = 1 .
small_T <− Sum the dimension indexed pos in small_T . This w i l l decrease the number of
dimensions in small_T by 1 . For example , i f pos =2, sum over the second dimension of small_T .
end e l s e
STEP 3 . 2 : Maximize over every dimension in small_T that corresponds to a node indexed in sum_over . While maximizing , s t o r e the values of the unobserved nodes indexed in the l i s t sum_over which give r i s e to the maximum values of the domain made up of the nodes indexed in the l i s t onto .
dependants <− The nodes in self.domain which are not observed .
N <− The length of the l i s t sum_over . f o r i <− 0 to N−1
i f sum_over[i] i s in dependants
Remove sum_over[i] from the list `dependants . end
pos <− Find the p o s i t i o n of the node sum_over[i] in the self.domain l i s t . This p o s i t i o n corresponds to the dimension in the array small_T that we need to maximize over .
argmax <− A l i s t of the i n d i c e s of the maximum values in the dimension indexed by pos in the
t a b l e small_T . For example , i f pos =2, f i n d the i n d i c e s of the maximum values in the second dimension of small_T .
maximizers [ sum_over [ i ] ] <− [ dependants , argmax ] small_T <− Maximize the dimension indexed pos in
small_T . This w i l l decrease the number of dimensions in small_T by 1 .
end end
onto . For example , i f ns[i]=2 then
the node indexed by onto[i] has the s i z e 2 .
STEP 4 : Create the new p o t e n t i a l obtained by m a r g i n a l i z i n g or maximizing t h i s p o t e n t i a l and return i t as output . small_pot <− POTENTIALS. i n i t ( onto , ns , small_T )
return [ small_pot , maximizers ]