5D Library › 双过程睡眠架构、慢波胶质淋巴清除与体温节律相位控制 · English
第六章 睡眠追踪器能知道什么,以及让你睡不着的那个分数
对照一把不完美的尺子
消费级戒指与手环根据动作、心率,有时还有皮肤温度来估计睡眠分期。它们的验证标准是由受训技师判读的多导睡眠图。但判读本身并不完美:在美国睡眠医学会的判读者间信度项目中,数以千计的判读者与参考判读的总体一致率约为 83%,N1 与 N3 的一致率更低 (Rosenberg and Van Hout 2013)。拿设备与一位判读者比较,就是拿它与一把有噪声的尺子比较。
第十一段程序把这一后果说清楚。真实分期来自第三章的马尔可夫链;两位人工判读者和一台设备各自以自己的混淆概率读取它。
推导
# 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)
)
# 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.第一部分显示了尺子的问题。两位判读者彼此在 0.788 的时段上一致(kappa 0.671),各自与真相一致 0.881(kappa 0.815)。设备与真相的 kappa 为 0.510,与一位判读者却只有 0.420——而一台与真相完全相同的完美设备,与那位判读者的 kappa 也只能达到 0.815,不会更高。验证研究测的是设备与判读者合在一起的表现,它把每台设备都封顶在判读者自己的准确度上。
第二部分显示了一个更微妙的问题。夜间平均值可以看上去几乎正确,而逐时段的表现却很差:设备的深睡眠分钟数平均 69.1,真实为 75.2,但它只认出了真实深睡眠时段的 0.571。漏判与误判在总数里相互抵消。在这个模型中,因为设备的错误逐时段独立,它的每夜深睡眠分钟数与真相的相关仍有 0.953;程序把这一点标为上界,因为真实设备在一夜之内的错误是相关的。
证据
已知的情况。 Chinoy 等人在同一夜将七款消费级设备与多导睡眠图比较,发现多数设备区分睡眠与清醒的表现尚可,但在具体睡眠分期上的一致性明显更弱,且因设备而异 (Chinoy et al. 2021)。
分数是带误差的测量
第十二段程序把每晚的睡眠分数看作真实质量加上一个在一夜之内不会平均掉的测量误差。
推导
# Chapter 6. A nightly sleep score as a noisy measurement. True sleep quality
# q_t varies from night to night (sd 1, lag-1 autocorrelation 0.3). The
# device's score is s_t = q_t + e_t, with a measurement error e_t of sd
# sigma_e that is independent from night to night but correlated within a
# night (so it does not average away). We ask:
# - how well does tonight's score track tonight's true quality?
# - how well does the CHANGE in score from last night track the change in
# quality?
# - how well does a 7-night average track the 7-night true average?
# - of nights flagged "worse" (score down by 1 sd of its changes or more),
# how many were truly worse by at least 0.5 sd of true quality?
# 20,000 simulated nights per row, fixed seed.
import math
import random
def corr(x, y):
n = len(x)
mx, my = sum(x) / n, sum(y) / n
return sum((a - mx) * (b - my) for a, b in zip(x, y)) / math.sqrt(
sum((a - mx) ** 2 for a in x) * sum((b - my) ** 2 for b in y)
)
def series(sig_e, n, rng, rho=0.3):
q, qs, ss = 0.0, [], []
for _ in range(n):
q = rho * q + math.sqrt(1 - rho * rho) * rng.gauss(0, 1)
qs.append(q)
ss.append(q + rng.gauss(0, sig_e))
return qs, ss
if __name__ == "__main__":
rng = random.Random(57012)
print(
" last column: flagged worse nights truly worse by 0.5 sd or more"
)
print(" error sd night r change r 7-night r truly worse")
for sig in (0.5, 1.0, 1.5):
q, s = series(sig, 20000, rng)
dq = [b - a for a, b in zip(q, q[1:])]
ds = [b - a for a, b in zip(s, s[1:])]
q7 = [sum(q[i:i + 7]) / 7 for i in range(0, 20000 - 7, 7)]
s7 = [sum(s[i:i + 7]) / 7 for i in range(0, 20000 - 7, 7)]
sd_ds = math.sqrt(sum(x * x for x in ds) / len(ds))
flagged = [i for i, x in enumerate(ds) if x <= -sd_ds]
true_worse = sum(1 for i in flagged if dq[i] <= -0.5) / len(flagged)
print(
" {:6.1f} {:6.3f} {:6.3f} {:6.3f} {:6.3f}"
.format(sig, corr(q, s), corr(dq, ds), corr(q7, s7), true_worse)
)
print(
"\nReading: day-to-day changes in the score are noisier than the"
" score itself,"
)
print(
"because the change subtracts two errors while the true change is"
" damped by"
)
print(
"night-to-night correlation. Weekly averages recover most of the"
" signal."
)
# last column: flagged worse nights truly worse by 0.5 sd or more
# error sd night r change r 7-night r truly worse
# 0.5 0.893 0.859 0.928 0.922
# 1.0 0.708 0.640 0.800 0.750
# 1.5 0.552 0.487 0.640 0.645
#
# Reading: day-to-day changes in the score are noisier than the score itself,
# because the change subtracts two errors while the true change is damped by
# night-to-night correlation. Weekly averages recover most of the signal.当测量误差等于真实质量的夜间波动时(中间一行),今晚的分数与今晚真实质量的相关为 0.708——但与昨晚相比的分数变化,与真实变化的相关只有 0.640,因为变化相减了两个误差。被分数标为明显变差的夜晚中,0.750 确实变差了半个标准差以上,所以每四个被标记的夜晚就有一个是噪声。七晚的平均值恢复了更多信号(0.800)。大多数人会对之作出反应的那个数字——「今早我的分数掉了」——是屏幕上最不可靠的数字。
当分数本身变成原因
Baron 等人描述了一些患者,他们对完美追踪数据的追求本身成了失眠的一部分,并称之为「正睡症」(orthosomnia)(Baron et al. 2017)。关于睡眠的信念本身就能起作用:在一项实验中,被告知自己睡得特别好的人,在认知测试中的表现好于被告知睡得很差的人,与他们实际睡得如何无关 (Draganich and Erdal 2014)。第十三段程序把一种可能的机制写成反馈环,下面的标注框明确说明它是假说。
推导
# Chapter 6. A hypothesis, written as a model: "orthosomnia" as a feedback
# loop. True sleep quality q_t = -beta x worry_t + (independent night-to-night
# variation). The device reports s_t = q_t + e_t (error sd 1.0, as the middle
# row of p12). Worry the next night responds to a disappointing score:
# worry_{t+1} = a x max(0, 0.5 - s_t), where a is how strongly the sleeper
# reacts to the number (a = 0 means the score is not looked at). beta = 0.4:
# one unit of worry costs 0.4 sd of sleep quality. 50,000 simulated nights per
# row, fixed seed. NOT established - see the hypothesis box.
import math
import random
BETA = 0.4
def run(a, rng, n=50000):
worry, qs = 0.0, []
for _ in range(n):
q = -BETA * worry + rng.gauss(0, 1)
s = q + rng.gauss(0, 1.0)
worry = a * max(0.0, 0.5 - s)
qs.append(q)
m = sum(qs) / n
sd = math.sqrt(sum((x - m) ** 2 for x in qs) / (n - 1))
bad = sum(x < -1.0 for x in qs) / n
return m, sd, bad
if __name__ == "__main__":
rng = random.Random(57013)
print(
" reaction a mean true quality sd share of truly bad nights"
)
print(" (q < -1)")
for a in (0.0, 0.5, 1.0, 1.5, 2.0):
m, sd, bad = run(a, rng)
print(
" {:6.1f} {:+7.3f} {:5.3f} {:6.3f}"
.format(a, m, sd, bad)
)
print(
"\nReading: in this model a sleeper who reacts to the score sleeps"
" worse on"
)
print(
"average than one who ignores it, and more of the bad nights are"
" produced by"
)
print(
"the reaction to measurement error. Whether real sleepers behave like"
" this is"
)
print(
"the question; the model only shows the size such a loop could have."
)
# reaction a mean true quality sd share of truly bad nights
# (q < -1)
# 0.0 +0.007 1.000 0.157
# 0.5 -0.195 1.025 0.214
# 1.0 -0.485 1.105 0.316
# 1.5 -0.988 1.309 0.482
# 2.0 -2.314 1.887 0.740
#
# Reading: in this model a sleeper who reacts to the score sleeps worse on
# average than one who ignores it, and more of the bad nights are produced by
# the reaction to measurement error. Whether real sleepers behave like this is
# the question; the model only shows the size such a loop could have.对分数没有反应时(),真正糟糕的夜晚占 0.157。一个对令人失望的分数会在次夜感到担忧的睡眠者,平均睡得更差: 时为 −0.195, 时为 −0.485,糟糕夜晚的比例升到 0.316。在 到 之间,反馈环开始失控(均值先是 −0.988,再到 −2.314):坏分数带来担忧,担忧带来坏夜晚,而单单测量误差就足以把它启动。
假说 · 尚未验证
作者的假说(尚未验证): 在会查看每晚睡眠分数的人中,把分数隐藏三周能减轻失眠症状,且在自述会对分数作出反应的人中减轻得更多。什么结果会证明它错:隐藏分数与显示分数两个时期之间没有差别(第八章检验这一点),或者差别与自述的反应性无关。
小结
睡眠追踪器的验证对照的是人工判读者,而判读者之间只在大约五分之四的时段上一致,所以没有任何设备能被证明超过判读者自己的准确度;夜间平均值可能看上去正确,而逐时段的分期却很差。每晚的分数是带误差的测量:它从一晚到下一晚的变化比分数本身更嘈杂,一周的平均值有用得多。关于睡眠的信念本身就影响次日的功能;在一个简单模型里,一个对嘈杂分数作出反应的睡眠者,可以把测量误差变成真实的坏夜晚。
参考文献
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).