统计与机器学习 / 46

概率与实验设计复习

从假设、目标到模型和评估。 概率与实验设计复习。

阅读说明: 保留原笔记的编号与章节顺序;代码只供查看,没有在本站运行。历史输出不代表当前版本的复现结果。截图/外链图以独立整理的公式、表格、示意或文字替换;纠错与必要补充均作说明。

0. Probability problem

  • There are 3 bags, one bag contains one blue and one red ball, one bag contains 2 red balls, one bag contains 2 blue balls. What is the prob that both balls are red given that the first ball you drew was red?(取一个盒子,里面两个都是红球)
PLAINTEXT · 仅供阅读
A: first ball is red P(A)=1/2
B: second ball is red 
AB: both balls are red P(AB) = 1/3
P(B|A) = P(AB) / P(A) = 2/3
PYTHON · 仅供阅读
import numpy as np

# 1 - red  0 - blue
ball = [[1, 1], [1, 0], [0, 0]]
FirstRedCount = 0
SecondRedCount = 0
for i in range(100000):
    abag = np.random.randint(0, 3)
    bball = np.random.randint(0, 2)
    if ball[abag][bball] == 1:
        FirstRedCount += 1
        if ball[abag][1 - bball] == 1:
            SecondRedCount += 1
rate = SecondRedCount / FirstRedCount
print('The First Red Count: %d' % FirstRedCount)
print('The Second Red Count: %d' % SecondRedCount)
print(rate)
原笔记输出 · 本次未执行
The First Red Count: 50039
The Second Red Count: 33264
0.6647614860408881
  • One hundred people line up to board a plane with 100 seats. One person in line is drunk, so he randomly chooses a seat. After that, each person entering the plane either sits in their assigned seat, if it is available, or, if not, chooses an unoccupied seat randomly. When the 100th passenger finally enters the plane, what is the prob that she finds her assigned seat unoccupied?
PLAINTEXT · 仅供阅读
drunk people在第一和在第99没有区别
P = 1/2

整理注: 经典飞机座位题假设第一个乘客随机选座,其余按规则入座;若随机选座者位于任意位置,结论需重新限定。


A smart engineer in Uber claims that he invented a new matching algorithm(i.e. match the driver and the user) which was much better than the current one in production. As his best friend and a data scientist/analyst, he asked you to use your fancy A/B testing technique to prove he is right.

1. What is your first step?

Transform business questions to quantitative questions and the key is to define metrics.
Skills required (connecting the dots):

  1. Communication. Ask the right and necessary questions.
  2. Business sense and due diligence.

业务目标到指标: 用户增长可拆新增、激活、留存;收入可拆转化、续费、客单价;体验可看等待、取消与满意度。主指标之外设置护栏,防止收入提升却损害用户体验。

In this example:

  • Drive side: canceling rate
  • User side: waiting time, user evaluation
  • Company side: revenue increase, user growth

2. How shall we do randomization? What are we going to randomize?

Randomize on the request to track the short term effect quickly.
Randomize on users to track the long term effect.
Randomize on drivers.

Randomizatoin = random assignment

Random sampling
Three common sampling methods: simple random sampling, stratified sampling, cluster sampling

3. What is the sample size?

Don’t forget the time frame.
Follow-up question: 7% traffic by 1 day or 1% traffic by 7 days?

  1. There may be correlation between days
  2. There may be day effect
  3. Allowing to measure long term effect

4. Data collection

Skills required (SQL)

  1. Syntax
    • SELECT … FROM … WHERE … GROUP BY … HAVING
    • SELF JOIN
    • Window function
    • WHERE and HAVING
  2. Query execution order

SQL逻辑顺序: FROM/JOIN → WHERE → GROUP BY → HAVING → SELECT → ORDER BY → LIMIT。这里是理解语义的逻辑次序,数据库优化器的物理执行顺序可以不同。

  1. Review method
    • Time constraint
    • Thinking/coding aloud

5. Perform hypothesis testing

Establish the right hypothesis
Select the right procedure
Skills required: basic stats concept

What if the hypothesis can not be rejected?
Segmentation/Subgroup analysis
Increase the observation time

What if we have conflicting metrics results?

What if the hypothesis is rejected?
Difference between treatment and control
Difference between launch and before-launch
Pay attention to ramp up plan and use of the hold-out group

整理注: 事后分组与延长实验会增加假阳性风险,应预先定义规则或使用合适的序贯检验与多重比较校正。

6. Common statistical concerns in A/B testing

统计检查清单: p不是H0为真的概率;不显著不等于无效;同时报告效应量与置信区间;预防多重检验和反复偷看;检查随机化、数据缺失、样本比例、时间效应与组间干扰。

7. A/B testing pipeline

  1. Objective define
  2. Metrics and effect define
  3. Experiment setup
  4. Conduct hypothesis
  5. Result interpretation

整理参考资料