Background

There are many proximity-based algorithms that can be used to detect potential data outliers. For example, the KNN (K-Nearest Neighbors) is a distance-based algorithm that work well in capturing outliers with extreme values. Specifically, when using the KNN algorithm using the Mahalanobis distance as the distance metric, it tends to capture more meaningful and subtle outliers considering the correlations between features.

More specifically, it is best to apply Euclidean distance measurements only if the features are 1) independent and 2) having variances equal to 1. The second condition can usually be satisfied by standardizing while the first condition usually remains an issue.

On the other hand, Mahalanobis distance measure first transforms the features into uncorrelated features with variances equal to 1 by dividing the features by their covariance matrix (or practically, multiplying by the inverse of the covariance matrix), then applies Euclidean distance on the transformed feature.

Problem

Given two observations $x$ and $y$:

While the KNN with Mahalanobis distance can be carried out in some available modules (i.e. this pyod algorithm), runtime is usually a concern. Because the matrix multiplication by $\Sigma^{-1}$ needs to be computed in each pairwise distance calculation and therefore significantly increase the runtime (imagine doing this with 10 million observations).

Solution

To address this problem, we can first find the square root matrix $A$ such that $A^TA = \Sigma^{-1}$

And then basically just run the normal KNN algorithm (with Euclidean distances) on the transformed observation $x'$ and $y'$, which will be significantly faster as it avoids extensive matrix multiplications:

Well you may ask what if there are no real square root matrix $A$, in which case the tranformed $x'$ and $y'$ will have imaginary parts? Luckily, it seems most of the time we can just use the real part of $A$ to do the transformation and the subsequent Mahalanobis distance calculation, provided that the estimation error ($A^TA - \Sigma^{-1}$) is small.
{"extraction_info": {"found_math": true, "script_math_tex": 12, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 1, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.8950963020324707, "perplexity": 419.2812240768907}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.3, "absolute_threshold": 20, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-04/segments/1610703514423.60/warc/CC-MAIN-20210118061434-20210118091434-00041.warc.gz"}