import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['font.sans-serif'] = ['Arial Unicode MS']
plt.rcParams['axes.unicode_minus'] = False

# 读取数据（排除昨天 20260125）
dates = ['20260123', '20260124']
dfs_a, dfs_c = [], []

for dt in dates:
    dfs_a.append(pd.read_csv(f'output/06a_str_pred分桶诊断_full/{dt}.csv'))
    dfs_c.append(pd.read_csv(f'output/06c_ros_pred分桶诊断_full/{dt}.csv'))

df_a = pd.concat(dfs_a, ignore_index=True)
df_c = pd.concat(dfs_c, ignore_index=True)

print(f"数据日期: {dates} (排除昨天)")
print("=" * 70)

# 定义实验组
exp_groups = ['对照组', '实验组-str+校准', '实验组-str+校准&ros-统计量', '实验组-先验地域降权']

# ========== 1. 业务指标对比（全部桶汇总） ==========
print("\n【1. 业务指标对比 - 全量汇总】")
print("-" * 70)

# 取全部桶的汇总数据，按组聚合
df_a_all = df_a[(df_a['bucket'] == '全部') & (df_a['apptype'] == 4)].copy()
df_a_summary = df_a_all.groupby('abcode').agg({
    'sample_cnt': 'sum',
    'return_cnt': 'sum',
    'dau': 'sum',
    'exp': 'sum'
}).reset_index()

# 计算加权平均（用样本量加权）
metrics = ['str_real', 'str_pred_avg', 'ros_real', 'ros_pred_avg', 'ros_stat_avg',
           'ros_pred_bias', 'ros_stat_bias', 'rovn_real', 'rovn_pred',
           'str_one', 'ros_one', 'str', 'ros', 'vovh24']

for m in metrics:
    df_a_all[f'{m}_weighted'] = df_a_all[m] * df_a_all['sample_cnt']

weighted_agg = df_a_all.groupby('abcode').agg({f'{m}_weighted': 'sum' for m in metrics})
for m in metrics:
    df_a_summary[m] = weighted_agg[f'{m}_weighted'].values / df_a_summary['sample_cnt'].values

# 打印对比表
print(f"\n{'组别':<30} {'样本量':>12} {'str_real':>10} {'ros_real':>10} {'rovn_real':>12} {'vovh24':>10}")
print("-" * 90)
ctrl_row = df_a_summary[df_a_summary['abcode'] == '对照组'].iloc[0]
for _, row in df_a_summary[df_a_summary['abcode'].isin(exp_groups)].iterrows():
    print(f"{row['abcode']:<30} {int(row['sample_cnt']):>12,} {row['str_real']:>10.4%} {row['ros_real']:>10.4f} {row['rovn_real']:>12.6f} {row['vovh24']:>10.6f}")

# 计算相对对照组的提升
print("\n【相对对照组提升】")
print("-" * 70)
print(f"{'组别':<35} {'str提升':>10} {'ros提升':>10} {'rovn提升':>10} {'vovh24提升':>10}")
print("-" * 70)
for _, row in df_a_summary[df_a_summary['abcode'].isin(exp_groups)].iterrows():
    if row['abcode'] == '对照组':
        continue
    str_lift = (row['str_real'] / ctrl_row['str_real'] - 1) * 100
    ros_lift = (row['ros_real'] / ctrl_row['ros_real'] - 1) * 100
    rovn_lift = (row['rovn_real'] / ctrl_row['rovn_real'] - 1) * 100
    vovh24_lift = (row['vovh24'] / ctrl_row['vovh24'] - 1) * 100
    print(f"{row['abcode']:<35} {str_lift:>+9.2f}% {ros_lift:>+9.2f}% {rovn_lift:>+9.2f}% {vovh24_lift:>+9.2f}%")

# ========== 2. 分桶偏差分析 ==========
print("\n\n【2. 分桶偏差分析 - 对照组】")
print("-" * 70)

# 只看对照组的分桶数据
for name, df in [('str_pred分桶', df_a), ('ros_pred分桶', df_c)]:
    df_ctrl = df[(df['abcode'] == '对照组') & (df['bucket'] != '全部') & (df['apptype'] == 4)].copy()
    df_ctrl['bucket'] = df_ctrl['bucket'].astype(int)

    # 按桶聚合（跨日期）
    df_bucket = df_ctrl.groupby('bucket').agg({
        'sample_cnt': 'sum',
        'return_cnt': 'sum',
        'ros_pred_bias': lambda x: np.average(x, weights=df_ctrl.loc[x.index, 'sample_cnt']),
        'ros_stat_bias': lambda x: np.average(x, weights=df_ctrl.loc[x.index, 'sample_cnt']),
        'ros_pred_mae_return': lambda x: np.average(x, weights=df_ctrl.loc[x.index, 'return_cnt']),
    }).reset_index()

    print(f"\n{name}（对照组）:")
    low_bias = df_bucket[df_bucket['bucket'] <= 3]['ros_pred_bias'].mean()
    high_bias = df_bucket[df_bucket['bucket'] >= 8]['ros_pred_bias'].mean()
    print(f"  低桶(1-3) ros_pred偏差: {low_bias:+.4f}")
    print(f"  高桶(8-10) ros_pred偏差: {high_bias:+.4f}")
    print(f"  偏差差异: {high_bias - low_bias:+.4f}")

