STM Stress Testing ML Models
Breaking your models on purpose to find weaknesses
Learning Objectives
- •Learn to stress test ML models specifically (not just strategies)
- •Understand adversarial inputs and model fragility
- •Build resilient models that handle edge cases gracefully
Explain Like I'm 5
Stress testing a strategy tells you if the strategy breaks. Stress testing a model tells you if the underlying ML model breaks. Different thing. What happens if a feature has a value the model never saw in training? What if data is missing? What if the distribution shifts? This is where production systems earn their keep.
Think of It This Way
Stress testing strategies is like testing a car on a rough road. Stress testing models is like testing the engine specifically — what happens at extreme temperatures, low fuel, dirty oil? The engine can fail even if the road is smooth.
1Model Fragility Points
2The Stress Test Playbook
Stress Test Results: Model Sensitivity by Feature Group
3Defensive Code Patterns
4Production Failure Patterns
Hands-On Code
ML Model Stress Test Suite
import numpy as np
def stress_test_model(model, X_test, feature_names):
"""Full model stress test suite."""
print("=== ML MODEL STRESS TESTS ===")
# 1. Extreme values
print("\n1. EXTREME VALUES")
base_pred = model.predict_proba(X_test[:1])[:, 1][0]
for i, name in enumerate(feature_names[:5]):
X_ext = X_test[:1].copy()
X_ext[0, i] = X_test[:, i].max() * 3
ext_pred = model.predict_proba(X_ext)[:, 1][0]
change = abs(ext_pred - base_pred)
status = '[WARN]' if change > 0.2 else '[PASS]'
print(f" {name} at 3x: change {change:.3f} {status}")
# 2. Missing values
print("\n2. MISSING VALUES")
try:
X_nan = X_test[:1].copy()
X_nan[0, 0] = np.nan
model.predict(X_nan)
print(" [PASS] Handles NaN gracefully")
except Exception:
print(" [FAIL] Crashes on NaN input")
# 3. All-zero input
print("\n3. ALL-ZERO INPUT")
X_zero = np.zeros_like(X_test[:1])
pred = model.predict_proba(X_zero)[:, 1][0]
print(f" Prediction: {pred:.3f}")
# 4. Prediction range check
print("\n4. PREDICTION RANGE")
preds = model.predict_proba(X_test)[:, 1]
extremes = (preds > 0.99).sum() + (preds < 0.01).sum()
print(f" Range: [{preds.min():.3f}, {preds.max():.3f}]")
print(f" Extreme predictions: {extremes}")Stress test your model, not just your strategy. These tests reveal fragility that only appears with unusual inputs — exactly the inputs that show up during market crises.
Knowledge Check
Q1.Your model crashes when a feature is NaN. In production, a data feed drops out causing NaN values. What happens?
Assignment
Run the full model stress test suite on your trained model. Identify fragility points. Implement defensive code (feature clipping, NaN handling, output sanity checks) for each identified weakness.