Conversation
replace flat attribute structure in SubspecialtyPredictionInputs with flexible approach using inflows/outflows dictionaries Key changes: - Add FlowInputs dataclass to represent individual patient flows - Organize flows by direction (inflows: arrivals, outflows: departures) - Update build_subspecialty_data() to construct new structure - Add FlowSelection for customisable flow inclusion - Add PredictionBundle to track arrivals, departures, and net flow - Update DemandPredictor.predict_subspecialty() to return bundle - Add predict_flow_total() method for flow-agnostic aggregation - Update compute_transfer_arrivals() to handle new structure
- Change cache and return types from DemandPrediction to PredictionBundle - Add flow_selection parameter to predict_all_levels() - Aggregate arrivals and departures separately at each hierarchical level - Compute net flow as difference of expectations at each level - Update docstrings to reflect bundle-based predictions
- Add offset tracking to DemandPrediction for negative support - Compute net flow as full probability distribution (not just expected value) - Update _expected_value() and _percentiles() to handle offset - Change PredictionBundle.net_flow from float to DemandPrediction - Apply net flow PMF computation at all hierarchy levels - Replace FlowSelection.admissions_only() with incoming_flow() and outgoing_flow() - Add test_hierarchy.py
- cap departure pmf at physical max - check that departure input type is always pmf and never poisson - return pmf of length 1 when zero patients exist in a specialty
- decouple DemandPredictor (core maths) from hierarchy orchestration - rename calculation.py to calculate.py - rename orchestrator.py to orchestrate.py - move calculate_hierarchical_stats to calculate.py - rename prediction_hierarchical_level to aggregate_predictions
- change DemandPrediction class to include mode and expectation - remove check of service_id since this is implicit in predict_service - correct docstrings for find_probability_threshold_index - add min_beds_with_probability to DemandPrediction
735957e to
ee02e83
Compare
This was referenced Feb 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hierarchical demand prediction
Summary
This branch introduces a hierarchical demand prediction system that models hospital bed demand across organisational levels (e.g. subspecialty → reporting unit → division → board → hospital). Instead of predicting demand only at a flat level, the system builds a tree-structured hierarchy, predicts at the bottom (service/subspecialty) level, then aggregates upward using convolution of probability distributions — with statistical capping at each level to control distribution size.
The branch also introduces a flow-based architecture that separately models arrivals and departures (with net flow computed as their difference), and a transfer predictor that estimates inter-service patient transfers.
New Modules
predict/hierarchy/structure.pyHierarchy,EntityType,HierarchyLevel— generic tree structure with prefixed IDs to avoid name collisions across levels. Supports YAML config loading and dataframe population.predict/hierarchy/orchestrate.pyHierarchicalPredictor— orchestrates a 3-phase algorithm: (1) bottom-up stats with top-down capping, (2) bottom-level PMF prediction, (3) upward aggregation via convolution.predict/hierarchy/calculate.pymax_supportcaps) used in Phase 1 capping.predict/hierarchy/__init__.pypredict/service.pybuild_service_data()— prepares per-service prediction inputs from trained models and patient snapshots. Defines flow types (ED current, ED yet-to-arrive, elective/emergency departures, transfers).compute_transfer_arrivals()converts departure PMFs into transfer arrival PMFs.predict/demand.pyDemandPredictor— core mathematical engine. Handles convolution of multiple distributions, PMF truncation with renormalization, and aggregation of child predictions into parent predictions.predict/distribution.pyDistribution— immutable discrete probability distribution with operations: convolution, binomial thinning, net flow (arrivals minus departures), clamping, and statistical queries.predict/types.pyDemandPrediction(PMF + expectation/mode/percentiles),PredictionBundle(arrivals + departures + net flow),FlowSelection(configurable boolean flags for which flow families to include — supports elective-only, emergency-only, custom cohorts).predictors/transfer_predictor.pyTransferProbabilityEstimator— sklearn-style estimator that learns transfer probabilities and destination distributions from patient movement data. Supports cohort-aware (elective vs emergency) and subgroup-aware (paediatric, adult male/female, young/senior) analysis.predictors/subgroup_definitions.pycreate_subgroup_functions()— 5 standard patient subgroup identifiers extracted fromsubgroup_predictor.pyfor reuse.Modified Modules
predict/emergency_demand.py— Corrected docstring forfind_probability_threshold_indexto properly describe P(X >= k) >= threshold semantics.predictors/subgroup_predictor.py— Extractedcreate_subgroup_functions()intosubgroup_definitions.pyand imported from there.predictors/__init__.py— Added exports for new predictors.New Tests (~74 test cases)
test_hierarchy.pytest_service.pybuild_service_data()construction, empirical YTA integration, multi-subgroup predictors,compute_transfer_arrivals()with various transfer scenariostest_transfer_predictor.pyNotebooks
4b_Predict_emergency_demand.ipynb— Major update to reflect refactoredDemandPredictionclass (added mode, expectation,min_beds_with_probability).4c_Evaluate_emergency_demand_predictions.ipynb— Reduced/refactored (540 line diff, net deletion).4d_Predict_emergency_demand_with_special_categories.ipynb— Extended with new evaluation plots.4e_Generate_predictions_using_hierarchy.ipynb— New notebook (910 lines) demonstrating the full hierarchical prediction workflow end-to-end.Configuration
config.yaml— Added hierarchy level definitions (specialty → division → hospital).hierarchy_config.yaml— New example config for a deeper hierarchy (subspecialty → reporting unit → division → board → hospital).Key Design Decisions
"entity_type:entity_name"keys prevent name collisions when different hierarchy levels share names, while the public API uses clean names.Distributionclass centralizes PMF operations (convolution, thinning, net flow) with proper truncation and renormalisation.