Hospital bed occupancy and bed capacity from admissions
Introduction
This tutorial is for a user who needs hospital bed occupancy, with or without a limit on beds. Bed occupancy is the sum of earlier admissions, each weighted by the probability that the patient is still in a bed. The tutorial computes the following.
occupancy from admissions, for a geometric and a log-normal length of stay;
occupancy under a cap on the number of beds, for the geometric stay and for any stay;
the occupancy of a ward with a pool of suspected and a pool of confirmed patients.
It assumes you have read Symptom onsets, hospital admissions and deaths as a chain of delays.
Packages used
using ComposableRecurrences
using ComposableRecurrences: Transform, Recent, Depletion, Flow
using CairoMakie, AlgebraOfGraphics, DataFramesMeta
using SpecialFunctions: erfc
CairoMakie.activate!(type = "png", px_per_unit = 2)
# Lines of the named series against day, one colour a series, with a
# dashed line at `level` when one is given.
function plot_series(series::Pair...; ylabel, level = nothing)
table = stack(
DataFrame("day" => eachindex(last(first(series))), series...),
Not(:day); variable_name = :series
)
layers = data(table) * mapping(:day, :value, color = :series => "") *
visual(Lines, linewidth = 2)
if level !== nothing
layers += mapping([level]) *
visual(HLines, color = :grey, linestyle = :dash)
end
return draw(layers; axis = (; xlabel = "Day", ylabel))
endOccupancy as a convolution and as a recurrence
For any distribution of the length of stay, occupancy is
where Convolution, lag 0 first. For a geometric stay each patient leaves with probability
The kernel of the Recurrence is the one weight add input. Here
T = 90
admissions = [30 * exp(-((t - 35) / 12)^2) for t in 1:T]
δ = 0.15
# Occupancy from `admissions` and the `survival` of the length of stay.
occupancy(survival, admissions) = Convolution(survival)(admissions)
geometric = (1 - δ) .^ (0:(T - 1))
by_convolution = occupancy(geometric, admissions)
by_recurrence = Recurrence([1 - δ])(; history = [0.0], add = admissions)
by_convolution ≈ by_recurrencetrueThe convolution and the recurrence agree to rounding error.
plot_series(
"Admissions" => admissions, "Occupancy" => by_convolution;
ylabel = "Patients"
)Occupancy peaks on day 39, four days after admissions, at 5.7 times their peak. The mean stay is
A stay that is not geometric
A log-normal stay with log-mean
where
# The survival on days 0 to `n - 1` of a log-normal stay with log-mean `μ`
# and log-standard deviation `σ`, discretised by day.
function lognormal_survival(μ, σ, n)
F(x) = x <= 0 ? 0.0 : erfc(-(log(x) - μ) / (σ * sqrt(2))) / 2
return 1 .- cumsum([F(l + 1) - F(l) for l in 0:(n - 1)])
end
# The log-mean at which the survival sums to `mean_days`, by bisection.
function matched_log_mean(mean_days, σ, n; lo = 0.0, hi = 3.0)
for _ in 1:50
mid = (lo + hi) / 2
short = sum(lognormal_survival(mid, σ, n)) < mean_days
short ? (lo = mid) : (hi = mid)
end
return lo
end
σ = 1.2
S = lognormal_survival(matched_log_mean(1 / δ, σ, T), σ, T)
by_lognormal = occupancy(S, admissions)
plot_series(
"Admissions" => admissions, "Geometric stay" => by_convolution,
"Log-normal stay" => by_lognormal; ylabel = "Patients"
)Both stays have a mean of 6.67 days, with
A bed cap on occupancy
With a geometric stay occupancy is a recurrence, and a modifier bounds it. With Transform(clamp, 0, B) gives
The cap acts before occupancy carries over to the next day, so a patient over the cap never occupies a bed. Here
beds = 120.0
capped_ward = Recurrence([1 - δ]; modifiers = (Transform(clamp, 0.0, beds),))
capped = capped_ward(; history = [0.0], add = admissions)
plot_series(
"Uncapped" => by_recurrence, "Capped" => capped;
ylabel = "Occupied beds", level = beds
)Capped occupancy is 120 beds on the 12 days from day 32 to day 43. The cap removes 670 bed-days. This model holds for the geometric stay only, and the patients over the cap leave the model.
A bed cap with any stay
A cap on the admissions holds for any stay and counts the patients turned away. Each day admits the demand up to the free beds.
where Recent reads. The cap is Transform(min, (B - Recent(S[2:end])) / S[1]) on a recurrence with a zero kernel and the demand as add.
Why the cap divides by
Of the day's admissions a share
# The admissions of each day when `demand` arrives at a ward of `beds`
# beds whose length of stay has the given `survival`.
function admit(survival, beds, demand)
cap = Transform(min, (beds - Recent(survival[2:end])) / survival[1])
ward = Recurrence([0.0]; modifiers = (cap,))
return ward(; history = [0.0], add = demand)
end
admitted = admit(S, beds, admissions)
plot_series(
"Admitted" => admitted, "Turned away" => admissions .- admitted;
ylabel = "Patients"
)With the log-normal stay 43 patients are turned away over the 90 days. The admitted and the turned away sum to the demand on every day. The same function gives the admissions for the geometric stay, and the occupancy of each is the convolution of its admissions with its survival.
admitted_geometric = admit(geometric, beds, admissions)
plot_series(
"Geometric stay" => occupancy(geometric, admitted_geometric),
"Log-normal stay" => occupancy(S, admitted);
ylabel = "Occupied beds", level = beds
)With the log-normal stay the ward is full on days 35 to 42. With the geometric stay
A ward with two linked pools
A ward holds suspected patients awaiting a test and confirmed patients. Each day some suspected patients are confirmed and move to the confirmed pool. Others are ruled out and leave, and confirmed patients are discharged. Confirmation and ruling out compete for the same suspected patients. Let
The rates are hazards, so the number that leaves a pool is at most the number it holds. The two pools are the strata of a recurrence without a kernel, suspected first.
The recurrence carries yesterday's pools forward and adds the admissions to the suspected one.
Without a
historythe pools start empty.A
Depletionwithout a draw then applies the day's flows, with its pools held in the strata.Each flow is a
Flowfrom one pool to another, or to0to leave, with a hazard rate as its demand.Each rate is a parameter, which can be given per ward or per day.
Here
# The suspected and confirmed patients of each day, as two rows, for
# confirmation rate `c`, rule-out rate `r` and discharge rate `d`.
function two_pool_ward(admissions, c, r, d)
flows = Depletion(
Flow(:suspected => :confirmed, c), Flow(:suspected => 0, r),
Flow(:confirmed => 0, d); pools = (:suspected, :confirmed)
)
ward = Recurrence(; modifiers = (flows,))
arrivals = [admissions'; zero(admissions')]
return ward(; add = arrivals, stop = length(admissions))
end
patients = two_pool_ward(admissions, 0.3, 0.4, 0.1)
plot_series(
"Suspected" => patients[1, :], "Confirmed" => patients[2, :];
ylabel = "Patients"
)Suspected patients peak on day 36 and confirmed patients on day 42.
Other flow forms and several wards
A flow with form = Share() moves a share, like the daily discharge probability form = Share(; bounded = false) takes any share, such as a rate whose prior has no upper bound. A flow with form = Truncate() moves a count capped by what the pool holds, such as a fixed number of transfers a day. With
Performance
The timed model is the ward with two pools and rate, share and count flows over 200 days. The figure gives its minimum time and that of a preallocated hand-written loop of the same model.
performance_plot("occupancy")Learning more
Clinical outcomes computes the admissions that this page starts from.
A step on groups writes a new step that moves values between pools.
Public API lists every public name with its docstring.