Advertisement
user_137

Class Bayesian

Oct 12th, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. class Bayesian:
  2.     # Baysian = { H: [pH, pD|H, pHpD|H, pH|D]}
  3.     def __init__(self, bayDict, n=3):
  4.         self.dic = bayDict
  5.         self.n = n
  6.     def calcPosterior(self):
  7.         d = self.dic
  8.         normalize = 0
  9.         for k in d.keys():
  10.             d[k][2] = d[k][0] * d[k][1]
  11.             normalize += d[k][2]
  12.         for k in d.keys():
  13.             d[k][3] = d[k][2] / normalize
  14.     def __str__(self):
  15.         pr = self.n
  16.         return '{'+''.join([str(k)+': %0.*f, %.*f, %.*f, %.*f\n ' %
  17.                       (pr, v[0], pr, v[1], pr, v[2], pr, v[3])
  18.                       for k,v in sorted(self.dic.items())])[:-2]+'}'
  19.  
  20. # Antibiotic on Bacteria
  21. # Exercise: Bayes Theorum and Total Probability
  22. # print('this is what it looks like before you run the class on it\n\n', positive, '\n\n -----\n')
  23.  
  24. positive = Bayesian({'a': [.999, .01, 0, 0], 'b': [.001, .99, 0, 0]}, 8)
  25. positive.calcPosterior()
  26. print("Bayesian = { H: [pH, pP|H, pH*pP|H, pH|P] }")
  27. print(positive)
  28. print("Bayesian = { S: [pS, pP|S, pS*pP|S, pS|P] }", '\n\n')
  29.  
  30. negative = Bayesian({'a': [.999, .99, 0, 0], 'b': [.001, .01, 0, 0]}, 8)
  31. negative.calcPosterior()
  32. print("Bayesian = { H: [pH, pN|H, pH*pN|H, pH|N] }")
  33. print(negative)
  34. print("Bayesian = { S: [pS, pN|S, pS*pN|S, pS|N] }", '\n\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement