diff --git a/docs/notebooks/3e_Predict_demand_from_patients_yet_to_arrive.md b/docs/notebooks/3e_Predict_demand_from_patients_yet_to_arrive.md index 5db7458c..bdef2cdf 100644 --- a/docs/notebooks/3e_Predict_demand_from_patients_yet_to_arrive.md +++ b/docs/notebooks/3e_Predict_demand_from_patients_yet_to_arrive.md @@ -14,7 +14,7 @@ In this notebook, I'll use the example of predicting the number of beds needed f - a weighted Poisson model using an empirical survival curve; arrival rates of patients who were admitted (at some point), are weighted by their probability of being admitted within a prediction window, calculated from a survival curve learned from past data - a weighted Poisson model using an aspirational approach; instead of using a survival curve learned from past data, it is assumed that the ED is meeting 4-hour targets for time to admission. -I also demonstrate making predictions by specialty for demand from incoming patients. +I also demonstrate making predictions by specialty for demand from incoming patients, and how to stratify the predictions to take day of week into account. ```python # Reload functions every time @@ -513,7 +513,7 @@ yta_model_empirical.fit(train_visits_copy, ``` Calculating time-varying arrival rates for data provided, which spans 45 unique dates - time interval of 0:15:00 within the prediction window. + Time interval of 0:15:00 used to bucket arrival rates. The error value for prediction will be 1e-07 To see the weights saved by this model, use the get_weights() method EmpiricalIncomingAdmissionPredictor has been fitted with survival curve containing 881 time points @@ -1070,27 +1070,29 @@ print( ) ``` - The calculated arrival rates for the first 10 discrete time intervals for the 23:00 prediction time are: [0.089, 0.044, 0.044, 0.089, 0.044, 0.022, 0.0, 0.0, 0.022, 0.0] + The calculated arrival rates for the first 10 discrete time intervals for the 12:00 prediction time are: [1.289, 1.067, 1.356, 1.2, 1.289, 1.356, 1.2, 1.178, 0.933, 0.889] #### Generate a prediction -Having inspected the inputs the model holds, we can now call `predict()` to produce a probability distribution for the number of patients yet to arrive who will need a bed within the prediction window. +We can now call `predict()` to get a distribution for how many yet-to-arrive patients are expected to need a bed within the prediction window. + +Pass **`prediction_time`** as the time of day to start from, as an `(hour, minute)` tuple. Pass **`prediction_window`** at predict time (not fit time), so the same fitted model can be used for different horizons. -The `predict()` method takes two things: +If the model was fitted **without** filters, there is only one key in **`weights`** (e.g. `unfiltered`), so **`filter_keys`** can be left out. If you fitted **with** filters, pass **`filter_keys`** as a single hospital **service** name or a list of names — the same names you used as keys in **`filters`**. Every service you ask for in one call shares the **same** **`prediction_time`**. -- A `prediction_context` dictionary keyed by filter. Because we fitted an unfiltered model, there is only one entry, `'unfiltered'`, whose value tells the model the time of day to predict from, given as an `(hour, minute)` tuple. -- A `prediction_window` keyword argument. This is now supplied at predict time rather than fit time, so a single trained model can be reused across different windows without retraining. +The result is still a dict whose keys match **`weights`** (e.g. service name); each value is the weighted Poisson distribution for that service. -The return value is a dictionary (keyed by filter) of weighted Poisson distributions representing the number of patients yet to arrive who are expected to need admission within the requested window. +**Note:** the old nested **`prediction_context`** dict (`{service: {"prediction_time": ...}}`, as the first argument or as `prediction_context=...`) still works but triggers a **`DeprecationWarning`** and will be removed in a later version. Prefer **`prediction_time=`** and **`filter_keys=`** when you can. ```python -prediction_context = { - 'unfiltered': { - 'prediction_time': tuple([12,0]) - } -} +# # deprecated code retained for reference +# prediction_context = { +# 'unfiltered': { +# 'prediction_time': tuple([12,0]) +# } +# } weighted_poisson_empirical = yta_model_empirical.predict( - prediction_context, + prediction_time=(12,0), prediction_window=timedelta(hours=8) # now passed at predict() method ) @@ -1148,7 +1150,7 @@ yta_model_by_spec_empirical.fit(train_visits_copy, To see the weights saved by this model, use the get_weights() method EmpiricalIncomingAdmissionPredictor has been fitted with survival curve containing 881 time points -
EmpiricalIncomingAdmissionPredictor(filters={'haem/onc': {'specialty': 'haem/onc'},
+
EmpiricalIncomingAdmissionPredictor(filters={'haem/onc': {'specialty': 'haem/onc'},
 
                                              'medical': {'specialty': 'medical'},
                                              'paediatric': {'specialty': 'paediatric'},
                                              'surgical': {'specialty': 'surgical'}},
-                                    verbose=True)
In a Jupyter environment, please rerun this cell to show the HTML representation or trust the notebook.
On GitHub, the HTML representation is unable to render, please try loading this page with nbviewer.org.