using Welford formula for sd
This commit is contained in:
parent
b130b6b6eb
commit
6047563033
1 changed files with 9 additions and 6 deletions
|
|
@ -65,16 +65,19 @@
|
||||||
"Reducing fn to compute the standard deviation. Returns 0 if no or only one item."
|
"Reducing fn to compute the standard deviation. Returns 0 if no or only one item."
|
||||||
([] #?(:clj (double-array 3) :cljs #js [0.0 0.0 0.0]))
|
([] #?(:clj (double-array 3) :cljs #js [0.0 0.0 0.0]))
|
||||||
([^doubles a]
|
([^doubles a]
|
||||||
(let [s (aget a 0) s2 (aget a 1) n (aget a 2)]
|
(let [s (aget a 0) n (aget a 2)]
|
||||||
(if (< 1 n)
|
(if (< 1 n)
|
||||||
(Math/sqrt (/ (- s2 (/ (* s s) n)) (dec n)))
|
(Math/sqrt (/ s (dec n)))
|
||||||
0.0)))
|
0.0)))
|
||||||
([^doubles a x]
|
([^doubles a x]
|
||||||
(let [s (aget a 0) s2 (aget a 1) n (aget a 2)]
|
(let [s (aget a 0) m (aget a 1) n (aget a 2)
|
||||||
|
d (- x m)
|
||||||
|
n (inc n)
|
||||||
|
m' (+ m (/ d n))]
|
||||||
(doto a
|
(doto a
|
||||||
(aset 0 (+ s x))
|
(aset 0 (+ s (* d (- x m'))))
|
||||||
(aset 1 (+ s2 (* x x)))
|
(aset 1 m')
|
||||||
(aset 2 (inc n))))))
|
(aset 2 n)))))
|
||||||
|
|
||||||
(defn last
|
(defn last
|
||||||
"Reducing function that returns the last value."
|
"Reducing function that returns the last value."
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue