5D Library › 六维超立方体游走、卦变马尔可夫链与模运算星宫晶格 · English

第三章 卦变的马尔可夫链与谱隙

从本卦到之卦

起一卦得到本卦,动爻变化得到之卦。给定本卦的阴阳排列 xx,之卦 yy 是随机的:本卦的每一阳爻以概率 a=P(9)/P(阳)a = P(9)/P(\text{阳}) 变阴,每一阴爻以概率 b=P(6)/P(阴)b = P(6)/P(\text{阴}) 变阳,各爻独立。蓍草法下 a=3/8a = 3/8、b=1/8b = 1/8;铜钱法下 a=b=1/4a = b = 1/4。转移概率 PxyP_{xy} 是六个两状态步骤的乘积。

把「之卦再作为下一次的本卦」反复进行,就得到立方体六十四个顶点上的一条马尔可夫链。这不是任何一种传统的占法,而是一个数学工具:它的谱隙说明一次变卦保留了本卦多少信息,它的平稳分布说明变卦本身把卦推向哪里。

推导

乘积链的谱。 单爻的两状态链特征值为 1 与 λ=1−a−b\lambda = 1 – a – b,平稳阳爻比例为 π=b/(a+b)\pi = b/(a+b)。六爻独立,所以整条链的特征函数是乘积 χS(x)=∏i∈S(xi−π)\chi_S(x) = \prod_{i \in S}(x_i – \pi),对应特征值 λ|S|\lambda^{|S|},SS 取遍六爻的全部子集。谱隙为 1−λ=a+b1 – \lambda = a + b。

第六段程序用精确分数建立 64×6464 \times 64 的转移矩阵,逐一验证这 64 个特征函数。

推导

# Chapter 3. From a hexagram to its changed hexagram: a Markov chain.
# Cast a hexagram and let its moving lines flip: given the cast's yin/yang
# pattern x, the changed hexagram y is random. Per line, a yang line
# flips with a = P(9) / P(yang) and a yin line with b = P(6) / P(yin):
#   yarrow  a = 3/8, b = 1/8        coins  a = b = 1/4
# Lines flip independently, so P(x -> y) is a product of six 2 x 2 steps.
# Repeating "take the changed hexagram as the next cast pattern" defines a
# Markov chain on the 64 vertices of the cube. Its eigenfunctions are the
# products chi_S(x) = prod over i in S of (x_i - pi), pi = b / (a + b)
# the stationary share of yang, with eigenvalues (1 - a - b)^|S|. The
# program builds the 64 x 64 matrix exactly and checks all of this.
from fractions import Fraction as F
from itertools import combinations

def chain(a, b):
    states = [tuple((n >> i) & 1 for i in range(6)) for n in range(64)]
    def step(u, v):
        if u == 1:
            return a if v == 0 else 1 - a
        return b if v == 1 else 1 - b
    p = {}
    for x in states:
        for y in states:
            pr = F(1)
            for u, v in zip(x, y):
                pr *= step(u, v)
            p[x, y] = pr
    return states, p

def check(a, b):
    states, p = chain(a, b)
    rows_ok = all(sum(p[x, y] for y in states) == 1 for x in states)
    pi = b / (a + b)
    lam = 1 - a - b
    ok = True
    for k in range(7):
        for s in combinations(range(6), k):
            def chi(x):
                out = F(1)
                for i in s:
                    out *= x[i] - pi
                return out
            for x in states:
                lhs = sum(p[x, y] * chi(y) for y in states)
                ok &= lhs == lam ** k * chi(x)
    qian, kun = (1,) * 6, (0,) * 6
    return rows_ok, ok, pi, lam, p[qian, kun], p[kun, qian], states, p

