What does The Arithmetic of Sleep conclude, in numbers, about “What a Sleep Tracker Can Know, and the Score That Keeps You Awake”?
Answer: Chapter 6 shows that even a perfect device agrees with a human scorer only at kappa 0.815, that a device may find only 0.571 of deep-sleep epochs, and that the night-to-night change in a score (correlation 0.640) is noisier than the score (0.708), and writes "orthosomnia" as a feedback loop explicitly marked as a hypothesis.
Scope of the numbers: It is not medical advice, gives no doses and recommends no product; for persistent insomnia the first-line treatment is cognitive behavioural therapy for insomnia. Marked boxes separate published evidence, runnable Python calculations (16 programs), classical sources and the author's hypotheses; apart from cited literature, all data in the book are synthetic.
Data table
The numbers below are printed verbatim by the chapter's Python program and can be reproduced by running it. standard-library Python onlyprinted values that match the answer: 0.571, 0.815
agreement Cohen's kappa scorer A vs scorer B 0.788 0.671 scorer A vs truth 0.881 0.815 device vs truth 0.694 0.510 device vs scorer A 0.637 0.420 perfect device (= truth) vs scorer A 0.881 0.815 Minutes per night by stage (mean over 200 nights) last column: share of epochs of this stage the device gets right stage truth device device - truth right wake 47.2 43.5 -3.8 0.543 light 253.1 274.6 +21.5 0.779 deep 75.2 69.1 -6.2 0.571 REM 104.4 92.9 -11.5 0.644 Night-to-night correlation of device deep minutes with true deep minutes: 0.953 Reading: a validation study compares the device with a scorer, not with the truth, so even a perfect device cannot score above the scorers' own agreement with the truth. The device's nightly averages can be close while it misses almost half of the deep-sleep epochs: errors in both directions cancel. Caution: here the device errs independently epoch by epoch, so its errors average out over a night. Real errors are correlated within a night (posture, heart-rate patterns), which p12 allows for; the 0.953 is an upper bound.
Show the full program
# Chapter 6. How well can a wrist or ring device stage sleep, and how would we
# know? True stages come from the p05 chain (N1 and N2 merged as "light"). Two
# human scorers and a device each read the true stage with their own confusion
# probabilities (illustrative values: the scorers agree with each other about
# 80 % of epochs, of the order reported for expert polysomnography scoring;
# the device is a plausible consumer tracker). We compute accuracy and Cohen's
# kappa against the truth and against one scorer, which is all a validation
# study can do.
import math
import random
W, L, D, R = range(4)
NAMES = ("wake", "light", "deep", "REM")
SCORER = [[0.90, 0.08, 0.00, 0.02],
[0.03, 0.89, 0.04, 0.04],
[0.00, 0.20, 0.80, 0.00],
[0.02, 0.07, 0.00, 0.91]]
DEVICE = [[0.55, 0.35, 0.02, 0.08],
[0.05, 0.78, 0.09, 0.08],
[0.01, 0.40, 0.57, 0.02],
[0.04, 0.30, 0.02, 0.64]]
def truth_night(rng):
st, since, seq = 0, 0, []
for e in range(960):
t = e / 120.0
s = math.exp(-t / 2.5)
u = rng.random()
if st == 0:
st = 1 if u < 0.045 else 0
elif st == 1:
st = 2 if u < 0.10 else (0 if u < 0.12 else 1)
elif st == 2:
p3 = 0.05 * s
pr = 0.0 if since < 100 else 0.03 * (0.5 + t / 8)
st = (
3
if u < p3
else (
4
if u < p3 + pr
else (
1
if u < p3 + pr + 0.006
else 0 if u < p3 + pr + 0.009 else 2
)
)
)
elif st == 3:
po = 0.012 + 0.05 * (1 - s)
st = 2 if u < po else 0 if u < po + 0.001 else 3
else:
pe = 1.0 / (20 + 50 * t / 8)
st = 2 if u < pe * 0.8 else 0 if u < pe else 4
since = 0 if st == 4 else since + 1
seq.append({0: W, 1: L, 2: L, 3: D, 4: R}[st])
return seq
def read(seq, conf, rng):
out = []
for s in seq:
u, acc = rng.random(), 0.0
for j, p in enumerate(conf[s]):
acc += p
if u < acc:
out.append(j)
break
else:
out.append(3)
return out
def kappa(a, b):
n = len(a)
po = sum(x == y for x, y in zip(a, b)) / n
pe = sum((a.count(k) / n) * (b.count(k) / n) for k in range(4))
return po, (po - pe) / (1 - pe)
if __name__ == "__main__":
rng = random.Random(57011)
T, A, B, Dv = [], [], [], []
deep_t, deep_d = [], []
for _ in range(200):
t = truth_night(rng)
dv = read(t, DEVICE, rng)
T += t
A += read(t, SCORER, rng)
B += read(t, SCORER, rng)
Dv += dv
deep_t.append(t.count(D) / 2)
deep_d.append(dv.count(D) / 2)
print(
" agreement Cohen's kappa"
)
for name, x, y in (
("scorer A vs scorer B", A, B),
("scorer A vs truth", A, T),
("device vs truth", Dv, T),
("device vs scorer A", Dv, A),
("perfect device (= truth) vs scorer A", T, A),
):
po, k = kappa(x, y)
print(" {:<38}{:6.3f} {:6.3f}".format(name, po, k))
print("\nMinutes per night by stage (mean over 200 nights)")
print(
" last column: share of epochs of this stage the device gets right"
)
print(" stage truth device device - truth right")
for k in range(4):
tt, dd = T.count(k) / 400, Dv.count(k) / 400
hit = sum(1 for x, y in zip(T, Dv) if x == k and y == k) / T.count(k)
print(
" {:<7} {:6.1f} {:6.1f} {:+6.1f} {:.3f}".format(
NAMES[k], tt, dd, dd - tt, hit
)
)
mt, md = sum(deep_t) / 200, sum(deep_d) / 200
cov = sum((a - mt) * (b - md) for a, b in zip(deep_t, deep_d))
r = cov / math.sqrt(
sum((a - mt) ** 2 for a in deep_t)
* sum((b - md) ** 2 for b in deep_d)
)
print(
"\nNight-to-night correlation of device deep minutes with true deep"
)
print("minutes: {:.3f}".format(r))
print("\nReading: a validation study compares the"
" device with a scorer, not with")
print("the truth, so even a perfect device cannot"
" score above the scorers' own")
print("agreement with the truth. The device's nightly"
" averages can be close while")
print("it misses almost half of the deep-sleep"
" epochs: errors in both directions")
print("cancel. Caution: here the device errs"
" independently epoch by epoch, so its")
print("errors average out over a night. Real errors"
" are correlated within a night")
print("(posture,")
print(
"heart-rate patterns), which p12 allows for; the {:.3f} is an upper"
" bound.".format(r)
)Single source and citation
Fan, Y. (2026). Two-Process Sleep Architecture, Slow-Wave Glymphatic Clearance and Thermoregulatory Phase Control (Version 1.0.0), Chapter 6: What a Sleep Tracker Can Know, and the Score That Keeps You Awake. 5D Library. https://doi.org/10.5281/zenodo.24090162
DOI: 10.5281/zenodo.24090162 · Read the monograph · 5DL-B-057 · v1.0.0 · CC-BY-NC-ND-4.0
BibTeX
@book{fan2026057,
author = {Fan, Yafu},
title = {Two-Process Sleep Architecture, Slow-Wave Glymphatic Clearance and Thermoregulatory Phase Control},
year = {2026},
publisher = {5D Library},
edition = {1.0.0},
doi = {10.5281/zenodo.24090162},
url = {https://doi.org/10.5281/zenodo.24090162}
}Other chapter answers in this book
- Chapter 1 · Sleep Pressure, the Clock, and What Caffeine Actually Does
- Chapter 2 · The Sleep Switch: Why Ageing Fragments the Night Before It Shortens It
- Chapter 3 · Sleep Architecture as a Markov Chain, and What One Night Can Tell You
- Chapter 4 · Does Sleep Wash the Brain? Diffusion, Flow and an Open Dispute
- Chapter 5 · Warm Hands, Cool Core: the Thermal Gate of Sleep
- Chapter 7 · The Defensive Qi by Day and by Night: the *Lingshu* and the Sleeping Immortal
- Chapter 8 · A Four-Step Check and a Pre-Registered Trial