5D Library › Four-Pillar Calendrical Computation and Formal Rule Engines · 中文
Chapter 5. Strength Is Scoring on a Weighted Graph
Writing “strong” and “weak” as a program
One of the central judgements of the Ziping art is the strength of the day master: if it is in season (the month branch), rooted (in the branches), supported by resources and helped by peers, it is strong; otherwise it is weak; if it is extremely weak and has no root, the chart “follows”. These statements can be written as a weighted graph. The day master is one node; the seven other positions (year stem and branch, month stem and branch, day branch, hour stem and branch) are nodes too, each carrying a weight vector over the five elements (a stem has one element, a branch the hidden-stem weights of Chapter 4); the edge to the day master takes a positive or negative weight according to the element relation, and the month and day branches carry extra multipliers.
Let support be the self element plus the element that generates it (times a resource weight), drain the three other relations (each times a weight), and the share of support in support plus drain. If is at least the threshold the chart is strong; if is below a floor and the day master’s element has no root in any of the four branches, it follows; otherwise it is weak.
Every number here — the month multiplier, the day-branch multiplier, the resource weight, the three drain weights, the hidden-stem weights, the two thresholds — differs from book to book and program to program. The next program sets three parameter sets, A, B and C, each defensible but not the rules of any particular school, and judges every one of the 518,400 charts of Chapter 1.
Calculation
# Chapter 5. A strength-scoring engine and three conventions.
# Every chart (60 year pillars x 12 months x 60 day pillars x 12 hours)
# is scored from the day stem's point of view. Each of the seven other
# positions contributes an element vector (stems: their own element;
# branches: hidden stems weighted as in p09), times a position weight.
# Support = same element + w_res x the element that generates the day
# element; drain = the three other relations, with weights. With
# r = support / (support + drain):
# strong if r >= tau; "follow" if r < phi and the day element has no
# root in any branch; otherwise weak.
# A, B and C are three invented but defensible parameter sets, not the
# rules of any named school.
HIDDEN = ((9,), (5, 9, 7), (0, 2, 4), (1,), (4, 1, 9), (2, 4, 6), (3, 5),
(5, 3, 1), (6, 8, 4), (7,), (4, 7, 3), (8, 0))
DAYS = (((8, 10), (9, 20)), ((9, 9), (7, 3), (5, 18)),
((4, 7), (2, 7), (0, 16)), ((0, 10), (1, 20)),
((1, 9), (9, 3), (4, 18)), ((4, 5), (6, 9), (2, 16)),
((2, 10), (5, 9), (3, 11)), ((3, 9), (1, 3), (5, 18)),
((4, 10), (8, 3), (6, 17)), ((6, 10), (7, 20)),
((7, 9), (3, 3), (4, 18)), ((4, 7), (0, 5), (8, 18)))
SCHOOLS = { # hidden, month w, day-branch w, w_res, drains, tau, phi
"A": ("S1", 3.0, 1.5, 1.0, (1.0, 1.0, 1.0), 0.50, 0.20),
"B": ("S2", 2.5, 1.2, 0.8, (0.8, 1.0, 1.2), 0.50, 0.20),
"C": ("S3", 4.0, 1.0, 1.0, (1.0, 1.0, 1.0), 0.45, 0.15)}
def branch_vec(b, scheme):
v, h = [0.0] * 5, HIDDEN[b]
if scheme == "S1":
pairs = [(h[0], 1.0)]
elif scheme == "S2":
pairs = zip(h, {1: (1.0,), 2: (0.7, 0.3), 3: (0.6, 0.3, 0.1)}[len(h)])
else:
pairs = [(s, d / 30) for s, d in DAYS[b]]
for s, w in pairs:
v[s // 2] += w
return v
def stem_vec(s):
v = [0.0] * 5
v[s // 2] = 1.0
return v
def classify_all(p):
scheme, mw, dw, wres, drains, tau, phi = p
bv = [branch_vec(b, scheme) for b in range(12)]
year, day = [], []
for y in range(60):
ys, yb = y % 10, y % 12
for mb in range(12):
ms = (2 * (ys % 5) + 2 + (mb - 2) % 12) % 10
v = [a + c + d + mw * e for a, c, d, e in zip(
stem_vec(ys), bv[yb], stem_vec(ms), bv[mb])]
roots = {e for e in range(5) if bv[yb][e] or bv[mb][e]}
year.append((v, roots))
for dd in range(60):
ds, db = dd % 10, dd % 12
for hb in range(12):
hs = (2 * (ds % 5) + hb) % 10
v = [dw * a + c + d for a, c, d in zip(bv[db], stem_vec(hs),
bv[hb])]
roots = {e for e in range(5) if bv[db][e] or bv[hb][e]}
day.append((ds // 2, v, roots))
out = []
for yv, yr in year:
for de, dv, dr in day:
v = [a + b for a, b in zip(yv, dv)]
sup = v[de] + wres * v[(de - 1) % 5]
dra = (drains[0] * v[(de + 1) % 5] + drains[1] * v[(de + 2) % 5]
+ drains[2] * v[(de + 3) % 5])
r = sup / (sup + dra)
if r >= tau:
out.append(0)
elif r < phi and de not in yr and de not in dr:
out.append(2)
else:
out.append(1)
return out
def kappa(a, b, k=3):
n = len(a)
conf = [[0] * k for _ in range(k)]
for x, y in zip(a, b):
conf[x][y] += 1
po = sum(conf[i][i] for i in range(k)) / n
pe = sum(sum(conf[i]) * sum(r[i] for r in conf) for i in range(k)) / n / n
return po, (po - pe) / (1 - pe)
if __name__ == "__main__":
cls = {s: classify_all(p) for s, p in SCHOOLS.items()}
n = len(cls["A"])
print(" {:,d} charts; share strong / weak / follow:".format(n))
for s, c in cls.items():
print(" {}: {:.3f} / {:.3f} / {:.3f}".format(
s, c.count(0) / n, c.count(1) / n, c.count(2) / n))
for a, b in (("A", "B"), ("A", "C"), ("B", "C")):
po, k = kappa(cls[a], cls[b])
print(" {} vs {}: agreement {:.3f}, Cohen's kappa {:.3f}".format(
a, b, po, k))
allsame = sum(x == y == z for x, y, z in zip(*cls.values())) / n
print(" all three agree on {:.3f} of charts".format(allsame))
print("\nReading: all three conventions agree on about two charts in")
print("three; kappa between 0.55 and 0.62 is only moderate agreement.")
print("The share of 'follow' charts ranges from 0.022 to 0.121: whether")
print("a chart follows is decided mostly by the convention.")
# 518,400 charts; share strong / weak / follow:
# A: 0.334 / 0.544 / 0.121
# B: 0.235 / 0.728 / 0.037
# C: 0.391 / 0.587 / 0.022
# A vs B: agreement 0.768, Cohen's kappa 0.553
# A vs C: agreement 0.781, Cohen's kappa 0.600
# B vs C: agreement 0.819, Cohen's kappa 0.622
# all three agree on 0.684 of charts
#
# Reading: all three conventions agree on about two charts in
# three; kappa between 0.55 and 0.62 is only moderate agreement.
# The share of 'follow' charts ranges from 0.022 to 0.121: whether
# a chart follows is decided mostly by the convention.Across the three parameter sets the share of strong charts runs from 0.235 to 0.391, weak from 0.544 to 0.728, and follow from 0.022 to 0.121. Pair by pair, the verdicts agree on 0.768 to 0.819 of charts, and Cohen’s κ (Cohen 1960), which removes chance agreement, is 0.553 to 0.622 — only “moderate” agreement on the usual scale (Landis and Koch 1977); all three agree on only 0.684 of charts. In about one chart in three, then, at least one defensible parameter set gives a verdict different from the other two. “Follow” depends on convention most of all: A, counting only main stems, makes 0.121 of charts follow; C, using the days-in-command table, only 0.022.
Which parameter matters most
Calculation
# Chapter 5. Which single parameter matters, and the hour next door.
# The engine of p10 is repeated here (programs in this book do not
# import one another). Starting from convention B, one parameter at a
# time is changed and the share of the 518,400 charts whose class
# (strong / weak / follow) changes is counted. Then the hour branch of
# every chart is moved to the neighbouring double-hour (same day) to see
# how often a one-slot timing error changes the class.
HIDDEN = ((9,), (5, 9, 7), (0, 2, 4), (1,), (4, 1, 9), (2, 4, 6), (3, 5),
(5, 3, 1), (6, 8, 4), (7,), (4, 7, 3), (8, 0))
DAYS = (((8, 10), (9, 20)), ((9, 9), (7, 3), (5, 18)),
((4, 7), (2, 7), (0, 16)), ((0, 10), (1, 20)),
((1, 9), (9, 3), (4, 18)), ((4, 5), (6, 9), (2, 16)),
((2, 10), (5, 9), (3, 11)), ((3, 9), (1, 3), (5, 18)),
((4, 10), (8, 3), (6, 17)), ((6, 10), (7, 20)),
((7, 9), (3, 3), (4, 18)), ((4, 7), (0, 5), (8, 18)))
B = ("S2", 2.5, 1.2, 0.8, (0.8, 1.0, 1.2), 0.50, 0.20) # as in p10
def branch_vec(b, scheme):
v, h = [0.0] * 5, HIDDEN[b]
if scheme == "S1":
pairs = [(h[0], 1.0)]
elif scheme == "S2":
pairs = zip(h, {1: (1.0,), 2: (0.7, 0.3), 3: (0.6, 0.3, 0.1)}[len(h)])
else:
pairs = [(s, d / 30) for s, d in DAYS[b]]
for s, w in pairs:
v[s // 2] += w
return v
def stem_vec(s):
v = [0.0] * 5
v[s // 2] = 1.0
return v
def classify_all(p):
scheme, mw, dw, wres, drains, tau, phi = p
bv = [branch_vec(b, scheme) for b in range(12)]
year, day = [], []
for y in range(60):
ys, yb = y % 10, y % 12
for mb in range(12):
ms = (2 * (ys % 5) + 2 + (mb - 2) % 12) % 10
v = [a + c + d + mw * e for a, c, d, e in zip(
stem_vec(ys), bv[yb], stem_vec(ms), bv[mb])]
roots = {e for e in range(5) if bv[yb][e] or bv[mb][e]}
year.append((v, roots))
for dd in range(60):
ds, db = dd % 10, dd % 12
for hb in range(12):
hs = (2 * (ds % 5) + hb) % 10
v = [dw * a + c + d for a, c, d in zip(bv[db], stem_vec(hs),
bv[hb])]
roots = {e for e in range(5) if bv[db][e] or bv[hb][e]}
day.append((ds // 2, v, roots))
out = []
for yv, yr in year:
for de, dv, dr in day:
v = [a + b for a, b in zip(yv, dv)]
sup = v[de] + wres * v[(de - 1) % 5]
dra = (drains[0] * v[(de + 1) % 5] + drains[1] * v[(de + 2) % 5]
+ drains[2] * v[(de + 3) % 5])
r = sup / (sup + dra)
if r >= tau:
out.append(0)
elif r < phi and de not in yr and de not in dr:
out.append(2)
else:
out.append(1)
return out
def variant(**kw):
names = ("scheme", "mw", "dw", "wres", "drains", "tau", "phi")
p = dict(zip(names, B))
p.update(kw)
return tuple(p[k] for k in names)
if __name__ == "__main__":
base = classify_all(B)
n = len(base)
print(" change from convention B class changes for")
for label, kw in (("month weight 2.5 -> 2.0", {"mw": 2.0}),
("month weight 2.5 -> 3.0", {"mw": 3.0}),
("threshold 0.50 -> 0.45", {"tau": 0.45}),
("threshold 0.50 -> 0.55", {"tau": 0.55}),
("hidden stems S2 -> S1", {"scheme": "S1"}),
("hidden stems S2 -> S3", {"scheme": "S3"}),
("resource weight 0.8 -> 1.0", {"wres": 1.0})):
other = classify_all(variant(**kw))
print(" {:28s} {:.3f}".format(
label, sum(a != b for a, b in zip(base, other)) / n))
moved = 0
for i, c in enumerate(base):
hb = i % 12
up, down = i - hb + (hb + 1) % 12, i - hb + (hb - 1) % 12
moved += (base[up] != c) + (base[down] != c)
p_move = moved / (2 * n)
print(" moving the hour one slot changes the class: {:.3f}".format(
p_move))
p_slot = 0.09974 # hour pillar wrong, normal s.d. 15 min (p05)
print(" with a 15-minute timing error: wrong class for about {:.3f}"
.format(p_slot * p_move))
print("\nReading: no single parameter is harmless. Counting only main")
print("stems moves the most charts, then the threshold. A one-slot")
print("error in the hour changes the class for about one chart in")
print("eight, so a quarter-hour of timing error misclassifies about")
print("one chart in eighty.")
# change from convention B class changes for
# month weight 2.5 -> 2.0 0.036
# month weight 2.5 -> 3.0 0.031
# threshold 0.50 -> 0.45 0.092
# threshold 0.50 -> 0.55 0.076
# hidden stems S2 -> S1 0.233
# hidden stems S2 -> S3 0.058
# resource weight 0.8 -> 1.0 0.052
# moving the hour one slot changes the class: 0.127
# with a 15-minute timing error: wrong class for about 0.013
#
# Reading: no single parameter is harmless. Counting only main
# stems moves the most charts, then the threshold. A one-slot
# error in the hour changes the class for about one chart in
# eight, so a quarter-hour of timing error misclassifies about
# one chart in eighty.Starting from B and changing one parameter at a time: moving the month multiplier from 2.5 to 2.0 or 3.0 changes the verdict for 0.036 and 0.031 of charts; moving the threshold from 0.50 to 0.45 or 0.55 for 0.092 and 0.076; the resource weight from 0.8 to 1.0 for 0.052; counting only main stems instead of the three weighted hidden stems for 0.233, and using the days-in-command weights for 0.058. How the hidden stems are counted matters far more than how heavily the season is weighted.
Finally, moving the hour branch of every chart to the neighbouring double-hour (same day) changes the verdict for 0.127 of charts, about one in eight. Multiplied by the chance of a wrong hour pillar from Chapter 3: with a birth-time error of standard deviation 15 minutes, about 0.013 of people are judged of opposite strength because of the timing error alone, about one in eighty.
Hypothesis · not established
The author’s conjecture (not yet tested): a good part of the disagreement between schools about the strength of a given chart is not a different understanding of fate but a different choice of two parameters, the hidden-stem weights and the threshold; once the parameters are written down, many disputes become differences that can be computed. What would show it wrong: if practitioners of different schools judged the same charts independently and their disagreements could not be explained by differences in a few parameters (for example, if κ stayed low in the agreement study of Chapter 8 even after fitting each practitioner’s parameters), the conjecture would fail.
Summary
The judgement of strength can be written as scoring on a weighted graph. Over the 518,400 charts, three defensible parameter sets reach a pairwise κ of only 0.553 to 0.622, and all three agree on 0.684 of charts; the share of “follow” charts lies between 0.022 and 0.121 and is almost wholly a matter of convention. The counting of hidden stems is the most sensitive parameter; one double-hour of difference changes the verdict for about one chart in eight.
References
Fan, Y. (2026). Four-Pillar Calendrical Computation and Formal Rule Engines: Julian-Day Sexagenary Cycles, Midnight-Boundary Disagreements, Birth-Time Sensitivity Bounds and Weighted-Graph Strength Scoring [四柱排盘的历法计算与规则引擎] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090180 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).