Compute A/B test sample size and Bayes posterior
Company: Roblox
Role: Data Scientist
Category: Statistics & Math
Difficulty: easy
Interview Round: Take-home Project
You are working through two short, self-contained statistics tasks that appear together in an online assessment for a Data Scientist role. The tasks are independent of each other and should each return a single value. You may use any standard scientific library (e.g. NumPy / SciPy in Python, or base R), and your code should run end to end.
### Constraints & Assumptions
- **Part A** is a classic two-sample design with **equal-sized** control and treatment groups and a **common, known** outcome variance (you estimate it from data).
- The hypothesis test in Part A is a **two-sided** test, approximated with the **normal (z)** distribution rather than the $t$ distribution.
- All probabilities in Part B are valid (each in $[0, 1]$) and describe a single binary event $A$ (so $P(\neg A) = 1 - P(A)$).
- Inputs are passed as ordinary numeric values / arrays; you do not need to parse files or handle I/O.
### Clarifying Questions to Ask
- For Part A, should `sigma` be the **population** standard deviation (`ddof=0`) or the **sample** standard deviation (`ddof=1`)? (Either is defensible; state your choice — it slightly changes the answer.)
- Is `delta` the **absolute** difference in means, or a standardized/relative effect? (Here it is absolute.)
- Does `N_total` mean the combined size of both groups, or per group? (Here: total = control + treatment.)
- Should rounding happen on the **per-group** size, on `N_total`, or both? (This changes whether `N_total` is guaranteed even.)
- For Part B, are $A$ and $B$ guaranteed to make $P(B) > 0$, or must I handle the degenerate $P(B) = 0$ case?
### Part A — Minimum sample size for a two-sided A/B z-test
You are given:
- `observed`: an array of numeric outcomes from historical data (use it to estimate the outcome standard deviation $\sigma$)
- `alpha`: significance level for a **two-sided** z-test (e.g. `0.05`)
- `power`: desired statistical power $1 - \beta$ (e.g. `0.8`)
- `delta`: the minimum detectable **absolute** difference in means between treatment and control
Assume the treatment and control groups are **equal size**, the outcome variance is the **same** in both groups, and you may approximate with a **normal (z) test** using `sigma = std(observed)`.
**Task:** compute the **minimum total sample size** `N_total = N_control + N_treatment` required to detect a mean difference of `delta` at two-sided significance `alpha` with power `power`. **Round up** to the nearest integer.
```hint Where to start
This is the standard "comparison of two means" power calculation. Write the detectable difference as a signal-to-noise ratio: the effect `delta` over the **standard error of the difference between the two group means**, then ask how large that ratio must be to reject at level `alpha` with the target `power`.
```
```hint The two z-quantiles
Two cutoffs combine additively. The two-sided significance contributes $z_{1-\alpha/2}$ (use `alpha/2` because the rejection region is split across both tails), and the power contributes $z_{\text{power}} = z_{1-\beta}$ (note: `power`, NOT `1 - power`). Get them from the **inverse normal CDF** (`norm.ppf` / `qnorm`).
```
```hint Per-group vs. total
Think about the **standard error of the difference** between two independent group means — it is not the same as the standard error of a single-group mean. Once you have the right SE expression, solve for the per-group $n$ and remember that the total size spans **both** groups. Apply `math.ceil` (or `ceiling`) to the per-group value so you never round below the requirement.
```
#### What This Part Should Cover
- A correct standard-error / power decomposition that produces the right **factor of 2** for the two-group difference, not a one-sample formula.
- Correct mapping of `alpha` (two-sided $\Rightarrow$ $z_{1-\alpha/2}$) and `power` ($\Rightarrow$ $z_{\text{power}}$) to inverse-normal quantiles.
- Correct handling of the per-group-vs-total distinction and **rounding up** so the result never under-powers the test.
- A defensible, explicitly-stated `std` convention (`ddof`), plus sane handling of degenerate inputs (`delta = 0`, fewer than 2 observations).
### Part B — One-step Bayes' rule
Given probabilities:
- `p_A` $= P(A)$
- `p_B_given_A` $= P(B \mid A)$
- `p_B_given_notA` $= P(B \mid \neg A)$
**Task:** compute and return `p_A_given_B` $= P(A \mid B)$.
```hint Where to start
Write Bayes' theorem and expand the denominator with the **law of total probability** over the partition $\{A, \neg A\}$ — every input you were given is exactly one term you need, so no extra information is required.
```
#### What This Part Should Cover
- Correct Bayes inversion with the denominator expanded as $P(B \mid A)P(A) + P(B \mid \neg A)P(\neg A)$.
- Correct use of $P(\neg A) = 1 - P(A)$.
- A note on the degenerate $P(B) = 0$ case (posterior undefined).
### What a Strong Answer Covers
These dimensions span both parts:
- Returns the two required outputs in the right types: `N_total` as an **integer**, `p_A_given_B` as a **float**.
- States assumptions/conventions explicitly rather than silently picking one (e.g. `ddof`, rounding point).
- Numerically sanity-checks at least one result (e.g. recovering the familiar $\approx (1.96 + 0.84)^2$ constant for `alpha=0.05`, `power=0.8`).
### Follow-up Questions
- Part A assumes a normal approximation with a known $\sigma$. When would you instead use a $t$-based or simulation-based sample-size calculation, and how much does it matter at the sample sizes you computed?
- How would the sample-size formula change for **unequal** group allocation (ratio $k = n_T / n_C$), or for a **one-sided** alternative?
- If the outcome is a **conversion rate** (Bernoulli) rather than a continuous metric, how would you estimate $\sigma$ and would you still use this formula?
- In Part B, how do the posterior odds compare to the prior odds, and what is the **Bayes factor / likelihood ratio** $P(B\mid A)/P(B\mid \neg A)$ telling you about how much evidence $B$ provides for $A$?
Quick Answer: This question evaluates understanding of statistical power and sample size calculation for a two-sided A/B z-test as well as basic Bayesian posterior computation, with emphasis on estimating outcome variance and updating probabilities in the Statistics & Math domain.