## AB测试

1. 有一个新模型A，目前系统中使用的是模型B，在正式上线前，需要通过AB测试来验证新模型在浙江用户上的效果，目前有如下三种方案：
• 根据user_id（完全随机生成）个位数的奇偶性将用户划分为实验组和对照组，对实验组施以模型A，对照组施以模型B。

• 将user_id个位数为奇数且为浙江用户作为实验组，其余用户为对照组。

• 将user_id个位数为奇数且为浙江用户作为实验组，将user_id个位数为偶数且为浙江用户作为对照组。

## AB测分组sql实现

### 1. 每个激活日期随机取1000个用户/10%用户

1. 随机1000个用户
``````select user_id, active_date, rn
from (
select user_id, active_date, row_number() over(
partition by active_date order by rand() ) as rn
from t_user_active
) a
where rn <= 1000;

``````
1. 随机10%用户
``````select user_id ,active_date, rn
from (
select user_id, active_date, percent_rank() over (
partition by active_date order by rand() ) as pr
from t_user_active
) a
where pr <= 0.1
``````

### 2. 随机均分成100组，每组取1000个用户/10%用户

1. 随机均分成100组，每组取1000个用户。
``````select user_id, nt, rn
from (
select user_id, nt, row_number() over(
partition by nt order by rand() ) as rn
from (
select user_id, ntile(100) over(order by rand()) as nt
from t_user_all
) a
)b
where rn <= 1000;

``````
1. 随机均分成100组，每组取10%个用户
``````select user_id, nt, rn from (
select user_id, nt, percent_rank() over (partition by nt order by        rand() ) as rn
from (
select user_id, ntile(100) over(order by rand()) as nt
from t_user_all)a
)b
where pr <= 0.1
``````