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

# 读取三个分桶维度的数据
df_a = pd.read_csv('output/06a_str_pred分桶诊断/20260125.csv')
df_b = pd.read_csv('output/06b_ros_real分桶诊断/20260125.csv')
df_c = pd.read_csv('output/06c_ros_pred分桶诊断/20260125.csv')

# 过滤对照组数据进行分析
df_a_ctrl = df_a[(df_a['abcode'] == '对照组') & (df_a['bucket'] != '全部')].copy()
df_b_ctrl = df_b[(df_b['abcode'] == '对照组') & (df_b['bucket'] != '全部')].copy()
df_c_ctrl = df_c[(df_c['abcode'] == '对照组') & (df_c['bucket'] != '全部')].copy()

df_a_ctrl['bucket'] = df_a_ctrl['bucket'].astype(int)
df_b_ctrl['bucket'] = df_b_ctrl['bucket'].astype(int)
df_c_ctrl['bucket'] = df_c_ctrl['bucket'].astype(int)

df_a_ctrl = df_a_ctrl.sort_values('bucket')
df_b_ctrl = df_b_ctrl.sort_values('bucket')
df_c_ctrl = df_c_ctrl.sort_values('bucket')

# 创建综合分析图
fig, axes = plt.subplots(3, 3, figsize=(16, 14))

# ========== 06a: str_pred 分桶 ==========
ax = axes[0, 0]
ax.bar(df_a_ctrl['bucket'], df_a_ctrl['ros_pred_bias'], alpha=0.7, label='ros_pred偏差', color='steelblue')
ax.bar(df_a_ctrl['bucket'], df_a_ctrl['ros_stat_bias'], alpha=0.5, label='ros_stat偏差', color='orange')
ax.axhline(y=0, color='red', linestyle='--', linewidth=1)
ax.set_xlabel('str_pred 分桶')
ax.set_ylabel('ROS 偏差')
ax.set_title('06a: str_pred分桶 - ROS偏差趋势\n(正=高估, 负=低估)')
ax.legend()
ax.set_xticks(range(1, 11))

ax = axes[0, 1]
ax.plot(df_a_ctrl['bucket'], df_a_ctrl['str_real'], 'o-', label='str_real', color='green')
ax.plot(df_a_ctrl['bucket'], df_a_ctrl['str_pred_avg'], 's--', label='str_pred', color='blue')
ax.set_xlabel('str_pred 分桶')
ax.set_ylabel('分享率')
ax.set_title('06a: str_pred分桶 - STR真实vs预测')
ax.legend()
ax.set_xticks(range(1, 11))

ax = axes[0, 2]
ax.plot(df_a_ctrl['bucket'], df_a_ctrl['ros_real'], 'o-', label='ros_real', color='green')
ax.plot(df_a_ctrl['bucket'], df_a_ctrl['ros_pred_avg'], 's--', label='ros_pred', color='blue')
ax.plot(df_a_ctrl['bucket'], df_a_ctrl['ros_stat_avg'], '^--', label='ros_stat', color='orange')
ax.set_xlabel('str_pred 分桶')
ax.set_ylabel('ROS')
ax.set_title('06a: str_pred分桶 - ROS真实vs预测vs统计量')
ax.legend()
ax.set_xticks(range(1, 11))

# ========== 06b: ros_real 分桶 (仅回流样本) ==========
ax = axes[1, 0]
ax.bar(df_b_ctrl['bucket'], df_b_ctrl['ros_pred_bias'], alpha=0.7, label='ros_pred偏差', color='steelblue')
ax.bar(df_b_ctrl['bucket'], df_b_ctrl['ros_stat_bias'], alpha=0.5, label='ros_stat偏差', color='orange')
ax.axhline(y=0, color='red', linestyle='--', linewidth=1)
ax.set_xlabel('ros_real 分桶 (仅回流)')
ax.set_ylabel('ROS 偏差')
ax.set_title('06b: ros_real分桶 - ROS偏差趋势\n(仅回流样本)')
ax.legend()
ax.set_xticks(range(1, 11))

ax = axes[1, 1]
ax.plot(df_b_ctrl['bucket'], df_b_ctrl['ros_real_avg'], 'o-', label='ros_real', color='green')
ax.plot(df_b_ctrl['bucket'], df_b_ctrl['ros_pred_avg'], 's--', label='ros_pred', color='blue')
ax.plot(df_b_ctrl['bucket'], df_b_ctrl['ros_stat_avg'], '^--', label='ros_stat', color='orange')
ax.set_xlabel('ros_real 分桶')
ax.set_ylabel('ROS')
ax.set_title('06b: ros_real分桶 - ROS各指标对比')
ax.legend()
ax.set_xticks(range(1, 11))

ax = axes[1, 2]
ax.plot(df_b_ctrl['bucket'], df_b_ctrl['ros_pred_mae'], 'o-', label='ros_pred MAE', color='blue')
ax.plot(df_b_ctrl['bucket'], df_b_ctrl['ros_stat_mae'], 's-', label='ros_stat MAE', color='orange')
ax.set_xlabel('ros_real 分桶')
ax.set_ylabel('MAE')
ax.set_title('06b: ros_real分桶 - MAE趋势\n(高回流区间误差更大)')
ax.legend()
ax.set_xticks(range(1, 11))

