Created
April 2, 2024 23:59
-
-
Save Jeremiah-England/5b9d2d4379bf0ca175f20a5fee90049d to your computer and use it in GitHub Desktop.
Rule of 72 Accuracy Plot
This file contains 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
""" | |
Plotting the accuracty of the rule of 72. | |
https://en.wikipedia.org/wiki/Rule_of_72 | |
""" | |
import math | |
import matplotlib.pyplot as plt | |
def rule_of_72(rate: float): | |
return 72 / rate | |
def actual_doubling_time(rate): | |
return math.log(2) / math.log(1 + rate / 100) | |
rates = [r / 10 for r in range(30, 201)] | |
rule_of_72_values = [rule_of_72(rate) for rate in rates] | |
actual_doubling_time_values = [actual_doubling_time(rate) for rate in rates] | |
# Now plot the accuracy with a separate y-axis on the right | |
fig, ax1 = plt.subplots() | |
plt.title("The Rule of 72 Accuracy") | |
ax1.set_ylabel("Doubling time") | |
ax1.plot(rates, actual_doubling_time_values, label="Actual doubling time", color="orange") | |
ax1.plot(rates, rule_of_72_values, label="Rule of 72", color="blue") | |
ax1.set_xlabel("Rate of return (%)") | |
ax1.legend(loc="upper left") | |
ax2 = ax1.twinx() | |
ax2.set_ylabel("Ratio") | |
ax2.plot( | |
rates, | |
[(a / r) for a, r in zip(actual_doubling_time_values, rule_of_72_values, strict=False)], | |
label="Ratio", | |
color="red", | |
) | |
ax2.legend(loc="upper right") | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment