Problems
Loading...
1 / 1

Data Drift Detection

MLOps
Easy

You are monitoring an ML model in production. To detect data drift, you compare the distribution of a feature in production against a reference distribution from training.

Given histogram bin counts for reference and production data, plus a drift threshold, compute the Total Variation Distance (TVD) between the two normalized distributions and determine whether drift has occurred.

Drift Calculation

  1. Normalize both histograms to probability distributions (divide each bin count by the total count)

  2. Compute the TVD:

TVD=12ipiqi\text{TVD} = \frac{1}{2} \sum_{i} |p_i - q_i|

where pp is the reference distribution and qq is the production distribution.

  1. Drift is detected when the score is strictly greater than the threshold

Return a dict with "score" (the TVD value as a float) and "drift_detected" (boolean).

Loading visualization...

Examples

Input:

reference_counts = [50, 50] production_counts = [55, 45] threshold = 0.1

Output:

{"score": 0.05, "drift_detected": False}

Normalized: ref = [0.5, 0.5], prod = [0.55, 0.45]. TVD = 0.5 * (0.05 + 0.05) = 0.05. Below threshold.

Input:

reference_counts = [50, 50] production_counts = [90, 10] threshold = 0.1

Output:

{"score": 0.4, "drift_detected": True}

Normalized: ref = [0.5, 0.5], prod = [0.9, 0.1]. TVD = 0.5 * (0.4 + 0.4) = 0.4. Above threshold.

Hint 1

To normalize, divide each bin count by the sum of all bin counts.

Hint 2

Python's zip() lets you iterate over two lists in parallel to compute element-wise differences.

Requirements

  • Normalize histogram counts to probability distributions
  • Compute Total Variation Distance between the two distributions
  • Compare score against threshold (strictly greater means drift)
  • Return a dict with "score" and "drift_detected" keys

Constraints

  • len(reference_counts) == len(production_counts)
  • 1 ≤ number of bins ≤ 1000
  • All counts are non-negative integers
  • Sum of each histogram is at least 1
  • 0 < threshold < 1
  • Time limit: 300 ms
Try Similar Problems