Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Bayesian:
- # Baysian = { H: [pH, pD|H, pHpD|H, pH|D]}
- def __init__(self, bayDict, n=3):
- self.dic = bayDict
- self.n = n
- def calcPosterior(self):
- d = self.dic
- normalize = 0
- for k in d.keys():
- d[k][2] = d[k][0] * d[k][1]
- normalize += d[k][2]
- for k in d.keys():
- d[k][3] = d[k][2] / normalize
- def __str__(self):
- pr = self.n
- return '{'+''.join([str(k)+': %0.*f, %.*f, %.*f, %.*f\n ' %
- (pr, v[0], pr, v[1], pr, v[2], pr, v[3])
- for k,v in sorted(self.dic.items())])[:-2]+'}'
- # Antibiotic on Bacteria
- # Exercise: Bayes Theorum and Total Probability
- # print('this is what it looks like before you run the class on it\n\n', positive, '\n\n -----\n')
- positive = Bayesian({'a': [.999, .01, 0, 0], 'b': [.001, .99, 0, 0]}, 8)
- positive.calcPosterior()
- print("Bayesian = { H: [pH, pP|H, pH*pP|H, pH|P] }")
- print(positive)
- print("Bayesian = { S: [pS, pP|S, pS*pP|S, pS|P] }", '\n\n')
- negative = Bayesian({'a': [.999, .99, 0, 0], 'b': [.001, .01, 0, 0]}, 8)
- negative.calcPosterior()
- print("Bayesian = { H: [pH, pN|H, pH*pN|H, pH|N] }")
- print(negative)
- print("Bayesian = { S: [pS, pN|S, pS*pN|S, pS|N] }", '\n\n')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement