示例#1
0
 def setChannel(self,distribution):
     cases = []
     pvalues = []
     for (names,pvalue) in distribution:
         cases.append(self.name+"_"+names[0])
         pvalues.append(pvalue)
     domain = efp.Dom(cases,names = efp.Name(self.name))
     self.channel = efp.State(pvalues,domain).as_chan()
     self.node_size = len(self.channel.cod[0])
示例#2
0
 def setChannel(self,cpt):
     codomainElements = OrderedSet([names[-1] for (names,_) in cpt])
     NumCodElems = len(codomainElements)
     codomain = efp.Dom([self.name+"_"+elem for elem in codomainElements],names = efp.Name(self.name))
     doms = []
     steps = 1
     for (i,pn) in enumerate(self.parents):
         domainElements = OrderedSet([names[i] for (names,_) in cpt])
         doms.append(efp.Dom([pn.name+"_"+elem for elem in domainElements],names = efp.Name(pn.name)))
         steps *= len(domainElements)
     dom = functools.reduce(lambda d1, d2: d1 + d2, doms)
     pvalues = [row for (_,row) in cpt]
     matrix = [pvalues[n:n+NumCodElems] for n in range(0,len(pvalues),NumCodElems)]
     states = [efp.State(matrix[j], codomain) for j in range(steps)]
     self.channel = efp.chan_from_states(states, dom)
     self.node_size = len(self.channel.cod[0])
示例#3
0
 def run(self, state=ep.State(1, [])):
     s = self.chan >> state
     s = s.partial_conditional(ep.yes_pred @ truth(self.cod))
     return s % ([0] + [1] * len(self.cod))
示例#4
0
 def run(self, state=ep.State(1, [])):
     return (self.chan >> state).normalize()
示例#5
0
    prob_choice(0.4, newassign(lambda: 0, range(2), []),
                newassign(lambda: 1, range(2), [])),
    observe(lambda x: x == 0, [range(2)])).run()

test2 = do(
    prob_choice(
        0.4,
        do(newassign(lambda: 0, range(2), []),
           observe(lambda x: x == 0, [range(2)])),
        do(newassign(lambda: 1, range(2), []),
           observe(lambda x: x == 0, [range(2)])))).run()

disease_dom = ['D', '~D']
mood_dom = ['M', '~M']
bool_dom = [True, False]
inital_state = ep.State([0.05, 0.5, 0.4, 0.05], [disease_dom, mood_dom])

test3 = do(
    ifthenelse(
        lambda d, m: d == 'D',
        newsample(lambda *_: ep.flip(9 / 10), bool_dom,
                  [disease_dom, mood_dom]),
        newsample(lambda *_: ep.flip(1 / 20), bool_dom,
                  [disease_dom, mood_dom])),
    observe(lambda d, m, t: t == True, [disease_dom, mood_dom, bool_dom]),
    discard(0, [disease_dom, mood_dom, bool_dom]),
    discard(1, [mood_dom, bool_dom])).run(inital_state)

# Use prob_choice and assign instead