-
Notifications
You must be signed in to change notification settings - Fork 36
/
mortgage_calculator.py
64 lines (54 loc) · 2.04 KB
/
mortgage_calculator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
import math
st.title("Mortgage Repayments Calculator")
st.write("### Input Data")
col1, col2 = st.columns(2)
home_value = col1.number_input("Home Value", min_value=0, value=500000)
deposit = col1.number_input("Deposit", min_value=0, value=100000)
interest_rate = col2.number_input("Interest Rate (in %)", min_value=0.0, value=5.5)
loan_term = col2.number_input("Loan Term (in years)", min_value=1, value=30)
# Calculate the repayments.
loan_amount = home_value - deposit
monthly_interest_rate = (interest_rate / 100) / 12
number_of_payments = loan_term * 12
monthly_payment = (
loan_amount
* (monthly_interest_rate * (1 + monthly_interest_rate) ** number_of_payments)
/ ((1 + monthly_interest_rate) ** number_of_payments - 1)
)
# Display the repayments.
total_payments = monthly_payment * number_of_payments
total_interest = total_payments - loan_amount
st.write("### Repayments")
col1, col2, col3 = st.columns(3)
col1.metric(label="Monthly Repayments", value=f"${monthly_payment:,.2f}")
col2.metric(label="Total Repayments", value=f"${total_payments:,.0f}")
col3.metric(label="Total Interest", value=f"${total_interest:,.0f}")
# Create a data-frame with the payment schedule.
schedule = []
remaining_balance = loan_amount
for i in range(1, number_of_payments + 1):
interest_payment = remaining_balance * monthly_interest_rate
principal_payment = monthly_payment - interest_payment
remaining_balance -= principal_payment
year = math.ceil(i / 12) # Calculate the year into the loan
schedule.append(
[
i,
monthly_payment,
principal_payment,
interest_payment,
remaining_balance,
year,
]
)
df = pd.DataFrame(
schedule,
columns=["Month", "Payment", "Principal", "Interest", "Remaining Balance", "Year"],
)
# Display the data-frame as a chart.
st.write("### Payment Schedule")
payments_df = df[["Year", "Remaining Balance"]].groupby("Year").min()
st.line_chart(payments_df)