Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
don't update moving average unless there's actually been input
Browse files Browse the repository at this point in the history
  • Loading branch information
ztellman committed Aug 8, 2012
1 parent e6b8837 commit 97dbc67
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions src/lamina/stats/moving_average.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
(defn update-count [^Counter counter val]
(Counter. (double (+ (.sum counter) (double val))) (inc (.cnt counter))))

(deftype-once MovingAverage
(deftype MovingAverage
[^{:volatile-mutable true} initialized?
^{:volatile-mutable true :tag double} rate
^AtomicReference counter
Expand All @@ -39,13 +39,15 @@
sum (.sum counter)
cnt (.cnt counter)
r* (/ sum interval (max 1 cnt))]
(if initialized?
(let [r rate]
(set! rate (double (+ r (* alpha (- r* r))))))
(do
(set! initialized? true)
(set! rate (double r*))))
(* interval rate))))
(if (= 0 cnt)
0.0
(if initialized?
(let [r rate]
(set! rate (double (+ r (* alpha (- r* r))))))
(do
(set! initialized? true)
(set! rate (double r*))
(* interval rate)))))))

(defn moving-average [interval window]
(let [alpha (- 1 (Math/exp (/ (- interval) window)))]
Expand Down

0 comments on commit 97dbc67

Please sign in to comment.