fivedollarwitch commited on
Commit
48c91b1
·
verified ·
1 Parent(s): 07d3427

Create StatBaseModel.py

Browse files
Files changed (1) hide show
  1. StatBaseModel.py +120 -0
StatBaseModel.py ADDED
@@ -0,0 +1,120 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Tuple, TypedDict, Optional
2
+ import datetime
3
+
4
+
5
+ class ProcessedSynapse(TypedDict):
6
+ id: Optional[str]
7
+ property_id: Optional[str]
8
+ listing_id: Optional[str]
9
+ address: Optional[str]
10
+ city: Optional[str]
11
+ state: Optional[str]
12
+ zip: Optional[str]
13
+ price: Optional[float]
14
+ beds: Optional[int]
15
+ baths: Optional[float]
16
+ sqft: Optional[int]
17
+ lot_size: Optional[int]
18
+ year_built: Optional[int]
19
+ days_on_market: Optional[int]
20
+ latitude: Optional[float]
21
+ longitude: Optional[float]
22
+ property_type: Optional[str]
23
+ last_sale_date: Optional[str]
24
+ hoa_dues: Optional[float]
25
+ query_date: Optional[str]
26
+ market: Optional[str]
27
+
28
+
29
+ class StatBaseModel:
30
+
31
+ def __init__(self):
32
+ self._load_model()
33
+
34
+ def _load_model(self):
35
+ """
36
+ Perform any actions needed to load the model.
37
+ EX: Establish API connections, download an ML model for inference, etc...
38
+ """
39
+ print("Loading model...")
40
+ # Optional model loading
41
+ print("Model loaded.")
42
+
43
+ def _get_average_for_market(self, market: str) -> int:
44
+ """
45
+ Get the average days on market for a house in a given market
46
+ :param market: the housing market
47
+ :return: the average days on market
48
+ """
49
+ # You probably want to update this based on the current season. Houses sell faster in the summer.
50
+ if market == 'San Francisco':
51
+ return 23
52
+ elif market == 'Los Angeles':
53
+ return 68
54
+ elif market == 'Seattle':
55
+ return 27
56
+ elif market == 'Austin':
57
+ return 78
58
+ elif market == 'Houston':
59
+ return 73
60
+ elif market == 'Chicago':
61
+ return 25
62
+
63
+
64
+ def _sale_date_predictor(self, input_data: ProcessedSynapse):
65
+ """
66
+ Calculate the expected sale date based on the national average
67
+ :param days_on_market: number of days this house has been on the market
68
+ :return: the predicted sale date, based on the national average of 34 days
69
+ """
70
+ if 'days_on_market' not in input_data:
71
+ return datetime.date.today() + datetime.timedelta(days=1)
72
+
73
+ if 'market' not in input_data:
74
+ average = 34
75
+
76
+ else:
77
+ average = self._get_average_for_market(input_data['market'])
78
+
79
+ days_on_market = input_data['days_on_market']
80
+ if days_on_market < average:
81
+ days_until_sale = average - days_on_market
82
+ sale_date = datetime.date.today() + datetime.timedelta(days=days_until_sale)
83
+ return sale_date
84
+ else:
85
+ return datetime.date.today() + datetime.timedelta(days=1)
86
+
87
+ def _get_price_multiplier(self, market: str) -> float:
88
+ """
89
+ Calculate the price multiplier based on the market
90
+ :param market: the marked the house is in
91
+ :return: the multiplier for the predicted price
92
+ """
93
+ # You may want to add more logic to check zipcode for more precise price multipliers
94
+ if market == 'San Francisco':
95
+ return 1.18 # 20% above listing
96
+ elif market == 'Los Angeles':
97
+ return 1.2 # 22% above listing
98
+ elif market == 'Seattle':
99
+ return 1.13 # 13% above listing
100
+ elif market == 'Austin':
101
+ return 1.11 # 11% above listing
102
+ elif market == 'Houston':
103
+ return 1.15 # 15% above listing
104
+ elif market == 'Chicago':
105
+ return 1.12 # 12% above listing
106
+
107
+ def run_inference(self, input_data: ProcessedSynapse) -> Tuple[float, str]:
108
+ """
109
+ Predict the sale price and sale date for the house represented by `input_data`
110
+ :param input_data: a formatted Synapse from the validator, representing a currently listed house
111
+ :return: the predicted sale price and predicted sale date for this home
112
+ """
113
+ listing_price = float(input_data['price']) if 'price' in input_data else 1.0
114
+ sale_multiplier = self._get_price_multiplier(input_data['market']) if 'market' in input_data else 1.0
115
+ predicted_sale_price = listing_price * sale_multiplier
116
+ predicted_sale_date = self._sale_date_predictor(input_data)
117
+ predicted_sale_date = predicted_sale_date.strftime("%Y-%m-%d")
118
+ print(f"Predicted sale price: {predicted_sale_price}")
119
+ print(f"Predicted sale date: {predicted_sale_date}")
120
+ return predicted_sale_price, predicted_sale_date