5D Library › Six-Dimensional Hypercube Walks, Hexagram Mutation Markov Chains and Modular Star-Palace Lattices · 中文
Chapter 3. The Markov Chain of Changing Hexagrams and Its Spectral Gap
From the cast hexagram to the changed one
A cast gives the cast hexagram (ben gua); letting the moving lines change gives the changed hexagram (zhi gua). Given the cast hexagram’s pattern , the changed hexagram is random: each yang line of the cast becomes yin with probability , each yin line becomes yang with probability , line by line independently. With yarrow and ; with coins . The transition probability is the product of six two-state steps.
Taking “the changed hexagram as the next cast pattern” again and again gives a Markov chain on the sixty-four vertices of the cube. This is not any traditional way of divining but a mathematical tool: its spectral gap says how much of the cast hexagram one change keeps, and its stationary distribution says where changing itself pushes the hexagrams.
Calculation
The spectrum of a product chain. The two-state chain of one line has eigenvalues 1 and and a stationary yang share . The six lines are independent, so the eigenfunctions of the whole chain are the products with eigenvalues , ranging over all subsets of the six lines. The spectral gap is .
The sixth program builds the transition matrix in exact fractions and checks all 64 eigenfunctions one by one.
Calculation
# 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.Both methods have spectral gap 1/2: any pattern of yin and yang in the cast hexagram keeps only half its “weight” through one change. The difference is in direction. The coin matrix is symmetric, and its stationary distribution gives all sixty-four hexagrams equally. The yarrow matrix is not — Qian changes into Kun with probability 729/262144 and Kun into Qian with only 1/262144, a factor of — and in its stationary distribution a line is yang only 1/4 of the time. Changing again and again, the yarrow method pushes the hexagrams towards yin.
Compared with changing one line at a time
The meaning of a spectral gap is clearest by comparison with another chain. The most natural random walk on the cube changes one line at a time: with probability 1/2 nothing happens, otherwise a randomly chosen line flips. Its eigenvalues are and its gap only 1/6 (Levin et al. 2009; Diaconis 1988). The seventh program starts all three chains from Qian and computes the total-variation distance to each one’s stationary distribution.
Calculation
# 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.The one-line walk needs 8 steps before the distance falls below 1/4; the two casting chains need only 3. The difference is exactly that between gaps of 1/6 and 1/2: with every line moving independently with probability 1/4, information is lost much faster than one line at a time. Yarrow and coins forget the starting point equally fast; they only head for different places.
Note
What this means for claims of “working out the change”. One change keeps part of the cast hexagram’s structure, not all of it: low-order patterns survive well, high-order ones hardly at all. Chapter 4 applies this to a good-or-bad score: the first-order part of a score keeps half its weight in the changed hexagram, the second-order part only a quarter.
Summary
The change from cast to changed hexagram is a product Markov chain on the cube whose eigenfunctions are products over lines with eigenvalues . Yarrow and coins both have spectral gap 1/2 and forget their starting point within three steps, far faster than the one-line walk (gap 1/6, 8 steps). The difference is direction: the coin chain is symmetric with a uniform stationary distribution; the yarrow chain is not, Qian changing into Kun 729 times as often as Kun into Qian, and in its stationary distribution only a quarter of the lines are yang.
References
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).