Compute the mSPRT likelihood-ratio statistic for a normal stream

Quick Overview

Implement the mixture sequential probability ratio test statistic for a stream of normal observations with a normal mixing prior and return it with a reject flag at a threshold of 20. It tests turning a closed form into numerically careful code and understanding why sequential tests allow continuous monitoring.

Compute the mSPRT likelihood-ratio statistic for a normal stream

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `msprt_statistic` in Python. It computes the mixture sequential probability ratio test (mSPRT) statistic for a single stream of normal observations, testing H0: mean = 0 against a two-sided alternative. The mixing prior on the mean is `N(0, tau2)` and the per-observation variance `sigma2` is known. `xbar` is the running mean after `n` observations. ```python def msprt_statistic(xbar: float, n: int, sigma2: float, tau2: float) -> tuple: ... ``` Use exactly this closed form: $$ \Lambda_n = \sqrt{\frac{\sigma^2}{\sigma^2 + n\tau^2}}\,\exp\left(\frac{n^2\,\tau^2\,\bar{x}^2}{2\,\sigma^2\,(\sigma^2 + n\tau^2)}\right) $$ In code form: ```python Lambda_n = sqrt(sigma2 / (sigma2 + n*tau2)) \ * exp((n**2 * tau2 * xbar**2) / (2 * sigma2 * (sigma2 + n*tau2))) ``` Requirements: - Return `(Lambda_n, reject)` as a `(float, bool)` tuple. - `reject` is the Python bool `Lambda_n >= 1 / alpha_default` with `alpha_default = 0.05`, so the threshold is 20. - Use the given `sigma2`. Do not substitute `2*sigma2`. - `tau2` appears in both the square-root prefactor and the denominator of the exponent. Examples: ```python msprt_statistic(0.1, 2000, 1.0, 0.01) # (2985.5769939351567, True) msprt_statistic(0.05, 2000, 1.0, 0.01) # (2.36006972263534, False) ``` Both calls share `sigma2 + n*tau2 = 21`. Halving the running mean divides the exponent by four, from about 9.52 to about 2.38, which takes the statistic from far above the threshold of 20 to well below it. ```hint Shared term One quantity appears in both the prefactor and the exponent. Compute it once and check each place it is used against the formula. ``` ```hint Long streams Think about what `math.exp` does when the running mean stays away from zero for hundreds of thousands of observations, and whether the reject decision needs the statistic itself. ``` ### Constraints & Assumptions - `n` is a non-negative integer, and `sigma2` and `tau2` are positive. - There is one stream with known variance. This is not the two-arm form that tests a difference in means. - The rejection threshold is fixed at 20, from `alpha_default = 0.05`. ### What a Strong Answer Covers - An exact implementation of the closed form, with `sigma2` and `tau2` in the specified places. - Native Python return types and the inclusive comparison against the threshold. - Numerical behavior for long streams, where the exponent can overflow. - Why the statistic can be checked after every observation without inflating the type I error. ### Follow-up Questions - Why does the mSPRT give an always-valid p-value, unlike a fixed-n z-test? - How would you choose `tau2` in practice? - Where does the closed form come from, and how does it change for a two-arm A/B test on the difference in means?

Overview: Implement the mixture sequential probability ratio test statistic for a stream of normal observations with a normal mixing prior and return it with a reject flag at a threshold of 20. It tests turning a closed form into numerically careful code and understanding why sequential tests allow continuous monitoring.

Nov 24, 2026
mediumData ScientistOtherAnalytics & Experimentation
0
0

Implement msprt_statistic in Python. It computes the mixture sequential probability ratio test (mSPRT) statistic for a single stream of normal observations, testing H0: mean = 0 against a two-sided alternative. The mixing prior on the mean is N(0, tau2) and the per-observation variance sigma2 is known. xbar is the running mean after n observations.

def msprt_statistic(xbar: float, n: int, sigma2: float, tau2: float) -> tuple:
    ...

Use exactly this closed form:

Λn=σ2σ2+nτ2 exp⁡(n2 τ2 xˉ22 σ2 (σ2+nτ2))\Lambda_n = \sqrt{\frac{\sigma^2}{\sigma^2 + n\tau^2}}\,\exp\left(\frac{n^2\,\tau^2\,\bar{x}^2}{2\,\sigma^2\,(\sigma^2 + n\tau^2)}\right)

In code form:

Lambda_n = sqrt(sigma2 / (sigma2 + n*tau2)) \
    * exp((n**2 * tau2 * xbar**2) / (2 * sigma2 * (sigma2 + n*tau2)))

Requirements:

  • Return (Lambda_n, reject) as a (float, bool) tuple.
  • reject is the Python bool Lambda_n >= 1 / alpha_default with alpha_default = 0.05 , so the threshold is 20.
  • Use the given sigma2 . Do not substitute 2*sigma2 .
  • tau2 appears in both the square-root prefactor and the denominator of the exponent.

Examples:

msprt_statistic(0.1, 2000, 1.0, 0.01)   # (2985.5769939351567, True)
msprt_statistic(0.05, 2000, 1.0, 0.01)  # (2.36006972263534, False)

Both calls share sigma2 + n*tau2 = 21. Halving the running mean divides the exponent by four, from about 9.52 to about 2.38, which takes the statistic from far above the threshold of 20 to well below it.

Constraints & Assumptions

  • n is a non-negative integer, and sigma2 and tau2 are positive.
  • There is one stream with known variance. This is not the two-arm form that tests a difference in means.
  • The rejection threshold is fixed at 20, from alpha_default = 0.05 .

What a Strong Answer Covers Guidance

  • An exact implementation of the closed form, with sigma2 and tau2 in the specified places.
  • Native Python return types and the inclusive comparison against the threshold.
  • Numerical behavior for long streams, where the exponent can overflow.
  • Why the statistic can be checked after every observation without inflating the type I error.

Follow-up Questions Guidance

  • Why does the mSPRT give an always-valid p-value, unlike a fixed-n z-test?
  • How would you choose tau2 in practice?
  • Where does the closed form come from, and how does it change for a two-arm A/B test on the difference in means?