#!/usr/bin/env python
# coding=utf-8
"""
Step3f: COPC 详细分析 - 统一口径
"""
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("Step3f: COPC 详细分析（统一口径）")
print("=" * 70)

# 1. 不同口径的 COPC 计算
print("\n【1. 不同口径的偏差计算】")

# 口径1: mean(real) / mean(pred) - 整体 COPC
copc1_ros = df['real_ros'].mean() / df['pred_ros'].mean()
copc1_str = df['real_str'].mean() / df['pred_str'].mean()

# 口径2: mean(pred - real) / mean(real) - 整体偏差率
bias2_ros = (df['pred_ros'].mean() - df['real_ros'].mean()) / df['real_ros'].mean()
bias2_str = (df['pred_str'].mean() - df['real_str'].mean()) / df['real_str'].mean()

# 口径3: mean((pred - real) / real) - 样本偏差率平均（之前用的）
bias3_ros = df['ros_bias_pct'].mean() / 100
bias3_str = df['str_bias_pct'].mean() / 100

print("ROS:")
print(f"  口径1 COPC = {copc1_ros:.4f} (real均值/pred均值)")
print(f"  口径2 偏差 = {bias2_ros:+.4f} ((pred均值-real均值)/real均值)")
print(f"  口径3 偏差 = {bias3_ros:+.4f} (样本偏差率平均)")

print("\nSTR:")
print(f"  口径1 COPC = {copc1_str:.4f}")
print(f"  口径2 偏差 = {bias2_str:+.4f}")
print(f"  口径3 偏差 = {bias3_str:+.4f}")

# 2. 分析口径差异原因
print("\n【2. 口径差异原因分析】")
print("口径2和口径3的差异来源于：")
print("  - 口径2: 用整体均值计算，大样本权重高")
print("  - 口径3: 每个样本权重相同，对小样本/极端值更敏感")

# 3. 加权 COPC（按样本量加权）
print("\n【3. 按样本量加权的分析】")
total_samples = df['sample_cnt'].sum()
weighted_pred_ros = (df['pred_ros'] * df['sample_cnt']).sum() / total_samples
weighted_real_ros = (df['real_ros'] * df['sample_cnt']).sum() / total_samples
weighted_copc_ros = weighted_real_ros / weighted_pred_ros

weighted_pred_str = (df['pred_str'] * df['sample_cnt']).sum() / total_samples
weighted_real_str = (df['real_str'] * df['sample_cnt']).sum() / total_samples
weighted_copc_str = weighted_real_str / weighted_pred_str

print(f"ROS 加权COPC = {weighted_copc_ros:.4f}")
print(f"STR 加权COPC = {weighted_copc_str:.4f}")

# 4. 结论
print("\n" + "=" * 70)
print("【Step3f 结论】")
print("=" * 70)
print("口径选择建议:")
print("  - 评估整体效果用 COPC（口径1）")
print("  - 评估单个视频准确度用样本偏差（口径3）")
print(f"\n整体结论:")
print(f"  - ROS COPC={copc1_ros:.2f}，整体偏高 {(1-copc1_ros)*100:.0f}%")
print(f"  - ROS 样本偏差={bias3_ros*100:+.0f}%，单个视频预估普遍偏高")
print(f"  - 差异原因：极端值拉高了样本偏差")

# 保存结论
output_dir = Path(__file__).parent / "output"
with open(output_dir / "step3f_结论.txt", 'w', encoding='utf-8') as f:
    f.write("=" * 70 + "\n")
    f.write("Step3f: COPC 详细分析\n")
    f.write("=" * 70 + "\n\n")
    f.write("【ROS】\n")
    f.write(f"COPC = {copc1_ros:.4f}\n")
    f.write(f"整体偏差 = {bias2_ros:+.4f}\n")
    f.write(f"样本偏差 = {bias3_ros:+.4f}\n\n")
    f.write("【STR】\n")
    f.write(f"COPC = {copc1_str:.4f}\n")
    f.write(f"整体偏差 = {bias2_str:+.4f}\n")
    f.write(f"样本偏差 = {bias3_str:+.4f}\n")

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