Specification: compute average of data points in each disjoint time window of width 7 ordered by time
Example input/output:
Input:
data
time | x |
---|---|
20 | 14 |
19 | 15 |
2 | 3 |
1 | 2 |
3 | 7 |
10 | 9 |
11 | 11 |
Output:
[4, 10, 14.5]
Python - Imperative
def average_window(data): def window(value): return math.floor(value["time"] / 7) if len(data) < 1: return [] data.sort(key=lambda v: v["time"]) result = [] current_window = window(data[0]) total, count = data[0]["x"], 1 for value in data[1:]: time_window = window(value) if time_window != current_window: result.append(total / count) current_window, total, count = time_window, 0, 0 total += value["x"] count += 1 result.append(total / count) return result
Python - Functional
def average_window(data): def window(value): return math.floor(value["time"] / 7) grouped_values = [ [point["x"] for point in data if window(point) == w] for w in set(map(window, data)) ] return [ sum(values) / len(values) for values in grouped_values ]
Python - Pandas
def average_window(data): def window(t): return math.floor(t / 7) result = (data.sort_values("time") .set_index("time").groupby(window).mean()) return result['x'].tolist()
R - Tidyverse
average_window <- function(data) { data %>% group_by(floor(time / 7)) %>% summarize(avg = mean(x)) %>% pull(avg) }
SQL - SQLite
SELECT AVG(x) as x FROM data GROUP BY cast(time / 7 as int) ORDER BY time
Datalog - Souffle
.decl window(w: number) window(t/7) :- data(t, _). .decl windowed(w: number, x: float) windowed(t/7, x) :- data(t, x). .decl windowed_total(w: number, x: float) windowed_total(w, total / n) :- window(w), total = sum x : { windowed(w, x) }, n = sum z : { windowed(w, x), z=1.0 }. average_window(v) :- windowed_total(_, v).
Q - kdb+
average_window: (value select[<time] avg x by 7 xbar time from data) `x