Advertisement
Guest User

Untitled

a guest
Apr 1st, 2015
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.12 KB | None | 0 0
  1. def VE(Net, QueryVar, EvidenceVars):
  2. '''
  3. Input: Net---a BN object (a Bayes Net)
  4. QueryVar---a Variable object (the variable whose distribution
  5. we want to compute)
  6. EvidenceVars---a LIST of Variable objects. Each of these
  7. variables has had its evidence set to a particular
  8. value from its domain using set_evidence.
  9.  
  10. VE returns a distribution over the values of QueryVar, i.e., a list
  11. of numbers one for every value in QueryVar's domain. These numbers
  12. sum to one, and the i'th number is the probability that QueryVar is
  13. equal to its i'th value given the setting of the evidence
  14. variables. For example if QueryVar = A with Dom[A] = ['a', 'b',
  15. 'c'], EvidenceVars = [B, C], and we have previously called
  16. B.set_evidence(1) and C.set_evidence('c'), then VE would return a
  17. list of three numbers. E.g. [0.5, 0.24, 0.26]. These numbers would
  18. mean that Pr(A='a'|B=1, C='c') = 0.5 Pr(A='b'|B=1, C='c') = 0.24
  19. Pr(A='c'|B=1, C='c') = 0.26
  20.  
  21. '''
  22. F = Net.factors()
  23. Z = list(set(min_fill_ordering(Net.factors(),QueryVar)).difference(set(EvidenceVars)))
  24. #print(EvidenceVars)
  25. #print(Z)
  26. dist = []
  27. for i in range(len(F)):
  28. for E in EvidenceVars:
  29. if E in F[i].scope:
  30. F[i]=restrict_factor(F[i], E, E.get_evidence())
  31. for v in Z:
  32. MPL=[]
  33. for f in F:
  34. if v in f.scope:
  35. MPL.append(f)
  36. MPL = list(set(MPL))
  37. for f in MPL:
  38. F.remove(f)
  39. MF = multiply_factors(MPL)
  40. #MF.recursive_print_values(MF.scope)
  41. F.append(sum_out_variable(MF,v))
  42. #print(F)
  43. #print(F[-1].values)
  44. Q = multiply_factors(F)
  45. Q_perms = rp(Q,Q.scope)
  46. #Q.recursive_print_values(Q.scope)
  47. vaiid = []
  48. x = sum(Q.values)
  49.  
  50. for perm in Q_perms:
  51. perm.append(Q.get_value(perm)/x)
  52.  
  53. for d in QueryVar.dom:
  54. for perm in Q_perms:
  55. if perm[Q.scope.index(QueryVar)] == d:
  56. dist.append(perm[-1])
  57. return dist
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement