1 /* Copyright (c) 2022, The Tor Project, Inc. */
2 /* See LICENSE for licensing information */
7 * \brief Header for stats.c
14 * Compute an N-count EWMA, aka N-EWMA. N-EWMA is defined as:
15 * EWMA = alpha*value + (1-alpha)*EWMA_prev
16 * with alpha = 2/(N+1).
19 * EWMA = value*2/(N+1) + EMA_prev*(N-1)/(N+1)
20 * = (value*2 + EWMA_prev*(N-1))/(N+1)
23 n_count_ewma_double(double avg
, double value
, uint64_t N
)
25 /* If the average was not previously computed, return value.
26 * The less than is because we have stupid C warning flags that
27 * prevent exact comparison to 0.0, so we can't do an exact
28 * check for uninitialized double values. Yay pedantry!
29 * Love it when it introduces surprising edge case bugs like
34 return (2*value
+ (N
-1)*avg
)/(N
+1);
37 /* For most stats, an N_EWMA of 100 is sufficient */
38 #define DEFAULT_STATS_N_EWMA_COUNT 100
39 #define stats_update_running_avg(avg, value) \
40 n_count_ewma_double(avg, value, DEFAULT_STATS_N_EWMA_COUNT)
42 #endif /* !defined(TOR_STATS_H) */