M6-001Medium从零实现统计 · Implement from scratch

手写一个两比例 z 检验,并给出效应差的置信区间。

Implement a two-proportion z-test by hand, including a confidence interval for the difference.
💡 考场高分一句话答案:

用合并比例算标准误做检验,用未合并标准误算 CI;返回 z、p、绝对差与区间。

Use the pooled proportion for the test statistic and the unpooled standard error for the interval; return z, p, the absolute difference and the interval.

📌 核心考察要点 (Key Points)

  • 检验用合并比例:$\hat p=(c_1+c_2)/(n_1+n_2)$,$SE=\sqrt{\hat p(1-\hat p)(1/n_1+1/n_2)}$
  • CI 用未合并:$SE=\sqrt{p_1(1-p_1)/n_1+p_2(1-p_2)/n_2}$
  • 边界:$c=0$ 或 $c=n$ 时正态近似失效,应改用 Wilson 区间或精确检验
  • 报告:绝对差比相对差更利于决策(相对差在基率低时会夸大)
  • Test uses pooled: $\hat p=(c_1+c_2)/(n_1+n_2)$, $SE=\sqrt{\hat p(1-\hat p)(1/n_1+1/n_2)}$
  • Interval uses unpooled: $SE=\sqrt{p_1(1-p_1)/n_1+p_2(1-p_2)/n_2}$
  • Edge cases: with zero or all successes the normal approximation fails; use Wilson or an exact test
  • Report absolute difference rather than relative, which exaggerates at low base rates

🔍 大厂面试官连环追问 (Follow-ups)

  • 什么时候必须用 Wilson 区间?
  • 如何做单侧检验?

嫌字多看太累?进入苏格拉底式对话推演实验室

零打字负担,以大厂考官真实困境切入,通过点选直觉选项暴露思维盲区,手把手推导 Why。

💬 开启对话式破冰 ➔
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