# ========== 3. 可视化 ==========
fig, axes = plt.subplots(2, 3, figsize=(16, 10))

# 3.1 业务指标对比柱状图
ax = axes[0, 0]
groups = ['对照组', 'str+校准', 'str+校准\n&ros统计量', '先验地域降权']
group_names = ['对照组', '实验组-str+校准', '实验组-str+校准&ros-统计量', '实验组-先验地域降权']
rovn_values = [df_a_summary[df_a_summary['abcode'] == g]['rovn_real'].values[0] * 1000 for g in group_names]
colors = ['gray', 'steelblue', 'orange', 'green']
bars = ax.bar(groups, rovn_values, color=colors, alpha=0.8)
ax.set_ylabel('rovn (×1000)')
ax.set_title('各组 ROVN 对比')
for bar, val in zip(bars, rovn_values):
    ax.text(bar.get_x() + bar.get_width()/2, bar.get_height(), f'{val:.3f}', ha='center', va='bottom')

# 3.2 str提升对比
ax = axes[0, 1]
str_values = [df_a_summary[df_a_summary['abcode'] == g]['str_real'].values[0] * 100 for g in group_names]
bars = ax.bar(groups, str_values, color=colors, alpha=0.8)
ax.set_ylabel('str_real (%)')
ax.set_title('各组 STR 对比')
for bar, val in zip(bars, str_values):
    ax.text(bar.get_x() + bar.get_width()/2, bar.get_height(), f'{val:.3f}', ha='center', va='bottom')

# 3.3 ros对比
ax = axes[0, 2]
ros_values = [df_a_summary[df_a_summary['abcode'] == g]['ros_real'].values[0] for g in group_names]
bars = ax.bar(groups, ros_values, color=colors, alpha=0.8)
ax.set_ylabel('ros_real')
ax.set_title('各组 ROS 对比')
for bar, val in zip(bars, ros_values):
    ax.text(bar.get_x() + bar.get_width()/2, bar.get_height(), f'{val:.3f}', ha='center', va='bottom')

# 3.4-3.5 两个分桶维度的偏差趋势
for idx, (name, df, title) in enumerate([
    ('06a', df_a, 'str_pred分桶 - ROS偏差'),
    ('06c', df_c, 'ros_pred分桶 - ROS偏差')
]):
    ax = axes[1, idx]
    for gname, color in [('对照组', 'gray'), ('实验组-str+校准', 'steelblue'),
                          ('实验组-str+校准&ros-统计量', 'orange')]:
        df_g = df[(df['abcode'] == gname) & (df['bucket'] != '全部') & (df['apptype'] == 4)].copy()
        df_g['bucket'] = df_g['bucket'].astype(int)
        df_g = df_g.groupby('bucket')['ros_pred_bias'].mean().reset_index()
        label = gname.replace('实验组-', '')
        ax.plot(df_g['bucket'], df_g['ros_pred_bias'], 'o-', label=label, color=color, alpha=0.8)
    ax.axhline(y=0, color='red', linestyle='--', linewidth=1, alpha=0.5)
    ax.set_xlabel('分桶')
    ax.set_ylabel('ros_pred偏差')
    ax.set_title(title)
    ax.legend(fontsize=8)
    ax.set_xticks(range(1, 11))

# 3.6 真实值 vs 预测值趋势
ax = axes[1, 2]
df_ctrl = df_a[(df_a['abcode'] == '对照组') & (df_a['bucket'] != '全部') & (df_a['apptype'] == 4)].copy()
df_ctrl['bucket'] = df_ctrl['bucket'].astype(int)
df_bucket = df_ctrl.groupby('bucket').agg({'ros_real': 'mean', 'ros_pred_avg': 'mean', 'ros_stat_avg': 'mean'}).reset_index()
ax.plot(df_bucket['bucket'], df_bucket['ros_real'], 'o-', label='ros_real', color='green', linewidth=2)
ax.plot(df_bucket['bucket'], df_bucket['ros_pred_avg'], 's--', label='ros_pred', color='blue', linewidth=2)
ax.plot(df_bucket['bucket'], df_bucket['ros_stat_avg'], '^--', label='ros_stat', color='orange', linewidth=2)
ax.set_xlabel('str_pred分桶')
ax.set_ylabel('ROS')
ax.set_title('str_pred分桶 - ROS真实vs预测 (对照组)')
ax.legend()
ax.set_xticks(range(1, 11))

plt.tight_layout()
plt.savefig('output/分桶诊断综合分析_full.png', dpi=150, bbox_inches='tight')
plt.close()

print(f"\n\n图表已保存至: output/分桶诊断综合分析_full.png")

# ========== 4. 实验组对比汇总表 ==========
print("\n\n【3. 实验组 vs 对照组 完整指标】")
print("=" * 70)

cols = ['abcode', 'sample_cnt', 'str_real', 'ros_real', 'ros_pred_bias', 'ros_stat_bias',
        'rovn_real', 'str_one', 'ros_one', 'vovh24']
summary_table = df_a_summary[df_a_summary['abcode'].isin(exp_groups)][cols].copy()
summary_table = summary_table.round(6)
print(summary_table.to_string(index=False))
