5D Library › Autonomic Sympathovagal Balance, Six-Meridian State-Space Models and Multimodal Biomarker Inversion · 中文

Chapter 4. The Six Stages as a Hidden Markov State Space

Writing “transmission between stages” as a transition matrix

The Shanghan Lun (Treatise on Cold Damage) sums up the course of externally contracted illness in six stages, taiyang, yangming, shaoyang, taiyin, shaoyin and jueyin, and distinguishes passing from one stage to the next in order (“sequential transmission”), skipping a stage, and two stages at once (“combined disease”) (the original wording is quoted in Chapter 7). Write this as a hidden Markov model (Rabiner 1989): the hidden state is one of the six stages, and each day it stays, moves to the next stage or skips one with given probabilities; the observations are four measurable physiological quantities, namely the temperature-rhythm amplitude and core-to-skin gradient of Chapter 3, plus gut (vagal) motility rate and the cortisol awakening response (CAR). Each stage has its own mean on the four measures (illustrative settings), and the Viterbi algorithm works back from fourteen days of observations to the stage on each day.

Calculation

# Chapter 4. p07: The six stages as a hidden Markov model. Hidden state:
# taiyang, yangming, shaoyang, taiyin, shaoyin, jueyin (illustrative
# order of ordinary progression). Daily transitions: stay 0.70, move to
# the next stage 0.20 ("sequential transmission"), skip one stage 0.05
# ("skipping"), any other stage 0.05 shared. Each day four measurements
# are seen, in standard units: temperature-rhythm amplitude, core-to-skin
# gradient, gut (vagal) motility rate, cortisol awakening response; each
# stage has its own mean (illustrative) and noise sd 0.8. Viterbi decoding
# with the true model, 3,000 courses of 14 days, seed 86.
import random
from math import log, pi

STAGES = ("taiyang", "yangming", "shaoyang", "taiyin", "shaoyin", "jueyin")
MEAN = ((1.0, 1.2, 0.0, 0.5), (1.5, -0.5, -0.8, 1.0),
        (0.5, 0.3, -0.3, 0.3), (-0.3, 0.0, 1.0, -0.3),
        (-1.0, 0.8, 0.3, -1.0), (-0.6, 1.2, -0.6, -0.7))
SD = 0.8

def trans():
    T = []
    for i in range(6):
        row = [0.05 / 3] * 6
        row[i] = 0.70
        if i + 1 < 6:
            row[i + 1] = 0.20
        if i + 2 < 6:
            row[i + 2] = 0.05
        z = sum(row)
        T.append([x / z for x in row])
    return T

T = trans()

def logpdf(x, m, sd=SD):
    return sum(-0.5 * ((a - b) / sd) ** 2 - log(sd) - 0.5 * log(2 * pi)
               for a, b in zip(x, m))

def course(rng, days=14):
    s, xs, ss = 0, [], []
    for _ in range(days):
        ss.append(s)
        xs.append([m + rng.gauss(0, SD) for m in MEAN[s]])
        u, acc = rng.random(), 0.0
        for j in range(6):
            acc += T[s][j]
            if u < acc:
                s = j
                break
    return ss, xs

def viterbi(xs, start=(1.0, 0, 0, 0, 0, 0)):
    lt = [[log(p) for p in row] for row in T]
    v = [(log(start[j]) if start[j] > 0 else -1e9) + logpdf(xs[0], MEAN[j])
         for j in range(6)]
    back = []
    for x in xs[1:]:
        nv, bk = [], []
        for j in range(6):
            i = max(range(6), key=lambda i: v[i] + lt[i][j])
            nv.append(v[i] + lt[i][j] + logpdf(x, MEAN[j]))
            bk.append(i)
        v, back = nv, back + [bk]
    s = max(range(6), key=lambda j: v[j])
    path = [s]
    for bk in reversed(back):
        s = bk[s]
        path.append(s)
    return path[::-1]

if __name__ == "__main__":
    rng = random.Random(86)
    hit = tot = 0
    by = [[0, 0] for _ in range(6)]
    conf = {}
    for _ in range(3000):
        ss, xs = course(rng)
        est = viterbi(xs)
        for a, b in zip(ss, est):
            hit += a == b
            tot += 1
            by[a][0] += a == b
            by[a][1] += 1
            if a != b:
                conf[(a, b)] = conf.get((a, b), 0) + 1
    print("   days decoded correctly: {:.3f}".format(hit / tot))
    for i, n in enumerate(STAGES):
        print("   {:<9} {:.3f}  ({} days)".format(n, by[i][0] / by[i][1],
                                                 by[i][1]))
    a, b = max(conf, key=conf.get)
    print("   most common mix-up: {} read as {}".format(STAGES[a],
                                                     STAGES[b]))
#    days decoded correctly: 0.830
#    taiyang   0.916  (11342 days)
#    yangming  0.871  (7267 days)
#    shaoyang  0.727  (6638 days)
#    taiyin    0.812  (5770 days)
#    shaoyin   0.700  (4695 days)
#    jueyin    0.853  (6288 days)
#    most common mix-up: shaoyin read as jueyin

