5D Library › Two-Process Sleep Architecture, Slow-Wave Glymphatic Clearance and Thermoregulatory Phase Control · 中文

Chapter 3. Sleep Architecture as a Markov Chain, and What One Night Can Tell You

Stages and their order

Scored by the standard rules, a night of sleep is a sequence of 30-second epochs, each labelled wake, N1, N2, N3 (slow-wave sleep) or REM (Iber et al. 2007). The sequence is not random. Deep N3 sleep is concentrated early in the night; REM recurs roughly every hour and a half and its episodes lengthen toward morning. Both facts follow from the two processes of Chapter 1: deep sleep tracks the decaying homeostatic pressure, REM tracks the circadian clock.

A Markov chain is the natural model for such a sequence: the next epoch’s stage depends on the current one through a table of transition probabilities. A non-homogeneous chain lets the table change through the night. The fifth program builds one in which entry to N3 scales with a decaying pressure s(t)=e−t/2.5hs(t) = e^{-t/2.5\,\text{h}}, REM can begin only after 50 minutes of non-REM since the last REM episode (an ultradian timer carried as part of the state), and REM episodes lengthen from about 10 to about 35 minutes. All rates are illustrative; what matters is what the structure produces.

Calculation

# Chapter 3. Sleep architecture as a non-homogeneous Markov chain on 30-second
# epochs.
# States: W (wake), N1, N2, N3 (slow-wave sleep), R (REM). Transition
# probabilities change through the night: entry to N3 scales with homeostatic
# pressure s(t) = exp(-t / 2.5 h), so deep sleep is front-loaded; REM can
# begin only after 50 min of NREM since the last REM bout (an ultradian timer,
# kept as part of the state), and REM bouts lengthen through the night. 8
# hours in bed = 960 epochs; 2,000 simulated nights. All rates are
# illustrative; the point is what the STRUCTURE produces.
import math
import random

W, N1, N2, N3, R = range(5)
NAMES = "W N1 N2 N3 R".split()

def step(st, t, since_rem, rng):
    s = math.exp(-t / 2.5)
    u = rng.random()
    if st == W:
        return N1 if u < 0.045 else W
    if st == N1:
        if u < 0.10:
            return N2
        return W if u < 0.12 else N1
    if st == N2:
        p_n3 = 0.05 * s
        p_r = 0.0 if since_rem < 100 else 0.03 * (0.5 + t / 8)
        if u < p_n3:
            return N3
        if u < p_n3 + p_r:
            return R
        if u < p_n3 + p_r + 0.006:
            return N1
        if u < p_n3 + p_r + 0.009:
            return W
        return N2
    if st == N3:
        p_out = 0.012 + 0.05 * (1 - s)
        if u < p_out:
            return N2
        return W if u < p_out + 0.001 else N3
    # REM: bouts lengthen from about 10 to about 35 minutes
    p_end = 1.0 / (20 + 50 * t / 8)
    if u < p_end * 0.8:
        return N2
    if u < p_end:
        return W
    return R

def simulate(rng):
    st, since, seq = W, 0, []
    for e in range(960):
        t = e / 120.0
        st = step(st, t, since, rng)
        since = 0 if st == R else since + 1
        seq.append(st)
    return seq

