Skip to content

Commit

Permalink
Improved ewma function.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbtourist committed Nov 18, 2012
1 parent bb169f4 commit 53159e5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
13 changes: 10 additions & 3 deletions src/nimrod/core/math.clj
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
(ns nimrod.core.math
(:require [nimrod.core.util :as util]))

; n = {1000, 10000, 100000}
; k = 1 - ((samples % n) / n)
; w = n ^ -k
; ewma = (v * w) + (ewma-1 * (1 - w))
(defn ewma [ewma-1 value samples]
(let
[ewma-1 (or ewma-1 [0 0 0])
; k decreases
k-1000 (float (- 1 (/ (mod samples 1000) 1000)))
k-10000 (float (- 1 (/ (mod samples 10000) 10000)))
k-100000 (float (- 1 (/ (mod samples 100000) 100000)))
w-1000 (Math/pow 10 (* -1 (if (< k-1000 1) k-1000 0)))
w-10000 (Math/pow 100 (* -1 (if (< k-10000 1) k-10000 0)))
w-100000 (Math/pow 1000 (* -1 (if (< k-100000 1) k-100000 0)))]
; w increases
w-1000 (Math/pow 1000 (* -1 (if (< k-1000 1) k-1000 0)))
w-10000 (Math/pow 10000 (* -1 (if (< k-10000 1) k-10000 0)))
w-100000 (Math/pow 100000 (* -1 (if (< k-100000 1) k-100000 0)))]
; to calculate new ewma: current value increases, current ewma decreases
[(float (+ (* value w-1000) (* (ewma-1 0) (- 1 w-1000))))
(float (+ (* value w-10000) (* (ewma-1 1) (- 1 w-10000))))
(float (+ (* value w-100000) (* (ewma-1 2) (- 1 w-100000))))]))
Expand Down
42 changes: 20 additions & 22 deletions test/nimrod/core/math_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,26 @@
[nimrod.core.math]))

(deftest compute-ewma
(testing "ewma with 10 values"
(let
[ewma1 (ewma nil 1 1000191)
ewma2 (ewma ewma1 23 1160000)
ewma3 (ewma ewma2 35 1170000)
ewma4 (ewma ewma3 41 1180000)
ewma5 (ewma ewma4 55 1190000)
ewma6 (ewma ewma5 63 1200000)
ewma7 (ewma ewma6 150 1200001)
ewma8 (ewma ewma7 85 1200200)
ewma9 (ewma ewma8 99 1200300)
ewma10 (ewma ewma9 100 1290000)]
(println ewma1)
(println ewma2)
(println ewma3)
(println ewma4)
(println ewma5)
(println ewma6)
(println ewma7)
(println ewma8)
(println ewma9)
(println ewma10))))
(testing "ewma from 0"
(loop [ewma1 nil i 1]
(if (< i 1000)
(recur (ewma ewma1 1 i) (inc i))
(println ewma1))))
(testing "ewma within 1000 values"
(loop [ewma1 [1000 10000 100000] i 1]
(if (< i 1000)
(recur (ewma ewma1 1 i) (inc i))
(println ewma1))))
(testing "ewma within 10000 values"
(loop [ewma1 [1000 10000 100000] i 1]
(if (< i 10000)
(recur (ewma ewma1 1 i) (inc i))
(println ewma1))))
(testing "ewma within 100000 values"
(loop [ewma1 [1000 10000 100000] i 1]
(if (< i 100000)
(recur (ewma ewma1 1 i) (inc i))
(println ewma1)))))

(deftest compute-count-mean-variance
(testing "count-mean-variance with 1 value"
Expand Down

0 comments on commit 53159e5

Please sign in to comment.