# -*- coding: utf-8 -*-
"""2027 全年收益：30 条/天，C（质量稀释）/ D（质量保持）两情景各 50% 混合，
逐月模拟（学习曲线 + 长尾 + 季节峰 + 账号损耗）。"""
import math, random, statistics as st, sys, json

sys.stdout.reconfigure(encoding="utf-8")
random.seed(7)
N = 20000
DAYS = 30
POST = 30

PROFILES = {
    "C": {"class": [(0.60, 0.04, 2.0, 0.38), (0.28, 0.18, 2.0, 0.70), (0.09, 0.80, 1.9, 1.00), (0.03, 5.00, 1.7, 1.00)]},
    "D": {"class": [(0.42, 0.12, 2.0, 0.55), (0.36, 0.45, 2.0, 0.85), (0.17, 1.80, 1.9, 1.00), (0.05, 9.00, 1.7, 1.00)]},
}
# 月度季节系数（1=2027 年 1 月）：618 与双十一有峰
SEASON = [1.00, 0.90, 1.00, 1.00, 1.00, 1.25, 1.00, 1.00, 1.05, 1.10, 1.80, 1.15]
BOOST_2026 = 1.25
HAIRCUT_2026 = 0.80


def pick_class(cls):
    r = random.random(); cum = 0
    for p, med, sig, act in cls:
        cum += p
        if r <= cum:
            return med, sig, act
    return cls[-1][1], cls[-1][2], cls[-1][3]


def simulate(profile_key):
    cls = PROFILES[profile_key]["class"]
    med, sig, act = pick_class(cls)
    month_rev = []
    prev1 = prev2 = 0.0
    alive = 1.0
    for m in range(12):
        growth = 1.0 + 0.15 * min(m, 5)          # 学习曲线，半年到顶 +75%
        n = POST * DAYS * act
        new = sum(random.lognormvariate(math.log(max(med * growth, 1e-6)), sig) for _ in range(int(n)))
        rev = (0.60 * new + 0.25 * prev1 + 0.15 * prev2) * SEASON[m] * BOOST_2026 * HAIRCUT_2026 * alive
        month_rev.append(rev)
        prev2, prev1 = prev1, new
        if random.random() < 0.06:               # 每月 6% 概率被判劣质 → 后续打三折
            alive *= 0.3
    return month_rev


def pct(xs, p):
    xs = sorted(xs); return xs[min(int(len(xs) * p), len(xs) - 1)]


if __name__ == "__main__":
    annual, monthly = [], [[] for _ in range(12)]
    for _ in range(N):
        key = "C" if random.random() < 0.5 else "D"
        mr = simulate(key)
        annual.append(sum(mr))
        for i, v in enumerate(mr):
            monthly[i].append(v)

    print("=== 2027 全年（30 条/天，C/D 各 50% 混合）===")
    print(f"年度: 均值 {st.mean(annual):,.0f} | 中位 {st.median(annual):,.0f} | "
          f"P25 {pct(annual,.25):,.0f} | P75 {pct(annual,.75):,.0f} | P90 {pct(annual,.9):,.0f} | P99 {pct(annual,.99):,.0f}")
    print("\n逐月中位数：")
    for i, xs in enumerate(monthly):
        print(f"  {i+1:2d} 月: 中位 {st.median(xs):,.0f} | 均值 {st.mean(xs):,.0f}")
    json.dump({"annual_mean": st.mean(annual), "annual_median": st.median(annual),
               "p25": pct(annual, .25), "p75": pct(annual, .75), "p90": pct(annual, .9),
               "monthly_median": [st.median(x) for x in monthly]},
              open(r"D:\Deepseek\douyin_fengdaquan\_annual_result.json", "w", encoding="utf-8"), ensure_ascii=False, indent=1)
