Challenge Exercises

Challenge Exercises

animation
analytics-ml
normalization

Challenge Exercises

Difficulty: Intermediate

Task: The code defines a MAD_THRESHOLD constant but does not implement MAD outlier detection. Implement a detectOutlierMAD() function and integrate it into the processTemperature() pipeline as the primary outlier detection method.

Hints:

  • MAD is more robust than Z-score for non-Gaussian data
  • You will need to implement a detectOutlierMAD() function (modified Z-score = 0.6745 * (x - median) / MAD)
  • Replace or complement the Z-score check with MAD

Expected Outcome: MAD should detect outliers even when extreme values skew the mean and standard deviation.

Difficulty: Intermediate

Task: Currently, forward-fill uses a fixed limit (MAX_MISSING_SAMPLES). Implement an exponential decay on the confidence of imputed values.

Requirements:

  • Add a confidence field to SensorReading
  • Reduce confidence by 10% for each consecutive imputed value
  • Stop imputing when confidence drops below 50%
  • Display confidence in serial output

Expected Outcome: Imputed values should be flagged with decreasing confidence as gaps grow longer.

Difficulty: Advanced

Task: Add plausibility checking between temperature and light sensors. If it is very bright (high light), temperature should be reasonable for daytime.

Requirements:

  • If light > 50000 lux and temperature < 10C, flag as suspicious
  • If light < 100 lux and temperature > 35C (outdoors), flag as suspicious
  • Add a new LED or serial indicator for cross-sensor anomalies

Expected Outcome: The system should detect when sensor readings are physically inconsistent with each other.

Difficulty: Advanced

Task: Replace the exponential smoothing filter with a simple Kalman filter for temperature.

Requirements:

  • Implement 1D Kalman filter with process noise and measurement noise
  • Estimate the Kalman gain dynamically
  • Output both the filtered value and the uncertainty estimate

Learning: Kalman filters provide optimal estimation when process and measurement noise characteristics are known.

These cross-sensor thresholds are scenario-specific plausibility checks, not universal physical impossibilities. The Kalman filter’s optimal-estimation claim depends on the model and noise assumptions, not simply on knowing two noise settings.

Back to the chapter