With the model exactly right, the share of days decoded correctly is 0.830. Taiyang is the easiest to recognise (0.916) and shaoyin the hardest (0.700); the most common error is reading shaoyin as jueyin. Shaoyang’s 0.727 is also low: on the four measures it sits between taiyang and yangming.

Identifiability: which stages cannot be told apart in the first place

Whether two stages can be separated depends on how far apart their means are, relative to the measurement noise. p08 uses the Bhattacharyya distance DD to give an upper bound e−D/2e^{-D}/2 on the one-day error, then lets the noise grow and watches the Viterbi accuracy fall.

Calculation

# Chapter 4. p08: How far apart are the stages? For two stages with
# equal-noise Gaussian measurements the Bhattacharyya distance is
# D = |m1 - m2|^2 / (8 sd^2), and a one-day Bayes error (equal priors) is
# at most exp(-D) / 2. Then the Viterbi accuracy of p07 as measurement
# noise grows, and the size of the model if "combined disease" (two
# stages at once) needs its own states: 6 + 15 = 21 states.
import importlib.util
import random
from itertools import combinations
from math import exp, log, pi

def load(name):
    spec = importlib.util.spec_from_file_location(name, name + ".py")
    m = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(m)
    return m

p07 = load("p07_six_stage_hmm")

def bhatt(a, b, sd):
    return sum((x - y) ** 2 for x, y in zip(a, b)) / (8 * sd * sd)

def accuracy(sd, n=1500, seed=86):
    p07.SD = sd
    p07.logpdf = lambda x, m: sum(-0.5 * ((a - b) / sd) ** 2 - log(sd)
                                  - 0.5 * log(2 * pi) for a, b in zip(x, m))
    rng = random.Random(seed)
    hit = tot = 0
    for _ in range(n):
        ss, xs = p07.course(rng)
        est = p07.viterbi(xs)
        hit += sum(a == b for a, b in zip(ss, est))
        tot += len(ss)
    return hit / tot

if __name__ == "__main__":
    pairs = sorted(combinations(range(6), 2),
                   key=lambda p: bhatt(p07.MEAN[p[0]], p07.MEAN[p[1]], 0.8))
    print("   closest pairs at sd 0.8    D      one-day error bound")
    for i, j in pairs[:3]:
        d = bhatt(p07.MEAN[i], p07.MEAN[j], 0.8)
        print("   {:<9} vs {:<9}  {:6.3f}  {:.3f}".format(
            p07.STAGES[i], p07.STAGES[j], d, exp(-d) / 2))
    for sd in (0.6, 0.8, 1.2, 1.6):
        print("   noise sd {:.1f}: Viterbi accuracy {:.3f}".format(
            sd, accuracy(sd)))
    print("   states with every two-stage combination: {} (transitions {})"
          .format(6 + 15, 21 * 20))
#    closest pairs at sd 0.8    D      one-day error bound
#    taiyang   vs shaoyang    0.232  0.396
#    shaoyin   vs jueyin      0.238  0.394
#    taiyin    vs shaoyin     0.412  0.331
#    noise sd 0.6: Viterbi accuracy 0.909
#    noise sd 0.8: Viterbi accuracy 0.829
#    noise sd 1.2: Viterbi accuracy 0.693
#    noise sd 1.6: Viterbi accuracy 0.589
#    states with every two-stage combination: 21 (transitions 420)

The two closest pairs are taiyang and shaoyang (DD = 0.232, one-day error bound 0.396) and shaoyin and jueyin (0.238, 0.394): looking at a single day, these pairs are hardly better than a coin toss. Viterbi still reaches 0.830 because of continuity across neighbouring days. As the noise standard deviation grows from 0.6 to 1.6, accuracy falls from 0.909 to 0.589. If “combined disease” is to have its own states, the six single stages plus fifteen two-stage combinations make 21 states with 420 transition probabilities, which could hardly be estimated from the amount of course data that can be collected.

Viterbi decoding accuracy falls as measurement noise grows (p08).

Hypothesis · not established

The author’s interpretation (not yet tested): the course through the six stages can be described by a hidden Markov model observed through the temperature rhythm, the core-to-skin gradient, gut motility and the cortisol awakening response, and such a model reproduces a transition structure in which sequential transmission is more common than skipping. What would show it wrong: in prospective course data where practitioners independently assign the stage day by day, a model fitted on these four measures predicts the practitioners’ assignments no better than a baseline that uses only the day number, or the fitted transition matrix does not show sequential transmission as more common than skipping.

Summary

Writing the six stages as a state space makes it possible to ask plainly which stages cannot be told apart on measurable quantities, and how little noise the measurements must have before the course can be tracked reliably. Under the illustrative settings, taiyang and shaoyang, and shaoyin and jueyin, are the hardest to separate; modelling “combined disease” on its own needs more parameters than the data can support.

References

Rabiner, Lawrence R. 1989. “A Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition.” Proceedings of the IEEE 77 (2): 257 to 286.

Fan, Y. (2026). Autonomic Sympathovagal Balance, Six-Meridian State-Space Models and Multimodal Biomarker Inversion: Heart-Rate Variability Spectral Decomposition, Thermoregulatory Phase Drift and Bayesian Syndrome Classification [自主神经交感迷走平衡、六经辨证状态空间与多模态标志物反演] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090227 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).