I think you could simplify your code by using map twice. What about:
(untested)

 (defn weighted-moving-average
   "Generate a lazy sequence consisting of weighted moving averages
   over the input sequence. The weighting is given by weight-fn, having
   a domain 0..1 which will be scaled across win-size elements to
   calculate the weighting."
   [weight-fn win-size input]
   (let [weights (reverse (for [t (range 1 (inc win-size))]
                                   (weight-fn (/ t win-size))))
          weight-sum (reduce + weights)
          normalized-weights (map #(/ % weight-sum) weights)
          avg #(reduce + (map * normalized-weights %))]
     (map avg (partition win-size 1 input))))

btw, why the 'reverse in weights?

On Tue, Jun 23, 2009 at 4:33 PM, Kyle Schaffrick <k...@raidi.us> wrote:

>
> On Tue, 23 Jun 2009 10:20:52 -0400
> Kyle Schaffrick <k...@raidi.us> wrote:
> >
> > On Mon, 22 Jun 2009 23:36:25 -0700 (PDT), Sean Devlin
> > <francoisdev...@gmail.com> wrote:
> > > Hey all,
> > > Does anyone know of a moving window function?  I'm curious if there
> > > are any tools like this for digital signals processing, 30-day
> > > moving averages, etc.
> >
> > If you need weighted moving averages, this is a tad ugly but seems to
> > work ..
> >
>
> Okay it is still a tad ugly but hopefully now it won't be because
> the blasted mail client mutilated it :)
>
>  (defn weighted-moving-average*
>    "Generate a lazy sequence consisting of weighted moving averages
>    over the pre-partitioned input sequence. The weights parameter
>    should be a sequence of weights the same length as the partition
>    size."
>    [weights input]
>    (lazy-seq
>      (when-let [input (seq input)]
>        (let [weight-sum         (apply + weights)
>              weighted-input-sum (apply + (map * weights
>                                                 (first input)))]
>          (cons (/ weighted-input-sum weight-sum)
>                (weighted-moving-average* weights (rest input)))))))
>
>  (defn weighted-moving-average
>    "Generate a lazy sequence consisting of weighted moving averages
>    over the input sequence. The weighting is given by weight-fn, having
>    a domain 0..1 which will be scaled across win-size elements to
>    calculate the weighting."
>    [weight-fn win-size input]
>    (let [weight-samples (reverse (for [t (range 1 (inc win-size))]
>                                    (/ t win-size)))
>          weights        (map weight-fn weight-samples)]
>      (weighted-moving-average*
>        weights (partition win-size 1 input))))
>
> >
> > -Kyle
> >
> >
>
> >
>


-- 
Professional: http://cgrand.net/ (fr)
On Clojure: http://clj-me.blogspot.com/ (en)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to