-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuying-vs-renting-calculator.html
More file actions
1853 lines (1624 loc) · 85.3 KB
/
buying-vs-renting-calculator.html
File metadata and controls
1853 lines (1624 loc) · 85.3 KB
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>🏠 Buying vs Renting Calculator</title>
<script src="https://cdn.plot.ly/plotly-2.27.0.min.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: #080707;
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1800px;
margin: 0 auto;
background: white;
border-radius: 20px;
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
overflow: hidden;
}
header {
background: #a24b4b;
color: white;
padding: 30px;
text-align: center;
}
header h1 {
font-size: 2.5em;
margin-bottom: 10px;
}
.overview {
background: #f8f9fa;
padding: 20px 30px;
border-bottom: 2px solid #e9ecef;
}
.overview p {
color: #495057;
line-height: 1.6;
margin: 10px 0;
}
.content {
display: block;
padding: 30px;
}
.inputs {
background: #f8f9fa;
padding: 25px;
border-radius: 10px;
margin-bottom: 30px;
}
.inputs form {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
}
.inputs .button-group {
grid-column: 1 / -1;
display: flex;
gap: 10px;
}
.inputs .button-group button {
flex: 1;
}
.input-group {
margin-bottom: 20px;
}
.input-group label {
display: block;
margin-bottom: 5px;
font-weight: 600;
color: #333;
}
.input-description {
font-size: 0.85em;
color: #666;
font-style: italic;
margin-bottom: 8px;
line-height: 1.4;
}
.input-group input,
.input-group select {
width: 100%;
padding: 10px;
border: 2px solid #ddd;
border-radius: 5px;
font-size: 1em;
transition: border-color 0.3s;
}
.input-group input:focus,
.input-group select:focus {
outline: none;
border-color: #a24b4b;
}
button {
width: 100%;
padding: 15px;
background: #a24b4b;
color: white;
border: none;
border-radius: 8px;
font-size: 1.1em;
font-weight: 600;
cursor: pointer;
transition: transform 0.2s, box-shadow 0.2s;
}
button:hover {
transform: translateY(-2px);
box-shadow: 0 5px 15px rgba(162, 75, 75, 0.4);
}
button:active {
transform: translateY(0);
}
.reset-button {
background: #7f8c8d;
margin-bottom: 15px;
}
.reset-button:hover {
box-shadow: 0 5px 15px rgba(127, 140, 141, 0.4);
}
.results {
display: none;
width: 100%;
}
.results.active {
display: block;
}
.results-panel {
width: 100%;
}
.summary-table {
width: 100%;
border-collapse: collapse;
margin-bottom: 30px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
border-radius: 8px;
overflow: hidden;
}
.summary-table thead {
background: #a24b4b;
color: white;
}
.summary-table th {
padding: 15px;
text-align: left;
font-weight: 600;
font-size: 1.1em;
}
.summary-table td {
padding: 12px 15px;
border-bottom: 1px solid #e9ecef;
}
.summary-table tbody tr:nth-child(even) {
background-color: #f8f9fa;
}
.summary-table tbody tr:hover {
background-color: #e9ecef;
}
.summary-table .label-cell {
font-weight: 600;
color: #495057;
}
.summary-table .value-cell {
text-align: right;
font-family: 'Courier New', monospace;
font-size: 1.1em;
color: #212529;
}
.plot-container {
margin-bottom: 40px;
padding: 20px;
background: white;
border-radius: 10px;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
width: 100%;
}
.plot-container > div {
width: 100% !important;
}
.section-title {
color: #333;
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 2px solid #a24b4b;
}
.loading {
display: none;
text-align: center;
padding: 40px;
font-size: 1.2em;
color: #a24b4b;
}
.loading.active {
display: block;
}
.error {
display: none;
padding: 20px;
background: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
border-radius: 8px;
margin-bottom: 20px;
}
.error.active {
display: block;
}
.checkbox-group {
margin: 20px 0;
padding: 15px;
background: white;
border-radius: 8px;
border: 2px solid #ddd;
}
.checkbox-group label {
display: flex;
align-items: center;
cursor: pointer;
margin-bottom: 0;
}
.checkbox-group input[type="checkbox"] {
width: auto;
margin-right: 10px;
cursor: pointer;
transform: scale(1.2);
}
.checkbox-info {
font-size: 0.85em;
color: #666;
margin-top: 8px;
line-height: 1.4;
}
.input-group input:disabled,
.input-group select:disabled {
background-color: #e9ecef;
color: #6c757d;
cursor: not-allowed;
opacity: 0.6;
}
.input-section {
grid-column: 1 / -1;
margin-top: 20px;
margin-bottom: 10px;
}
.input-section:first-child {
margin-top: 0;
}
.section-header {
font-size: 1.4em;
font-weight: 700;
color: #a24b4b;
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 3px solid #a24b4b;
}
.section-content {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 20px;
}
.section-content .checkbox-group {
grid-column: 1 / -1;
}
</style>
</head>
<body>
<div class="container">
<header>
<h1>🏠 Buying vs Renting Calculator</h1>
</header>
<div class="overview">
<p><strong>Compare the financial outcomes of buying vs. renting</strong> over time. This calculator models both scenarios, taking into account mortgage payments, property taxes, insurance, maintenance, HOA fees, and investment returns.</p>
<h3 style="margin-top: 20px; margin-bottom: 10px; color: #333;">How It Works</h3>
<p style="margin-bottom: 10px;"><strong>Buying Scenario:</strong> You purchase a home with a down payment and take out a mortgage. Each month, you make mortgage payments (principal + interest), pay property taxes, insurance, HOA fees, and maintenance costs. Over time, you build equity as you pay down the mortgage principal, and your home appreciates in value. If your monthly housing costs are lower than rent, the difference is invested in the market and earns returns.</p>
<p style="margin-bottom: 10px;"><strong>Renting Scenario:</strong> You rent a property and invest the money you would have used for a down payment in the market. Each month, you pay rent (which increases annually). The difference between what you would have spent on total housing costs (if you bought) and your actual rent is invested in the market, earning returns over time. If rent exceeds what homeownership would cost, no additional investment is made that month.</p>
<p style="margin-bottom: 10px;"><strong>Net Worth Calculation:</strong> For buying, your net worth equals the current home value minus the remaining mortgage balance, plus any investment gains. For renting, your net worth is the total amount invested plus investment returns. The calculator compares these two scenarios to show which strategy builds more wealth over your chosen time period.</p>
<p style="margin-bottom: 10px;"><strong>Historical Data:</strong> You can use actual historical data from the San Jose House Price Index (1991-2025) for home appreciation and NASDAQ Composite returns (1971-2025) for investment performance. This shows how real market conditions would have affected your decision during different time periods.</p>
<h3 style="margin-top: 20px; margin-bottom: 10px; color: #333;">Updates</h3>
<p style="margin-bottom: 10px;"><strong>December 27, 2025 12:00:</strong> Improved bar chart clarity by simplifying the net worth breakdown visualizations and adding net worth line overlays to both buying and renting charts. Added detailed tables of raw calculated values for both scenarios, allowing users to explore the month-by-month breakdown of all components.</p>
<p style="margin-bottom: 10px;"><strong>December 27, 2025 09:10:</strong> Fixed property tax calculation. Property taxes now grow at a fixed rate (default 2%) rather than scaling with home value appreciation. This better reflects reality in California where Proposition 19 caps annual property tax increases at 2%, regardless of how much the home's market value increases. The property tax is calculated based on the original assessed value with the annual cap applied. Also fixed the renting scenario monthly contribution calculation to use the current monthly housing cost (which varies over time) instead of the static initial housing cost, providing more accurate investment comparisons. The impact of these changes slightly increases the final net worth for the home buying scenario but has a larger increase in the renting scenario because the amount of money invested every month now increases to reflect the increasing housing costs.</p>
<h3 style="margin-top: 20px; margin-bottom: 10px; color: #333;">TODO</h3>
<ul style="margin-bottom: 10px; padding-left: 30px;">
<li style="margin-bottom: 8px;">Consider investment account taxation. Currently, all investment returns are treated as completely tax-free (like a Roth IRA), which overstates the renting scenario benefits. In reality, taxable brokerage accounts require capital gains tax on appreciation (15-20% federal + state), and traditional 401k/IRA accounts require income tax on the entire withdrawal (contributions + gains). Add options to model different account types: taxable brokerage, Roth IRA, or traditional 401k/IRA.</li>
<li style="margin-bottom: 8px;">Add tax write-offs for home ownership, including mortgage interest deduction and property tax deduction (SALT). These can be significant, especially in the early years of the mortgage when interest payments are highest. Note that the Tax Cuts and Jobs Act (2017) capped SALT deductions at $10,000 and increased the standard deduction, making itemization less common.</li>
</ul>
<h3 style="margin-top: 20px; margin-bottom: 10px; color: #333;">Other Calculators</h3>
<ul style="margin-bottom: 10px; padding-left: 30px;">
<li style="margin-bottom: 8px;"><a href="https://research-tools.pwlcapital.com/research/rent-vs-buy" target="_blank" rel="noopener noreferrer">PWL Capital Rent vs. Buy Calculator</a></li>
</ul>
</div>
<div class="content">
<div class="inputs">
<form id="calculator-form">
<!-- Home Buying Section -->
<div class="input-section">
<h2 class="section-header">🏠 Home Buying</h2>
<div class="section-content">
<div class="input-group">
<label for="home_price">Home Price ($)</label>
<div class="input-description">The purchase price of the home you're considering</div>
<input type="number" id="home_price" name="home_price" value="1000000" required>
</div>
<div class="input-group">
<label for="down_payment_percentage">Down Payment (%)</label>
<div class="input-description">Percentage of home price paid upfront (typically 10-20%)</div>
<input type="number" id="down_payment_percentage" name="down_payment_percentage" value="20" step="0.1" required>
</div>
<div class="input-group">
<label for="loan_interest_rate">Loan APY (%)</label>
<div class="input-description">Annual interest rate on the mortgage loan</div>
<input type="number" id="loan_interest_rate" name="loan_interest_rate" value="6.5" step="0.01" required>
</div>
<div class="input-group">
<label for="mortgage_term_years">Loan Term (years)</label>
<div class="input-description">Length of the loan in years (commonly 15 or 30 years)</div>
<input type="number" id="mortgage_term_years" name="mortgage_term_years" value="30" required>
</div>
<div class="input-group">
<label for="property_tax_rate">Property Tax Rate (%)</label>
<div class="input-description">Annual property tax as a percentage of home value (CA average ~1%)</div>
<input type="number" id="property_tax_rate" name="property_tax_rate" value="1.02373" step="0.01" required>
</div>
<div class="input-group">
<label for="property_tax_growth_rate">Property Tax Growth Rate (%)</label>
<div class="input-description">Annual increase in property tax (CA Prop 19 caps this at 2% per year)</div>
<input type="number" id="property_tax_growth_rate" name="property_tax_growth_rate" value="2.0" step="0.1" disabled required>
</div>
<div class="input-group">
<label for="property_insurance_rate">Insurance Rate (%)</label>
<div class="input-description">Annual insurance premium as percentage of home value (typically 0.3-0.5%)</div>
<input type="number" id="property_insurance_rate" name="property_insurance_rate" value="0.35" step="0.01" required>
</div>
<div class="input-group">
<label for="monthly_hoa_fees">Monthly HOA Fees ($)</label>
<div class="input-description">Monthly homeowners association fees (if applicable)</div>
<input type="number" id="monthly_hoa_fees" name="monthly_hoa_fees" value="400" required>
</div>
<div class="input-group">
<label for="annual_hoa_increase">HOA Annual Increase (%)</label>
<div class="input-description">Expected annual percentage increase in HOA fees</div>
<input type="number" id="annual_hoa_increase" name="annual_hoa_increase" value="3.0" step="0.1" required>
</div>
<div class="input-group">
<label for="annual_maintenance_rate">Annual Maintenance Rate (%)</label>
<div class="input-description">Annual maintenance and repair costs as percentage of home value (typically 1-2%)</div>
<input type="number" id="annual_maintenance_rate" name="annual_maintenance_rate" value="1.0" step="0.1" required>
</div>
<div class="input-group">
<label for="annual_home_value_growth_rate">Value Growth Rate (%)</label>
<div class="input-description">Expected annual appreciation (used if historical data not selected)</div>
<input type="number" id="annual_home_value_growth_rate" name="annual_home_value_growth_rate" value="5.0" step="0.1" required>
</div>
<div class="checkbox-group">
<label>
<input type="checkbox" id="use_san_jose_hpi" name="use_san_jose_hpi">
Use Historical San Jose HPI Data
</label>
<div class="checkbox-info">
Use actual San Jose-Sunnyvale-Santa Clara House Price Index data (1991-2025) instead of flat growth rate for home appreciation
</div>
</div>
</div>
</div>
<!-- Renting Section -->
<div class="input-section">
<h2 class="section-header">🏢 Renting</h2>
<div class="section-content">
<div class="input-group">
<label for="monthly_rent">Monthly Rent ($)</label>
<div class="input-description">Monthly rent payment in the renting scenario</div>
<input type="number" id="monthly_rent" name="monthly_rent" value="2500" required>
</div>
<div class="input-group">
<label for="annual_rent_increase">Annual Rent Increase (%)</label>
<div class="input-description">Expected yearly increase in rent</div>
<input type="number" id="annual_rent_increase" name="annual_rent_increase" value="6.0" step="0.1" required>
</div>
</div>
</div>
<!-- Analysis Section -->
<div class="input-section">
<h2 class="section-header">📊 Analysis</h2>
<div class="section-content">
<div class="input-group">
<label for="first_year">First Year</label>
<div class="input-description">The year when the house is purchased (determines which historical data to use)</div>
<input type="number" id="first_year" name="first_year" value="1995" min="1971" max="2024" required>
</div>
<div class="input-group">
<label for="analysis_length">Analysis Length (years)</label>
<div class="input-description">How many years to run the analysis (e.g., 10 years means analyze from First Year to First Year + 10)</div>
<input type="number" id="analysis_length" name="analysis_length" value="30" min="1" max="50" required>
</div>
<div class="input-group">
<label for="market_annual_return_rate">Market Annual Return (%)</label>
<div class="input-description">Expected annual return on investments (used if historical data not selected)</div>
<input type="number" id="market_annual_return_rate" name="market_annual_return_rate" value="7.0" step="0.1" required>
</div>
<div class="checkbox-group">
<label>
<input type="checkbox" id="use_nasdaq_data" name="use_nasdaq_data">
Use Historical NASDAQ Data
</label>
<div class="checkbox-info">
Use actual NASDAQ Composite historical returns (1971-2025) instead of flat market return for investment growth
</div>
</div>
</div>
</div>
<div class="button-group">
<button type="button" class="reset-button" id="reset-button">Reset to Defaults</button>
<button type="submit">Calculate</button>
</div>
</form>
</div>
<div class="results-panel">
<div class="loading" id="loading">
<p>Calculating...</p>
</div>
<div class="error" id="error">
<p id="error-message"></p>
</div>
<div class="results" id="results">
<table class="summary-table">
<thead>
<tr>
<th>Metric</th>
<th>Value</th>
</tr>
</thead>
<tbody>
<tr>
<td class="label-cell">Monthly Mortgage Payment</td>
<td class="value-cell" id="mortgage_payment">-</td>
</tr>
<tr>
<td class="label-cell">Total Monthly Housing Cost</td>
<td class="value-cell" id="total_monthly_housing_cost">-</td>
</tr>
<tr>
<td class="label-cell">Down Payment</td>
<td class="value-cell" id="down_payment">-</td>
</tr>
<tr>
<td class="label-cell">Final Home Net Worth (Buying)</td>
<td class="value-cell" id="final_home_net_worth">-</td>
</tr>
<tr>
<td class="label-cell">Final Investment Net Worth (Renting)</td>
<td class="value-cell" id="final_renting_net_worth">-</td>
</tr>
</tbody>
</table>
<div class="plot-container">
<h2 class="section-title">Breakdown by Scenario</h2>
<h3>Renting Scenario: Monthly Costs Over Time</h3>
<p style="margin-bottom: 15px; color: #555; line-height: 1.6;">
This chart shows monthly rent (which increases annually) and the monthly amount invested in the market
(the difference between total housing budget and rent).
</p>
<div id="plot8"></div>
</div>
<div class="plot-container">
<div id="plot3"></div>
</div>
<div class="plot-container">
<h3>Buying Scenario: Monthly Costs Over Time</h3>
<p style="margin-bottom: 15px; color: #555; line-height: 1.6;">
This line chart shows the individual monthly payment components over the life of the mortgage.
Notice how <strong style="color: #FF5252;">Interest Payments</strong> (red) decrease over time while
<strong style="color: #4CAF50;">Principal Payments</strong> (green) increase. The other costs remain constant.
</p>
<div id="plot5"></div>
</div>
<div class="plot-container">
<div id="plot4"></div>
</div>
<div class="plot-container">
<h2 class="section-title">Comparison: Net Worth Over Time</h2>
<div id="plot2"></div>
</div>
<div class="plot-container" id="source_data_section" style="display: none;">
<h2 class="section-title" style="margin-top: 30px; margin-bottom: 20px;">Source Data</h2>
<div id="nasdaq_source_container" style="display: none;">
<h3>NASDAQ Composite Index - Analysis Period</h3>
<p style="margin-bottom: 15px; color: #555; line-height: 1.6;">
This chart shows the NASDAQ Composite index values (downsampled to monthly) used to calculate investment returns
for the renting scenario. The index is rebased to 100 at the start of the analysis period.
</p>
<div id="plot6"></div>
</div>
<div id="hpi_source_container" style="display: none; margin-top: 30px;">
<h3>San Jose House Price Index - Analysis Period</h3>
<p style="margin-bottom: 15px; color: #555; line-height: 1.6;">
This chart shows the actual San Jose-Sunnyvale-Santa Clara House Price Index values
used to calculate home appreciation during the analysis period.
</p>
<div id="plot7"></div>
</div>
</div>
<div class="plot-container">
<h2 class="section-title" style="margin-top: 30px; margin-bottom: 20px;">Comparing Scenarios Across Start Date</h2>
<p style="margin-bottom: 15px; color: #555; line-height: 1.6;">
This chart shows how the buying vs. renting decision varies based on market timing.
Each line represents the net worth difference (Buying - Renting) for a simulation starting in a different year
from 1992 through 2024 minus your analysis duration. For example, with a 10-year analysis, simulations run from
1992 to 2014. Positive values mean buying comes out ahead, negative values mean renting is better.
This demonstrates how historical market conditions at different entry points would have affected the outcome.
</p>
<div id="plot9"></div>
<h3 style="margin-top: 30px; margin-bottom: 15px;">Final Net Worth Difference by Start Year</h3>
<div id="comparison-table-container"></div>
</div>
<div class="plot-container">
<h2 class="section-title" style="margin-top: 30px; margin-bottom: 20px;">Detailed Monthly Calculations</h2>
<h3 style="margin-bottom: 15px;">Buying Scenario - All Variables</h3>
<p style="margin-bottom: 15px; color: #555; line-height: 1.6;">
This table shows every variable used in calculating the buying scenario net worth for each month.
</p>
<div style="overflow-x: auto; margin-bottom: 40px;">
<div id="buying-detail-table"></div>
</div>
<h3 style="margin-bottom: 15px;">Renting Scenario - All Variables</h3>
<p style="margin-bottom: 15px; color: #555; line-height: 1.6;">
This table shows every variable used in calculating the renting scenario net worth for each month.
</p>
<div style="overflow-x: auto;">
<div id="renting-detail-table"></div>
</div>
</div>
</div>
</div>
</div>
</div>
<script>
// San Jose HPI Data
const SAN_JOSE_HPI_QUARTERLY = {
"1991-1": 100.00, "1991-2": 99.05, "1991-3": 98.77, "1991-4": 97.81,
"1992-1": 97.80, "1992-2": 97.47, "1992-3": 97.23, "1992-4": 96.02,
"1993-1": 94.66, "1993-2": 94.17, "1993-3": 93.37, "1993-4": 92.90,
"1994-1": 92.46, "1994-2": 92.51, "1994-3": 93.56, "1994-4": 91.85,
"1995-1": 94.06, "1995-2": 93.79, "1995-3": 93.55, "1995-4": 94.49,
"1996-1": 95.11, "1996-2": 96.79, "1996-3": 99.07, "1996-4": 100.50,
"1997-1": 103.75, "1997-2": 108.56, "1997-3": 111.55, "1997-4": 114.02,
"1998-1": 118.96, "1998-2": 125.20, "1998-3": 128.87, "1998-4": 129.99,
"1999-1": 133.59, "1999-2": 141.20, "1999-3": 146.68, "1999-4": 149.21,
"2000-1": 163.44, "2000-2": 174.82, "2000-3": 185.96, "2000-4": 194.86,
"2001-1": 202.81, "2001-2": 196.95, "2001-3": 193.95, "2001-4": 191.16,
"2002-1": 196.82, "2002-2": 204.64, "2002-3": 208.26, "2002-4": 207.90,
"2003-1": 210.69, "2003-2": 212.00, "2003-3": 211.37, "2003-4": 213.84,
"2004-1": 222.91, "2004-2": 235.08, "2004-3": 240.17, "2004-4": 242.12,
"2005-1": 264.52, "2005-2": 267.17, "2005-3": 265.29, "2005-4": 270.26,
"2006-1": 287.01, "2006-2": 299.63, "2006-3": 285.97, "2006-4": 272.08,
"2007-1": 278.58, "2007-2": 286.52, "2007-3": 277.52, "2007-4": 275.76,
"2008-1": 246.30, "2008-2": 236.77, "2008-3": 225.33, "2008-4": 208.81,
"2009-1": 191.53, "2009-2": 199.42, "2009-3": 207.69, "2009-4": 214.65,
"2010-1": 205.01, "2010-2": 214.72, "2010-3": 210.00, "2010-4": 203.55,
"2011-1": 206.88, "2011-2": 208.29, "2011-3": 206.67, "2011-4": 205.84,
"2012-1": 205.66, "2012-2": 219.73, "2012-3": 226.62, "2012-4": 234.03,
"2013-1": 242.52, "2013-2": 264.85, "2013-3": 267.63, "2013-4": 268.22,
"2014-1": 275.63, "2014-2": 280.37, "2014-3": 295.65, "2014-4": 292.96,
"2015-1": 304.35, "2015-2": 318.17, "2015-3": 321.76, "2015-4": 323.74,
"2016-1": 325.11, "2016-2": 351.25, "2016-3": 345.77, "2016-4": 335.84,
"2017-1": 354.08, "2017-2": 366.80, "2017-3": 369.90, "2017-4": 375.47,
"2018-1": 388.51, "2018-2": 407.30, "2018-3": 403.72, "2018-4": 394.59,
"2019-1": 404.93, "2019-2": 413.56, "2019-3": 405.67, "2019-4": 407.78,
"2020-1": 427.95, "2020-2": 422.58, "2020-3": 437.46, "2020-4": 451.35,
"2021-1": 469.26, "2021-2": 496.39, "2021-3": 513.43, "2021-4": 520.03,
"2022-1": 562.52, "2022-2": 571.90, "2022-3": 502.40, "2022-4": 506.22,
"2023-1": 523.24, "2023-2": 556.42, "2023-3": 537.52, "2023-4": 539.43,
"2024-1": 548.98, "2024-2": 573.69, "2024-3": 561.91, "2024-4": 563.32,
"2025-1": 572.25, "2025-2": 563.88, "2025-3": 541.15
};
// NASDAQ Data will be loaded from CSV
let nasdaqData = null;
// Load NASDAQ CSV data
async function loadNASDAQData() {
try {
const response = await fetch('NASDAQCOM.csv');
const text = await response.text();
const lines = text.split('\n');
const data = {};
for (let i = 1; i < lines.length; i++) {
const line = lines[i].trim();
if (!line) continue;
const parts = line.split(',');
if (parts.length >= 2 && parts[1]) {
data[parts[0]] = parseFloat(parts[1]);
}
}
nasdaqData = data;
} catch (error) {
console.warn('Could not load NASDAQ data:', error);
nasdaqData = {};
}
}
// Get monthly NASDAQ returns
function getNASDAQReturns(months, endDate) {
if (!nasdaqData || Object.keys(nasdaqData).length === 0) {
return Array(months).fill(0.10 / 12); // Fallback to 10% annual
}
// Convert to monthly end-of-month values
const monthlyValues = {};
const dates = Object.keys(nasdaqData).sort();
let currentMonth = null;
let currentValue = null;
for (const dateStr of dates) {
const date = new Date(dateStr);
const monthKey = `${date.getFullYear()}-${date.getMonth() + 1}`;
if (currentMonth !== monthKey) {
if (currentMonth && currentValue) {
monthlyValues[currentMonth] = currentValue;
}
currentMonth = monthKey;
}
currentValue = nasdaqData[dateStr];
}
if (currentMonth && currentValue) {
monthlyValues[currentMonth] = currentValue;
}
// Get sorted month keys
const sortedMonths = Object.keys(monthlyValues).sort();
// Find end month
const endDateObj = new Date(endDate);
const endMonthKey = `${endDateObj.getFullYear()}-${endDateObj.getMonth() + 1}`;
let endIndex = sortedMonths.length - 1;
for (let i = sortedMonths.length - 1; i >= 0; i--) {
if (sortedMonths[i] <= endMonthKey) {
endIndex = i;
break;
}
}
const startIndex = Math.max(0, endIndex - months + 1);
// Calculate returns
const returns = [];
for (let i = startIndex; i <= endIndex; i++) {
if (i === 0) {
returns.push(0.0);
} else {
const prevValue = monthlyValues[sortedMonths[i - 1]];
const currValue = monthlyValues[sortedMonths[i]];
const monthlyReturn = (currValue - prevValue) / prevValue;
returns.push(monthlyReturn);
}
}
// Pad with average if needed
if (returns.length < months) {
const avgReturn = returns.reduce((a, b) => a + b, 0) / returns.length || 0;
while (returns.length < months) {
returns.unshift(avgReturn);
}
}
return returns.slice(0, months);
}
// Calculate mortgage payment
function calculateMortgagePayment(principal, annualRate, years) {
const monthlyRate = annualRate / 12;
const numPayments = years * 12;
if (monthlyRate === 0) {
return principal / numPayments;
}
return principal * (monthlyRate * Math.pow(1 + monthlyRate, numPayments)) /
(Math.pow(1 + monthlyRate, numPayments) - 1);
}
// Make payment breakdown
function makePayment(paymentAmount, interestRate, principalRemaining) {
const interestPayment = principalRemaining * interestRate;
const principalPayment = paymentAmount - interestPayment;
return {
interest: interestPayment,
principal: principalPayment,
newPrincipal: principalRemaining - principalPayment
};
}
// Main calculation function
async function calculate(formData) {
// Parse inputs
const homePrice = parseFloat(formData.get('home_price'));
const downPaymentPercentage = parseFloat(formData.get('down_payment_percentage')) / 100;
const loanInterestRate = parseFloat(formData.get('loan_interest_rate')) / 100;
const mortgageTermYears = parseInt(formData.get('mortgage_term_years'));
const firstYear = parseInt(formData.get('first_year'));
const analysisLength = parseInt(formData.get('analysis_length'));
const propertyTaxRate = parseFloat(formData.get('property_tax_rate')) / 100;
const propertyTaxGrowthRate = parseFloat(formData.get('property_tax_growth_rate') || document.getElementById('property_tax_growth_rate').value) / 100;
const propertyInsuranceRate = parseFloat(formData.get('property_insurance_rate')) / 100;
const monthlyHoaFees = parseFloat(formData.get('monthly_hoa_fees'));
const annualHoaIncrease = parseFloat(formData.get('annual_hoa_increase')) / 100;
const annualMaintenanceRate = parseFloat(formData.get('annual_maintenance_rate')) / 100;
const annualHomeValueGrowthRate = parseFloat(formData.get('annual_home_value_growth_rate')) / 100;
const useSanJoseHPI = formData.get('use_san_jose_hpi') === 'on';
const monthlyRent = parseFloat(formData.get('monthly_rent'));
const annualRentIncrease = parseFloat(formData.get('annual_rent_increase')) / 100;
const marketAnnualReturnRate = parseFloat(formData.get('market_annual_return_rate')) / 100;
const useNasdaqData = formData.get('use_nasdaq_data') === 'on';
// Calculate home values over time
let homeValues = [];
if (useSanJoseHPI) {
// Get sorted quarters
const sortedQuarters = Object.keys(SAN_JOSE_HPI_QUARTERLY).sort();
const startQuarter = `${firstYear}-1`;
const endQuarter = `${firstYear + analysisLength - 1}-4`;
let startIndex = sortedQuarters.findIndex(q => q >= startQuarter);
if (startIndex === -1) startIndex = 0;
let endIndex = sortedQuarters.length - 1;
for (let i = sortedQuarters.length - 1; i >= 0; i--) {
if (sortedQuarters[i] <= endQuarter) {
endIndex = i;
break;
}
}
const hpiValues = sortedQuarters.slice(startIndex, endIndex + 1)
.map(q => SAN_JOSE_HPI_QUARTERLY[q]);
// Pad if needed
const totalQuarters = analysisLength * 4;
if (hpiValues.length < totalQuarters) {
const avgGrowth = hpiValues.length > 1 ?
(hpiValues[hpiValues.length - 1] / hpiValues[0] - 1) / (hpiValues.length - 1) : 0;
while (hpiValues.length < totalQuarters) {
hpiValues.push(hpiValues[hpiValues.length - 1] * (1 + avgGrowth));
}
}
const baseHPI = hpiValues[0];
for (let m = 0; m < analysisLength * 12; m++) {
const quarterIndex = Math.min(Math.floor(m / 3), hpiValues.length - 1);
const monthInQuarter = m % 3;
let interpolatedHPI = hpiValues[quarterIndex];
if (quarterIndex + 1 < hpiValues.length) {
const nextHPI = hpiValues[quarterIndex + 1];
interpolatedHPI = hpiValues[quarterIndex] +
(nextHPI - hpiValues[quarterIndex]) * (monthInQuarter / 3);
}
// console.log(`Month ${m}: HPI=${interpolatedHPI}; BaseHPI=${baseHPI}; home price=${homePrice}`);
homeValues.push(homePrice * (interpolatedHPI / baseHPI));
}
} else {
for (let m = 0; m < analysisLength * 12; m++) {
homeValues.push(homePrice * Math.pow(1 + annualHomeValueGrowthRate, m / 12));
}
}
// Load NASDAQ data if needed
let nasdaqMonthlyReturns = [];
if (useNasdaqData) {
const endYear = firstYear + analysisLength;
const endDate = `${endYear - 1}-12-31`;
nasdaqMonthlyReturns = getNASDAQReturns(analysisLength * 12, endDate);
}
// Buying scenario calculations
const principal = homePrice * (1 - downPaymentPercentage);
const monthlyRate = loanInterestRate / 12;
const mortgagePayment = calculateMortgagePayment(principal, loanInterestRate, mortgageTermYears);
const monthlyPropertyTax = (homePrice * propertyTaxRate) / 12;
const monthlyPropertyInsurance = (homePrice * propertyInsuranceRate) / 12;
const monthlyMaintenance = (homePrice * annualMaintenanceRate) / 12;
const totalMonthlyHousingCost = mortgagePayment + monthlyPropertyTax +
monthlyPropertyInsurance + monthlyHoaFees + monthlyMaintenance;
// Track buying scenario over time
const remainingPrincipals = [];
const cumulativeInterest = [];
const cumulativePropertyTax = [];
const cumulativeInsurance = [];
const cumulativeHOA = [];
const cumulativeMaintenance = [];
const monthlyInterestPayments = [];
const monthlyPrincipalPayments = [];
const monthlyPropertyTaxList = [];
const monthlyInsuranceList = [];
const monthlyHOAList = [];
const monthlyMaintenanceList = [];
const monthlyHousingCosts = [];
const buyingInvestmentReturns = [];
const buyingContributions = [];
const monthlyRents = [];
let currentPrincipal = principal;
let totalInterest = 0;
let totalPropertyTax = 0;
let totalInsurance = 0;
let totalHOA = 0;
let totalMaintenance = 0;
let currentHOA = monthlyHoaFees;
let buyingInvestmentNetWorth = 0;
let totalBuyingInvestmentReturns = 0;
let totalBuyingContributions = 0;
let currentRent = monthlyRent;
let annualPropertyTax = homePrice * propertyTaxRate;
for (let m = 0; m < analysisLength * 12; m++) {
// Annual increases
if (m > 0 && m % 12 === 0) {
currentHOA *= (1 + annualHoaIncrease);
currentRent *= (1 + annualRentIncrease);
annualPropertyTax *= (1 + propertyTaxGrowthRate);
}
monthlyRents.push(currentRent);
// Dynamic insurance and maintenance based on home value
const currentInsurance = (homeValues[m] * propertyInsuranceRate) / 12;
const currentMaintenance = (homeValues[m] * annualMaintenanceRate) / 12;
const currentTax = annualPropertyTax / 12;
// Mortgage payment
let interestPayment = 0;
let principalPayment = 0;
if (m < mortgageTermYears * 12) {
const payment = makePayment(mortgagePayment, monthlyRate, currentPrincipal);
interestPayment = payment.interest;
principalPayment = payment.principal;
currentPrincipal = payment.newPrincipal;
}
totalInterest += interestPayment;
totalPropertyTax += currentTax;
totalInsurance += currentInsurance;
totalHOA += currentHOA;
totalMaintenance += currentMaintenance;
// Calculate current month's total housing cost
const currentMonthlyHousingCost = interestPayment + principalPayment +
currentTax + currentInsurance + currentHOA + currentMaintenance;
// If rent exceeds housing cost, invest the difference in buying scenario
const buyingMonthlyContribution = Math.max(0, currentRent - currentMonthlyHousingCost);
const monthlyGrowthRate = useNasdaqData ?
nasdaqMonthlyReturns[m] : marketAnnualReturnRate / 12;
const buyingReturnsThisMonth = buyingInvestmentNetWorth * monthlyGrowthRate;
totalBuyingInvestmentReturns += buyingReturnsThisMonth;
totalBuyingContributions += buyingMonthlyContribution;
buyingInvestmentNetWorth = buyingInvestmentNetWorth * (1 + monthlyGrowthRate) + buyingMonthlyContribution;
remainingPrincipals.push(currentPrincipal);
cumulativeInterest.push(totalInterest);
cumulativePropertyTax.push(totalPropertyTax);
cumulativeInsurance.push(totalInsurance);
cumulativeHOA.push(totalHOA);
cumulativeMaintenance.push(totalMaintenance);
monthlyInterestPayments.push(interestPayment);
monthlyPrincipalPayments.push(principalPayment);
monthlyPropertyTaxList.push(currentTax);
monthlyInsuranceList.push(currentInsurance);
monthlyHOAList.push(currentHOA);
monthlyMaintenanceList.push(currentMaintenance);
monthlyHousingCosts.push(currentMonthlyHousingCost);
buyingInvestmentReturns.push(totalBuyingInvestmentReturns);
buyingContributions.push(totalBuyingContributions);
}
const homeAppreciation = homeValues.map(v => v - homePrice);
const homeNetWorths = homeValues.map((v, i) => v - remainingPrincipals[i] + buyingInvestmentReturns[i] + buyingContributions[i]);
// Renting scenario
const netWorths = [];
const contributions = [];
const investmentReturns = [];
const cumulativeRent = [];
const monthlyContributions = [];