Algorithm Notes: Frechet Distance Metric

A measure of similarity between curves that takes into account the position and order of points along the curve

1 concept

1.1 Intuitive understanding

  •  The owner takes path A and the dog takes path B, they have different pace schemes
  • The length of the shortest dog leash required by the owner and the dog to complete the two paths
    • (Length of leash required at one pace), but longer at other paces

1.2 Mathematical understanding

F(A, B)=\inf _{\alpha, \beta} \max _{t \in[0,1]}\{d(A(\alpha(t)), B(\beta(t)))\}

  • The Infimum of the Maximum Distance Between Two Curves A(α(t)) and B(β(t))
    • t is understood as time
    • α(t) and β(t) are understood as the speed of change of people and dogs over time
    • A(α(t)) and B(β(t)) represent the positions of people and dogs at time t
    • ——> The infimum of the maximum value means that under each human-dog speed scheme, there is a corresponding maximum distance; then for all speed schemes, which of these maximum distances is the smallest?

 

2 The difference with Hausdorff distance

Math notes/scipy notes: Hausdorff distance (Hausdorff)_UQI-LIUWJ's Blog-CSDN Blog

  • The Fréchet metric takes into account the flow of two curves, as pairs of points whose distance contributes to the Fréchet distance are swept successively along the respective curves.
    • This makes the Fréchet distance a better measure of the similarity of curves than the Hausdorff distance for an arbitrary set of points.
    • It is possible for two curves to have a smaller Hausdorff distance but a larger Fréchet distance.

     

3 Discretized Frechet distance

The discrete Fréchet distance is an approximation of the continuous Fréchet distance. When the discrete points selected by the curve are enough, the discrete Fréchet distance is approximately equal to the continuous Fréchet distance.

 3.1 python implementation

3.1.1 Datasets

import numpy as np
a_array = np.array([2, 4, 6, 8])
b_array = np.array([2, 3, 4, 6, 5, 7, 8])

3.1.2 Distance function

#距离函数
def euc_dist(pt1, pt2):
        return np.sqrt(np.square(pt2[0] - pt1[0]) + np.square(pt2[1] - pt1[1]))

 3.1.3 Frechet distance

def _c(ca, i, j, P, Q):  
        if ca[i, j] > -1:
            return ca[i, j]
        #ca[i,j]之前计算过
        elif i == 0 and j == 0:  
            ca[i, j] = euc_dist(P[0], Q[0])
            #刚刚考虑P序列的0和Q序列的0时
        elif i > 0 and j == 0:  
            ca[i, j] = max(_c(ca, i - 1, 0, P, Q), euc_dist(P[i], Q[0]))
           # 刚刚考虑P序列的i和Q序列的0时
        elif i == 0 and j > 0:  
            ca[i, j] = max(_c(ca, 0, j - 1, P, Q), euc_dist(P[0], Q[j]))
            #刚刚考虑P序列的0和Q序列的j时
        elif i > 0 and j > 0:  
            ca[i, j] = max(min(_c(ca, i - 1, j, P, Q),  # P动Q不动
                               _c(ca, i - 1, j - 1, P, Q),  # P不动Q动
                               _c(ca, i, j - 1, P, Q)),  # 一起动
                           euc_dist(P[i], Q[j]))
            #min是不考虑i,j时,至少需要多长的”狗绳“
            #再取max表示考虑i,j时,需要多长的”狗绳“
        else: 
            ca[i, j] = float("inf")
            # 非法的无效数据,算法中不考虑,此时 i<0,j<0
        return ca[i, j]
def frechet_distance(P, Q):
        ca = np.ones((len(P), len(Q)))
        ca = np.multiply(ca, -1)
        # ca初始化成全-1的矩阵,shape = ( len(a), len(b) )
        dis = _c(ca, len(P) - 1, len(Q) - 1, P, Q)  
        return dis

 3.1.4 Calculation results

curve_line_a = list(zip(range(len(a_array)), a_array))
curve_line_b = list(zip(range(len(b_array)), b_array))
#给a,b添加x坐标
curve_line_a
#[(0, 2), (1, 4), (2, 6), (3, 8)]

frechet_distance(curve_line_a,curve_line_b)
#3.0

 4 disadvantages

  • sensitive to noise

Reference content:

Frechet distance distance calculation principle and python implementation_frechet distance_spatial_coder's blog-CSDN blog

Curve similarity measurement - detailed explanation of Fréchet distance calculation and python calculation of curve distance-IOTWORD Internet of Things

Fréchet distance matlab, discrete Fréchet (Frechet) distance evaluation curve similarity - Diabo's blog - CSDN blog

Guess you like

Origin blog.csdn.net/qq_40206371/article/details/130039377