Skip to content

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 ​

julia
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))
end

Occupancy as a convolution and as a recurrence ​

For any distribution of the length of stay, occupancy is

where is the occupancy and the admissions on day . The survival is the kernel of a Convolution, lag 0 first. For a geometric stay each patient leaves with probability on each day, and   . The geometric distribution is memoryless, so for this stay alone the sum is a recurrence of one lag.

The kernel of the Recurrence is the one weight   on lag 1, and the admissions are its add input. Here  , and admissions follow a bell-shaped curve that peaks at 30 a day on day 35 of 90.

julia
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_recurrence
true

The convolution and the recurrence agree to rounding error.

julia
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   days.

A stay that is not geometric ​

A log-normal stay with log-mean and log-standard deviation , discretised by day, has

where is the probability of a stay of whole days and the log-normal distribution function. Occupancy is again  . Here  , and is chosen so that the mean number of days in a bed, , equals the of the geometric stay.

julia
# 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  . The log-normal stay gives a lower, later peak, 140 patients on day 40 against 171 on day 39, and a longer tail. With the log-normal stay 31% of patients stay at most one day and 12% more than 14 days, against 15% and 10% with the geometric stay.

A bed cap on occupancy ​

With a geometric stay occupancy is a recurrence, and a modifier bounds it. With beds, 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  .

julia
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 is the demand for beds, the admissions and the beds still held by earlier admissions. The held beds are a weighted sum of the recurrence's own past outputs, which 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 is still in a bed at the end of the day. The cap therefore divides the free beds by , and   on the days it binds. The survival is non-increasing, so    and the bound is not negative.

julia
# 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.

julia
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  , the ward is full on days 32 to 43, as under the cap on occupancy, and 101 patients are turned away.

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 be the suspected pool, the confirmed pool and the admissions. With confirmation rate , rule-out rate and discharge rate ,

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 history the pools start empty.

  • A Depletion without a draw then applies the day's flows, with its pools held in the strata.

  • Each flow is a Flow from one pool to another, or to 0 to leave, with a hazard rate as its demand.

  • Each rate is a parameter, which can be given per ward or per day.

Here  ,   and  .

julia
# 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 above, except that the day's admissions can also leave that day. A share must lie between 0 and 1. 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 wards the strata are the suspected pools, then the confirmed ones.

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.

julia
performance_plot("occupancy")

Learning more ​