Compute cumulative O'Brien-Fleming alpha spent at unequally spaced sample-size looks
Quick Overview
Write a Python function that turns cumulative sample sizes at unequally spaced interim looks into the cumulative O'Brien-Fleming alpha spent at each look. It tests information fractions, numerically careful use of the normal survival function, and practical group sequential design.
Compute cumulative O'Brien-Fleming alpha spent at unequally spaced sample-size looks
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement `obf_spent_at_sample_sizes` in `solution.py`:
```python
def obf_spent_at_sample_sizes(cumulative_n: np.ndarray, alpha: float = 0.05) -> np.ndarray:
```
`cumulative_n` is a 1-D increasing array holding the cumulative sample size at each look of a group sequential test. The last entry is the planned maximum, and the looks may be unequally spaced. Return the cumulative O'Brien-Fleming alpha spent at each look.
Requirements:
- Use the actual information fractions `t_k = cumulative_n[k] / cumulative_n[-1]`, with no equal-spacing assumption.
- The spend at look k is `2 * norm.sf(norm.ppf(1 - alpha / 2) / sqrt(t_k))`, using `scipy.stats.norm`.
- Return raw floats as an `np.ndarray`. The final entry equals `alpha` exactly.
Example:
```python
obf_spent_at_sample_sizes(np.array([1000, 2500, 4000, 5000]))
# -> [1.172644684252e-05, 5.574596680784e-03, 2.842963075307e-02, 0.05]
```
```hint Information fractions
Write out `t` for the example input before computing anything, and compare the first look with where an equally spaced four-look design would put it.
```
```hint The final entry
Check whether `2 * norm.sf(norm.ppf(1 - alpha / 2))` equals `alpha` bit for bit in floating point.
```
### Constraints & Assumptions
- `cumulative_n` is a non-empty 1-D array of positive, strictly increasing sample sizes (integers or floats). Its last entry is the planned maximum.
- `alpha` is the overall two-sided significance level, a float in (0, 1) with default 0.05.
- Statistical information grows in proportion to sample size, so `n_k / n_max` is the information fraction.
### What a Strong Answer Covers
- Converting sample sizes to information fractions at the real look positions, with the last fraction exactly 1.0.
- A vectorized evaluation with `norm.sf`, and why the survival function matters at early looks.
- Meeting the exact-alpha requirement at the final look despite floating-point round-off.
- When `n_k / n_max` is a valid information fraction and when it is not.
- Handling bad input: a zero or decreasing entry, an empty array, or a call made before the planned maximum is reached.
### Follow-up Questions
- How would you convert cumulative spent alpha into the per-look nominal boundary z-values?
- What changes if the final look does not reach the planned maximum sample size?
- If the metric's variance shifts during the test, or users contribute many correlated events, is `n_k / n_max` still the right information fraction?
Overview: Write a Python function that turns cumulative sample sizes at unequally spaced interim looks into the cumulative O'Brien-Fleming alpha spent at each look. It tests information fractions, numerically careful use of the normal survival function, and practical group sequential design.
cumulative_n is a 1-D increasing array holding the cumulative sample size at each look of a group sequential test. The last entry is the planned maximum, and the looks may be unequally spaced. Return the cumulative O'Brien-Fleming alpha spent at each look.
Requirements:
Use the actual information fractions
t_k = cumulative_n[k] / cumulative_n[-1]
, with no equal-spacing assumption.
The spend at look k is
2 * norm.sf(norm.ppf(1 - alpha / 2) / sqrt(t_k))
, using
scipy.stats.norm
.
Return raw floats as an
np.ndarray
. The final entry equals
alpha
exactly.