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