Rule of 72 – Helpful code
Ever heard of the Rule of 72? It’s a vintage finance shortcut that tells you what number of years it takes for an funding to double at a given rate of interest—with out achieving for a calculator! Just about, if you wish to perceive when you’re going to double your cash, which are rising with 7% consistent with 12 months, then merely divide 72 by way of 7 and spot the approximate solution. It really works like that and it’s roughly adequate, for values between 5 and 10%.
For all different values, the formulation looks as if this:
With Python the formulation looks as if this:
|
import math
def exact_doubling_time(rate_percent): rate_decimal = rate_percent / 100.0 go back math.log(2)/math.log(1+rate_decimal) |
If you wish to see how actual the formulation is, then a excellent comparability vs the precise worth looks as if this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
def rule_of_72(rate_percent): go back 72 / rate_percent
def rule_of_70(rate_percent): go back 70 / rate_percent
def rule_of_69(rate_percent): go back 69 / rate_percent
rate_ex = 8 approx_72 = rule_of_72(rate_ex) approx_70 = rule_of_70(rate_ex) approx_69 = rule_of_69(rate_ex) exact_val = exact_doubling_time(rate_ex)
print(f“Passion Fee: {rate_ex}%”) print(f“Rule of 72: {approx_72:.2f} years”) print(f“Rule of 70: {approx_70:.2f} years”) print(f“Rule of 69: {approx_69:.2f} years”) print(f“Precise: {exact_val:.3f} years”)
for r_percent in [1, 2, 5, 8, 10, 20]: exact_t = exact_doubling_time(r_percent) t72 = rule_of_72(r_percent) print(f“Fee: {r_percent}%n” f” Precise = {exact_t:.3f} | Rule 72 = {t72:.3f} | Error = {100*(t72 – exact_t)/exact_t:.2f}%n”) |
The execution of the code from above like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
Passion Fee: 8% Rule of 72: 9.00 years Rule of 70: 8.75 years Rule of 69: 8.62 years Precise: 9.006 years
Fee: 1% Precise = 69.661 | Rule 72 = 72.000 | Error = 3.36%
Fee: 2% Precise = 35.003 | Rule 72 = 36.000 | Error = 2.85%
Fee: 5% Precise = 14.207 | Rule 72 = 14.400 | Error = 1.36%
Fee: 8% Precise = 9.006 | Rule 72 = 9.000 | Error = –0.07%
Fee: 10% Precise = 7.273 | Rule 72 = 7.200 | Error = –1.00%
Fee: 20% Precise = 3.802 | Rule 72 = 3.600 | Error = –5.31% |
The YT video, explaining the code and is right here:
The GitHub code is right here: https://github.com/Vitosh/Python_personal/tree/master/YouTube/023_Python-Rule-of-72
Revel in!