Exemplo n.º 1
0
        def sampleGivenMB(self, v, state):
            MBval = Table(v.name, shape=v.nvalues)
            children = v.out_v
            index = {}
            for vert in v.family:       # replaced [v]+list(v.in_v)
                    index[vert.name] = state[vert.name]
            # index = {var.name:state}    for var in v.family
            
            childrenAndIndex = []
            for child in children:
                cindex = {}
                # family = a node and all its parents
                for cvert in child.family: # replaced [child]+list(child.in_v)
                    # cvert is either a child or an uncle(parent of child) of v
                    # cindex contains the state of all variables in the family
                    # of a child of v
                    cindex[cvert.name] = state[cvert.name]       
                childrenAndIndex.append((child,cindex))

            #OPTIMIZE: could vectorize this code
            for value in range(v.nvalues):
                index[v.name] = value
                # initialise each element of the distribution with the
                # conditional probability table values of the variable
                # Pr(v=i)=Pr(v=i|Pa(v)=index)
                # index is randomly selected at each iteration
                MBval[value] = v.distribution[index]
                ##################################################
                # this could be replaced by Table multiplication instead
                # of an element-wise multiplication
                # in that case we don't need all those index dictionnaries
                ##################################################
                for child,cindex in childrenAndIndex:
                    cindex[v.name] = value
                    MBval[value] *= child.distribution[cindex]

            MBval.normalize()

            #######################################
            # added a sample() function in Distribution
            #######################################
            return MBval.sample()
Exemplo n.º 2
0
        def Marginalise(self, v, N):
            """ Compute the Pr(v) where v is a variable name,
            N is the number of iterations of MCMC to perform.
            """
            # the return distribution
            ###########################################################
            #vDist = RawCPT(v, (self.BNet.v[v].nvalues,1))
            # vDist should be a potential instance. What do you think?
            ###########################################################
            vDist = Table(v, shape=self.BNet.v[v].nvalues)
            nonEvidence = []
            # find out which values are not specified in evidence
            for vv in self.BNet.v.values():
                if not self.evidence.has_key(vv.name): nonEvidence.append(vv.name)

            # state is first selected at random            
            state = copy.copy(self.evidence)
            for vname in nonEvidence:
                # CHECK: legal values are 0 - nvalues-1: checked OK
                state[vname] = random.randint(0, self.BNet.v[vname].nvalues-1)

            for i in range(N):
                if i > self.cut:
                        #########################################
                        # What is this line for ????
                        # shouldn't we stop iteration here?
                        # what is cut for?
                        ##################################
                        vDist[state[v]] += 1
                for vname in nonEvidence:
                        state[vname] = self.sampleGivenMB(self.BNet.v[vname], state)
                        ################################################
                        # added this line: did you forget it, or i didn't understand
                        # anything in your code
                        #################################################
                        vDist[state[v]] += 1

            # added a normalize() function in Table
            vDist.normalize()
            return vDist