Permalink
Browse files

Merge pull request #1 from Quantisan/london

rollling functions, not yet optimised
  • Loading branch information...
2 parents 6461dfe + 8a60caf commit 2d953c1c420b4f6ba5b1e172d278711ac95bb9db @otfrom otfrom committed Jan 15, 2012
Showing with 100 additions and 1 deletion.
  1. +72 −1 modules/incanter-core/src/incanter/zoo.clj
  2. +28 −0 modules/incanter-core/test/incanter/zoo_test.clj
@@ -31,4 +31,75 @@
gamma pow sqrt diag trans regularized-beta ncol
nrow identity-matrix decomp-cholesky decomp-svd
matrix length log10 sum sum-of-squares sel matrix?
- cumulative-sum solve vectorize bind-rows)]))
+ cumulative-sum solve vectorize bind-rows)]
+ [incanter.stats :only (mean median)]))
+
+;;;;; Start of ROLL functions ;;;;;
+
+(defn partialsums [start coll]
+ (lazy-seq
+ (if-let [coll (seq coll)]
+ (cons start (partialsums (+ start (first coll)) (rest coll)))
+ (list start))))
+
+(defn big-n-roll-mean
+"
+ Returns the unweighted mean of the previous n data points.
+ Optimised for larger n (e.g. n > 20) window.
+
+ References:
+ http://en.wikipedia.org/wiki/Moving_average#Simple_moving_average
+ http://www.learningclojure.com/2010/03/moving-average-of-list.html
+"
+ [n coll]
+ (map #(/ % n)
+ (let [start (apply + (take n coll))
+ diffseq (map - (drop n coll) coll)]
+ (partialsums start diffseq))))
+
+(defn roll-apply
+"
+ A generic function for applying a function to rolling window of a collection.
+
+ Arguments:
+ f -- function to be applied
+ n -- size of rolling window
+ coll -- collection of data
+"
+ [f n coll]
+ (map f (partition n 1 coll)))
+
+;;;;; TODO rolls, optimise each ;;;;;
+
+(defn roll-mean
+"
+ Returns the unweighted mean of the previous n data points.
+
+ References:
+ http://en.wikipedia.org/wiki/Moving_average#Simple_moving_average
+"
+ [n coll]
+ (roll-apply mean n coll))
+
+(defn roll-median
+"
+ Returns the rolling median of the previous n elements.
+"
+ [n coll]
+ (roll-apply median n coll))
+
+(defn roll-max
+"
+ Returns the rolling max of the previous n elements.
+"
+ [n coll]
+ (roll-apply #(apply max %) n coll))
+
+(defn roll-min
+"
+ Returns the rolling min of the previous n elements.
+"
+ [n coll]
+ (roll-apply #(apply min %) n coll))
+
+;;;;; End of ROLL functions ;;;;;
@@ -0,0 +1,28 @@
+(ns incanter.zoo-test
+ (:use clojure.test
+ (incanter zoo core stats)))
+
+(def integers (iterate inc 0))
+(def sma-5 [2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 10.0 11.0])
+
+(deftest roll-apply-test
+ (is (= [3 6 9 12 15]
+ (take 5 (roll-apply #(apply + %) 3 integers)))))
+
+(deftest roll-mean-test
+ (is (= sma-5
+ (take 10 (roll-mean 5 integers))))
+ (is (= [17 18 19 20 21 22 23 24 25 26]
+ (take 10 (big-n-roll-mean 35 integers)))))
+
+(deftest roll-median-test
+ (is (= [2.0 3.0 4.0 5.0 6.0]
+ (take 5 (roll-median 5 integers)))))
+
+(deftest roll-max-test
+ (is (= [2 3 4 5 6]
+ (take 5 (roll-max 3 integers)))))
+
+(deftest roll-min-test
+ (is (= [0 1 2 3 4]
+ (take 5 (roll-min 3 integers)))))

0 comments on commit 2d953c1

Please sign in to comment.