rethinkpriorities / squigglepy

Squiggle programming language for intuitive probabilistic estimation features in Python
MIT License
65 stars 8 forks source link

Rule of 72 #8

Closed erwald closed 1 year ago

erwald commented 1 year ago

Now and again I find myself wanting to convert from doubling time to growth rate and back. I normally Rule of 72 this, but that gets inaccurate starting around growth rates ≥15%.

The formula for calculating this properly is simple but kinda annoying to remember and write repeatedly:

import math

def growth_rate_to_doubling_time(growth_rate):
    return math.log(2) / math.log(1 + growth_rate)

def doubling_time_to_growth_rate(doubling_time):
    return math.exp(math.log(2) / doubling_time) - 1

print(growth_rate_to_doubling_time(0.1)) # returns 7.27 -- 72/10 = 7.2
print(growth_rate_to_doubling_time(0.04)) # returns 17.67 -- 72/5 = 18
print(doubling_time_to_growth_rate(10)) # returns 0.072 -- 72/10 = 7.2%
print(doubling_time_to_growth_rate(3)) # returns 0.260 -- 72/10 = 24.0%

More problematically, I can't plug squigglepy distributions into these functions, since math.log/math.exp (and np.log/np.exp) don't work on distributions.

Tbh I'm not sure if this is a good fit for squigglpy as it has little to do with probabilities etc. (It's a better fit if you see squigglepy more as a general BOTEC/modelling framework.) But figured I'd raise it and let you decide.

peterhurford commented 1 year ago

Seems totally reasonable to put this in utils. There are a bunch of other things there. I think being a general BOTEC/modeling framework sounds good to me.

Want to implement this?

erwald commented 1 year ago

Yeah sure, I should get a PR up later this week.