Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A quick moving average
(defn sma [n s]
{:pre [(pos? n)]}
(concat
(repeat (dec n) nil)
(map #(/ (reduce + %) n) (partition n 1 s))))
@ihodes

This comment has been minimized.

Copy link

ihodes commented Dec 2, 2011

 (defn simple-moving-average                                                                                                                            
   "Calculates the moving average of 's with window size 'w.                                                                                     

    Returns a list with length less 'w than the original."                                                                                       
   [xs w]                                                                                                                                        
   (map #(/ (reduce + %) w)                                                                                                                      
         (partition w 1 xs)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.