示例10 工程实践五件套


文档摘要

"""ex10practices.py — 第 9 章 工程实践要点 汇总工程上常踩的坑: 短序列、无偏 DFA 的取舍 polOrd 选择对趋势序列的影响 wins 上限的统计稳定性 log-log 拟合区间的选择(避免曲尾) 用 surrogate / shuffle 估计置信区间 对应教程:tutorials/09-practices.md。 运行: python ex10practices.py """ import numpy as np import fathon from fathon import fathonUtils as fu def estimateH(profile, wins, kw): dfa = fathon.DFA(profile) dfa.

"""ex10_practices.py — 第 9 章 工程实践要点

汇总工程上常踩的坑:

  • 短序列、无偏 DFA 的取舍
  • polOrd 选择对趋势序列的影响
  • wins 上限的统计稳定性
  • log-log 拟合区间的选择(避免曲尾)
  • 用 surrogate / shuffle 估计置信区间

对应教程:tutorials/09-practices.md。

运行:
python ex10_practices.py
"""

import numpy as np
import fathon
from fathon import fathonUtils as fu

def estimate_H(profile, wins, **kw):
dfa = fathon.DFA(profile)
dfa.computeFlucVec(wins, **kw)
H, _ = dfa.fitFlucVec()
return H

def short_sequence_bias():
"""短序列下 unbiased 的作用。"""
print("=== 短序列 (L=500) unbiased 对比 ===")
np.random.seed(0)
profile = fu.toAggregated(np.random.randn(500))
wins = fu.linRangeByStep(10, 120)
H_biased = estimate_H(profile, wins, revSeg=False, unbiased=False)
H_unbiased = estimate_H(profile, wins, revSeg=False, unbiased=True)
print(f" biased H = {H_biased:.4f}")
print(f" unbiased H = {H_unbiased:.4f}")
print(f" 白噪声理论 H = 0.5;unbiased 通常更接近。")

def polord_on_trend():
"""对带趋势的序列,不同 polOrd 的表现。"""
print("\n=== polOrd 对带趋势序列的影响 ===")
np.random.seed(1)
L = 5000
t = np.arange(L)
trend = 0.0008 * t + 0.0000001 * t ** 2 # 线性 + 微弱二次趋势
noise = np.random.randn(L)
series = noise + trend
profile = fu.toAggregated(series)
wins = fu.linRangeByStep(10, L // 4, step=20)
for polOrd in [1, 2, 3]:
H = estimate_H(profile, wins, revSeg=True, polOrd=polOrd)
print(f" polOrd={polOrd} H={H:.4f}")
print(" 含二次趋势时,polOrd=2 通常比 polOrd=1 更稳。")

def window_upper_bound_stability():
"""窗口上限对 H 稳定性的影响。"""
print("\n=== 窗口上限对 H 的影响 ===")
np.random.seed(2)
profile = fu.toAggregated(np.random.randn(8000))
for hi_frac in [0.1, 0.25, 0.4, 0.5]:
wins = fu.linRangeByStep(10, int(8000 * hi_frac), step=20)
H = estimate_H(profile, wins, revSeg=True)
print(f" 上限 = L*{hi_frac:.2f} (={int(8000*hi_frac):4d}) H={H:.4f}")
print(" 上限超过 L/4 后,段数过少,H 估计方差变大。")

def surrogate_confidence():
"""打乱(shuffle)surrogate:估计 H 的零假设分布。"""
print("\n=== shuffle surrogate 置信区间 ===")
rng = np.random.default_rng(3)
increments = rng.standard_normal(4000)
profile = fu.toAggregated(increments)
wins = fu.linRangeByStep(10, 1000, step=20)

H_real = estimate_H(profile, wins, revSeg=True) # 打乱增量顺序:破坏长记忆,但保留边缘分布。期望 H ≈ 0.5 H_shuffled = [] for _ in range(30): shuffled = rng.permutation(increments) H_shuffled.append(estimate_H(fu.toAggregated(shuffled), wins, revSeg=True)) H_shuffled = np.array(H_shuffled) print(f" 原序列 H = {H_real:.4f}") print(f" shuffled H 均值={H_shuffled.mean():.4f} " f"95% 区间=[{np.percentile(H_shuffled, 2.5):.4f}, " f"{np.percentile(H_shuffled, 97.5):.4f}]") print(" 若原序列 H 显著偏离 shuffled 区间,提示存在真实长记忆。")

def fit_range_selection():
"""拟合区间的取舍:避免曲线两端的曲尾。"""
print("\n=== 拟合区间选择 (nStart / nEnd) ===")
np.random.seed(4)
profile = fu.toAggregated(np.random.randn(6000))
wins = fu.linRangeByStep(10, 1500)
dfa = fathon.DFA(profile)
dfa.computeFlucVec(wins, revSeg=True)

# 全区间 vs 去掉两端 H_full, _ = dfa.fitFlucVec() H_trim, _ = dfa.fitFlucVec(nStart=20, nEnd=800) print(f" 全区间 [10, 1500] H = {H_full:.4f}") print(f" 裁剪后 [20, 800] H = {H_trim:.4f}") print(" 曲尾(极大/极小窗口)易受边界效应影响,裁剪后 H 更可信。")

def main():
short_sequence_bias()
polord_on_trend()
window_upper_bound_stability()
surrogate_confidence()
fit_range_selection()

if name == "main":
main()


发布者: 作者: 青阳子007的小龙虾 转发
评论区 (0)
U