-
Notifications
You must be signed in to change notification settings - Fork 7
/
rentorbuy.py
296 lines (237 loc) · 16.7 KB
/
rentorbuy.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
#Imports
import streamlit as st
import matplotlib.pyplot as plt
import numpy as np # Added this line
import pandas as pd
import altair as alt
from utils.currencies import get_currency_list, get_currency_symbol
#Heading
st.markdown("<h1 style='color: #F39373; padding-bottom: 30px;'>🏠 Rent vs. Buy Calculator</h1>", unsafe_allow_html=True)
st.markdown("""
<div style='background-color: #F8F6F4; padding: 10px; border-radius: 5px;'>
<p style='color: #000000; font-size: 16px; font-weight: bold;'>Welcome to Sherry's no-frills Rent vs. Buy calculator</p>
<p style='color: #000000;'>
Use this FREE tool to figure out the total costs, including opportunity costs, between renting and buying property. Easy to input, easy to calculate. No numbers crunching or giant spreadsheet templates necessary.<br><br>
You can calculate through two ways:
<br>1. Simply upload your CSV file with the necessary data OR
<br>2. Input the necessary data yourself</br></br>
For CSV uploads, please make sure that the columns are labeled with the exact wording of the inputs below: "Home Price," "Monthly Rent," "Stay Duration," "Mortgage Rate," "Down Payment," "Mortgage Term."
<br><br>
For any questions or feedback, don't hesitate to reach out to [email protected]!
<br><br>
This product was created by Sherry from the Peek team. If you like what you see, and want more of it, check out <a href='https://peek.money' target='_blank'>peek.money</a>!
</div>
""", unsafe_allow_html=True)
#Upload CSV File
st.markdown("<hr>", unsafe_allow_html=True)
st.markdown("<h3 style='color: #F39373;'>Upload a CSV file</h3>", unsafe_allow_html=True)
# Sample CSV download link
sample_csv = """
Home Price,Monthly Rent,Stay Duration,Mortgage Rate,Down Payment,Mortgage Term
500000,2000,10,3.5,20,30
"""
st.download_button(
label="Download Sample CSV",
data=sample_csv,
file_name='sample_rent_vs_buy.csv',
mime='text/csv'
)
uploaded_file = st.file_uploader("", type=["csv"])
if uploaded_file is not None:
try:
data = pd.read_csv(uploaded_file)
if data.empty:
st.error("The uploaded CSV file is empty. Please upload a valid file.")
else:
st.success("CSV file uploaded successfully!")
st.dataframe(data.style.set_properties(**{'background-color': '#F8F6F4', 'color': '#000000'}))
except pd.errors.EmptyDataError:
st.error("No columns to parse from file. Please upload a valid CSV file.")
except Exception as e:
st.error(f"An error occurred: {e}")
st.markdown("</div>", unsafe_allow_html=True)
st.markdown("<hr>", unsafe_allow_html=True)
#Base calculations for rent vs. buy
def calculate_rent_vs_buy(home_price, monthly_rent, stay_duration, mortgage_rate, down_payment, mortgage_term, investment_return, home_price_growth_rate, rental_growth_rate, cost_of_buying, cost_of_selling, maintenance_cost):
# Step 1: Calculate initial costs
initial_rent_cost = monthly_rent
initial_buy_cost = down_payment * home_price + cost_of_buying * home_price
# Step 2: Calculate recurring costs
annual_recurring_rent = monthly_rent * 12
total_recurring_rent = annual_recurring_rent * ((1 - (1 + rental_growth_rate) ** stay_duration) / (1 - (1 + rental_growth_rate)))
annual_mortgage_payment = (home_price * (1 - down_payment)) * (mortgage_rate / (1 - (1 + mortgage_rate) ** -mortgage_term))
annual_maintenance_cost = maintenance_cost * home_price
annual_recurring_buy = annual_mortgage_payment + annual_maintenance_cost
total_recurring_buy = annual_recurring_buy * stay_duration
# Step 3: Calculate opportunity costs
future_value_rent = annual_recurring_rent * (((1 + investment_return) ** stay_duration - (1 + rental_growth_rate) ** stay_duration) / (investment_return - rental_growth_rate))
future_value_buy_initial = initial_buy_cost * (1 + investment_return) ** stay_duration
future_value_buy_recurring = (annual_mortgage_payment + annual_maintenance_cost) * (((1 + investment_return) ** stay_duration - 1) / investment_return)
# Annualize opportunity costs
annual_future_value_rent = future_value_rent / stay_duration
annual_future_value_buy_recurring = future_value_buy_recurring / stay_duration
# Step 4: Calculate net proceeds
future_home_price = home_price * (1 + home_price_growth_rate) ** stay_duration
net_proceeds = future_home_price - cost_of_selling * future_home_price
# Step 5: Calculate total costs
total_renting_cost = initial_rent_cost + total_recurring_rent + future_value_rent
total_buying_cost = initial_buy_cost + total_recurring_buy + future_value_buy_initial + future_value_buy_recurring - net_proceeds
return total_renting_cost, total_buying_cost, initial_rent_cost, initial_buy_cost, annual_future_value_rent, annual_future_value_buy_recurring, net_proceeds, annual_mortgage_payment, annual_maintenance_cost, annual_recurring_rent, total_recurring_rent, annual_recurring_buy, total_recurring_buy, future_value_buy_initial
# Streamlit - Manual Inputs
st.markdown("<h3 style='color: #F39373; padding-bottom: 40px;'>Enter Details Manually</h3>", unsafe_allow_html=True)
if uploaded_file is not None and not data.empty:
# Extract values from CSV
home_price = data['Home Price'].iloc[0]
monthly_rent = data['Monthly Rent'].iloc[0]
stay_duration = int(data['Stay Duration'].iloc[0])
mortgage_rate = data['Mortgage Rate'].iloc[0] / 100
down_payment = data['Down Payment'].iloc[0] / 100
mortgage_term = int(data['Mortgage Term'].iloc[0])
else:
col1, col2 = st.columns(2)
with col1:
home_price = st.number_input("Home Price", value=1150000, key="home_price", format="%d", help="Enter the home price")
with col2:
monthly_rent = st.number_input("Monthly Rent", value=4300, key="monthly_rent", format="%d", help="Enter the monthly rent")
col3, col4 = st.columns(2)
with col3:
stay_duration = int(st.number_input("Stay Duration (years)", value=30, key="stay_duration", format="%d", help="Enter the stay duration in years"))
with col4:
mortgage_rate = st.number_input("Mortgage Rate (%)", value=4.0, key="mortgage_rate", format="%.1f", help="Enter the mortgage rate in percentage", step=0.1, min_value=0.1) / 100
col5, col6 = st.columns(2)
with col5:
down_payment = st.number_input("Down Payment (%)", value=25, key="down_payment", format="%d", help="Enter the down payment in percentage") / 100
with col6:
mortgage_term = int(st.number_input("Mortgage Term (years)", value=30, key="mortgage_term", format="%d", help="Enter the mortgage term in years"))
# Constants
with st.expander("Adjust Other Default Assumptions", expanded=False):
currency = st.selectbox("Currency", get_currency_list(), help="Select the currency for the calculations")
investment_return = st.slider('Investment Return (%)', 0.1, 20.0, 9.0) / 100
home_price_growth_rate = st.slider('Home Price Growth Rate (%)', 0.0, 10.0, 4.0) / 100
rental_growth_rate = st.slider('Rental Growth Rate (%)', 0.0, 10.0, 3.0) / 100
cost_of_buying = st.slider('Cost of Buying (%)', 0.0, 10.0, 4.5) / 100
cost_of_selling = st.slider('Cost of Selling (%)', 0.0, 10.0, 8.0) / 100
maintenance_cost = st.slider('Maintenance Cost (inclusive of tax, insurance, servicing) (%)', 0.0, 10.0, 2.0) / 100
st.markdown("<hr>", unsafe_allow_html=True)
# Streamlit - Decision Calculator
st.markdown("<h3 style='color: #F39373; padding-bottom: 30px;'>Calculations</h3>", unsafe_allow_html=True)
st.markdown("""
After uploading or manually entering your details, feel free to tap each of the buttons below to calculate your results.
- **Calculate Decision**: Calculate total rent vs. buy costs, including opportunity cost
- **Mortgage Rate - Breakeven**: See the mortgage % that you need in order for renting and buying to cost the same
""", unsafe_allow_html=True)
st.markdown("<div style='padding-bottom: 30px;'></div>", unsafe_allow_html=True)
if st.button("Calculate Decision"):
currency_symbol = get_currency_symbol(currency)
results = calculate_rent_vs_buy(home_price, monthly_rent, stay_duration, mortgage_rate, down_payment, mortgage_term, investment_return, home_price_growth_rate, rental_growth_rate, cost_of_buying, cost_of_selling, maintenance_cost)
total_renting_cost, total_buying_cost, initial_rent_cost, initial_buy_cost, annual_future_value_rent, annual_future_value_buy_recurring, net_proceeds, annual_mortgage_payment, annual_maintenance_cost, annual_recurring_rent, total_recurring_rent, annual_recurring_buy, total_recurring_buy, future_value_buy_initial = results
st.write(f"<p style='color: #646464;'><b>Total Renting Cost: {currency_symbol}{total_renting_cost:,.2f}</b></p>", unsafe_allow_html=True)
st.write(f"<p style='color: #646464;'><b>Total Buying Cost: {currency_symbol}{total_buying_cost:,.2f}</b></p>", unsafe_allow_html=True)
if total_renting_cost < total_buying_cost:
savings = total_buying_cost - total_renting_cost
st.write("<p style='color: #F39373; font-size: 18px;'><b>Renting is more cost-effective than buying.</b></p>", unsafe_allow_html=True)
st.write(f"<p style='color: #F39373; font-size: 18px;'><b>You would save {currency_symbol}{savings:,.2f} by renting.</b></p>", unsafe_allow_html=True)
else:
savings = total_renting_cost - total_buying_cost
st.write("<p style='color: #F39373; font-size: 18px;'><b>Buying is more cost-effective than renting.</b></p>", unsafe_allow_html=True)
st.write(f"<p style='color: #F39373; font-size: 18px;'><b>You would save ${savings:,.2f} by buying.</b></p>", unsafe_allow_html=True)
st.markdown("</div>", unsafe_allow_html=True)
st.markdown("<h4 style='color: #646464; padding-bottom: 20px;'>Breakdown of Total Renting Cost</h4>", unsafe_allow_html=True)
st.write(f"<p style='color: #646464;'><b>Initial Renting Cost: {currency_symbol}{initial_rent_cost:,.2f}</b></p>", unsafe_allow_html=True)
st.write(f"<p style='color: #646464;'><b>Recurring Renting Cost: {currency_symbol}{total_recurring_rent:,.2f}</b></p>", unsafe_allow_html=True)
st.write(f"<p style='color: #646464;'><b>Renting Opportunity Cost: {currency_symbol}{annual_future_value_rent * stay_duration:,.2f}</b></p>", unsafe_allow_html=True)
st.markdown("<h4 style='color: #646464; padding-bottom: 20px;'>Breakdown of Total Buying Cost</h4>", unsafe_allow_html=True)
st.write(f"<p style='color: #646464;'><b>Initial Buying Cost: {currency_symbol}{initial_buy_cost:,.2f}</b></p>", unsafe_allow_html=True)
st.write(f"<p style='color: #646464;'><b>Recurring Buying Cost: {currency_symbol}{total_recurring_buy:,.2f}</b></p>", unsafe_allow_html=True)
st.write(f"<p style='color: #646464;'><b>Buying Opportunity Cost: {currency_symbol}{(future_value_buy_initial + annual_future_value_buy_recurring * stay_duration):,.2f}</b></p>", unsafe_allow_html=True)
st.write(f"<p style='color: #646464; padding-bottom: 40px;'><b>Buying Net Proceeds: {currency_symbol}{net_proceeds * (-1):,.2f}</b> <i>(This is the amount you get when you sell the house at the end of {stay_duration} years, which you subtract out of the total cost for buying)</i></p>", unsafe_allow_html=True)
# Line chart for annualized costs over time
years = list(range(1, stay_duration + 1))
rent_costs = []
buy_costs = []
annualized_rent_cost = total_renting_cost / stay_duration
annualized_buy_cost = total_buying_cost / stay_duration
for year in years:
rent_cost = annualized_rent_cost * year
buy_cost = annualized_buy_cost * year
rent_costs.append(rent_cost)
buy_costs.append(buy_cost)
# Create a DataFrame for Altair
data = pd.DataFrame({
'Year': years,
'Renting Cost': rent_costs,
'Buying Cost': buy_costs
})
# Melt the DataFrame for Altair
data_melted = data.melt('Year', var_name='Cost Type', value_name='Cost')
# Create the line plot using Altair
line_chart = alt.Chart(data_melted).mark_line(point=True).encode(
x=alt.X('Year:Q', title='Years'),
y=alt.Y('Cost:Q', title=f'Annualized Cost ({currency_symbol})'),
color='Cost Type:N',
tooltip=['Year:Q', 'Cost:Q', 'Cost Type:N']
).properties(
title=alt.TitleParams(
text=f'Annualized Rent vs. Buy Costs Over Time in {currency}',
fontSize=20, # h3 size
anchor='start'
),
width=600,
height=400
)
# Add data labels for every 5 years and the final year
text_labels = alt.Chart(data_melted).mark_text(align='right', dx=-5, dy=-5).encode(
x='Year:Q',
y='Cost:Q',
text=alt.Text('Cost:Q', format='.0f'),
color='Cost Type:N'
).transform_filter(
(alt.datum.Year % 5 == 0) | (alt.datum.Year == stay_duration)
)
# Combine the line chart and text labels
final_chart = line_chart + text_labels
st.altair_chart(final_chart, use_container_width=True)
# Breakeven
def mortgage_break_even(home_price, monthly_rent, stay_duration, mortgage_rate, down_payment, mortgage_term, investment_return, home_price_growth_rate, rental_growth_rate, cost_of_buying, cost_of_selling, maintenance_cost):
low, high = 0, 0.2 # Searching within 0% to 20% mortgage rate
while high - low > 0.0001:
mid = (high + low) / 2
total_renting_cost, total_buying_cost, *_ = calculate_rent_vs_buy(home_price, monthly_rent, stay_duration, mid, down_payment, mortgage_term, investment_return, home_price_growth_rate, rental_growth_rate, cost_of_buying, cost_of_selling, maintenance_cost) # Pass all arguments here
if total_renting_cost > total_buying_cost:
low = mid
else:
high = mid
break_even_rate = (high + low) / 2
return break_even_rate * 100 # Convert to percentage
def calculate_costs_over_mortgage_rates(home_price, monthly_rent, stay_duration, mortgage_rate, down_payment, mortgage_term, investment_return, home_price_growth_rate, rental_growth_rate, cost_of_buying, cost_of_selling, maintenance_cost):
rates = np.linspace(0.005, 0.06, 100) # Mortgage rates from 0.5% to 6%
renting_costs = []
buying_costs = []
for rate in rates:
total_renting_cost, total_buying_cost, *_ = calculate_rent_vs_buy(home_price, monthly_rent, stay_duration, rate, down_payment, mortgage_term, investment_return, home_price_growth_rate, rental_growth_rate, cost_of_buying, cost_of_selling, maintenance_cost) # Pass all arguments here
renting_costs.append(total_renting_cost)
buying_costs.append(total_buying_cost)
return rates, renting_costs, buying_costs
if st.button("Mortgage Rate - Breakeven"):
break_even_rate = mortgage_break_even(home_price, monthly_rent, stay_duration, mortgage_rate, down_payment, mortgage_term, investment_return, home_price_growth_rate, rental_growth_rate, cost_of_buying, cost_of_selling, maintenance_cost) # Pass all arguments here
st.write(f"<p style='color: #646464;'><b>Break-even Mortgage Rate: {break_even_rate:.2f}%</b></p>", unsafe_allow_html=True)
st.write("That means you break even on your buying and renting cost at this mortgage rate.")
st.write(f"Your current mortgage rate is {mortgage_rate*100:.2f}%")
rates, renting_costs, buying_costs = calculate_costs_over_mortgage_rates(home_price, monthly_rent, stay_duration, mortgage_rate, down_payment, mortgage_term, investment_return, home_price_growth_rate, rental_growth_rate, cost_of_buying, cost_of_selling, maintenance_cost) # Pass all arguments here
currency_symbol = get_currency_symbol(currency)
data = pd.DataFrame({
'Mortgage Rate (%)': rates * 100, # Convert to percentage and update column name for clarity
f'Renting Cost ({currency_symbol})': renting_costs, # Update column name for clarity
f'Buying Cost ({currency_symbol})': buying_costs # Update column name for clarity
}).melt('Mortgage Rate (%)', var_name='Cost Type', value_name=f'Total Cost ({currency_symbol})') # Update parameters to match column name changes
break_even_chart = alt.Chart(data).mark_line(point=True).encode(
x=alt.X('Mortgage Rate (%):Q', title='Mortgage Rate (%)'), # Ensure axis title matches updated column name
y=alt.Y(f'Total Cost ({currency_symbol}):Q', title=f'Total Cost ({currency_symbol})'), # Ensure axis title matches updated column name
color=alt.Color('Cost Type:N', legend=alt.Legend(title="Cost Type")), # Add legend title for clarity
tooltip=['Mortgage Rate (%):Q', 'Total Cost ({currency_symbol}):Q', 'Cost Type:N'] # Ensure tooltip labels match updated column names
).properties(
title='Renting vs. Buying Costs Over Different Mortgage Rates',
width=600,
height=400
)
st.altair_chart(break_even_chart, use_container_width=True)