#!/usr/bin/env python
# coding=utf-8
"""
Step3c: 深入分析 ROS 偏高原因
"""
import pandas as pd
from pathlib import Path

# 读取数据
data_file = Path(__file__).parent / "output" / "step3b_整体偏差_合并.csv"
df = pd.read_csv(data_file)

print("=" * 70)
print("Step3c: ROS 偏高原因深入分析")
print("=" * 70)

# 1. ROS 偏差与样本量的关系
print("\n【1. ROS 偏差与样本量（曝光量）的关系】")
df['sample_group'] = pd.cut(df['sample_cnt'], bins=[0, 150, 300, 500, 10000],
                            labels=['<150', '150-300', '300-500', '>500'])
group_ros = df.groupby('sample_group', observed=True).agg({
    'ros_bias_pct': ['mean', 'std'],
    'pred_ros': 'mean',
    'real_ros': 'mean',
    'sample_cnt': 'count'
}).round(2)
group_ros.columns = ['ROS偏差%', '偏差标准差', '预估ROS', '真实ROS', '视频数']
print(group_ros.to_string())

# 2. ROS 偏差与真实 STR 的关系（分享率低的视频 ROS 是否更不准）
print("\n【2. ROS 偏差与真实分享率(STR)的关系】")
df['str_group'] = pd.cut(df['real_str'], bins=[0, 0.005, 0.01, 0.02, 1],
                         labels=['<0.5%', '0.5-1%', '1-2%', '>2%'])
group_str = df.groupby('str_group', observed=True).agg({
    'ros_bias_pct': 'mean',
    'pred_ros': 'mean',
    'real_ros': 'mean',
    'sample_cnt': 'count'
}).round(2)
group_str.columns = ['ROS偏差%', '预估ROS', '真实ROS', '视频数']
print(group_str.to_string())

# 3. ROS 偏差与真实 ROS 的关系
print("\n【3. ROS 偏差与真实ROS的关系】")
df['real_ros_group'] = pd.cut(df['real_ros'], bins=[0, 2, 4, 6, 100],
                              labels=['<2', '2-4', '4-6', '>6'])
group_real_ros = df.groupby('real_ros_group', observed=True).agg({
    'ros_bias_pct': 'mean',
    'pred_ros': 'mean',
    'real_ros': 'mean',
    'sample_cnt': 'count'
}).round(2)
group_real_ros.columns = ['ROS偏差%', '预估ROS', '真实ROS', '视频数']
print(group_real_ros.to_string())

# 4. 相关性分析
print("\n【4. 相关性分析】")
print(f"ROS偏差 vs 样本量: r = {df['ros_bias_pct'].corr(df['sample_cnt']):.3f}")
print(f"ROS偏差 vs 真实STR: r = {df['ros_bias_pct'].corr(df['real_str']):.3f}")
print(f"ROS偏差 vs 真实ROS: r = {df['ros_bias_pct'].corr(df['real_ros']):.3f}")
print(f"ROS偏差 vs 真实VOV: r = {df['ros_bias_pct'].corr(df['real_vov']):.3f}")

# 5. 结论
print("\n" + "=" * 70)
print("【Step3c 结论】")
print("=" * 70)

# 找出规律
low_sample = df[df['sample_cnt'] < 150]['ros_bias_pct'].mean()
high_sample = df[df['sample_cnt'] > 500]['ros_bias_pct'].mean()
print(f"低曝光组 ROS 偏差: {low_sample:+.1f}%")
print(f"高曝光组 ROS 偏差: {high_sample:+.1f}%")

if low_sample > high_sample:
    print("\n规律: 曝光量越低，ROS 预估偏差越大")
    print("原因推测: 低曝光视频的分享样本少，ROS 真实值波动大，模型难以准确预估")

# 保存结论
output_dir = Path(__file__).parent / "output"
with open(output_dir / "step3c_结论.txt", 'w', encoding='utf-8') as f:
    f.write("=" * 70 + "\n")
    f.write("Step3c: ROS 偏高原因分析\n")
    f.write("=" * 70 + "\n\n")
    f.write("【发现】\n")
    f.write(f"1. 低曝光组 ROS 偏差: {low_sample:+.1f}%\n")
    f.write(f"2. 高曝光组 ROS 偏差: {high_sample:+.1f}%\n")
    f.write(f"3. 曝光量越低，ROS 预估偏差越大\n\n")
    f.write("【原因推测】\n")
    f.write("1. XGB 模型整体对 ROS 预估偏高（系统性偏差）\n")
    f.write("2. 低曝光视频分享样本少，真实 ROS 波动大\n")
    f.write("3. 模型可能使用了全局平均 ROS 作为先验，导致预估值被拉高\n")

print(f"\n结果已保存到 output/ 目录")
