Exercise 2 - Becoming a millionaire#
This lesson is modified from A taste of Python from geo-python.
A low-effort way to become a millionaire#
According to New Trader U: 3 Low-Effort Ways To Become A Millionaire, Standard and Poor’s 500 index fund (S&P 500) tracks the stock performance of 500 of the largest companies listed on stock exchanges in the US. The average historical rate an investor can expect long-term on S&P 500 is 8% annual compounded growth. The formula is:
\( FV = PM * \frac{[(1 + r)^{nt} – 1]}{r} \)
Where:
FV: Future value of the investment (e.g., 1,000,000)
PM: Monthly investment amount (e.g., 300)
r: Monthly interest rate (e.g. 0.64%)
n: Number of times the interest is compounded per year (i.e., 12 for monthly compounding)
t: Number of years (e.g., 20 years)
To convert annual interest rate of 8% to monthly interest rate you can use this formula \( r = (1 + 0.08)^{1/n} – 1 \)
For example, for FV= 1,000,000 and t = 40 years, then PM ≈ 310.5. Note while there is wide variance in monthly and annual returns, long-term destination will likely lead to being a millionaire based on historical performance of past 100 years.
Exercise 1#
Write a code to check that if you invisted about 310.5 you will be a millionaire in 40 years.
Display your answer as “For a monthly invistment of 310.5 for 40 years, the future value will be 1.0 million”
Note that 1.0 is rounded and the actual value is 1.0001451368328833.
How to round a number in python? Check online
# Given PM of $310.5 and t of 40 years find FV
#Parameters
pm = 310.5 # Monthly investment
annual_rate = 0.08 # Annual interest rate (8%)
n = 12 # Compounded monthly
r = (1 + annual_rate)**(1/n) - 1 #Monthly interest rate
t = 40 # Investment period in years
# Calculate future value (FV) using the formula
fv = pm * (((1 + r)**(n * t) - 1) / r)
# Display the result
rounded_fv = round(fv / 1000000, 1)
# Display the result
print(f"For a monthly investment of $ {pm} for {t} years, the future value will be $ {rounded_fv} million")
print(f"The actual value is {fv / 1000000:.16f} million")
For a monthly investment of $ 310.5 for 40 years, the future value will be $ 1.0 million
The actual value is 1.0001451368328833 million
Exercise 2#
(2) Write a code to check how much do you need to invest monthly to become a millionaire in 25 years?
Display your answer as “For a future value of 1.0 million in 25 years, you need to invist 1100.0 per month”
Note that 1100.0 is rounded and the actual value is 1100.120953605599.
\( FV = PM * \frac{[(1 + r)^{nt} – 1]}{r} \)
# Find PM given t of 25 years and FV of $1,000,000
# Parameters
fv = 1e6 # Future value (in dollars)
annual_rate = 0.08 # Annual interest rate (8%)
n = 12 # Compounded monthly
t = 25 # Investment period in years
r = (1 + annual_rate)**(1/n) - 1 # Calculate monthly interest rate using the formula
# Calculate monthly investment (PM) using the formula
# PM = FV / [ ((1 + r)^(nt) - 1) / r * (1 + r) ]
pm = fv*r / ((1 + r)**(n * t) - 1)
# Round the monthly investment to 1 decimal place
rounded_pm = round(pm, 0)
# Display the result
print(f"For a future value of $ {fv/1e6:.1f} million in {t} years, you need to invest $ {rounded_pm:.0f} per month")
print(f"The actual value is {pm:.12f} per month")
For a future value of $ 1.0 million in 25 years, you need to invest $ 1100 per month
The actual value is 1100.120953605599 per month
Exercise 3#
Write a code to find out that if you will invest 1000 per month, after how many years will you become a millionaire?
Display your answer as “If you invisited 1000.0 per month, you will become a milloire in 26.1 years”
Note you need to import math module to solve the exponential equation using logarithms.
We did not learn logarithms, but we learned how to find functions in the documentation of the math module
\( FV = PM * \frac{[(1 + r)^{nt} – 1]}{r} \)
# Find t given PM $1,000 per month and FV of $1,000,000
import math
# Parameters
pm = 1000 # Monthly investment
fv = 1e6 # Future value (in dollars)
annual_rate = 0.08 # Annual interest rate (8%)
n = 12 # Compounded monthly
r = (1 + annual_rate)**(1/n) - 1 # Calculate monthly interest rate using the formula
# Solve for t (time in years) using logarithms
# FV = PM *((1 + r)^(n * t) - 1) / r
# log(FV*r/PM + 1) = (n*T)log(1 + r )
# Rearrange to find t:
# t = log((FV * r / PM) + 1) / (n * log(1 + r))
t = math.log((fv * r / pm) + 1) / (n * math.log(1 + r))
# Round t to 1 decimal place
rounded_t = round(t, 1)
# Display the result
print(f"If you invested $ {pm:.1f} per month, you will become a millionaire in {rounded_t} years")
If you invested $ 1000.0 per month, you will become a millionaire in 26.1 years