Foundations to Frontier
Tests
N. MLOps and AI Systems Engineering › N1 Data Engineering

Data Quality and Validation

Most failures of deployed models start as data problems: a unit changes upstream, a category is renamed, a sensor sticks, a column fills with nulls, or the serving code computes a feature differently from training. Data validation catches them before they reach training or predictions, with a schema (types, ranges, categories, missing rates) and distribution checks against a reference. The exercises infer a schema from weather-station records, implement range, missing-value and category checks plus PSI and L-infinity drift statistics, train a rain classifier, and inject eight realistic bugs to see which checks catch which. Practice problems cover false alarms on small batches, gradual shifts, training-serving skew, data missing not at random and slow drift.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Checking the ingredients

A chef can follow a recipe perfectly and still ruin a dish if the sugar jar was filled with salt. Machine-learning models are the same: the code may be flawless, but if a column arrives in the wrong units or a sensor gets stuck, the model quietly produces nonsense. Data validation is tasting the ingredients before cooking: checking that every value looks like it should and that today’s data resembles the data the model learned from.

🎮 MLOps lab
bugaccuracyschemadriftpressure in kPa0.4120✅✅humidity as a fraction0.6075❌✅30% of wind missing0.8015✅❌region renamed0.8055✅✅stuck temperature sensor0.8105❌✅hours in UTC0.8095❌❌temperature and humidity swapped0.5940✅✅wind in km/h for one sensor type0.7640✅✅

Clean accuracy 0.809. With batches of 100 clean records, PSI exceeds 0.1 in 95% of batches and 0.2 in 15%.

N1-2, seed 0. Range checks miss plausible-looking values; distribution checks ignore missing values; neither notices hours shifted to UTC. Small batches make PSI noisy: sampling alone adds about 9/n with ten bins.

Why engineers care

In Exercise 2, pressure sent in kilopascals instead of hectopascals cut accuracy from 0.809 to 0.412, and both kinds of check caught it; hours sent in UTC slipped past both.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Data validation checks…
  2. 2. A value of 101 for air pressure in hectopascals is…
  3. 3. Training-serving skew means…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

By hand

  • Schema: the expected type, range, allowed categories and missing rate of each column, inferred from good data and then reviewed.
  • Range and type checks: catch wrong units, swapped columns and corrupt values that leave the expected range.
  • Population stability index (PSI): Σ(qᵢ − pᵢ)ln(qᵢ/pᵢ) over bins of a reference distribution p and a new one q; rules of thumb treat 0.1 as a moderate and 0.2 as a large shift.
  • L-infinity distance: the largest difference in any category’s frequency, used for categorical columns by TensorFlow Data Validation.
  • Training-serving skew: a feature computed differently in the serving system than in training.
  • Missingness mechanisms: missing completely at random, at random given observed data, or not at random (depending on the missing value itself).
  • Tools: TensorFlow Data Validation, Great Expectations, Deequ, Pandera, Evidently.
📝 Quick check (Level 1)
0/3 answered
  1. 1. A PSI of 0.25 usually counts as…
  2. 2. TensorFlow Data Validation compares categorical columns by…
  3. 3. Data missing not at random means missingness depends on…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 25 minUndergraduate students and practising engineers.

Validating weather-station records

Exercise 1.

  • Records with five numeric columns (temperature, pressure, humidity, wind, hour), two categorical ones (region, sensor type) and a rain label; 1% of numeric values missing.
  • Schema inference (quantile ranges widened by 10%, missing rates, category sets), validation with reasons, PSI on reference deciles, L-infinity distance for categories.
  • A logistic-regression rain model whose featurization stores the training means and spreads.

Practice.

  • Infer a schema, then review and version it; treat changes to it like code changes.
  • Run checks at every boundary: ingestion, before training, and on serving inputs.
  • Combine range checks with distribution checks, and include missing rates in both.
  • Tune thresholds for the batch size you monitor, and weight them by how much the model relies on each feature.
  • Share one feature implementation between training and serving, and log served features to compare.
  • Compare with a fixed reference as well as with recent data.
def psi(ref, new, bins=10):
    edges = quantile(ref, linspace(0, 1, bins + 1))[1:-1]
    p = histogram_share(ref, edges); q = histogram_share(new, edges)
    p, q = clip(p, 1e-4, None), clip(q, 1e-4, None)
    return sum((q - p) * log(q / p))
🐍 Exercise 1: schema, checks and drift statistics
Infer a schema from clean records, implement range, missing-value and category checks, PSI and L-infinity drift statistics, and train a rain classifier.
📝 Quick check (Level 2)
0/3 answered
  1. 1. A schema inferred from data should be…
  2. 2. Range checks alone miss…
  3. 3. Training-serving skew is best prevented by…

Level 3: Advanced

★★★★☆ Advanced⏱ 25 minGraduate level, with full derivations and proofs.

Theory