if __name__ == "__main__":
    for label, a, b in (("yarrow", F(3, 8), F(1, 8)),
                        ("coins", F(1, 4), F(1, 4))):
        rows_ok, ok, pi, lam, qk, kq, states, p = check(a, b)
        sym = all(p[x, y] == p[y, x] for x in states for y in states)
        print("   {}: rows sum to 1: {}; eigenfunctions verified: {}"
              .format(label, rows_ok, ok))
        print("      second eigenvalue {} -> spectral gap {};"
              " stationary yang share {}".format(lam, 1 - lam, pi))
        print("      P(Qian -> Kun) = {}, P(Kun -> Qian) = {}".format(qk, kq))
        print("      matrix symmetric: {}".format(sym))
    print("\nReading: both methods give a chain with the same spectral gap,")
    print("1/2: every pattern of lines loses half its weight per step. The")
    print("coin chain is symmetric and drifts to all 64 hexagrams equally.")
    print("The yarrow chain is not: Qian turns into Kun 729 times as often")
    print("as Kun turns into Qian, and the chain drifts to a yin-heavy")
    print("state in which a line is yang only a quarter of the time.")
#    yarrow: rows sum to 1: True; eigenfunctions verified: True
#       second eigenvalue 1/2 -> spectral gap 1/2; stationary yang share 1/4
#       P(Qian -> Kun) = 729/262144, P(Kun -> Qian) = 1/262144
#       matrix symmetric: False
#    coins: rows sum to 1: True; eigenfunctions verified: True
#       second eigenvalue 1/2 -> spectral gap 1/2; stationary yang share 1/2
#       P(Qian -> Kun) = 1/4096, P(Kun -> Qian) = 1/4096
#       matrix symmetric: True
#
# Reading: both methods give a chain with the same spectral gap,
# 1/2: every pattern of lines loses half its weight per step. The
# coin chain is symmetric and drifts to all 64 hexagrams equally.
# The yarrow chain is not: Qian turns into Kun 729 times as often
# as Kun turns into Qian, and the chain drifts to a yin-heavy
# state in which a line is yang only a quarter of the time.

两种方法的谱隙都是 1/2:本卦的任何一种阴阳格局,经一次变卦只保留一半的「权重」。不同在于方向。铜钱法的矩阵是对称的,平稳分布是六十四卦均等;蓍草法的矩阵不对称——乾变坤的概率为 729/262144,坤变乾只有 1/262144,相差 36=7293^6 = 729 倍——平稳分布中每爻为阳的比例只有 1/4。反复变卦,蓍草法会把卦推向阴。

与「一次只变一爻」比较

谱隙的意义在与另一条链比较时最清楚。立方体上最自然的随机游走是每次只变一爻:以 1/2 的概率不动,否则随机挑一爻翻转。它的特征值为 1−k/61 – k/6,谱隙只有 1/6 (Levin et al. 2009; Diaconis 1988)。第七段程序从乾出发,计算三条链与各自平稳分布之间的总变差距离。

推导

# Chapter 3. How fast each chain forgets where it started.
# Start at Qian (all yang) and measure, after t steps, the total-variation
# distance to the chain's stationary distribution. Three chains:
#   one-line walk  a "lazy" walk on the cube: each step, with prob 1/2
#                  nothing changes, otherwise one of the six lines chosen
#                  at random flips (eigenvalues 1 - k/6: gap 1/6)
#   coins          the change chain of p06 (gap 1/2), stationary uniform
#   yarrow         the change chain of p06 (gap 1/2), stationary yang 1/4
# All three are symmetric under permuting lines, so the distance can be
# computed exactly on the number w of yang lines. The mixing time is the
# first t with distance below 1/4.
from fractions import Fraction as F
from math import comb

def binom(p):
    return [comb(6, w) * p ** w * (1 - p) ** (6 - w) for w in range(7)]

def tv(p, q):
    return sum(abs(a - b) for a, b in zip(p, q)) / 2

def walk_dist(t):
    d = [F(0)] * 7
    d[6] = F(1)
    for _ in range(t):
        n = [F(0)] * 7
        for w, pw in enumerate(d):
            n[w] += pw / 2
            if w:
                n[w - 1] += pw * F(w, 12)
            if w < 6:
                n[w + 1] += pw * F(6 - w, 12)
        d = n
    return d

