forked from teobeeguan/MortgageSimulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMortgageSimulator.py
228 lines (180 loc) · 6.66 KB
/
MortgageSimulator.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 12 17:12:17 2021
@author: Teo Bee Guan
"""
import streamlit as st
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import numpy as np
st.set_page_config(
page_title="Mortgage Loan Simulator")
st.title("Mortgage Loan Simulator")
st.header("**Mortgage Details**")
col1, col2 = st.beta_columns(2)
with col1:
st.subheader("Home Value")
home_value = st.number_input("Enter your home value($): ", min_value=0.0, format='%f')
st.subheader("Loan Interest Rate")
interest_rate = st.number_input("Enter your home loan interest rate(%): ", min_value=0.0, format='%f')
with col2:
st.subheader("Down Payment Percent")
down_payment_percent = st.number_input("Enter your down payment percent(%): ", min_value=0.0, format='%f')
st.subheader("Target Payment Period (Years)")
payment_years = st.number_input("Enter your target payment period (years): ", min_value=3, format='%d')
down_payment = home_value* (down_payment_percent / 100)
loan_amount = home_value - down_payment
payment_months = payment_years*12
interest_rate = interest_rate / 100
periodic_interest_rate = (1+interest_rate)**(1/12) - 1
monthly_installment = -1*np.pmt(periodic_interest_rate , payment_months, loan_amount)
st.subheader("**Down Payment:** $" + str(round(down_payment,2)))
st.subheader("**Loan Amount:** $" + str(round(loan_amount, 2)))
st.subheader("**Monthly Installment:** $" + str(round(monthly_installment, 2)))
st.markdown("---")
st.header("**Mortgage loan Amortization**")
principal_remaining = np.zeros(payment_months)
interest_pay_arr = np.zeros(payment_months)
principal_pay_arr = np.zeros(payment_months)
for i in range(0, payment_months):
if i == 0:
previous_principal_remaining = loan_amount
else:
previous_principal_remaining = principal_remaining[i-1]
interest_payment = round(previous_principal_remaining*periodic_interest_rate, 2)
principal_payment = round(monthly_installment - interest_payment, 2)
if previous_principal_remaining - principal_payment < 0:
principal_payment = previous_principal_remaining
interest_pay_arr[i] = interest_payment
principal_pay_arr[i] = principal_payment
principal_remaining[i] = previous_principal_remaining - principal_payment
month_num = np.arange(payment_months)
month_num = month_num + 1
principal_remaining = np.around(principal_remaining, decimals=2)
fig = make_subplots(
rows=2, cols=1,
vertical_spacing=0.03,
specs=[[{"type": "table"}],
[{"type": "scatter"}]
]
)
fig.add_trace(
go.Table(
header=dict(
values=['Month', 'Principal Payment($)', 'Interest Payment($)', 'Remaining Principal($)']
),
cells = dict(
values =[month_num, principal_pay_arr, interest_pay_arr, principal_remaining]
)
),
row=1, col=1
)
fig.add_trace(
go.Scatter(
x=month_num,
y=principal_pay_arr,
name= "Principal Payment"
),
row=2, col=1
)
fig.append_trace(
go.Scatter(
x=month_num,
y=interest_pay_arr,
name="Interest Payment"
),
row=2, col=1
)
fig.update_layout(title='Mortgage Installment Payment Over Months',
xaxis_title='Month',
yaxis_title='Amount($)',
height= 800,
width = 1200,
legend= dict(
orientation="h",
yanchor='top',
y=0.47,
xanchor= 'left',
x= 0.01
)
)
st.plotly_chart(fig, use_container_width=True)
st.markdown("---")
st.header("**Home Equity (With Constant Market Value)**")
cumulative_home_equity = np.cumsum(principal_pay_arr)
cumulative_interest_paid = np.cumsum(interest_pay_arr)
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=month_num,
y=cumulative_home_equity,
name="Cumulative Home Equity"
)
)
fig.add_trace(
go.Scatter(
x=month_num,
y=cumulative_interest_paid,
name="Cumulative Interest Paid"
)
)
fig.update_layout(title='Cumulative Home Equity Over Time',
xaxis_title='Month',
yaxis_title='Amount($)',
height= 500,
width = 1200,
legend= dict(
orientation="h",
yanchor='top',
y=0.98,
xanchor= 'left',
x= 0.01
)
)
st.plotly_chart(fig, use_container_width=True)
st.markdown("---")
st.header("**Forecast Growth**")
st.subheader("Forecast Growth (Per Year)")
forecast_growth = st.number_input("Enter your forecast growth rate(%): ", format='%f')
growth_per_month = (forecast_growth / 12.0) / 100
growth_array = np.full(payment_months, growth_per_month)
forecast_cumulative_growth = np.cumprod(1+growth_array)
forecast_home_value= home_value*forecast_cumulative_growth
cumulative_percent_owned = (down_payment_percent/100) + (cumulative_home_equity/home_value)
forecast_home_equity = cumulative_percent_owned*forecast_home_value
fig = go.Figure()
fig.add_trace(
go.Scatter(
x=month_num,
y=forecast_home_value,
name="Forecast Home Value"
)
)
fig.add_trace(
go.Scatter(
x=month_num,
y=forecast_home_equity,
name="Forecast Home Equity Owned"
)
)
fig.add_trace(
go.Scatter(
x=month_num,
y=principal_remaining,
name="Remaining Principal"
)
)
fig.update_layout(title='Forecast Home Value Vs Forecast Home Equity Over Time',
xaxis_title='Month',
yaxis_title='Amount($)',
height= 500,
width = 1200,
legend= dict(
orientation="h",
yanchor='top',
y=1.14,
xanchor= 'left',
x= 0.01
)
)
st.plotly_chart(fig, use_container_width=True)