PSI as a divergence. PSI = Σ(qᵢ − pᵢ)ln(qᵢ/pᵢ) = KL(q‖p) + KL(p‖q), the symmetric Kullback–Leibler divergence over the bins. Each term is non-negative because qᵢ − pᵢ and ln(qᵢ/pᵢ) have the same sign, so PSI is zero only when the binned distributions agree. For small differences PSI ≈ Σ(qᵢ − pᵢ)²/pᵢ, a chi-square statistic divided by n; for a new sample of size n from the reference distribution its expected value is about (B − 1)/n for B bins. Thresholds tuned on large batches therefore fire on small ones.

What checks can see. A range check sees each value alone and catches only shifts that leave the range. A distribution check sees the shape of one column but not values it ignores (missing ones here), and not relations between columns or between features and labels. A shift that leaves every marginal distribution unchanged, such as relabelling the hours consistently, is invisible to both; only checks on relations (for example between hour and temperature) or on model outputs and accuracy can catch it.

Missing data. Mean imputation leaves a column’s mean unchanged but shrinks its variance to (1 − m)σ² for a missing share m. When values are missing not at random, missingness itself carries information; a missing-value indicator lets the model use it, but the model then depends on the missingness mechanism staying the same.

📝 Quick check (Level 3)
0/3 answered
  1. 1. PSI equals…
  2. 2. Expected PSI from sampling alone is about…
  3. 3. Mean imputation with a missing share m scales the variance by…

Level 4: Expert

★★★★★ Expert⏱ 30 minResearchers: original papers and open problems.

Which realistic data bugs do schema checks and distribution checks catch, and how much do the missed ones matter?

History.

  • D. Rubin (1976) classified missing data as missing completely at random, at random, or not at random.
  • The population stability index comes from credit scoring, where it has long been used to check that applicant populations still match a scorecard’s development sample.
  • D. Sculley and colleagues (2015) described data dependencies as a main source of hidden technical debt in ML systems.
  • Google’s TFX (D. Baylor and colleagues, 2017) and its data validation (E. Breck and colleagues, 2019) made schemas and skew detection part of production pipelines; Amazon’s Deequ (S. Schelter and colleagues, 2018) and the open-source Great Expectations followed similar ideas.

Exercise 2.

  • A rain classifier is trained on 20,000 records; a new batch of 2,000 is evaluated clean and with each of eight bugs.
  • Schema checks: range (more than 1% out of range), missing rate, unseen categories (more than 1%); drift checks: PSI above 0.2 for numeric columns, L-infinity above 0.1 for categorical ones, against the training data.

Seed 0 (clean accuracy 0.8090; no false alarms from either kind of check on 100 clean batches):

bug accuracy schema checks drift checks
pressure in kPa 0.4120 caught caught
humidity as a fraction 0.6075 missed caught
30% of wind missing 0.8015 caught missed
a region renamed 0.8055 caught caught
stuck temperature sensor 0.8105 missed caught
hours in UTC 0.8095 missed missed
temperature and humidity swapped 0.5940 caught caught
wind in km/h for one sensor type 0.7640 caught caught

Seeds 1 and 2 gave the same pattern of caught and missed bugs; clean accuracy 0.8055 and 0.8075, with pressure in kPa 0.4005 and 0.4055, humidity as a fraction 0.6210 and 0.6145, and swapped columns 0.6095 and 0.6065.

Findings.

  • Together the two kinds of check caught seven of eight bugs, but each alone missed two, in every seed: range checks missed plausible-looking values, and PSI missed a burst of missing values it ignores.
  • The three most damaging bugs (kPa, fraction, swap) cost 18 to 41 points and were each caught by at least one check.
  • Hours in UTC passed every check and did not hurt this model, which does not use hour directly; a model that did would fail silently.
🐍 Exercise 2 (research): which realistic data bugs do schema checks and distribution checks catch, and how much do the missed ones matter?
Inject eight data bugs into a clean batch, measure the damage to a trained model, and record which checks catch each.

Data validation in practice

  • Pipelines run validation as a step that blocks training or alerts on serving data (TFX’s ExampleValidator, Great Expectations checkpoints, dbt tests).
  • Skew detection compares features logged at serving time with the training pipeline’s features for the same entities.
  • Scientific data has its own quality flags; respect them, and check instrument units and calibration versions as part of the schema.

References

  1. D. B. Rubin (1976). “Inference and missing data.” Biometrika.
  2. D. Sculley and colleagues (2015). “Hidden technical debt in machine learning systems.” NeurIPS.
  3. D. Baylor and colleagues (2017). “TFX: a TensorFlow-based production-scale machine learning platform.” KDD.
  4. E. Breck, N. Polyzotis, S. Roy, S. E. Whang and M. Zinkevich (2019). “Data validation for machine learning.” SysML.
  5. S. Schelter and colleagues (2018). “Automating large-scale data quality verification.” VLDB.
  6. N. Polyzotis, S. Roy, S. E. Whang and M. Zinkevich (2018). “Data lifecycle challenges in production machine learning: a survey.” SIGMOD Record.