Skip to content

Commit

Permalink
Merge branch 'master' of [email protected]:bradford/infer
Browse files Browse the repository at this point in the history
  • Loading branch information
bradford committed Aug 9, 2010
2 parents c7e209a + b4bb37f commit 224741d
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/infer/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -282,3 +282,11 @@
(if (not (map? v))
[new-key v]
(flatten-level new-key v)))) nil nested-map))))

(defn max-by [keyfn coll]
(if (empty? coll) nil
(let [maxer (fn [max-elem next-elem]
(if (> (keyfn max-elem) (keyfn next-elem))
max-elem
next-elem))]
(reduce maxer coll))))
8 changes: 0 additions & 8 deletions src/infer/learning.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,6 @@
#(pmap mi-from-matrix (feature-target-pairs A %))
(range 0 (column-count A))))

(defn- max-by [keyfn coll]
(if (empty? coll) nil
(let [maxer (fn [max-elem next-elem]
(if (> (keyfn max-elem) (keyfn next-elem))
max-elem
next-elem))]
(reduce maxer coll))))

(defn index-of-max [v indices]
(second (max-by first (map vector v indices))))

Expand Down
27 changes: 27 additions & 0 deletions src/infer/streaming.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(ns infer.streaming)

;;WARNING: gnarlley unguarded mutation back here. This is not meant to be called by multiple concurrent callers, it is meant to be used only in isolation. It can be wrapped with a checkpointer, but not a memoizer. It is stricly for use with updating streams.
(defn rolling [f p n]
(let [q (RollingQueue. n)]
(fn [x]
(if (.isPrimed q)
(f x (.enqueue q x))
(let [pr (p x (.enqueue q x))]
(if (.isPrimed q) pr Double/NaN))))))

;; (defn sum [new old]

;; sum = sum - compoundQueueOfDoublesfixedLengthQueue.Enqueue(d) + d;
;; return sum;
;; }

;; protected override double prime(double d)
;; {
;; sum = initialSum.Calculate(d);
;; compoundQueueOfDoublesfixedLengthQueue.Enqueue(d);
;; return sum;
;; }
;; }


;;http://en.wikipedia.org/wiki/Selection_algorithm
35 changes: 34 additions & 1 deletion src/infer/trees.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
(ns infer.trees)
(ns infer.trees
(:use infer.core)
(:use infer.measures))

(defn leaf
"takes an index i into a vector v.
Expand All @@ -17,3 +19,34 @@
[i p l r]
(fn [v]
(if (p (v i)) (l v) (r v))))

(defn midpoints [points]
(map (comp mean vector)
(rest points) points))

(defn count-split
[region i s]
(let [l (count (filter #(<= (nth % i) s) region))
r (- (count region) l)]
[l r]))

(defn count-within
([region i l r]
(count (filter #(and (> (nth % i) l)
(<= (nth % i) r)) region))))

;;TODO: this only does the counts term of the computiton, we still need to add mean y vals term.
(defn best-split
([r]
(map #(best-split r %)
(range 0 (count (first r)))))
([r i]
(let [values (into #{} (map #(nth % i) r))
counts (map #(count-split r i %) values)
tot (count r)
gain (map
(fn [[l r] v]
[v (/ (* l r) tot)])
counts values)]
(apply vector i (max-by second gain)))))

3 changes: 2 additions & 1 deletion test/infer/inc_stats_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
(is (= 17 (.getAtLookback q 1)))
(is (= 3 (.getAtLookback q 2)))
(is (= 1 (.getAtIndex q 2)))
(is (= 3 (.getLength q)))))))
(is (= 3 (.getLength q)))))))

26 changes: 25 additions & 1 deletion test/infer/trees_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,28 @@
;;TODO:
;;continuous vs. discrete features
;;missing values
;;optimal stopping / pruning
;;optimal stopping / pruning

(deftest midpoints-test
(let [points [1 2 3 4 5]]
(is (= [1.5 2.5 3.5 4.5]
(midpoints points)))))

(deftest count-splits
(is (= [2 1]
(count-split [[2][3][4]] 0 3))))

(deftest count-withins
(is (= 2
(count-within [[2][3][4][5][6]] 0 3 5))))


(deftest best-splits
(is (= [[0 4 6/5]
[1 5 6/5]
[2 7 6/5]]
(best-split [[2 2 4]
[3 4 6]
[4 5 7]
[5 7 9]
[6 9 9]]))))

0 comments on commit 224741d

Please sign in to comment.