5D Library › 体制转换分数凯利、最大回撤约束与生命周期杠杆配置 · English

第三章 隐藏的体制:熊市能多快被识别,体制感知的凯利能带来什么

滤波器与它的延迟

如果市场有两种隐藏体制,下注者对当前体制的最好信息是一个概率。汉密尔顿滤波器每月根据当月收益,利用各体制的均值、波动率与持续性更新这个概率 (Hamilton 1989)。p05 在第一章的市场上运行 400,000 个月,并让滤波器知道真实参数,这是最有利的情形。

推导

# Chapter 3. p05: How fast can a hidden bear regime be recognized? The
# Hamilton filter of the two-regime market (p02), with the true
# parameters known, updates the chance of "bear" each month from that
# month's excess return. Delay = months from the first bear month
# until the filtered chance first exceeds a threshold; a false alarm
# is a bull month whose filtered chance exceeds it. 400,000 months,
# seed 98.
import random
from math import exp, pi, sqrt

BULL, BEAR = (0.12 / 12, 0.13 / sqrt(12)), (-0.25 / 12, 0.28 / sqrt(12))
STAY = (0.98, 0.92)

def dens(x, m, sd):
    return exp(-0.5 * ((x - m) / sd) ** 2) / (sd * sqrt(2 * pi))

def step(p_bear, x):
    prior = p_bear * STAY[1] + (1 - p_bear) * (1 - STAY[0])
    a = prior * dens(x, *BEAR)
    b = (1 - prior) * dens(x, *BULL)
    return a / (a + b)

