Likelihood Weighting
- LikelihoodWeighting.pyx
cdef extern from "dbrv.h": char dbrv__setup(DBRV *self, char *connectionString); char dbrv__logP(DBRV *self, char *nodeName, PyObject *parentValues, double *prob); char dbrv__randomSample(DBRV *self, char *nodeName, PyObject *parentValues, unsigned int *sample); char dbrv__tearDown(DBRV *self); cdef class NodeSample: cdef node cdef int stateIndex cdef list states cdef list isSet cdef list parents cdef class LikelihoodWeighting: cdef list __orderedNodes cdef size_t __numSample def __init__(self, orderedNodes, numSamples): self.__orderedNodes = orderedNodes self.__numSamples = numSamples cdef __randomSample(self, NodeSample nodeSample): print("Call to LikelihoodWeighting.__likelihoodWeighting()") cdef double __lwSample(self, NodeSample nodeSample, ev, sampleIndex): cdef node node = nodeSample.node # Check if already set if nodeSample.isSet[sampleIndex]: return 0 # Check if evidence if node in ev: # TODO Working with state indexes here. Is that right? nodeSample.stateIndex = ev[node] nodeSample.isSet[sampleIndex] = True # Calculate log[P(x|y,z)] # Remember that frequencies are listed frequency + 1 # ***** return 0.5 # Sample ancestors before sampling present node for parent in bnodeSample.parents: self.__lwSample(parent, ev, sampleIndex) self.__randomSample(nodeSample) return 0 cdef __likelihoodWeighting(self, evidence): for nodeSample in self.__orderedNodes: for i from 0 <= i < self.__numSamples: self.__lwSample(nodeSample, evidence, i)
You could leave a comment if you were logged in.