# 6. Similarity Functions

- **Created by Andrés Segura Tinoco**
- **Created on May 20, 2019**
- **Updated on Mar 19, 2021**

In statistics and related fields, a **similarity measure** or similarity function is a real-valued function that quantifies the similarity between two objects. In short, a similarity function quantifies how much alike two data objects are <a href="#link_one">[1]</a>.

## 6.1. Common similarity functions


```python
# Load the Python libraries
from math import *
from decimal import Decimal
from scipy import stats as ss
import sklearn.metrics.pairwise as sm
import math
```

\begin{align}
    similarity(X, Y) = d(X, Y) = \sqrt{\sum_{i=1}^n (X_i - Y_i)^2} \tag{1}
\end{align}


```python
# (1) Euclidean distance function
def euclidean_distance(x, y):
    return sqrt(sum(pow(a-b,2) for a, b in zip(x, y)))
```

\begin{align}
    similarity(X, Y) = d(X, Y) = \sum_{i=1}^n |X_i - Y_i| \tag{2}
\end{align}


```python
# (2) manhattan distance function
def manhattan_distance(x, y):
    return sum(abs(a-b) for a,b in zip(x,y))
```

\begin{align}
    similarity(X, Y) = d(X, Y) = (\sum_{i=1}^n |X_i - Y_i|^p)^\frac{1}{p} \tag{3}
\end{align}


```python
# (3) Minkowski distance function
def _nth_root(value, n_root):
    root_value = 1/float(n_root)
    return round(Decimal(value) ** Decimal(root_value),3)

def minkowski_distance(x, y, p = 3):
    return float(_nth_root(sum(pow(abs(a-b), p) for a,b in zip(x, y)), p))
```

\begin{align}
    similarity(X, Y) = cos(\theta) = \frac{\vec{X}.\vec{Y}}{\|\vec{X}\|.\|\vec{Y}\|} = \frac{\sum_{i=1}^n X_i.Y_i}{\sqrt{\sum_{i=1}^n X_i^2}.\sqrt{\sum_{i=1}^n Y_i^2}} \tag{4}
\end{align}


```python
# (4) Cosine similarity function
def _square_rooted(x):
    return round(sqrt(sum([a*a for a in x])),3)

def cosine_similarity(x, y):
    numerator = sum(a*b for a,b in zip(x,y))
    denominator = _square_rooted(x) * _square_rooted(y)
    return round(numerator/float(denominator),3)
```

\begin{align}
    similarity(X, Y) = \frac{cov(X, Y)}{\sigma_X . \sigma_Y} = \frac{\sum_{i=1}^n (X_i - \bar{X}).(Y_i - \bar{Y})}{\sqrt{\sum_{i=1}^n (X_i - \bar{X})^2 . (Y_i - \bar{Y})^2}} \tag{5}
\end{align}


```python
# (5) Pearson similarity function
def _avg(x):
    assert len(x) > 0
    return float(sum(x)) / len(x)

def pearson_similarity(x, y):
    assert len(x) == len(y)
    n = len(x)
    assert n > 0
    avg_x = _avg(x)
    avg_y = _avg(y)
    diffprod = 0
    xdiff2 = 0
    ydiff2 = 0
    for idx in range(n):
        xdiff = x[idx] - avg_x
        ydiff = y[idx] - avg_y
        diffprod += xdiff * ydiff
        xdiff2 += xdiff * xdiff
        ydiff2 += ydiff * ydiff

    return diffprod / math.sqrt(xdiff2 * ydiff2)
```

\begin{align}
    similarity(X, Y) = J(X, Y) = \frac{|X \cap Y|}{|X \cup Y|} = \frac{|X \cap Y|}{|X| + |Y| - |X \cap Y|} \tag{6}
\end{align}


```python
# (6) Jaccard similarity function
def jaccard_similarity(x, y):
    intersection_cardinality = len(set.intersection(*[set(x), set(y)]))
    union_cardinality = len(set.union(*[set(x), set(y)]))
    return intersection_cardinality / float(union_cardinality)
```

## 6.2. Manual examples


```python
# Vectors
x = [-4.593481, -5.478033, 1.127111,  1.252885, -2.286953]  # Messi
y = [-4.080334, -3.406618, 4.334073, -0.485612, -2.817897]  # CR
z = [-4.048185, -5.546171, 0.505673,  0.616553, -1.730906]  # Neymar
```

### Euclidean distance


```python
euclidean_distance(x, y)
```




    4.259455195846412




```python
euclidean_distance(x, z)
```




    1.1841800466723797



### Manhattan distance


```python
manhattan_distance(x, y)
```




    8.060965




```python
manhattan_distance(x, z)
```




    2.4272509999999996



### Minkowski distance


```python
minkowski_distance(x, y)
```




    3.619




```python
minkowski_distance(x, z)
```




    0.941



### Cosine similarity


```python
cosine_similarity(x, y)
```




    0.842




```python
cosine_similarity(x, z)
```




    0.99



### Pearson similarity


```python
pearson_similarity(x, y)
```




    0.8214001476231276




```python
pearson_similarity(x, z)
```




    0.9888645775446726



### Jaccard similarity


```python
a = [0,  1, 2, 3, 4, 5]
b = [-1, 1, 2, 0, 3, 5]
```


```python
jaccard_similarity(a, b)
```




    0.7142857142857143



## 6.3. Sklearn examples


```python
corr = sm.euclidean_distances([x], [y])
float(corr[0])
```




    4.259455195846413




```python
corr = sm.manhattan_distances([x], [y])
float(corr[0])
```




    8.060965




```python
corr = sm.cosine_similarity([x], [y])
float(corr[0])
```




    0.841904969009294




```python
corr, p_value = ss.pearsonr(x, y)
corr
```




    0.8214001476231275



## Reference

<a name='link_one' href='https://en.wikipedia.org/wiki/Similarity_measure' target='_blank' >[1]</a> Wikipedia - Similarity measure.  

---
<a href="https://ansegura7.github.io/Algorithms/">« Home</a>

e79592db92a45d90d69613d9b86840e28894a12f
ipynb
Jupyter Notebook
similarity-functions/SimilarityFunctions.ipynb
ansegura7/Algorithms
4788c183ff42964dacc2bb51d7715f120d79c447
MIT
2020-01-08T17:10:32.000Z
2022-02-20T07:34:25.000Z
similarity-functions/SimilarityFunctions.ipynb
suanhwee1234/Algorithms
7ac304fac42a8dec50580c78e623b0f5c021373b
MIT
similarity-functions/SimilarityFunctions.ipynb
suanhwee1234/Algorithms
7ac304fac42a8dec50580c78e623b0f5c021373b
MIT
2019-07-15T20:14:17.000Z
2021-09-28T03:06:38.000Z
Qwen/Qwen-72B
1. YES
2. YES