-- 按 ros_pred（ros预测值）分桶诊断
-- 目的：检验模型在不同 ros 预测区间的校准度
-- 包含"全部"汇总行
WITH t_raw AS
(
    SELECT  *
            ,REPLACE(GET_JSON_OBJECT(extend_alg,'$.scoresMap'),"\\","") AS scoresmap
            ,CASE   WHEN page IN ("回流后沉浸页&内页feed","详情后沉浸页","首页feed","详情页") THEN "推荐"
                    WHEN page IN ("回流页","其他") THEN "非推荐"
                    ELSE "其他"
            END AS page_type
    FROM    loghubods.dwd_recsys_alg_sample_all_20250212
    WHERE   dt = '${dt}'
    AND     apptype IN ("0","4")
    AND     abcode IN ("ab0","ab1","ab2","ab3","ab4","ab5","ab6","ab7","ab8","ab9")
    AND     abcode NOT IN ("ab100")
    AND     extend_alg IS NOT NULL
    AND     GET_JSON_OBJECT(extend_alg,'$.scoresMap') IS NOT NULL
)
,t_filtered AS
(
    SELECT  *
    FROM    t_raw
    WHERE   page_type = "推荐"
)
,t_base AS
(
    SELECT  dt
            ,apptype
            ,CASE   WHEN apptype IN ("4") AND abcode IN ("ab0","ab1") THEN "实验组-先验地域降权"
                    WHEN apptype IN ("4") AND abcode IN ("ab6","ab7") THEN "实验组-str+校准&ros-统计量"
                    WHEN apptype IN ("4") AND abcode IN ("ab8","ab9") THEN "实验组-str+校准"
                    WHEN apptype IN ("4") AND abcode IN ("ab2","ab3","ab4","ab5") THEN "对照组"
                    ELSE "其他"
            END AS abcode
            ,mid
            ,vid
            ,is_share
            ,share_cnt
            ,is_return_noself
            ,return_n_uv_noself
            ,CAST(GET_JSON_OBJECT(scoresmap,'$.fmRov') AS DOUBLE) AS str_pred
            ,1.22 * pow(CAST(GET_JSON_OBJECT(scoresmap,'$.NorXGBScore') AS DOUBLE), 1.15) AS ros_pred
            ,CAST(GET_JSON_OBJECT(scoresmap,'$.hasReturnRovScore') AS DOUBLE) AS ros_stat
    FROM    t_filtered
)
,t_valid AS
(
    SELECT  *
    FROM    t_base
    WHERE   str_pred IS NOT NULL
    AND     ros_pred IS NOT NULL
)
,t_bucket AS
(
    SELECT  *
            ,NTILE(10) OVER (PARTITION BY dt, apptype, abcode ORDER BY ros_pred) AS bucket
    FROM    t_valid
)
SELECT  dt
        ,apptype
        ,abcode
        ,COALESCE(CAST(bucket AS STRING), '全部') AS bucket
        -- 样本量
        ,COUNT(1) AS sample_cnt
        ,SUM(is_return_noself) AS return_cnt
        ,round(SUM(is_return_noself) / COUNT(1), 6) AS return_rate
        -- ros_pred 区间
        ,round(MIN(ros_pred), 4) AS ros_pred_min
        ,round(MAX(ros_pred), 4) AS ros_pred_max
        ,round(AVG(ros_pred), 4) AS ros_pred_avg
        ,round(AVG(ros_stat), 4) AS ros_stat_avg
        -- ros 真实值（全量样本）
        ,round(SUM(return_n_uv_noself) / NULLIF(SUM(is_return_noself), 0), 4) AS ros_real
        -- ros 偏差（正=高估，负=低估）
        ,round(AVG(ros_pred) - SUM(return_n_uv_noself) / NULLIF(SUM(is_return_noself), 0), 4) AS ros_pred_bias
        ,round(AVG(ros_stat) - SUM(return_n_uv_noself) / NULLIF(SUM(is_return_noself), 0), 4) AS ros_stat_bias
        -- 仅回流样本的 ros
        ,round(AVG(IF(is_return_noself=1, return_n_uv_noself, NULL)), 4) AS ros_real_return
        ,round(AVG(IF(is_return_noself=1, ros_pred, NULL)), 4) AS ros_pred_return
        ,round(AVG(IF(is_return_noself=1, ros_stat, NULL)), 4) AS ros_stat_return
        -- 仅回流样本的偏差
        ,round(AVG(IF(is_return_noself=1, ros_pred - return_n_uv_noself, NULL)), 4) AS ros_pred_bias_return
        ,round(AVG(IF(is_return_noself=1, ros_stat - return_n_uv_noself, NULL)), 4) AS ros_stat_bias_return
        -- 仅回流样本的 MAE
        ,round(AVG(IF(is_return_noself=1, ABS(ros_pred - return_n_uv_noself), NULL)), 4) AS ros_pred_mae_return
        ,round(AVG(IF(is_return_noself=1, ABS(ros_stat - return_n_uv_noself), NULL)), 4) AS ros_stat_mae_return
        -- str 相关
        ,round(SUM(is_return_noself) / COUNT(1), 6) AS str_real
        ,round(AVG(str_pred), 6) AS str_pred_avg
        -- rovn = str * ros（裂变率）
        ,round(SUM(return_n_uv_noself) / COUNT(1), 6) AS rovn_real
        ,round(AVG(str_pred * ros_pred), 6) AS rovn_pred
        ,round(AVG(str_pred * ros_stat), 6) AS rovn_stat
FROM    t_bucket
GROUP BY dt, apptype, abcode, bucket
GROUPING SETS ((dt, apptype, abcode, bucket), (dt, apptype, abcode))
ORDER BY dt DESC, apptype, abcode, bucket
;
