ruby 32 lines · 7 steps

Rolling averages with each_cons in Ruby

A MetricSeries turns a stream of readings into windowed averages, smoothed points, and anomaly detection.

Explained by highlit
1class MetricSeries
2 def initialize(readings, window: 5)
3 @readings = readings
4 @window = window
5 end
6 
7 def rolling_averages
8 return [] if @readings.size < @window
9 
10 @readings.each_cons(@window).map do |slice|
11 slice.sum.fdiv(@window).round(2)
12 end
13 end
14 
15 def smoothed_points(timestamps)
16 offset = @window - 1
17 labels = timestamps.drop(offset)
18 
19 labels.zip(rolling_averages).map do |at, value|
20 { at: at, value: value }
21 end
22 end
23 
24 def anomalies(threshold:)
25 baseline = rolling_averages
26 
27 @readings.drop(@window - 1).zip(baseline).filter_map do |actual, avg|
28 deviation = (actual - avg).abs
29 { actual: actual, expected: avg, deviation: deviation.round(2) } if deviation > threshold
30 end
31 end
32end
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1`each_cons` gives you overlapping sliding windows without any manual index bookkeeping.
  2. 2Aligning derived series with their source requires dropping the leading `window - 1` elements the window consumes.
  3. 3`filter_map` combines transformation and selection in one pass, keeping only the results that matter.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Rolling averages with each_cons in Ruby — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code