Skip to content
Course Notes: Introduction to Financial Concepts in Python
add text here
import numpy as np
import numpy_financial as npf
# Set the value of the home you are looking to buy
home_value = 800000
# What percentage are you paying up-front?
down_payment_percent = 0.2
# Calculate the dollar value of the down payment
down_payment = home_value*down_payment_percent
print("Initial Down Payment: " + str(down_payment))
# Calculate the value of the mortgage loan required after the down payment
mortgage_loan = home_value-down_payment
print("Mortgage Loan: " + str(mortgage_loan))# Derive the equivalent monthly mortgage rate from the annual rate
mortgage_rate = 0.0375
mortgage_rate_periodic = ((1+mortgage_rate)**(1/12)-1)
# How many monthly payment periods will there be over 30 years?
mortgage_payment_periods = 12*30
# Calculate the monthly mortgage payment (multiply by -1 to keep it positive)
periodic_mortgage_payment = -1*npf.pmt(rate=mortgage_rate_periodic, nper=mortgage_payment_periods, pv=mortgage_loan)
print("Monthly Mortgage Payment: " + str(round(periodic_mortgage_payment, 2)))# Loop through each mortgage payment period
for i in range(0, mortgage_payment_periods):
# Handle the case for the first iteration
if i == 0:
previous_principal_remaining = mortgage_loan
else:
previous_principal_remaining = principal_remaining[i-1]
# Calculate the interest and principal payments
interest_payment = round(previous_principal_remaining*mortgage_rate_periodic, 2)
principal_payment = round(periodic_mortgage_payment - interest_payment, 2)
# Catch the case where all principal is paid off in the final period
if previous_principal_remaining - principal_payment < 0:
principal_payment = previous_principal_remaining
# Collect the principal remaining values in an array
principal_remaining[i] = previous_principal_remaining - principal_payment
# Print the payments for the first few periods
print_payments(i, interest_payment, principal_payment, principal_remaining)# Loop through each mortgage payment period
for i in range(0, mortgage_payment_periods):
# Handle the case for the first iteration
if i == 0:
previous_principal_remaining = mortgage_loan
else:
previous_principal_remaining = principal_remaining[i-1]
# Calculate the interest based on the previous principal
interest_payment = round(previous_principal_remaining*mortgage_rate_periodic, 2)
principal_payment = round(periodic_mortgage_payment - interest_payment, 2)
# Catch the case where all principal is paid off in the final period
if previous_principal_remaining - principal_payment < 0:
principal_payment = previous_principal_remaining
# Collect the historical values
interest_paid[i] = interest_payment
principal_paid[i] = principal_payment
principal_remaining[i] = previous_principal_remaining - principal_payment
# Plot the interest vs principal
plt.plot(interest_paid, color="red")
plt.plot(principal_paid, color="blue")
plt.legend(handles=[interest_plot, principal_plot], loc=2)
plt.show()import numpy as np
# Calculate the cumulative home equity (principal) over time
cumulative_home_equity = np.cumsum(principal_paid)
# Calculate the cumulative interest paid over time
cumulative_interest_paid = np.cumsum(interest_paid)
# Calculate your percentage home equity over time
cumulative_percent_owned = down_payment_percent + (cumulative_home_equity/home_value)
print(cumulative_percent_owned)
# Plot the cumulative interest paid vs equity accumulated
plt.plot(cumulative_interest_paid, color='red')
plt.plot(cumulative_home_equity, color='blue')
plt.legend(handles=[interest_plot, principal_plot], loc=2)
plt.show()import numpy as np
# Calculate the cumulative growth over time
cumulative_growth_forecast = np.cumprod(1+np.array(growth_array))
# Forecast the home value over time
home_value_forecast = home_value*cumulative_growth_forecast
# Forecast the home equity value owned over time
cumulative_home_value_owned = home_value_forecast*cumulative_percent_owned
# Plot the home value vs equity accumulated
plt.plot(home_value_forecast, color='red')
plt.plot(cumulative_home_value_owned, color='blue')
plt.legend(handles=[homevalue_plot, homeequity_plot], loc=2)
plt.show()import numpy as np
import pandas as pd
# Cumulative drop in home value over time as a ratio
cumulative_decline_forecast = np.cumprod(1+decline_array)
# Forecast the home value over time
home_value_forecast = cumulative_decline_forecast*home_value
# Find all periods where your mortgage is underwater
underwater = home_value_forecast<principal_remaining
pd.value_counts(underwater)
# Plot the home value vs principal remaining
plt.plot(home_value_forecast, color='red')
plt.plot(principal_remaining, color='blue')
plt.legend(handles=[homevalue_plot, principal_plot], loc=2)
plt.show()# Enter your annual salary
salary = 85000
# Assume a tax rate of 30%
tax_rate = 0.3
# Calculate your salary after taxes
salary_after_taxes = salary*(1-tax_rate)
print("Salary after taxes: " + str(round(salary_after_taxes, 2)))
# Calculate your monthly salary after taxes
monthly_takehome_salary = salary_after_taxes/12
print("Monthly takehome salary: " + str(round(monthly_takehome_salary, 2)))# Enter your monthly rent
monthly_rent = 1200
# Enter your daily food budget
daily_food_budget = 30
# Calculate your monthly food budget assuming 30 days per month
monthly_food_budget = 30*daily_food_budget
# Set your monthly entertainment budget
monthly_entertainment_budget = 200
# Allocate funds for unforeseen expenses, just in case
monthly_unforeseen_expenses = 250
# Next, calculate your total monthly expenses
monthly_expenses = monthly_rent+monthly_food_budget+monthly_entertainment_budget+monthly_unforeseen_expenses
print("Monthly expenses: " + str(round(monthly_expenses, 2)))
# Finally, calculate your monthly take-home savings
monthly_savings = monthly_takehome_salary - monthly_expenses
print("Monthly savings: " + str(round(monthly_savings, 2)))import numpy as np
# Create monthly forecasts up to 15 years from now
forecast_months = 12*15
# Set your annual salary growth rate
annual_salary_growth = 0.05
# Calculate your equivalent monthly salary growth rate
monthly_salary_growth = (1 + 0.05)**(1/12)- 1
# Forecast the cumulative growth of your salary
cumulative_salary_growth_forecast = np.cumprod(np.repeat(1 + monthly_salary_growth, forecast_months))
# Calculate the actual salary forecast
salary_forecast = cumulative_salary_growth_forecast*monthly_takehome_salary
# Plot the forecasted salary
plt.plot(salary_forecast, color='blue')
plt.show()import numpy as np
# Set the annual inflation rate
annual_inflation = 0.025
# Calculate the equivalent monthly inflation rate
monthly_inflation = (1+0.025)**(1/12)-1
# Forecast cumulative inflation over time
cumulative_inflation_forecast = np.cumprod(np.repeat(1+monthly_inflation,forecast_months))
# Calculate your forecasted expenses
expenses_forecast = cumulative_inflation_forecast*monthly_expenses
# Plot the forecasted expenses
plt.plot(expenses_forecast, color='red')
plt.show()import numpy as np
# Calculate your savings for each month
savings_forecast = salary_forecast-expenses_forecast
# Calculate your cumulative savings over time
cumulative_savings = np.cumsum(np.array(savings_forecast))
# Print the final cumulative savings after 15 years
final_net_worth = cumulative_savings[-1]
print("Your final net worth: " + str(round(final_net_worth, 2)))
# Plot the forecasted savings
plt.plot(cumulative_savings, color='blue')
plt.show()import numpy as np
# Set the annual investment return to 7%
investment_rate_annual = 0.07
# Calculate the monthly investment return
investment_rate_monthly = (1+investment_rate_annual)**(1/12)-1
# Calculate your required monthly investment to amass $1M
required_investment_monthly = np.pmt(rate=investment_rate_monthly, nper=15*12, pv=0, fv=-1000000)
print("You will have to invest $" + str(round(required_investment_monthly, 2)) + " per month to amass $1M over 15 years")import numpy as np
# Calculate your monthly deposit into your investment account
investment_deposit_forecast = monthly_investment_percentage*cash_flow_forecast
# The rest goes into your savings account
savings_forecast_new = cash_flow_forecast * (1-monthly_investment_percentage)
# Calculate your cumulative savings over time
cumulative_savings_new = np.cumsum(np.array(savings_forecast_new))
# Plot your forecasted monthly savings vs investments
plt.plot(investment_deposit_forecast, color='red')
plt.plot(savings_forecast_new, color='blue')
plt.legend(handles=[investments_plot, savings_plot], loc=2)
plt.show()