Always-Valid p-Value Sequence via mSPRT
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `always_valid_pvalue_series(xbars, ns, sigma2, tau2) -> np.ndarray` in `solution.py`. A stream of normal observations with known variance `sigma2` is tested sequentially under `H0: mean = 0` with a mixture sequential probability ratio test (mSPRT) that uses an `N(0, tau2)` mixing prior on the mean. At each look `k` you are given the running mean `xbars[k]` and the number of observations so far `ns[k]`. Return the always-valid p-value at every look.
```python
def always_valid_pvalue_series(xbars, ns, sigma2: float, tau2: float) -> np.ndarray:
...
```
Requirements:
- The mixture likelihood ratio at look `k` is `Lambda_k = sqrt(sigma2/(sigma2+ns[k]*tau2)) * exp(ns[k]**2 * tau2 * xbars[k]**2 / (2*sigma2*(sigma2+ns[k]*tau2)))`.
- The instantaneous p-value is `min(1, 1/Lambda_k)`.
- The always-valid p-value is the running minimum of the instantaneous p-values through look `k` (`np.minimum.accumulate`), so the sequence is non-increasing and never above `1.0`.
- Return an array the same length as the inputs.
Example:
```text
Input: xbars = [0.05, 0.08, 0.10, 0.11, 0.115]
ns = [500, 1000, 2000, 3000, 4000]
sigma2 = 1.0, tau2 = 0.01
Output: [1.0, 0.1808398813444063, 0.0003349436313420758,
1.3107154382039912e-07, 3.9762692587151045e-11]
```
At the first look `Lambda_1` is about 0.69, so `1/Lambda_1` is above 1 and the p-value is clipped to `1.0`. From the second look on, the evidence grows quickly as the running mean stays away from 0 and the sample size increases.
```hint Vectorize the formula
Every term in `Lambda_k` is elementwise in `ns` and `xbars`, so the whole series takes a few NumPy operations and no Python loop.
```
```hint Watch the exponent
For a nonzero mean the exponent grows roughly linearly in `n`. Check what `np.exp` returns once its argument passes about 709, and whether your result still makes sense.
```
### Constraints & Assumptions
- `xbars` and `ns` are one-dimensional, have the same length (one entry per look) and are ordered in time.
- `ns` holds cumulative counts, so it is positive and non-decreasing.
- `sigma2 > 0` and `tau2 > 0` are known constants.
- The inputs may arrive as Python lists or NumPy arrays.
### What a Strong Answer Covers
- A vectorized implementation that follows the given `Lambda_k` formula and reproduces the example output.
- Clipping the instantaneous p-value at 1 and taking the running minimum so the series never increases.
- Why the result controls type I error at level alpha under continuous monitoring (the martingale property of `Lambda_k` under `H0` and Ville's inequality).
- Numerical stability for large samples, such as computing in log space.
- Time and space complexity, and edge cases such as a running mean of exactly 0.
### Follow-up Questions
- Why is the running minimum used instead of the raw `1/Lambda_k`?
- How would you guard against numeric overflow in `exp` for large samples?
- How does the choice of `tau2` change how quickly the test detects a true effect of a given size?
Overview: Implement the always-valid p-value series of a mixture sequential probability ratio test on a normal stream with known variance. It tests computing the mixture likelihood ratio at each look, clipping and taking a running minimum, and explaining why the result stays valid under continuous monitoring.