Skip to content

Instantly share code, notes, and snippets.

@lorne-luo
Created April 1, 2019 02:12
Show Gist options
  • Save lorne-luo/42bc78caf05242f3c9c89d62efbee675 to your computer and use it in GitHub Desktop.
Save lorne-luo/42bc78caf05242f3c9c89d62efbee675 to your computer and use it in GitHub Desktop.
Germen Tank Problem
from collections import Counter
class Tank():
def __init__(self, value):
self.tab = dict(Counter(value))
def mult(self, x, factor):
self.tab[x] = self.tab[x] * factor
def likelihood(self, observe, hypo):
if hypo < observe:
return 0
else:
return 1.0 / hypo
def normalize(self, fraction=1.0):
total = sum(self.tab.values())
factor = float(fraction) / total
for x in self.tab:
self.tab[x] *= factor
return sum(self.tab)
def update(self, observe):
for hypo in self.tab.keys():
like = self.likelihood(observe, hypo)
self.mult(hypo, like)
return self.normalize()
def print(self):
for val, prob in sorted(self.tab.items()):
print(val, prob)
def confidence(self, target=99):
target = 1 - 99 / 100.0
sumup = 0
for key in reversed(sorted(self.tab.keys())):
sumup += self.tab[key]
if sumup > target:
print('confidence = %s' % key)
break
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()
# suite.confidence()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment