Skip to content

Instantly share code, notes, and snippets.

@ejackson
Created December 2, 2011 16:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ejackson/1423801 to your computer and use it in GitHub Desktop.
Save ejackson/1423801 to your computer and use it in GitHub Desktop.
A quick moving average
(defn sma [n s]
{:pre [(pos? n)]}
(concat
(repeat (dec n) nil)
(map #(/ (reduce + %) n) (partition n 1 s))))
@ihodes
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