if __name__ == "__main__":
    rng = random.Random(98)
    s, p, states, probs = 0, 0.2, [], []
    for _ in range(400000):
        if rng.random() > STAY[s]:
            s = 1 - s
        x = rng.gauss(*(BULL if s == 0 else BEAR))
        p = step(p, x)
        states.append(s)
        probs.append(p)
    starts = [i for i in range(1, len(states))
              if states[i] == 1 and states[i - 1] == 0]
    lens = []
    i = 0
    while i < len(states):
        if states[i] == 1:
            j = i
            while j < len(states) and states[j] == 1:
                j += 1
            lens.append(j - i)
            i = j
        else:
            i += 1
    print("   bear spells {}, mean length {:.1f} months".format(
        len(lens), sum(lens) / len(lens)))
    print("   threshold  median delay  missed spells  false alarms/bull mo")
    for th in (0.3, 0.5, 0.7):
        delays, missed = [], 0
        for st in starts:
            k = st
            while k < len(states) and states[k] == 1 and probs[k] <= th:
                k += 1
            if k < len(states) and states[k] == 1:
                delays.append(k - st + 1)
            else:
                missed += 1
        delays.sort()
        bull = [probs[i] for i in range(len(states)) if states[i] == 0]
        print("   {:9.1f}  {:12d}  {:13.3f}  {:20.3f}".format(
            th, delays[len(delays) // 2], missed / len(starts),
            sum(q > th for q in bull) / len(bull)))
#    bear spells 6376, mean length 12.5 months
#    threshold  median delay  missed spells  false alarms/bull mo
#          0.3             2          0.155                 0.071
#          0.5             2          0.193                 0.037
#          0.7             3          0.224                 0.017

熊市段落平均持续 12.5 个月。阈值取 0.5 时,滤波器判定「熊市」前的延迟中位数是两个月(包括第一个熊市月份);0.193 的熊市段落直到结束都未被识别,0.037 的牛市月份出现误报。把阈值降到 0.3,能抓到更多段落(漏报 0.155),代价是误报加倍(0.071)。不存在既延迟短又误报少的阈值,因为牛市中的一个坏月份看起来就像普通的熊市月份。

体制感知的凯利

动态下注者可以每月把杠杆设为:在滤波概率下,预期超额收益与其预期二阶矩之比的 kk 倍。在滤波器确信的牛市月份,这个比值约为 7,所以 p06 把杠杆上限设为 3;另外检验一个模型过于乐观的下注者:认为牛市是 0.18,熊市只有 -0.10。

推导

# Chapter 3. p06: Static versus regime-aware Kelly over ten years in the
# market of p02, starting in a bull month. A dynamic bettor sets
# leverage before each month from a Hamilton filter's chance of bear
# (p05): leverage = k * E[excess] / E[second moment], kept between 0
# and 3. "true" uses the real parameters; "optimistic" believes bull
# 0.18 (vol 0.13) and bear -0.10 (vol 0.20). Static leverages are k
# times the whole-history Kelly 1.46. Cash earns 0.02 a year. 10,000
# paths, seed 98.
import random
from math import exp, pi, sqrt

R12 = sqrt(12)
TRUE = ((0.12 / 12, 0.13 / R12), (-0.25 / 12, 0.28 / R12))
OPTI = ((0.18 / 12, 0.13 / R12), (-0.10 / 12, 0.20 / R12))
STAY, RF = (0.98, 0.92), 0.02 / 12

def dens(x, m, sd):
    return exp(-0.5 * ((x - m) / sd) ** 2) / (sd * sqrt(2 * pi))

def prior(p):
    return p * STAY[1] + (1 - p) * (1 - STAY[0])

def lev(p, k, mod):
    (mb, sb), (mr, sr) = mod
    q = prior(p)
    m = q * mr + (1 - q) * mb
    m2 = q * (sr * sr + mr * mr) + (1 - q) * (sb * sb + mb * mb)
    return min(max(k * m / (m2 - m * m), 0.0), 3.0)

def update(p, x, mod):
    q = prior(p)
    a, b = q * dens(x, *mod[1]), (1 - q) * dens(x, *mod[0])
    return a / (a + b)

RULES = (("static 0.5 x", None, 0.5), ("static 1.0 x", None, 1.0),
         ("true 0.25 x", TRUE, 0.25), ("true 0.5 x", TRUE, 0.5),
         ("optimistic 0.25 x", OPTI, 0.25))

if __name__ == "__main__":
    rng = random.Random(98)
    wealth = {r[0]: [] for r in RULES}
    draw = {r[0]: [] for r in RULES}
    for _ in range(10000):
        s = 0
        st = {r[0]: [1.0, 1.0, 0.0, 0.0] for r in RULES}
        for _ in range(120):
            if rng.random() > STAY[s]:
                s = 1 - s
            x = rng.gauss(*TRUE[s])
            for name, mod, k in RULES:
                v = st[name]
                L = lev(v[3], k, mod) if mod else k * 1.46
                v[0] = max(v[0] * (1 + RF + L * x), 0.0)
                v[1] = max(v[1], v[0])
                v[2] = max(v[2], 1 - v[0] / v[1])
                if mod:
                    v[3] = update(v[3], x, mod)
        for name in st:
            wealth[name].append(st[name][0])
            draw[name].append(st[name][2])
    print("   rule               median wealth  DD>=20%  DD>=50%  10th pct")
    for name, _, _ in RULES:
        ws, dd = sorted(wealth[name]), draw[name]
        print("   {:<18} {:13.2f}  {:7.3f}  {:7.3f}  {:8.2f}".format(
            name, ws[5000], sum(d >= 0.2 for d in dd) / 1e4,
            sum(d >= 0.5 for d in dd) / 1e4, ws[1000]))
#    rule               median wealth  DD>=20%  DD>=50%  10th pct
#    static 0.5 x                1.77    0.700    0.181      0.77
#    static 1.0 x                2.21    0.960    0.564      0.38
#    true 0.25 x                 2.69    0.678    0.005      1.34
#    true 0.5 x                  4.65    0.998    0.382      1.15
#    optimistic 0.25 x           3.67    0.992    0.116      1.20

知道真实参数时,按自身凯利 0.25 倍下注的动态下注者表现很好:十年后财富中位数 2.69,只有 0.005 的路径出现腰斩以上回撤,第十百分位数 1.34。取 0.5 倍时中位数升到 4.65,但 20% 以上的回撤几乎必然发生(0.998),因为牛市月份里上限 3 一直起作用。静态下注者远远落后:全历史凯利的财富中位数 2.21,腰斩的机会 0.564。

乐观模型显示这一优势建立在什么之上。认为熊市体制比实际温和时,同样的 0.25 规则在 0.116 的路径上腰斩,在 0.992 的路径上出现 20% 以上的回撤。体制感知的凯利能把对体制的了解转化为增长,却不能凭空产生这种了解,而熊市体制的参数只能从很少几段熊市中估计。

假说 · 尚未验证

作者的诠释(尚未验证): 在长期股指历史的样本外检验中,只有当熊市体制的参数是由至少数段完整熊市估计出来时,体制滤波的杠杆才能在回撤上胜过固定的凯利比例。什么会证明它错: 在滚动的样本外检验中,参数只由一两段熊市估计的体制滤波杠杆,其最大回撤小于平均敞口相同的固定杠杆。

小结

即使滤波器知道真实的体制参数,也需要大约两个月才能识别熊市,并漏掉约五分之一的熊市段落。知道真实参数时,谨慎的体制感知凯利优于静态凯利;模型一旦低估熊市体制,大部分回撤保护就会消失。

参考文献

Hamilton, James D. 1989. “A New Approach to the Economic Analysis of Nonstationary Time Series and the Business Cycle.” Econometrica 57 (2): 357 to 384.

Fan, Y. (2026). Regime-Switching Fractional Kelly, Maximum-Drawdown Constraints and Lifecycle Leverage Allocation: Cyclical Drift Estimation Errors, Ruin-Bounded Bet Sizing and Human-Capital Amortization [体制转换分数凯利、最大回撤约束与生命周期杠杆配置] (Version 1.0.0). 5D Library. https://doi.org/10.5281/zenodo.24090256 · Licensed under Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0).