Update StatisticalBaseModel.py
Browse files- StatisticalBaseModel.py +60 -7
StatisticalBaseModel.py
CHANGED
|
@@ -23,6 +23,7 @@ class ProcessedSynapse(TypedDict):
|
|
| 23 |
last_sale_date: Optional[str]
|
| 24 |
hoa_dues: Optional[float]
|
| 25 |
query_date: Optional[str]
|
|
|
|
| 26 |
|
| 27 |
|
| 28 |
class StatisticalBaseModel:
|
|
@@ -39,29 +40,81 @@ class StatisticalBaseModel:
|
|
| 39 |
# Optional model loading
|
| 40 |
print("Model loaded.")
|
| 41 |
|
| 42 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
Calculate the expected sale date based on the national average
|
| 45 |
:param days_on_market: number of days this house has been on the market
|
| 46 |
:return: the predicted sale date, based on the national average of 34 days
|
| 47 |
"""
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
sale_date = datetime.date.today() + datetime.timedelta(days=days_until_sale)
|
| 52 |
return sale_date
|
| 53 |
else:
|
| 54 |
return datetime.date.today() + datetime.timedelta(days=1)
|
| 55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
def run_inference(self, input_data: ProcessedSynapse) -> Tuple[float, str]:
|
| 57 |
"""
|
| 58 |
Predict the sale price and sale date for the house represented by `input_data`
|
| 59 |
:param input_data: a formatted Synapse from the validator, representing a currently listed house
|
| 60 |
:return: the predicted sale price and predicted sale date for this home
|
| 61 |
"""
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
| 64 |
predicted_sale_date = predicted_sale_date.strftime("%Y-%m-%d")
|
| 65 |
print(f"Predicted sale price: {predicted_sale_price}")
|
| 66 |
print(f"Predicted sale date: {predicted_sale_date}")
|
| 67 |
-
return predicted_sale_price, predicted_sale_date
|
|
|
|
| 23 |
last_sale_date: Optional[str]
|
| 24 |
hoa_dues: Optional[float]
|
| 25 |
query_date: Optional[str]
|
| 26 |
+
market: Optional[str]
|
| 27 |
|
| 28 |
|
| 29 |
class StatisticalBaseModel:
|
|
|
|
| 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
|