5D Library › 四柱排盘的历法计算与规则引擎 · English

第五章 旺衰是加权图上的打分

把「身强身弱」写成程序

子平术的核心判断之一是日主的旺衰:日主得令(月支)、得地(地支有根)、得生(印星)、得助(比劫),则身强,否则身弱;极弱而无根者,取「从格」。这些说法可以写成一张加权图:日主是一个节点,其余七个位置(年干、年支、月干、月支、日支、时干、时支)各是一个节点,每个节点带一个五行权重向量(天干是单一五行,地支按第四章的藏干权重),连向日主的边按五行关系取正或负的权重,月支与日支的位置另有倍数。

设支持为同我五行加上生我五行(乘以印的权重),耗泄为我生、我克、克我三种五行(各乘以权重),rr 为支持占支持与耗泄之和的比例。rr 不低于阈值 τ\tau 为身强;rr 低于下限 φ\varphi 且日主五行在四个地支中都没有根,为从格;其余为身弱。

这里的每一个数——月令倍数、日支倍数、印的权重、三种耗泄的权重、藏干的权重、两个阈值——在不同的书与软件中都不一样。下面的程序设了三套参数 A、B、C,都是说得通的取法,但不是任何一个具体流派的规则,然后对第一章的全部 518,400 种四柱逐一判定。

推导

# 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.
三种参数下身强、身弱、从格在全部四柱中的比例。

三套参数下,身强的比例从 0.235 到 0.391,身弱从 0.544 到 0.728,从格从 0.022 到 0.121。两两比较,判定相同的盘占 0.768 到 0.819,扣除偶然一致后的 Cohen’s κ (Cohen 1960) 为 0.553 到 0.622,按通行的分级只是「中等」一致 (Landis and Koch 1977);三套参数完全一致的盘只占 0.684。也就是说,大约三张盘中有一张,至少一套说得通的参数会给出与其他两套不同的判定。从格尤其依赖约定:只取本气的 A 把 0.121 的盘判为从格,用人元司令天数的 C 只有 0.022。

哪一个参数最要紧

推导

# 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.

从参数 B 出发,每次只改一个参数:月令倍数从 2.5 改为 2.0 或 3.0,判定改变的盘为 0.036 与 0.031;阈值从 0.50 改为 0.45 或 0.55,改变 0.092 与 0.076;印的权重从 0.8 改为 1.0,改变 0.052;藏干从三气按比例改为只取本气,改变 0.233,改为人元司令天数,改变 0.058。藏干怎样计数,比月令看得多重要得多。

最后,把每张盘的时支移到相邻的时辰(同一天),判定改变的比例为 0.127,约八张盘中一张。把它与第三章的时柱出错概率相乘:出生时间有标准差 15 分钟的误差时,约 0.013 的人仅仅因为时间误差被判为相反的旺衰,约八十分之一。

假说 · 尚未验证

作者的推测(尚未验证): 不同流派对同一张盘的旺衰争论,有相当一部分不是对命理的不同理解,而是对藏干权重与阈值这两个参数的不同取值;把参数写明,许多争论会变成可以计算的差异。什么会证明这一推测错:若让不同流派的实践者对同一批盘独立判定,他们之间的分歧不能由少数参数的差别解释(例如,用第八章的一致性研究,拟合各人的参数后仍然 κ 很低),这一推测就不成立。

小结

旺衰判断可以写成加权图上的打分。三套说得通的参数对 518,400 种四柱的判定,两两 κ 只有 0.553 到 0.622,三者一致的盘占 0.684;从格的比例在 0.022 与 0.121 之间,几乎完全由约定决定。藏干的计数方法是最敏感的参数;时支差一格,判定对约八分之一的盘改变。

参考文献

Cohen, Jacob. 1960. “A Coefficient of Agreement for Nominal Scales.” Educational and Psychological Measurement 20 (1): 37–46.
Landis, J. Richard, and Gary G. Koch. 1977. “The Measurement of Observer Agreement for Categorical Data.” Biometrics 33 (1): 159–74.

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).