# ========== 06c: ros_pred 分桶 ==========
ax = axes[2, 0]
ax.bar(df_c_ctrl['bucket'], df_c_ctrl['ros_pred_bias'], alpha=0.7, label='ros_pred偏差', color='steelblue')
ax.bar(df_c_ctrl['bucket'], df_c_ctrl['ros_stat_bias'], alpha=0.5, label='ros_stat偏差', color='orange')
ax.axhline(y=0, color='red', linestyle='--', linewidth=1)
ax.set_xlabel('ros_pred 分桶')
ax.set_ylabel('ROS 偏差')
ax.set_title('06c: ros_pred分桶 - ROS偏差趋势')
ax.legend()
ax.set_xticks(range(1, 11))

ax = axes[2, 1]
ax.plot(df_c_ctrl['bucket'], df_c_ctrl['ros_real'], 'o-', label='ros_real', color='green')
ax.plot(df_c_ctrl['bucket'], df_c_ctrl['ros_pred_avg'], 's--', label='ros_pred', color='blue')
ax.set_xlabel('ros_pred 分桶')
ax.set_ylabel('ROS')
ax.set_title('06c: ros_pred分桶 - ROS校准度')
ax.legend()
ax.set_xticks(range(1, 11))

ax = axes[2, 2]
ax.plot(df_c_ctrl['bucket'], df_c_ctrl['return_rate'] * 100, 'o-', color='purple')
ax.set_xlabel('ros_pred 分桶')
ax.set_ylabel('回流率 (%)')
ax.set_title('06c: ros_pred分桶 - 各桶回流率')
ax.set_xticks(range(1, 11))

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

# ========== 打印关键发现 ==========
print("=" * 60)
print("分桶诊断综合分析 - 关键发现")
print("=" * 60)

# 06a 分析
print("\n【06a: str_pred分桶】高分享预测样本的ROS表现")
print("-" * 50)
a_low = df_a_ctrl[df_a_ctrl['bucket'] <= 3]['ros_pred_bias'].mean()
a_high = df_a_ctrl[df_a_ctrl['bucket'] >= 8]['ros_pred_bias'].mean()
print(f"  低str_pred桶(1-3) ROS偏差: {a_low:.4f}")
print(f"  高str_pred桶(8-10) ROS偏差: {a_high:.4f}")
if a_high > a_low:
    print(f"  → 高分享预测样本ROS高估更严重 (+{a_high - a_low:.4f})")
else:
    print(f"  → 低分享预测样本ROS高估更严重 (+{a_low - a_high:.4f})")

# 06b 分析
print("\n【06b: ros_real分桶】真实回流量级与预测误差")
print("-" * 50)
b_low = df_b_ctrl[df_b_ctrl['bucket'] <= 3]['ros_pred_bias'].mean()
b_high = df_b_ctrl[df_b_ctrl['bucket'] >= 8]['ros_pred_bias'].mean()
print(f"  低ros_real桶(1-3) ROS偏差: {b_low:.4f}")
print(f"  高ros_real桶(8-10) ROS偏差: {b_high:.4f}")
print(f"  → 高回流样本被严重低估 (bias={b_high:.2f})")

mae_low = df_b_ctrl[df_b_ctrl['bucket'] <= 3]['ros_pred_mae'].mean()
mae_high = df_b_ctrl[df_b_ctrl['bucket'] >= 8]['ros_pred_mae'].mean()
print(f"  低ros_real桶MAE: {mae_low:.4f}")
print(f"  高ros_real桶MAE: {mae_high:.4f}")

# 06c 分析
print("\n【06c: ros_pred分桶】ROS模型自身校准度")
print("-" * 50)
c_low = df_c_ctrl[df_c_ctrl['bucket'] <= 3]['ros_pred_bias'].mean()
c_high = df_c_ctrl[df_c_ctrl['bucket'] >= 8]['ros_pred_bias'].mean()
print(f"  低ros_pred桶(1-3) ROS偏差: {c_low:.4f}")
print(f"  高ros_pred桶(8-10) ROS偏差: {c_high:.4f}")
if c_high < 0:
    print(f"  → 高预测区间实际低于预测 (低估程度={abs(c_high):.2f})")

# 全局汇总
print("\n" + "=" * 60)
print("核心结论")
print("=" * 60)
df_a_all = df_a[(df_a['abcode'] == '对照组') & (df_a['bucket'] == '全部')]
df_b_all = df_b[(df_b['abcode'] == '对照组') & (df_b['bucket'] == '全部')]
df_c_all = df_c[(df_c['abcode'] == '对照组') & (df_c['bucket'] == '全部')]

print(f"\n全量样本指标（对照组）:")
print(f"  样本量: {df_a_all['sample_cnt'].values[0]:,}")
print(f"  回流样本: {df_a_all['return_cnt'].values[0]:,}")
print(f"  str_real: {df_a_all['str_real'].values[0]:.4%}")
print(f"  ros_real: {df_a_all['ros_real'].values[0]:.4f}")
print(f"  ros_pred偏差: {df_a_all['ros_pred_bias'].values[0]:.4f}")
print(f"  ros_stat偏差: {df_a_all['ros_stat_bias'].values[0]:.4f}")

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