# Pearson Correlation Vs. Z-normalized Euclidean Distance

It is [well understood](https://arxiv.org/pdf/1601.02213.pdf) that the z-normalized Euclidean distance, $ED_{z-norm}$, and the Pearson correlation, $PC$, between any two subsequences with length $m$ share the following relationship:

$ED_{z-norm} = \sqrt {2 * m * (1 - PC)}$

Naturally, when the two subsequences are perfectly correlated (i.e., $PC = 1$), then we get:

\begin{align}
    ED_{z-norm} ={}&
        \sqrt {2 * m * (1 - PC)}
    \\
    ={}&
        \sqrt {2 * m * (1 - 1)}
    \\
    ={}&
        \sqrt {2 * m * 0}
    \\
    ={}&
        \sqrt {0}
    \\
    ={}&
        0
    \\
\end{align}

Similarly, when the two subsequences are completely uncorrelated (i.e., $PC = 0$), then we get:

\begin{align}
    ED_{z-norm} ={}&
        \sqrt {2 * m * (1 - PC)}
    \\
    ={}&
        \sqrt {2 * m * (1 - 0)}
    \\
    ={}&
        \sqrt {2 * m * 1}
    \\
    ={}&
        \sqrt {2 * m}
    \\
\end{align}

In other words, the largest possible z-normalized distance between any pair of subsequences with length $m$ is $\sqrt{2 * m}$. The maximum distance can never be bigger!

Finally, when two subsequences are anti-correlated (i.e., $PC = -1$), then we get:

\begin{align}
    ED_{z-norm} ={}&
        \sqrt {2 * m * (1 - PC)}
    \\
    ={}&
        \sqrt {2 * m * (1 - (-1))}
    \\
    ={}&
        \sqrt {2 * m * 2}
    \\
    ={}&
        \sqrt {4 * m}
    \\
    ={}&
        2 * \sqrt {m}
    \\
\end{align}

Note that while $2 * \sqrt {m}$ (i.e., anti-correlated) is obviously larger than $\sqrt {2m}$ (i.e., uncorrelated), it is basically impossible for a matrix profile distance to be "worse" than uncorrelated (i.e., larger than $\sqrt {2m}$) due to the fact that it is defined as the distance to its one-nearest-neighbor. For example, given a subsequence `T[i : i + m]`, the matrix profile is supposed to return the z-norm distance to its one-nearest-neighbor. So, even if there existed another z-norm subsequence, `T[j : j + m]`, along the time series that was perfectly anti-correlated with `T[i : i + m]`, then any subsequence that is even slightly shifted away from location `j` must have a smaller distance than $2 * \sqrt {m}$. Therefore, a perfectly anti-correlated subsequence would/could (almost?) never be a one-nearest-neighbor to `T[i : i + m]` especially if `T` is long.


```python

```

0f4933beaabe19f40f8dcbb804267fb7bc4a49eb
ipynb
Jupyter Notebook
docs/Pearson.ipynb
alvii147/stumpy
6dacfcf35ce03255951d70e5dd2f8b3f4e20a27f
BSD-3-Clause
docs/Pearson.ipynb
alvii147/stumpy
6dacfcf35ce03255951d70e5dd2f8b3f4e20a27f
BSD-3-Clause
docs/Pearson.ipynb
alvii147/stumpy
6dacfcf35ce03255951d70e5dd2f8b3f4e20a27f
BSD-3-Clause
Qwen/Qwen-72B
1. YES
2. YES