• incanter

# cosine-similarity

## incanter.stats

• (cosine-similarity a b)

http://en.wikipedia.org/wiki/Cosine_similarity
http://www.appliedsoftwaredesign.com/cosineSimilarityCalculator.php

The Cosine Similarity of two vectors a and b is the ratio: a dot b / ||a|| ||b||

Let d1 = {2 4 3 1 6}
Let d2 = {3 5 1 2 5}

Cosine Similarity (d1, d2) = dot(d1, d2) / ||d1|| ||d2||

dot(d1, d2) = (2)*(3) + (4)*(5) + (3)*(1) + (1)*(2) + (6)*(5) = 61

||d1|| = sqrt((2)^2 + (4)^2 + (3)^2 + (1)^2 + (6)^2) = 8.12403840464

||d2|| = sqrt((3)^2 + (5)^2 + (1)^2 + (2)^2 + (5)^2) = 8

Cosine Similarity (d1, d2) = 61 / (8.12403840464) * (8)
= 61 / 64.9923072371
= 0.938572618717

### Source incanter/stats.clj:2983 top

```(defn cosine-similarity
"
http://en.wikipedia.org/wiki/Cosine_similarity
http://www.appliedsoftwaredesign.com/cosineSimilarityCalculator.php

The Cosine Similarity of two vectors a and b is the ratio: a dot b / ||a|| ||b||

Let d1 = {2 4 3 1 6}
Let d2 = {3 5 1 2 5}

Cosine Similarity (d1, d2) =  dot(d1, d2) / ||d1|| ||d2||

dot(d1, d2) = (2)*(3) + (4)*(5) + (3)*(1) + (1)*(2) + (6)*(5) = 61

||d1|| = sqrt((2)^2 + (4)^2 + (3)^2 + (1)^2 + (6)^2) = 8.12403840464

||d2|| = sqrt((3)^2 + (5)^2 + (1)^2 + (2)^2 + (5)^2) = 8

Cosine Similarity (d1, d2) = 61 / (8.12403840464) * (8)
= 61 / 64.9923072371
= 0.938572618717
"
[a b]
(let [counts
(apply merge-with +
(map
(fn [[x y]]
{:dot (* x y)
:a (pow x 2)
:b (pow y 2)})
(map vector a b)))]
(/ (:dot counts)
(* (sqrt (:a counts))
(sqrt (:a counts))))))```
Vars in incanter.stats/cosine-similarity: * / defn let
Used in 0 other vars