Browse files

don't update moving average unless there's actually been input

  • Loading branch information...
1 parent e6b8837 commit 97dbc676a4bf90ede43b264feb9e75b143202499 @ztellman committed Aug 8, 2012
Showing with 10 additions and 8 deletions.
  1. +10 −8 src/lamina/stats/moving_average.clj
View
18 src/lamina/stats/moving_average.clj
@@ -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
@@ -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)))]

0 comments on commit 97dbc67

Please sign in to comment.