def summary(nights):
    tot = [0] * 5
    n3_third = [0, 0, 0]
    r_third = [0, 0, 0]
    wakes, latency, cycles = [], [], []
    for seq in nights:
        on = next(i for i, s in enumerate(seq) if s != W)
        latency.append(on / 2)
        w = 0
        rem_onsets = []
        for i, s in enumerate(seq):
            tot[s] += 1
            third = min(2, i // 320)
            if s == N3:
                n3_third[third] += 1
            if s == R:
                r_third[third] += 1
            if i > on and s == W and seq[i - 1] != W:
                w += 1
            if s == R and seq[i - 1] != R:
                rem_onsets.append(i)
        wakes.append(w)
        cycles += [(b - a) / 2 for a, b in zip(rem_onsets, rem_onsets[1:])]
    n = sum(tot)
    return tot, n, n3_third, r_third, wakes, latency, cycles

if __name__ == "__main__":
    rng = random.Random(57005)
    nights = [simulate(rng) for _ in range(2000)]
    tot, n, n3t, rt, wakes, lat, cyc = summary(nights)
    print("Share of time in bed by stage")
    print(
        "   "
        + "   ".join(
            "{} {:5.1%}".format(NAMES[i], tot[i] / n) for i in range(5)
        )
    )
    print(
        "Share of all N3 falling in each third of the night:   {:.2f}  {:.2f}"
        "  {:.2f}".format(*[x / sum(n3t) for x in n3t])
    )
    print(
        "Share of all REM falling in each third of the night:  {:.2f}  {:.2f}"
        "  {:.2f}".format(*[x / sum(rt) for x in rt])
    )
    print(
        "Mean sleep-onset latency {:.1f} min; mean awakenings after onset"
        " {:.1f};\nmean interval between REM onsets {:.0f} min".format(
            sum(lat) / len(lat), sum(wakes) / len(wakes), sum(cyc) / len(cyc)
        )
    )
# Share of time in bed by stage
#    W 10.3%   N1  6.2%   N2 47.4%   N3 15.5%   R 20.6%
# Share of all N3 falling in each third of the night:   0.69  0.25  0.07
# Share of all REM falling in each third of the night:  0.14  0.36  0.50
# Mean sleep-onset latency 10.2 min; mean awakenings after onset 3.5;
# mean interval between REM onsets 92 min

The structure alone reproduces the familiar architecture: 47.4 % of time in bed in N2, 15.5 % in N3, 20.6 % in REM, 10.3 % awake, a sleep-onset latency of 10.2 minutes and REM episodes beginning on average every 92 minutes. It also reproduces the asymmetry of the night without being told it directly: 0.69 of all N3 falls in the first third of the night and only 0.07 in the last, while 0.50 of REM falls in the last third. Front-loaded deep sleep and back-loaded REM are consequences of one decaying process and one timer.

A simulated hypnogram (one night) and the share of each third of the night spent in N3 and REM, averaged over 2,000 simulated nights.

The same sleeper, a different night

The sixth program asks a question every owner of a sleep tracker should ask. Take a sleeper whose physiology is exactly the same every night — the chain above, unchanged — and count the minutes of deep sleep and REM.

Calculation

# Chapter 3. How much does one night tell you? The same sleeper, with exactly
# the same physiology every night (the p05 chain, unchanged), still produces
# different nights, because stage sequences are random. We count minutes of N3
# ("deep sleep") and of REM per night over 2,000 nights, then ask how many
# nights of averaging are needed to detect a real change of 10 minutes of N3
# (two-sided alpha 0.05, power 0.80, comparing a block of nights before with a
# block after).
import math
import random

W, N1, N2, N3, R = range(5)

def step(st, t, since_rem, rng):
    s = math.exp(-t / 2.5)
    u = rng.random()
    if st == W:
        return N1 if u < 0.045 else W
    if st == N1:
        if u < 0.10:
            return N2
        return W if u < 0.12 else N1
    if st == N2:
        p_n3 = 0.05 * s
        p_r = 0.0 if since_rem < 100 else 0.03 * (0.5 + t / 8)
        if u < p_n3:
            return N3
        if u < p_n3 + p_r:
            return R
        if u < p_n3 + p_r + 0.006:
            return N1
        if u < p_n3 + p_r + 0.009:
            return W
        return N2
    if st == N3:
        p_out = 0.012 + 0.05 * (1 - s)
        if u < p_out:
            return N2
        return W if u < p_out + 0.001 else N3
    p_end = 1.0 / (20 + 50 * t / 8)
    if u < p_end * 0.8:
        return N2
    if u < p_end:
        return W
    return R

def night(rng):
    st, since, n3, rem = W, 0, 0, 0
    for e in range(960):
        st = step(st, e / 120.0, since, rng)
        since = 0 if st == R else since + 1
        n3 += st == N3
        rem += st == R
    return n3 / 2, rem / 2

if __name__ == "__main__":
    rng = random.Random(57006)
    data = [night(rng) for _ in range(2000)]
    for k, name in ((0, "N3 (deep)"), (1, "REM")):
        v = sorted(d[k] for d in data)
        m = sum(v) / len(v)
        sd = math.sqrt(sum((x - m) ** 2 for x in v) / (len(v) - 1))
        print(
            "{:<10} mean {:5.1f} min  sd {:4.1f} min  middle 90 % {:5.1f} to"
            " {:5.1f} min".format(name, m, sd, v[100], v[1899])
        )
    sd = math.sqrt(
        sum((d[0] - sum(x[0] for x in data) / 2000) ** 2 for d in data) / 1999
    )
    n = math.ceil(2 * (1.959964 + 0.841621) ** 2 * sd ** 2 / 10.0 ** 2)
    print(
        "\nNights per block needed to detect a true 10-minute change in"
        " N3: {}".format(n)
    )
    print(
        "Share of single nights whose N3 is 10 or more minutes below this"
        "\nsleeper's own mean: {:.3f}".format(
            sum(d[0] <= sum(x[0] for x in data) / 2000 - 10 for d in data)
            / 2000
        )
    )
    print(
        "\nReading: with nothing at all changed, a sizeable share of nights"
        " look 'worse"
    )
    print(
        "by ten minutes of deep sleep'. One night's number is mostly the"
        " dice."
    )
# N3 (deep)  mean  75.5 min  sd 32.4 min  middle 90 %  24.0 to 131.0 min
# REM        mean 100.3 min  sd 38.7 min  middle 90 %  44.5 to 169.5 min
#
# Nights per block needed to detect a true 10-minute change in N3: 165
# Share of single nights whose N3 is 10 or more minutes below this
# sleeper's own mean: 0.404
#
# Reading: with nothing at all changed, a sizeable share of nights look 'worse
# by ten minutes of deep sleep'. One night's number is mostly the dice.

Deep sleep averages 75.5 minutes but has a standard deviation of 32.4 minutes, and the middle 90 % of nights range from 24.0 to 131.0 minutes. REM ranges from 44.5 to 169.5. Nothing about the sleeper changed. In this model, 0.404 of nights show deep sleep ten or more minutes below the sleeper’s own average — which is to say that “I got ten minutes less deep sleep last night” is something that happens on about two nights in five for no reason at all.

To detect a real change of 10 minutes in average deep sleep, comparing a block of nights before with a block after, needs 165 nights in each block. A week of tracking cannot see it.

Note

How far to trust these numbers. A Markov chain with constant rates gives each stage geometrically distributed bout lengths, which may make night-to-night variation larger than in some real sleepers; real nights also vary for real reasons (stress, alcohol, illness, a late meal) that add to it. So the 32.4-minute figure is an illustration of what chance alone can do, not a measurement. The direction of the conclusion does not depend on it: the chance component of a single night is large relative to the changes people try to read into it.

Summary

A non-homogeneous Markov chain with one decaying pressure and one ultradian timer reproduces the architecture of a night — about 15 % deep sleep and 20 % REM, deep sleep concentrated early (0.69 in the first third) and REM late (0.50 in the last third), cycles of about 92 minutes — without being given those facts. Run for a sleeper whose physiology never changes, the same chain produces deep sleep ranging from 24.0 to 131.0 minutes across the middle 90 % of nights. One night’s number is mostly chance; detecting a 10-minute change needs months, not days.

References

Iber, Conrad, Sonia Ancoli-Israel, Andrew L. Chesson, and Stuart F. Quan. 2007. The AASM Manual for the Scoring of Sleep and Associated Events: Rules, Terminology and Technical Specifications. American Academy of Sleep Medicine.

Fan, Y. (2026). Two-Process Sleep Architecture, Slow-Wave Glymphatic Clearance and Thermoregulatory Phase Control: Adenosine Homeostasis, Flip-Flop Switch Bistability, Core-Distal Heat Flux and Sleep-Stage Markov Chains [双过程睡眠架构、慢波胶质淋巴清除与体温节律相位控制] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090162 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).