From 5ddf96bc33666d4e32d4b7d3688ae63ee033b3bc Mon Sep 17 00:00:00 2001 From: Olivia Appleton Date: Thu, 3 Nov 2022 18:34:25 -0400 Subject: [PATCH] Port more of my math methods --- pymanifold/utils/math.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/pymanifold/utils/math.py b/pymanifold/utils/math.py index 4203c07..1556daf 100644 --- a/pymanifold/utils/math.py +++ b/pymanifold/utils/math.py @@ -10,3 +10,29 @@ def number_to_prob_cpmm1(current: float, start: float, end: float, isLogScale: b if isLogScale: return log10(current - start + 1) / log10(end - start + 1) return (current - start) / (end - start) + + +def pool_to_prob_cpmm1(yes: float, no: float, p: float) -> float: + """Go from a pool of YES/NO to a probability using Maniswap.""" + if yes <= 0 or no <= 0 or not (0 < p < 1): + raise ValueError() + pno = p * no + return pno / ((1 - p) * yes + pno) + + +def pool_to_number_cpmm1(yes: float, no: float, p: float, start: float, end: float, isLogScale: bool = False) -> float: + """Go from a pool of probability to a numeric answer.""" + if start >= end: + raise ValueError() + probability = pool_to_prob_cpmm1(yes, no, p) + return prob_to_number_cpmm1(probability, start, end, isLogScale) + + +def prob_to_number_cpmm1(probability: float, start: float, end: float, isLogScale: bool = False) -> float: + """Go from a probability to a numeric answer.""" + if isLogScale: + ret: float = (end - start + 1)**probability + start - 1 + else: + ret = start + (end - start) * probability + ret = max(start, min(end, ret)) + return ret