/
aggregate.cljc
291 lines (248 loc) · 10.4 KB
/
aggregate.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#_"SPDX-License-Identifier: GPL-3.0"
(ns sicmutils.util.aggregate
"Namespace with algorithms for aggregating sequences in various ways."
(:require [sicmutils.algebra.fold :as af]
[sicmutils.generic :as g]))
(def ^{:doc "Sums either:
- a series `xs` of numbers, or
- the result of mapping function `f` to `(range low high)`
Using the generic [[sicmutils.generic/+]] function."
:arglists '([xs], [f low high])}
generic-sum
(af/fold->sum-fn g/+))
;; ## Dynamically Bindable Summation
;;
;; The following functions allow for fold-agnostic summation. If a user
;; calls [[sum]] or [[scan]] in some function, they can later tune the way
;; summations happen deep in their code by binding [[*fold*]].
;;
;; For summation functions with explicit folds baked in,
;; see [[sicmutils.algebra.fold/fold->sum-fn]]
;; and [[sicmutils.algebra.fold/fold->scan-fn]].
(def
^{:dynamic true
:doc "Fold used to aggregate values encountered by [[sum]] or [[fold]].
Rebind this value to change the behavior of [[sum]] or [[fold]].
Defaults to [[sicmutils.algebra.fold/kahan-babushka-neumaier-fold]]."}
*fold*
af/kahan-babushka-neumaier)
;; The following two functions are identical
;; to [[sicmutils.algebra.fold/fold->sum-fn]]
;; and [[sicmutils.algebra.fold/fold->scan-fn]] with [[*fold*]] baked in. I did
;; this for performance reasons; to use those functions and keep [[*fold*]]
;; dynamically rebindable I would have to pass the var `#'*fold*`.
;; De-referencing this var has a performance penalty that is not acceptable,
;; given that [[sum]] and [[scan]] are used in the inner loops of numerical
;; integration routines.
(defn sum
"Takes either:
- a series `xs` of numbers
- A transformation function `f`, an inclusive-lower bound `low` and
exclusive-upper bound `upper`
And returns the result of aggregating either `xs` or `(map f (range low
high))` using the fold dynamically bound to [[*fold*]].
Use `binding` to substitute in a different fold:
```clj
(require '[sicmutils.algebra.fold :as af])
(binding [*fold* (af/join af/kahan af/min af/max)]
(sum inc 0 10))
;;=> [55.0 1 10]
```"
([xs]
(*fold*
(reduce *fold* (*fold*) xs)))
([f low high]
(let [xs (range low high)]
(transduce (map f) *fold* xs))))
(defn scan
"Takes either:
- a series `xs` of numbers
- A transformation function `f`, an inclusive-lower bound `low` and
exclusive-upper bound `upper`
And returns a lazy sequence of all intermediate values seen while aggregating
either `xs` or `(map f (range low high))` using the fold dynamically bound
to [[*fold*]].
Use `binding` to substitute in a different fold:
```clj
(require '[sicmutils.algebra.fold :as af])
(binding [*fold* (af/join af/kahan af/min af/max)]
(scan inc 0 3))
;;=> ([1.0 1 1] [3.0 1 2] [6.0 1 3])
```"
([xs]
(->> (reductions *fold* (*fold*) xs)
(rest)
(map *fold*)))
([f low high]
(scan
(map f (range low high)))))
;; ## Pairwise Summation
;;
;; In contrast to [compensated summation
;; algorithms](https://en.wikipedia.org/wiki/Kahan_summation_algorithm), simply
;; adding up a vector of numbers recursively in pairs can offer great error
;; reduction with good performance.
;;
;; Adding up numbers from left to right usually means that as the accumulated
;; sum grows, you are adding the smaller numbers in the sequence into a
;; progressively larger total.
;;
;; By pairwise-adding up numbers recursively, you are more likely to be adding
;; numbers of similar magnitude together.
;;
;; [[*cutoff*]] lets you tune your "leaf size". A value of `1` would send you
;; all the way down to each single value; the default of 128, taken from Julia's
;; default, means that when you get to 128 elements or fewer you defer to naive
;; summation.
(def ^{:dynamic true
:doc "Dynamically bindable size below which [[pairwise-sum]] will defer
to [[sum]] to aggregate values."}
*cutoff*
128)
;; The [[pairwise-sum]] implementation below was inspired by the `pairwiseSum`
;; implementation in
;; the [`math-functions`](https://hackage.haskell.org/package/math-functions-0.3.4.2/docs/src/Numeric.Sum.html#pairwiseSum)
;; Haskell package. Those docs state:
;;
;; -- This approach is perhaps 10% faster than 'KBNSum', but has poorer
;; -- bounds on its error growth. Instead of having roughly constant
;; -- error regardless of the size of the input vector, in the worst case
;; -- its accumulated error grows with /O(log n)/.
;;
;; NOTE that it might be interesting to combine [[pairwise-sum]] with the
;; compensated summation algorithms from [[sicmutils.algebra.fold]]. Page 7 of
;; this [paper by
;; Klein](https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.582.288&rep=rep1&type=pdf)
;; seems to describe a way to aggregate the intermediate states of compensated
;; summation up through the binary addition tree (ie, implement a monoid on the
;; compensated summation state itself); but I couldn't figure out the indices!
(defn pairwise-sum
"Given a vector of numbers, returns the [pairwise
summation](https://en.wikipedia.org/wiki/Pairwise_summation) of the vector
generated by arranging the vector into a binary tree and summing leaves
together all the way up to the root.
If `xs` is /not/ a vector, [[pairwise-sum]] will realize all elements into a
vector before operating.
If the initial vector, or some recursive slice, reaches a count
<= [[*cutoff*]], [[pairwise-sum]] defers to `(reduce + xs)`.
### Performance Discussion
[[pairwise-sum]] is perhaps 10% faster than [[sum]]
with [[sicmutils.algebra.fold/kbn]] bound to [[*fold*]], but has poorer bounds
on its error growth. Instead of having roughly constant error regardless of
the size of the input, in the worst case its accumulated error grows with
$O(\\log n)$.
This improvement is due to the fact that [[pairwise-sum]] tends to add up
numbers of similar magnitude, instead of adding deltas into a progressively
larger sum.
This implementation was inspired by the `pairwiseSum` implementation in
the [`math-functions`](https://hackage.haskell.org/package/math-functions-0.3.4.2/docs/src/Numeric.Sum.html#pairwiseSum)
Haskell package. The notes above were adapted from that function's docs."
([xs]
(letfn [(f [v]
(let [n (count v)]
(if (<= n *cutoff*)
(reduce + v)
(let [split-idx (bit-shift-right n 1)
l (subvec v 0 split-idx)
r (subvec v split-idx)]
(+ (f l)
(f r))))))]
(f (if (vector? xs)
xs
(into [] xs)))))
([f low high]
(pairwise-sum
(mapv f (range low high)))))
;; ## Monoids
;;
;; This section implements combinators that use various binary functions to
;; generate n-arity versions of those functions.
(defn- combiner
"If `stop?` is false, returns `f`. Else, returns a binary reducing function that
returns a `reduced` value if its left argument returns `true` for `stop?`,
else aggregates with `f`."
[f stop?]
(if stop?
(fn [l r]
(if (stop? l)
(reduced l)
(f l r)))
f))
(defn monoid
"Accepts a binary (associative) aggregation function `plus` and an identity
element `id` and returns a multi-arity function that will combine its
arguments via `plus`. A 0-arity call returns `id`.
optionally takes an `annihilate?` function that should return true for any `x`
such that `(plus x <any>) == x`.
If the `annihilate?` function is supplied, then if the aggregation produces a
value that returns `(annihilate? true)` at any point, the reduction will
return immediately."
([plus id]
(monoid plus id nil))
([plus id annihilate?]
(let [acc (combiner plus annihilate?)]
(fn
([] id)
([x] x)
([x y] (plus x y))
([x y & more]
(reduce acc x (cons y more)))))))
(defn group
"Similar to [[monoid]] for types with invertible elements. Accepts:
- binary `minus` and (associative) `plus` functions
- a unary `negate` function
- an element `id` that obeys `(plus id other) == (plus other id) == other`
- optionally, an `annihilate?` function that should return true for any `x`
such that `(plus x <any>) == x`.
And returns a function that will SUBTRACT elements. Given `x`, `y`, `z`, for
example, the returned function will return `(- x y z)`, implemented as `(minus
x (plus y z))`
If the `annihilate?` function is supplied, then if the aggregation produces a
value that returns `(annihilate? true)` at any point, the reduction will
return immediately."
([minus plus invert id]
(group minus plus invert id nil))
([minus plus invert id annihilate?]
(let [acc (combiner plus annihilate?)]
(fn
([] id)
([x] (invert x))
([x y] (minus x y))
([x y & more]
(minus x (reduce acc y more)))))))
(defn merge-fn
"NOTE that the returned function recurs on increasing indices internally instead
of walking through the lists directly. This method of traversing vectors is
more efficient, and this function is called so often that the performance gain
is worth it, and reads almost like the explicit sequence traversal."
[compare add zero? make]
(fn
([] [])
([xs] xs)
([xs ys]
(loop [i (long 0)
j (long 0)
result (transient [])]
(let [x (nth xs i nil)
y (nth ys j nil)]
(cond (not x) (into (persistent! result) (subvec ys j))
(not y) (into (persistent! result) (subvec xs i))
:else (let [[x-tags x-coef] x
[y-tags y-coef] y
compare-flag (compare x-tags y-tags)]
(cond
;; If the terms have the same tag set, add the coefficients
;; together. Include the term in the result only if the new
;; coefficient is non-zero.
(zero? compare-flag)
(let [sum (add x-coef y-coef)]
(recur (inc i)
(inc j)
(if (zero? sum)
result
(conj! result (make x-tags sum)))))
;; Else, pass the smaller term on unchanged and proceed.
(neg? compare-flag)
(recur (inc i) j (conj! result x))
:else
(recur i (inc j) (conj! result y))))))))))