1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
| import statistics
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["PingFang SC", "Arial Unicode MS", "SimHei", "sans-serif"]
plt.rcParams["axes.unicode_minus"] = False
days = list(range(1, len(earnings) + 1))
mu = statistics.mean(earnings)
sigma = statistics.pstdev(earnings)
median = statistics.median(earnings)
q1, _, q3 = statistics.quantiles(earnings, n=4, method="inclusive")
z_scores = [(x - mu) / sigma for x in earnings]
rolling7 = [
statistics.mean(earnings[max(0, i - 6):i + 1])
for i in range(len(earnings))
]
high_days = [(d, x) for d, x, z in zip(days, earnings, z_scores) if z >= 2]
fig, axes = plt.subplots(2, 1, figsize=(11, 7), constrained_layout=True)
fig.suptitle("每日收入水平分析", fontsize=14)
axes[0].plot(days, earnings, marker="o", label="每日收入")
axes[0].plot(days, rolling7, color="orange", linewidth=2, label="7 日均值")
axes[0].axhline(mu, color="green", linestyle="--", label=f"均值 {mu:.3f}")
axes[0].fill_between(
days,
[mu - sigma] * len(days),
[mu + sigma] * len(days),
color="green",
alpha=0.1,
label="±1σ 区间",
)
axes[0].scatter(
[d for d, _ in high_days],
[x for _, x in high_days],
color="red",
zorder=5,
label="高异常日",
)
axes[0].set_title("每日收入与 7 日均值")
axes[0].set_xlabel("天数")
axes[0].set_ylabel("收入")
axes[0].grid(alpha=0.3)
axes[0].legend()
axes[1].bar(
days,
z_scores,
color=["tab:red" if z >= 2 else "tab:blue" for z in z_scores],
)
axes[1].axhline(0, color="black", linewidth=0.8)
axes[1].axhline(2, color="red", linestyle="--", linewidth=1, label="+2σ")
axes[1].axhline(-2, color="red", linestyle="--", linewidth=1)
axes[1].set_title("每日收入 Z-score")
axes[1].set_xlabel("天数")
axes[1].set_ylabel("z-score")
axes[1].grid(alpha=0.3)
axes[1].legend()
plt.show()
|