if __name__ == "__main__":
    uniform = binom(F(1, 2))
    print("   t    one-line walk    coins     yarrow")
    mix = {}
    for t in range(1, 31):
        rows = {"walk": tv(walk_dist(t), uniform),
                "coins": tv(binom(F(1, 2) + F(1, 2) * F(1, 2) ** t),
                            uniform),
                "yarrow": tv(binom(F(1, 4) + F(3, 4) * F(1, 2) ** t),
                             binom(F(1, 4)))}
        for k, v in rows.items():
            if k not in mix and v < F(1, 4):
                mix[k] = t
        if t <= 6 or t in (10, 15, 20):
            print("   {:2d}     {:7.4f}       {:7.4f}   {:7.4f}".format(
                t, float(rows["walk"]), float(rows["coins"]),
                float(rows["yarrow"])))
    print("\n   mixing time (distance < 1/4): one-line walk {},"
          " coins {}, yarrow {}".format(mix["walk"], mix["coins"],
                                       mix["yarrow"]))
    print("\nReading: changing one line at a time, the cube takes about")
    print("eight steps to forget its start; letting every line move with")
    print("probability 1/4 forgets it in three. The two divination chains")
    print("mix equally fast but towards different places.")
#    t    one-line walk    coins     yarrow
#     1      0.8906        0.4868    0.6841
#     2      0.6823        0.2523    0.3636
#     3      0.5868        0.1232    0.2030
#     4      0.5000        0.0603    0.1070
#     5      0.4171        0.0297    0.0546
#     6      0.3447        0.0148    0.0276
#    10      0.1589        0.0009    0.0017
#    15      0.0619        0.0000    0.0001
#    20      0.0246        0.0000    0.0000
#
#    mixing time (distance < 1/4): one-line walk 8, coins 3, yarrow 3
#
# Reading: changing one line at a time, the cube takes about
# eight steps to forget its start; letting every line move with
# probability 1/4 forgets it in three. The two divination chains
# mix equally fast but towards different places.

一次只变一爻的游走需要 8 步,距离才降到 1/4 以下;两条起卦法的链都只需要 3 步。两者的差别正是谱隙 1/6 与 1/2 的差别:每爻都以 1/4 的概率独立地动,信息丢得比一次一爻快得多。蓍草法与铜钱法以同样的速度遗忘出发点,只是走向不同的地方。

从乾出发,到平稳分布的总变差距离随步数的变化(纵轴为对数刻度;虚线为 1/4)。

注

这对「卦变推演」一类说法意味着什么。 一次变卦保留的是本卦的一部分结构,而不是全部:低阶的格局保留得多,高阶的格局几乎不留。第四章会把这一点用于吉凶判别分数:分数中一阶的部分在之卦中保留一半,二阶的部分只保留四分之一。

小结

本卦到之卦的变化是立方体上的乘积马尔可夫链,特征函数是各爻的乘积,特征值为 (1−a−b)|S|(1-a-b)^{|S|}。蓍草法与铜钱法的谱隙都是 1/2,三步之内遗忘出发点,比一次只变一爻的游走(谱隙 1/6,需要 8 步)快得多。区别在于方向:铜钱法的链对称、平稳分布均匀;蓍草法的链不对称,乾变坤是坤变乾的 729 倍,平稳分布中每爻只有四分之一为阳。

参考文献

Diaconis, Persi. 1988. Group Representations in Probability and Statistics. Institute of Mathematical Statistics.
Levin, David A., Yuval Peres, and Elizabeth L. Wilmer. 2009. Markov Chains and Mixing Times. American Mathematical Society.

Fan, Y. (2026). Six-Dimensional Hypercube Walks, Hexagram Mutation Markov Chains and Modular Star-Palace Lattices: Boolean Cube Spectral Gaps, Yarrow-versus-Coin Probability Biases and Twelve-Palace Dihedral Projections [六维超立方体游走、卦变马尔可夫链与模运算星宫晶格] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090176 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).