-
Notifications
You must be signed in to change notification settings - Fork 348
/
Copy pathmortgage_calculator.py
414 lines (339 loc) · 9.88 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
# /// script
# requires-python = ">=3.9"
# dependencies = [
# "marimo",
# "matplotlib==3.9.2",
# "mortgage==1.0.5",
# "numpy==1.26.4",
# ]
# ///
import marimo
__generated_with = "0.8.19"
app = marimo.App()
@app.cell(hide_code=True)
def __(mo):
mo.md("""# Mortgage Calculator""")
return
@app.cell(hide_code=True)
def __(mo):
mo.md("""## Income""")
return
@app.cell
def __(mo):
income = mo.ui.number(1, 1e8, step=50, value=100, label="income (thousands)")
retirement_contribution = mo.ui.number(10, 40, step=10, value=19.5)
mo.md(
f"""
How much do you expect to make this year, **after taxes**?
{income.center()}
"""
)
return income, retirement_contribution
@app.cell
def __(mo):
mo.callout(
mo.md(
"""
**Heads-up!**
This calculator does not take taxes into account. Please make sure
that you provide **after-tax** income. Also exclude 401k contributions.
You can estimate your federal and state taxes using an online
calculator, such as the one
[linked here](https://smartasset.com/taxes/income-taxes).
"""
),
kind="warn",
)
return
@app.cell
def __(income, mo, np, retirement_contribution):
standard_deduction = 25100
taxable_income = (
income.value * 1000
- standard_deduction
- retirement_contribution.value * 1000
)
def calculate_federal_tax(income):
brackets = [
(0.10, 0, 20550),
(0.12, 20551, 83550),
(0.22, 83551, 178150),
(0.24, 178151, 340100),
(0.32, 340101, 431900),
(0.35, 431901, 647850),
(0.37, 647850, np.inf),
]
tax = 0
for rate, low, high in brackets:
tax += rate * (min(income, high) - low)
if income <= high:
break
return tax
fed_tax = calculate_federal_tax(taxable_income)
ca_tax = 5390.38 + 0.093 * (taxable_income - 122428)
net_cash = income.value * 1000
net_cash_per_month = net_cash / 12
mo.md(
f"""
With an after-tax income of **\${income.value*1000:,}**, you'll take home
**\${net_cash_per_month:,.02f}** every month.
"""
)
return (
ca_tax,
calculate_federal_tax,
fed_tax,
net_cash,
net_cash_per_month,
standard_deduction,
taxable_income,
)
@app.cell
def __(mo):
home_price = mo.ui.number(
100, 5000, step=100, value=500, label="home price (thousands)"
)
down_payment_pct = mo.ui.number(
0, 100, step=5, value=20, label="down payment (%)"
)
rate = mo.ui.number(1.0, 8.0, step=0.25, value=5.0, label="interest rate (%)")
years = mo.ui.number(5, 45, step=1, value=30, label="mortgage term (years)")
property_tax_rate = mo.ui.number(
1, 10, step=0.25, value=1.25, label="property tax (%)"
)
utilities = mo.ui.number(100, 1000, value=500, label="utilities monthly ($)")
home_insurance = mo.ui.number(
100, 1000, value=150, label="home insurance monthly ($)"
)
home_purchase_parameters = [home_price, down_payment_pct, rate, years]
home_expense_parameters = [
property_tax_rate,
utilities,
home_insurance,
]
mo.md(
f"""
## Loan
Next, enter some details about the home you'd like to purchase,
the mortgage you qualify for, and additional home expenses.
"""
)
return (
down_payment_pct,
home_expense_parameters,
home_insurance,
home_price,
home_purchase_parameters,
property_tax_rate,
rate,
utilities,
years,
)
@app.cell
def __(home_expense_parameters, home_purchase_parameters, mo):
mo.hstack([home_purchase_parameters, home_expense_parameters])
return
@app.cell
def __(down_payment_pct, home_price, mortgage, rate, years):
down_payment = down_payment_pct.value / 100 * home_price.value
principal = home_price.value - down_payment
loan = mortgage.Loan(
principal=principal * 1e3, interest=rate.value / 100, term=years.value
)
return down_payment, loan, principal
@app.cell
def __(
down_payment,
home_insurance,
home_price,
loan,
mo,
net_cash,
net_cash_per_month,
property_tax_rate,
rate,
utilities,
):
property_tax_monthly = (
property_tax_rate.value / 100 * home_price.value * 1e3 / 12
)
home_insurance_monthly = home_insurance.value
mortgage_monthly = float(loan.monthly_payment)
utilities_monthly = utilities.value
monthly_home_payment = (
property_tax_monthly
+ home_insurance_monthly
+ mortgage_monthly
+ utilities_monthly
)
annual_home_payment = monthly_home_payment * 12
cash_less_housing = net_cash - annual_home_payment
mo.md(
f"""
You're purchasing a home worth **\${home_price.value * 1000:,}**, with a
down payment of **\${down_payment*1000:,.02f}**.
At a rate of **{rate.value}**%,
you will owe **\${annual_home_payment:,.02f}** per year on home expenses.
That's **\${monthly_home_payment:,.02f}** per month, which is
**{monthly_home_payment / (net_cash_per_month) * 100:,.02f}%** of
your take-home pay.
You'll have **\${cash_less_housing/12:,.02f}** left over per
month for expenses and saving.
"""
).callout()
return (
annual_home_payment,
cash_less_housing,
home_insurance_monthly,
monthly_home_payment,
mortgage_monthly,
property_tax_monthly,
utilities_monthly,
)
@app.cell
def __(mo):
mo.md("""## Monthly Expenses""")
return
@app.cell
def __(mo):
mo.md(
"""
In addition to paying for your home, you'll have monthly expenses on
necessities and entertainment. Let's estimate these to see how much
you'll save per month, after all expenses.
"""
)
return
@app.cell
def __(mo):
vacation = mo.ui.number(0, 100000, step=100, value=100)
groceries = mo.ui.number(0, 100000, step=100, value=100)
dining_out = mo.ui.number(0, 100000, step=100, value=100)
gifts = mo.ui.number(0, 100000, step=100, value=100)
car_payment = mo.ui.number(0, 100000, step=100, value=100)
entertainment = mo.ui.number(0, 100000, value=100)
clothing = mo.ui.number(0, 100000, value=100)
misc = mo.ui.number(0, 100000, step=100, value=100)
return (
car_payment,
clothing,
dining_out,
entertainment,
gifts,
groceries,
misc,
vacation,
)
@app.cell
def __(
car_payment,
clothing,
dining_out,
entertainment,
gifts,
groceries,
misc,
mo,
vacation,
):
def _row(kind, control):
return {"Expense": kind, "Amount ($)": control}
mo.ui.table(
[
_row("groceries", groceries),
_row("dining out", dining_out),
_row("gifts", gifts),
_row("car payment", car_payment),
_row("entertainment", entertainment),
_row("clothing", clothing),
_row("vacation", vacation),
_row("miscellaneous", misc),
]
)
return
@app.cell
def __(
car_payment,
cash_less_housing,
clothing,
dining_out,
entertainment,
gifts,
groceries,
misc,
mo,
vacation,
):
monthly_expenses = (
groceries.value
+ dining_out.value
+ gifts.value
+ car_payment.value
+ entertainment.value
+ clothing.value
+ vacation.value
+ misc.value
)
annual_cash_saved = cash_less_housing - monthly_expenses * 12
mo.md(
f"""
Your total monthly expenses are **\${monthly_expenses:,.02f}**.
This means you will save **\${annual_cash_saved/12:,.02f}** per month,
or **\${annual_cash_saved:,.02f}** annually.
"""
)
return annual_cash_saved, monthly_expenses
@app.cell
def __(loan):
schedule = loan.schedule()[1:]
return (schedule,)
@app.cell
def __(np, schedule):
_interest, _principal = zip(
*[(payment.interest, payment.principal) for payment in schedule]
)
interest_payments = np.array(_interest)
principal_payments = np.array(_principal)
return interest_payments, principal_payments
@app.cell
def __(interest_payments, mo, np, principal_payments, years):
import matplotlib.pyplot as plt
fig, axs = plt.subplots(1, 2)
fig.set_size_inches(9, 3)
_cum_interest = np.cumsum(interest_payments)
_cum_principal = np.cumsum(principal_payments)
axs[0].plot(_cum_interest, label="interest")
axs[0].plot(_cum_principal, label="principal")
axs[0].plot(_cum_interest + _cum_principal, label="total")
for _year in range(5, years.value, 5):
axs[1].axvline(_year * 12, linestyle="--", color="lightgray")
axs[0].legend()
axs[0].set_title("Cumulative")
axs[0].set_xlabel("Months")
axs[1].plot(interest_payments, label="interest")
axs[1].plot(principal_payments, label="principal")
axs[1].plot(interest_payments + principal_payments, label="total")
for _year in range(5, years.value, 5):
axs[1].axvline(_year * 12, linestyle="--", color="lightgray")
axs[1].set_title("Monthly")
axs[1].legend()
axs[1].set_xlabel("Months")
plt.tight_layout()
mo.md(
f"""
## Payments
The plots below visualize your mortgage payments, over the duration of the
entire mortgage. The left plot shows cumulative payments, and the right one
shows monthly payments.
{mo.as_html(fig)}
"""
)
return axs, fig, plt
@app.cell
def __():
import marimo as mo
import mortgage
import numpy as np
return mo, mortgage, np
if __name__ == "__main__":
app.run()