Module: Sentiero::Analytics::Stats

Instance Method Summary collapse

Instance Method Details

#mean(values) ⇒ Object



12
13
14
# File 'lib/sentiero/analytics/stats.rb', line 12

def mean(values)
  values.sum.to_f / values.size
end

#offset_ms(anchor, timestamp) ⇒ Object

Milliseconds from anchor to timestamp, floored at 0; 0 if either is nil.



17
18
19
20
21
# File 'lib/sentiero/analytics/stats.rb', line 17

def offset_ms(anchor, timestamp)
  return 0 unless anchor && timestamp

  [timestamp - anchor, 0].max.round
end

#percentile(sorted, pct) ⇒ Object

Nearest-rank percentile; sorted must be pre-sorted and non-empty.



7
8
9
10
# File 'lib/sentiero/analytics/stats.rb', line 7

def percentile(sorted, pct)
  rank = (pct / 100.0 * sorted.size).ceil
  sorted[rank.clamp(1, sorted.size) - 1]
end

#top_counts(counts, limit:) ⇒ Object

Top limit [key, count] pairs, highest count first, ties broken by key so the ordering is deterministic.



25
26
27
# File 'lib/sentiero/analytics/stats.rb', line 25

def top_counts(counts, limit:)
  counts.sort_by { |key, count| [-count, key] }.first(limit)
end