Skip to content

Instantly share code, notes, and snippets.

@traverseda
Last active September 4, 2017 23:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save traverseda/2c3056aede8b01a9d40c11d6b916774f to your computer and use it in GitHub Desktop.
Save traverseda/2c3056aede8b01a9d40c11d6b916774f to your computer and use it in GitHub Desktop.
from sympy import S
from wrapt import ObjectProxy
"""
A class for quickly doing math on percentages,
using bayes theorum.
"""
one = S(1)
class Probability(ObjectProxy):
"""
A percentage object, you can use it to properly
add or subtract percentage from one another.
When used with a regular number, acts like
a regular number.
"""
def __init__(self,wrapped):
super().__init__(S(wrapped))
def __add__(self,other):
if isinstance(other, Probability):
return Probability(self+(one-self)*other)
return super().__add__(other)
def __sub__(self,other):
if isinstance(other, Probability):
return Probability(self-self*other)
return super().__sub__(other)
p = Probability
print(p(0.9)+p(0.2)+p(0.5))
print(p(0.9)+p("1/3"))
print(p(0.9)-p("1/3"))
print(p("1/3")+p("1/3")+p("1/3")+p("1/3")+p("1/3"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment