Created
December 9, 2014 13:57
-
-
Save xccds/0129fed50b578a9e4cad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import Counter | |
class Tank(): | |
def __init__(self,value): | |
self.tab = dict(Counter(value)) | |
def Values(self): | |
return self.tab.keys() | |
def Mult(self, x, factor): | |
self.tab[x] = self.tab.get(x, 0) * factor | |
def Likelihood(self, data, hypo): | |
if hypo < data: | |
return 0 | |
else: | |
return 1.0/hypo | |
def Normalize(self, fraction=1.0): | |
total = sum(self.tab.itervalues()) | |
factor = float(fraction) / total | |
for x in self.tab: | |
self.tab[x] *= factor | |
return sum(self.tab) | |
def Update(self, data): | |
for hypo in self.Values(): | |
like = self.Likelihood(data, hypo) | |
self.Mult(hypo, like) | |
return self.Normalize() | |
def Print(self): | |
for val, prob in sorted(self.tab.iteritems()): | |
print val, prob | |
def main(): | |
suite = Tank([500, 1000, 1500, 2000, 3000]) | |
for roll in [4, 180, 75, 1007, 1003, 1500]: | |
suite.Update(roll) | |
print 'After more obs' | |
suite.Print() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment