Skip to content

Instantly share code, notes, and snippets.

@dmod
Last active August 24, 2020 01:40
Show Gist options
  • Save dmod/a18bc8f7d66f30df624b199b1ccd2dbb to your computer and use it in GitHub Desktop.
Save dmod/a18bc8f7d66f30df624b199b1ccd2dbb to your computer and use it in GitHub Desktop.
from decimal import *
MONTHS_IN_A_YEAR = Decimal(12)
LOAN_TERM_YEARS = Decimal(30)
HOME_PRICE = Decimal(550000)
DOWN_PAYMENT = Decimal(50000)
INTEREST_RATE = Decimal(.030)
OPPORTUNITY_INITIAL_INVESTMENT = Decimal(5000)
OPPORTUNITY_INTEREST_RATE = Decimal(.07)
INITIAL_LOAN_PRINCIPAL = HOME_PRICE - DOWN_PAYMENT
REMAINING_LOAN_PRINCIPAL = INITIAL_LOAN_PRINCIPAL
INTEREST_RATE_PER_MONTH = INTEREST_RATE / MONTHS_IN_A_YEAR
NUMBER_OF_PAYMENTS = LOAN_TERM_YEARS * MONTHS_IN_A_YEAR
# IDK this part
numerator = INTEREST_RATE_PER_MONTH * ((1 + INTEREST_RATE_PER_MONTH) ** NUMBER_OF_PAYMENTS)
denominator = ((1 + INTEREST_RATE_PER_MONTH) ** NUMBER_OF_PAYMENTS) - 1
TOTAL_PAYMENT = INITIAL_LOAN_PRINCIPAL * (numerator / denominator)
print(f"Total payment: {TOTAL_PAYMENT:.2f}")
total_interest_paid = 0
opportunity_balance = OPPORTUNITY_INITIAL_INVESTMENT
for month_index in range(int(NUMBER_OF_PAYMENTS)):
current_month = month_index + 1
print(f"----- MONTH: {current_month} -----")
interest_payment = REMAINING_LOAN_PRINCIPAL * INTEREST_RATE_PER_MONTH
total_interest_paid += interest_payment
print(f"Interest payment: {interest_payment:.2f}")
principal_payment = TOTAL_PAYMENT - interest_payment
print(f"Principal payment: {principal_payment:.2f}")
REMAINING_LOAN_PRINCIPAL = REMAINING_LOAN_PRINCIPAL - principal_payment
print(f"Remaining loan principal: {REMAINING_LOAN_PRINCIPAL:.2f}")
if current_month % MONTHS_IN_A_YEAR == 0:
print("At the year, compounding interest...")
opportunity_balance = opportunity_balance + (opportunity_balance * OPPORTUNITY_INTEREST_RATE)
print()
print(f"Total interest paid: {total_interest_paid:.2f}")
print(f"Total paid: {(total_interest_paid + INITIAL_LOAN_PRINCIPAL):.2f}")
print(f"Opportunity balance: {opportunity_balance:.2f}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment