|
8 | 8 | from __future__ import annotations
|
9 | 9 |
|
10 | 10 | from typing import Dict, Generator, Tuple, Optional
|
| 11 | +from datetime import date, timedelta |
11 | 12 |
|
12 | 13 | import numpy as np # type: ignore
|
13 | 14 | import pandas as pd # type: ignore
|
@@ -98,16 +99,16 @@ def __init__(self, p: Parameters):
|
98 | 99 | self.census_df = census_df
|
99 | 100 |
|
100 | 101 | #if p.n_days_since_first_hospitalized is None:
|
101 |
| - # continue |
102 |
| - |
| 102 | + # p.n_days_since_first_hospitalized = int(self.argmin_ds(p)) |
| 103 | + # p.date_first_hospitalized = date.today() - timedelta(days=p.n_days_since_first_hospitalized) |
103 | 104 | if p.n_days_since_first_hospitalized is not None and p.doubling_time is None:
|
104 | 105 | # optimize doubling_time
|
105 | 106 | argmin_dt = None
|
106 | 107 | min_loss = 2.0**99
|
107 | 108 | censes = dict()
|
108 | 109 | current_infecteds = dict()
|
109 | 110 | for dt in np.linspace(1,15,29):
|
110 |
| - censes[dt], current_infecteds[dt] = self.run_projection(p, dt) |
| 111 | + censes[dt], current_infecteds[dt] = self.run_projection_dt(p, dt) |
111 | 112 | # self.current_infected = current_infecteds[dt] # log it into state for no reason really
|
112 | 113 | self.census_df = censes[dt] # log it into state for loss
|
113 | 114 | loss_dt = self.loss_dt(p)
|
@@ -156,7 +157,7 @@ def __init__(self, p: Parameters):
|
156 | 157 |
|
157 | 158 | return None
|
158 | 159 |
|
159 |
| - def run_projection(self, p: Parameters, doubling_time: float) -> Tuple[pd.DataFrame, float]: |
| 160 | + def run_projection_dt(self, p: Parameters, doubling_time: float) -> Tuple[pd.DataFrame, float]: |
160 | 161 | intrinsic_growth_rate = self._intrinsic_growth_rate(doubling_time)
|
161 | 162 |
|
162 | 163 | recovery_days = p.recovery_days
|
@@ -207,8 +208,18 @@ def loss_dt(self, p: Parameters) -> float:
|
207 | 208 | return (p.current_hospitalized - predicted_current_hospitalized) ** 2
|
208 | 209 |
|
209 | 210 | def loss_ds(self, p: Parameters) -> float:
|
210 |
| - pass |
211 |
| - |
| 211 | + """loss with respect to days_since """ |
| 212 | + |
| 213 | + ## get the predicted number hospitalized today |
| 214 | + pred_current_hospitalized = self.census_df['hospitalized'].loc[p.n_days_since_first_hospitalized] |
| 215 | + |
| 216 | + ## compare against the actual (user inputed) number |
| 217 | + ## squared difference is the loss to be optimized |
| 218 | + return (p.current_hospitalized - pred_current_hospitalized)**2 |
| 219 | + |
| 220 | + def argmin_ds(self, p: Parameters) -> float: |
| 221 | + losses_df = (self.census_df.hospitalized - p.current_hospitalized) ** 2 |
| 222 | + return losses_df.argmin() |
212 | 223 |
|
213 | 224 | @staticmethod
|
214 | 225 | def _intrinsic_growth_rate(doubling_time: Optional[float]) -> float:
|
@@ -264,10 +275,11 @@ def sim_sir_df(
|
264 | 275 | s: float, i: float, r: float, beta: float, gamma: float, n_days: int
|
265 | 276 | ) -> pd.DataFrame:
|
266 | 277 | """Simulate the SIR model forward in time."""
|
267 |
| - return pd.DataFrame( |
| 278 | + dat = pd.DataFrame( |
268 | 279 | data=gen_sir(s, i, r, beta, gamma, n_days),
|
269 | 280 | columns=("day", "susceptible", "infected", "recovered"),
|
270 | 281 | )
|
| 282 | + return dat |
271 | 283 |
|
272 | 284 |
|
273 | 285 | def get_dispositions(
|
|
0 commit comments