Created
May 7, 2010 21:03
-
-
Save swannodette/393995 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn pearson-fast | |
[v1 v2] | |
(let [v1 (doubles v1) | |
v2 (doubles v2) | |
dim (double (count v1)) | |
meanv1 (/ (areduce v1 i ret 0 | |
(+ ret (aget v1 i))) dim) | |
meanv2 (/ (areduce v2 i ret 0 | |
(+ ret (aget v2 i))) dim) | |
sum (areduce v1 i ret 0 | |
(+ ret (* (- (aget v1 i) meanv1) | |
(- (aget v2 i) meanv2)))) | |
norm1 (areduce v1 i ret 0 | |
(+ ret (* (Math/pow | |
(- (aget v1 i) meanv1) 2)))) | |
norm2 (areduce v1 i ret 0 | |
(+ ret (* (Math/pow | |
(- (aget v2 i) meanv2) 2))))] | |
(/ sum (Math/sqrt (* norm1 norm2))))) | |
(def a1 (double-array (take 1000 (repeatedly rand)))) | |
(def a2 (double-array (take 1000 (repeatedly rand)))) | |
(dotimes [_ 10] | |
(time | |
(dotimes [_ 1000] | |
(pearson-fast a1 a2)))) | |
(defn pearson-slow | |
[v1 v2] | |
(let [dim (count v1) | |
meanv1 (double (/ (reduce + v1) dim)) | |
meanv2 (double (/ (reduce + v2) dim)) | |
sum (reduce + (map (fn [i j] | |
(* (- i meanv1) | |
(- j meanv2))) v1 v2)) | |
norm1 (reduce + (map (fn [i] | |
(Math/pow (- i meanv1) 2)) v1)) | |
norm2 (reduce + (map (fn [i] | |
(Math/pow (- i meanv2) 2)) v2))] | |
(/ sum (Math/sqrt (* norm1 norm2))))) | |
(def v1 (into [] (take 1000 (repeatedly rand)))) | |
(def v2 (into [] (take 1000 (repeatedly rand)))) | |
(dotimes [_ 10] | |
(time | |
(dotimes [_ 1000] | |
(pearson-slow v1 v2)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment