Skip to content

Instantly share code, notes, and snippets.

@pingles
Created July 12, 2010 23:07
Show Gist options
  • Save pingles/473203 to your computer and use it in GitHub Desktop.
Save pingles/473203 to your computer and use it in GitHub Desktop.
(defn exponential-smooth-recur
[x f]
(letfn [(smoothed-val [m n f]
(+
(* m (- 1 f))
(* n f)))]
(reverse (lazy-seq
(loop [observed (rest x) smoothed (list (first x))]
(if (empty? observed)
smoothed
(recur (rest observed) (cons (smoothed-val (first smoothed) (first observed) f) smoothed))))))))
(println (exponential-smooth-recur '(1 2 3 4) 0.9))
(defn exponential-smooth-partial
[x f]
(letfn [
(smooth
[alpha st xt]
(conj st
(+
(* alpha xt)
(* (- 1 alpha) (last st)))))]
(reduce (partial smooth f) [(first x)] (rest x))))
(println (exponential-smooth-partial '(1 2 3 4) 0.9))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment