Created
May 13, 2024 22:33
-
-
Save Adobe-Android/480aa2aad84fabe3475debd02c9a7383 to your computer and use it in GitHub Desktop.
Codify compound interest formulas
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
#!/bin/env python3 | |
# Formula Resources: | |
# https://www.omnicalculator.com/finance/compound-interest#example-2-complex-calculation-of-the-value-of-an-investment | |
def main(): | |
starting_balance = 5_000 | |
years_to_achieve = 1 | |
rate_of_return = 0.03 | |
compound_frequency = 12 # number of times compounded per year | |
final_balance = 0 | |
# Below, in a through d, the formula is broken down into its individual steps | |
a = 1 + (rate_of_return / compound_frequency) | |
print(a) | |
b = (compound_frequency * years_to_achieve) | |
print(b) | |
# How Python handles exponents | |
# https://stackoverflow.com/questions/73577000/what-is-the-shorthand-way-of-raising-a-variable-to-the-power-of-in-python-3 | |
c = a ** b | |
print(c) | |
d = starting_balance * c | |
print(d) | |
# Final formula | |
final_balance = starting_balance * (1 + (rate_of_return / compound_frequency)) ** (compound_frequency * years_to_achieve) | |
print(final_balance) | |
if __name__ == "__main__": | |
main() |
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
#!/bin/env python3 | |
# Formula Resources: | |
# https://www.omnicalculator.com/finance/compound-interest#example-2-complex-calculation-of-the-value-of-an-investment | |
# https://structx.com/annual_compound_interest_with_contributions.html | |
# https://math.stackexchange.com/questions/4150453/compound-interest-with-regular-monthly-contributions-formula | |
def main(): | |
starting_balance = 10_000 | |
years_to_achieve = 10 | |
rate_of_return = 0.05 | |
compound_frequency = 12 # number of times compounded per year | |
final_balance = 0 | |
amount_invested_monthly = 1_000 | |
# Below, in a through f, the formula is broken down into its individual steps | |
a = 1 + (rate_of_return / compound_frequency) | |
print(a) | |
b = (compound_frequency * years_to_achieve) | |
print(b) | |
# How Python handles exponents | |
# https://stackoverflow.com/questions/73577000/what-is-the-shorthand-way-of-raising-a-variable-to-the-power-of-in-python-3 | |
c = a ** b | |
print(c) | |
d = starting_balance * c | |
print(d) | |
# Steps e and f are specific to factoring in future contributions and are not required otherwise | |
e = (c - 1) / (rate_of_return / compound_frequency) | |
print(e) | |
f = d + (amount_invested_monthly * e) | |
print(f) | |
# Final formula | |
final_balance = starting_balance * (1 + (rate_of_return / compound_frequency)) ** (compound_frequency * years_to_achieve) + amount_invested_monthly * (((1 + (rate_of_return / compound_frequency)) ** (compound_frequency * years_to_achieve)) - 1) / (rate_of_return / compound_frequency) | |
print(final_balance) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment