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

Method reducefactor

libpgm/tablecpdfactor.py:214–254  ·  view source on GitHub ↗

Reduce the factor knowing that *vertex* equals *value*. Reducing the factor means erasing all possibilities for *vertex* other than *value* from the distribution, and removing *vertex* from the scope. Arguments: 1. *vertex* -- The UUID of the variable whose outc

(self, vertex, value)

Source from the content-addressed store, hash-verified

212 del(self.stride[vertex])
213
214 def reducefactor(self, vertex, value):
215 '''
216 Reduce the factor knowing that *vertex* equals *value*. Reducing the factor means erasing all possibilities for *vertex* other than *value* from the distribution, and removing *vertex* from the scope.
217
218 Arguments:
219 1. *vertex* -- The UUID of the variable whose outcome is known.
220 2. *value* -- The known outcome of that variable.
221
222 Attributes modified:
223 *vals*, *scope*, *stride*, *card* -- Modified to reflect the data of the reduced factor.
224
225 '''
226 vscope = self.scope.index(vertex)
227 vstride = self.stride[vertex]
228 vcard = self.card[vscope]
229 result = [0 for i in range(len(self.vals)/self.card[vscope])]
230
231 # added step: find value index from evidence
232 try:
233 index = self.inputbn.Vdata[vertex]['vals'].index(value)
234 except:
235 raise Exception, "Second arg was not a possible value of first arg."
236
237 # machinery that calculates values in summed out factor
238 k = 0
239 lcardproduct = 1
240 for i in range(vscope):
241 lcardproduct *= self.card[i]
242 for i in range(len(result)):
243 result[i] += self.vals[k + (vstride * index)]
244 k += 1
245 if (k % lcardproduct == 0):
246 k += (lcardproduct * (vcard - 1))
247 self.vals = result
248
249 # modify scope, card, and stride in new factor
250 self.scope.remove(vertex)
251 del(self.card[vscope])
252 for i in range(vscope, len(self.stride)-1):
253 self.stride[self.scope[i]] /= vcard
254 del(self.stride[vertex])
255
256 def copy(self):
257 '''Return a copy of the factor.'''

Callers 4

condprobveMethod · 0.80
gibbssampleMethod · 0.80
nextMethod · 0.80
test_reducefactorMethod · 0.80

Calls

no outgoing calls

Tested by 1

test_reducefactorMethod · 0.64