-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdistance.clj
More file actions
55 lines (51 loc) · 2.25 KB
/
distance.clj
File metadata and controls
55 lines (51 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
(ns lambda-ml.distance
"Functions that compute measures of distance between values."
(:require [lambda-ml.core :as c]))
(defn cosine
"Returns the cosine distance between two points by subtracting the cosine
similarity from 1. Assumes that both points are represented as sequences of
the same dimension. Given a dimension d, returns the distance between two
points as if the values for all other dimensions were set to zero."
([x y]
(- 1 (/ (c/dot-product x y)
(* (Math/sqrt (c/dot-product x x))
(Math/sqrt (c/dot-product y y))))))
([x y d]
(cosine (vector (nth x d)) (vector (nth y d)))))
(defn euclidean
"Returns the Euclidean distance (squared) between two points. Assumes that
both points are represented as sequences of the same dimension. Given a
dimension d, returns the distance between two points as if the values for all
other dimensions were set to zero."
([x y]
(->> (map - x y)
(map #(* % %))
(reduce +)))
([x y d]
(euclidean (vector (nth x d)) (vector (nth y d)))))
(defn haversine
"Returns the great-circle distance between two points represented as
geographic coordinates. Given a dimension d, returns the distance between the
two points as if the value for the other dimension was set to zero."
([[lat1 lng1] [lat2 lng2]]
(let [r 3959.9 ; miles; km = 6372.8
dlat (Math/toRadians (- lat2 lat1))
dlng (Math/toRadians (- lng2 lng1))
lat1 (Math/toRadians lat1)
lat2 (Math/toRadians lat2)
a (+ (* (Math/sin (/ dlat 2)) (Math/sin (/ dlat 2)))
(* (Math/sin (/ dlng 2)) (Math/sin (/ dlng 2)) (Math/cos lat1) (Math/cos lat2)))]
(* r 2 (Math/asin (Math/sqrt a)))))
([x y d]
(let [other (mod (inc d) 2)]
(haversine (assoc x other 0) (assoc y other 0)))))
(defn jaccard
"Returns the Jaccard distance between two points by subtracting the Jaccard
similarity coefficient from 1. Assumes that both points are represented as
sequences of the same dimension. Given a dimension d, returns the distance
between two points as if the values for all other dimensions were set to zero."
([x y]
(- 1 (/ (reduce + (map min x y))
(reduce + (map max x y)))))
([x y d]
(jaccard (vector (nth x d)) (vector (nth y d)))))