Occupancy and capacity
Introduction
Bed occupancy is the admissions weighted by the chance of still being in a bed, a convolution over the length of stay.
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)Occupancy two ways
For any length-of-stay distribution, occupancy is the admissions weighted by the probability of still being in a bed:
where Convolution with the survival as its kernel, Convolution(S)(admissions). A geometric stay, in which each patient leaves with probability
In the recurrence the kernel weight on lag 1 is add.
T = 90
admissions = [30 * exp(-((t - 35) / 12)^2) for t in 1:T]
δ = 0.15
by_convolution = Convolution((1 - δ) .^ (0:(T - 1)))(admissions)
by_recurrence = Recurrence([1 - δ])(; history = [0.0], add = admissions)
@chain DataFrame(
"day" => 1:T, "Admissions" => admissions,
"Occupancy (convolution)" => by_convolution,
"Occupancy (recurrence)" => by_recurrence
) begin
stack(Not(:day); variable_name = :series, value_name = :count)
data(_) * mapping(:day, :count, color = :series) * visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Patients"))
endThe convolution and the recurrence agree to rounding error, so their lines overlap. Occupancy peaks on day 39, four days after admissions, at 5.7 times their height. Each patient stays
A stay that is not geometric
Real stays are rarely geometric: the chance of leaving changes with the time already spent in bed, and a few patients stay much longer. A log-normal stay with log-mean
where 1 .- cumsum(pmf), lag 0 first, and occupancy is again
σ = 1.2
function survival(μ)
F(x) = x <= 0 ? 0.0 : erfc(-(log(x) - μ) / (σ * sqrt(2))) / 2
pmf = [F(k + 1) - F(k) for k in 0:(T - 1)]
return 1 .- cumsum(pmf)
end
function matched_mu(target; lo = 0.0, hi = 3.0)
for _ in 1:50 # bisect on μ until the mean days in bed match
mid = (lo + hi) / 2
sum(survival(mid)) < target ? (lo = mid) : (hi = mid)
end
return lo
end
S = survival(matched_mu(1 / δ))
by_lognormal = Convolution(S)(admissions)
@chain DataFrame(
"day" => 1:T, "Admissions" => admissions,
"Geometric stay" => by_convolution, "Log-normal stay" => by_lognormal
) begin
stack(Not(:day); variable_name = :series, value_name = :count)
data(_) * mapping(:day, :count, color = :series) * visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Patients"))
endBoth stays have a mean of 6.67 days, with
A bed cap on occupancy
With a geometric stay occupancy is a recurrence, so a modifier can bound it. Transform(clamp, lo, hi) bounds each day's occupancy. With
The one modifier Transform(clamp, 0, B). The cap applies before occupancy carries over to the next day, so patients turned away never occupy a bed later.
beds = 120.0
capped = Recurrence([1 - δ]; modifiers = (Transform(clamp, 0.0, beds),))(; history = [0.0], add = admissions)
@chain DataFrame(day = 1:T, Uncapped = by_recurrence, Capped = capped) begin
stack(Not(:day); variable_name = :series, value_name = :count)
data(_) * mapping(:day, :count, color = :series) * visual(Lines, linewidth = 2) +
mapping([beds]) * visual(HLines, color = :grey, linestyle = :dash)
draw(_; axis = (xlabel = "Day", ylabel = "Occupied beds"))
endCapped occupancy holds at 120 beds for 12 days, days 32 to 43, then falls before the uncapped one. The cap removes 670 bed-days. This cap needs the geometric stay, as only that stay makes occupancy a recurrence. It also deletes the patients it turns away.
A bed cap with any stay
Capping the admissions rather than the occupancy works with any stay and keeps 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. Of the day's admissions a share
function admit(survival, beds, demand)
cap = Transform(min, (beds - Recent(survival[2:end])) / survival[1])
return Recurrence([0.0]; modifiers = (cap,))(; history = [0.0], add = demand)
end
geometric = (1 - δ) .^ (0:(T - 1))
admitted = admit(S, beds, admissions)
admitted_occupancy = Convolution(S)(admitted)
@chain DataFrame(
"day" => 1:T, "Admitted" => admitted, "Turned away" => admissions .- admitted
) begin
stack(Not(:day); variable_name = :series, value_name = :count)
data(_) * mapping(:day, :count, color = :series) * visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Patients"))
endWith the log-normal stay admissions fall to the beds freed each day while the ward is full. Over the 90 days 43 patients are turned away. Admitted and turned-away patients add up to the demand, so no patient is lost. Occupancy of those admitted reaches the 120 beds and never passes them:
@chain DataFrame(
"day" => 1:T,
"Geometric stay" => Convolution(geometric)(admit(geometric, beds, admissions)),
"Log-normal stay" => admitted_occupancy
) begin
stack(Not(:day); variable_name = :series, value_name = :count)
data(_) * mapping(:day, :count, color = :series) * visual(Lines, linewidth = 2) +
mapping([beds]) * visual(HLines, color = :grey, linestyle = :dash)
draw(_; axis = (xlabel = "Day", ylabel = "Occupied beds"))
endThe log-normal ward is full on days 35 to 42. With the geometric stay S: the cap and the convolution stay the same.
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 share leaving a pool never exceeds what it holds. The two pools are the strata of a recurrence with the unit kernel, suspected first.
The recurrence carries yesterday's pools forward and adds the admissions to the suspected one.
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, so it can differ by ward or change over time.
Here
ward_flows = Depletion(
Flow(:suspected => :confirmed, 0.3), Flow(:suspected => 0, 0.4),
Flow(:confirmed => 0, 0.1); pools = (:suspected, :confirmed)
)
ward = Recurrence([1.0]; modifiers = (ward_flows,))
patients = ward(; history = zeros(2, 1), add = [admissions'; zero(admissions')], stop = T)
@chain DataFrame(day = 1:T, Suspected = patients[1, :], Confirmed = patients[2, :]) begin
stack(Not(:day); variable_name = :series, value_name = :count)
data(_) * mapping(:day, :count, color = :series) * visual(Lines, linewidth = 2)
draw(_; axis = (xlabel = "Day", ylabel = "Patients"))
endSuspected patients peak first, on day 36. Confirmed patients peak six days later, on day 42, and stay longer, as they leave more slowly.
A flow with form = Share() moves a share, like the daily discharge probability form = Truncate() moves a count capped by what the pool holds, such as a fixed number of transfers a day. With
Performance
Each bar times the ward with two linked pools and rate, share and count flows over 200 days, against a preallocated hand-written loop.
performance_plot("occupancy")Learning more
API overview: a map of every operator, coupling and modifier.
A blockwise step: a step of your own that moves values between pools.
Public API: every public name with its docstring.