Created
May 7, 2010 19:55
-
-
Save dermatthias/393933 to your computer and use it in GitHub Desktop.
This file contains 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 [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))))) | |
user> (def v1 (double-array (take 100 (repeatedly rand)))) | |
user> (def v2 (double-array (take 100 (repeatedly rand)))) | |
user> (dotimes [_ 10] (time (pearson-fast v1 v2))) | |
"Elapsed time: 0.192087 msecs" | |
"Elapsed time: 0.261311 msecs" | |
"Elapsed time: 0.142108 msecs" | |
"Elapsed time: 0.141602 msecs" | |
"Elapsed time: 0.145241 msecs" | |
"Elapsed time: 0.140171 msecs" | |
"Elapsed time: 0.140597 msecs" | |
"Elapsed time: 0.140423 msecs" | |
"Elapsed time: 0.140051 msecs" | |
"Elapsed time: 0.141224 msecs" |
This file contains 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-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))))) | |
user> (def w1 (take 100 (repeatedly rand))) | |
user> (def w2 (take 100 (repeatedly rand))) | |
user> (dotimes [_ 10] (time (pearson-slow w1 w2))) | |
"Elapsed time: 0.570365 msecs" | |
"Elapsed time: 0.515203 msecs" | |
"Elapsed time: 0.511854 msecs" | |
"Elapsed time: 0.509913 msecs" | |
"Elapsed time: 0.513456 msecs" | |
"Elapsed time: 0.514767 msecs" | |
"Elapsed time: 0.519651 msecs" | |
"Elapsed time: 0.517207 msecs" | |
"Elapsed time: 0.537696 msecs" | |
"Elapsed time: 0.521394 msecs" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment