Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def VE(Net, QueryVar, EvidenceVars):
- '''
- Input: Net---a BN object (a Bayes Net)
- QueryVar---a Variable object (the variable whose distribution
- we want to compute)
- EvidenceVars---a LIST of Variable objects. Each of these
- variables has had its evidence set to a particular
- value from its domain using set_evidence.
- VE returns a distribution over the values of QueryVar, i.e., a list
- of numbers one for every value in QueryVar's domain. These numbers
- sum to one, and the i'th number is the probability that QueryVar is
- equal to its i'th value given the setting of the evidence
- variables. For example if QueryVar = A with Dom[A] = ['a', 'b',
- 'c'], EvidenceVars = [B, C], and we have previously called
- B.set_evidence(1) and C.set_evidence('c'), then VE would return a
- list of three numbers. E.g. [0.5, 0.24, 0.26]. These numbers would
- mean that Pr(A='a'|B=1, C='c') = 0.5 Pr(A='b'|B=1, C='c') = 0.24
- Pr(A='c'|B=1, C='c') = 0.26
- '''
- F = Net.factors()
- Z = list(set(min_fill_ordering(Net.factors(),QueryVar)).difference(set(EvidenceVars)))
- #print(EvidenceVars)
- #print(Z)
- dist = []
- for i in range(len(F)):
- for E in EvidenceVars:
- if E in F[i].scope:
- F[i]=restrict_factor(F[i], E, E.get_evidence())
- for v in Z:
- MPL=[]
- for f in F:
- if v in f.scope:
- MPL.append(f)
- MPL = list(set(MPL))
- for f in MPL:
- F.remove(f)
- MF = multiply_factors(MPL)
- #MF.recursive_print_values(MF.scope)
- F.append(sum_out_variable(MF,v))
- #print(F)
- #print(F[-1].values)
- Q = multiply_factors(F)
- Q_perms = rp(Q,Q.scope)
- #Q.recursive_print_values(Q.scope)
- vaiid = []
- x = sum(Q.values)
- for perm in Q_perms:
- perm.append(Q.get_value(perm)/x)
- for d in QueryVar.dom:
- for perm in Q_perms:
- if perm[Q.scope.index(QueryVar)] == d:
- dist.append(perm[-1])
- return dist
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement