#!/usr/bin/env python
# coding=utf-8
"""
Step1: 验证"低vov高曝光"现象是否存在
"""
import pandas as pd
from pathlib import Path

# 读取数据
data_file = Path(__file__).parent / "output" / "step1_验证现象_20260111_20260113.csv"
df = pd.read_csv(data_file)

print("=" * 70)
print("Step1: 验证低vov高曝光现象是否存在")
print("=" * 70)

# 1. 数据概览
print("\n【1. 数据概览】")
print(f"样本数: {len(df)} 条（item x 天）")
print(f"日期范围: {df['dt'].min()} ~ {df['dt'].max()}")
print(f"曝光rank范围: {df['曝光rank'].min()} ~ {df['曝光rank'].max()}")

# 2. vov0 分布
print("\n【2. vov0 整体分布】")
print(f"均值: {df['vov0'].mean():.4f}")
print(f"中位数: {df['vov0'].median():.4f}")
print(f"标准差: {df['vov0'].std():.4f}")
print(f"最小值: {df['vov0'].min():.4f}")
print(f"最大值: {df['vov0'].max():.4f}")

# 3. 按曝光rank分组看vov分布
print("\n【3. 按曝光rank分组看vov0】")
df['rank_group'] = pd.cut(df['曝光rank'], bins=[0, 5, 10, 20, 50],
                          labels=['Top1-5', 'Top6-10', 'Top11-20', 'Top21-50'])
group_stats = df.groupby('rank_group', observed=True).agg({
    'vov0': ['mean', 'median', 'min', 'count'],
    'exp': 'mean'
}).round(4)
group_stats.columns = ['vov0均值', 'vov0中位数', 'vov0最小', '样本数', '平均曝光']
print(group_stats.to_string())

# 4. 找出低vov高曝光的item
median_vov = df['vov0'].median()
print(f"\n【4. 识别低vov高曝光item】")
print(f"定义: 曝光rank<=10 且 vov0<中位数({median_vov:.4f})")

problem_items = df[(df['曝光rank'] <= 10) & (df['vov0'] < median_vov)]
top10 = df[df['曝光rank'] <= 10]
print(f"符合条件的记录数: {len(problem_items)}")
print(f"占Top10总记录数: {len(problem_items)}/{len(top10)} = {len(problem_items)/len(top10)*100:.1f}%")

if len(problem_items) > 0:
    print("\n问题item详情:")
    print(problem_items[['dt', 'vid', '曝光rank', 'exp', 'vov0', '标题']].to_string(index=False))

# 5. 结论
print("\n" + "=" * 70)
print("【Step1 结论】")
print("=" * 70)
if len(problem_items) > 0:
    print(f"现象存在: {len(problem_items)}/{len(top10)} ({len(problem_items)/len(top10)*100:.1f}%) 的Top10记录存在低vov高曝光问题")
    print(f"  - 这些item的vov0低于中位数{median_vov:.4f}，但曝光rank进入Top10")
else:
    print("现象不存在")

# 保存问题item到文件
output_file = Path(__file__).parent / "output" / "step1_问题item.csv"
problem_items.to_csv(output_file, index=False)
print(f"\n问题item已保存到: {output_file}")

# 保存分析结论到文件
conclusion_file = Path(__file__).parent / "output" / "step1_结论.txt"
with open(conclusion_file, 'w', encoding='utf-8') as f:
    f.write("=" * 70 + "\n")
    f.write("Step1: 验证低vov高曝光现象是否存在\n")
    f.write("=" * 70 + "\n\n")

    f.write("【数据概览】\n")
    f.write(f"样本数: {len(df)} 条（item x 天）\n")
    f.write(f"日期范围: {df['dt'].min()} ~ {df['dt'].max()}\n")
    f.write(f"曝光rank范围: {df['曝光rank'].min()} ~ {df['曝光rank'].max()}\n\n")

    f.write("【vov0 整体分布】\n")
    f.write(f"均值: {df['vov0'].mean():.4f}\n")
    f.write(f"中位数: {df['vov0'].median():.4f}\n")
    f.write(f"标准差: {df['vov0'].std():.4f}\n")
    f.write(f"最小值: {df['vov0'].min():.4f}\n")
    f.write(f"最大值: {df['vov0'].max():.4f}\n\n")

    f.write("【按曝光rank分组看vov0】\n")
    f.write(group_stats.to_string() + "\n\n")

    f.write("【结论】\n")
    if len(problem_items) > 0:
        f.write(f"现象存在: {len(problem_items)}/{len(top10)} ({len(problem_items)/len(top10)*100:.1f}%) 的Top10记录存在低vov高曝光问题\n")
        f.write(f"定义: 曝光rank<=10 且 vov0<中位数({median_vov:.4f})\n")
    else:
        f.write("现象不存在\n")

print(f"分析结论已保存到: {conclusion_file}")
