Last active
September 4, 2017 23:10
-
-
Save traverseda/2c3056aede8b01a9d40c11d6b916774f 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 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