Skip to content

Clarify Mahalanobis documentation - #272

Open
davide-grheco wants to merge 2 commits into
JuliaStats:masterfrom
davide-grheco:master
Open

davide-grheco wants to merge 2 commits into
JuliaStats:masterfrom
davide-grheco:master

Conversation

@davide-grheco

Copy link
Copy Markdown

The Mahalanobis function passes the provided matrix to the Mahalanobis formula without inverting it. A user would generally expect the function to accept the covariance matrix and that is what the documentation said. However, to obtain the Mahalanobis distance it is required to pass the inverse of the covariance matrix as the function is currently formulated.

This closes issue #244
In my opinion it would be better if the Mahalanobis function did the inversion of the matrix. The user would likely expect the Mahalanobis function to accept the covariance matrix rather than requiring the inverse. However I do understand this may break plenty downstream uses of the library.

The Mahalanobis function passes the provided matrix to the Mahalanobis
formula without inverting it. A user would generally expect the function
to accept the covariance matrix and that is what the documentation said.
However, to obtain the Mahalanobis distance it is required to pass the
inverse of the covariance matrix as the function is currently
formulated.
@codecov

codecov Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.04%. Comparing base (886ad02) to head (e052e76).
⚠️ Report is 2 commits behind head on master.

❗ There is a different number of reports uploaded between BASE (886ad02) and HEAD (e052e76). Click for more details.

HEAD has 3 uploads less than BASE
Flag BASE (886ad02) HEAD (e052e76)
5 2
Additional details and impacted files
@@             Coverage Diff             @@
##           master     #272       +/-   ##
===========================================
- Coverage   95.86%   79.04%   -16.83%     
===========================================
  Files          10       10               
  Lines         944      606      -338     
===========================================
- Hits          905      479      -426     
- Misses         39      127       +88     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@nalimilan nalimilan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment thread src/mahalanobis.jl Outdated
Create a Mahalanobis distance (i.e., a bilinear form) with covariance matrix `Q`.
Create a Mahalanobis distance `sqrt((x - y)' * Q * (x - y))`
with positive semidefinite matrix `Q`. Note that `Q` enters the bilinear form directly:
to obtain the classical Mahalanobis distance, pass the inverse of the covariance

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it really correct to oppose "the classical Mahalanobis distance" to this method's definition? AFAICT this is also the classical distance, it's just that the function expects a different input from the standard. How about "to obtain the Mahalanobis distance from the covariance matrix S, pass its inverse, i.e. Mahalanobis(inv(S))."?

Comment thread src/mahalanobis.jl Outdated

Create a Mahalanobis distance (i.e., a bilinear form) with covariance matrix `Q`.
Create a Mahalanobis distance `sqrt((x - y)' * Q * (x - y))`
with positive semidefinite matrix `Q`. Note that `Q` enters the bilinear form directly:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"enters" seems weird here: maybe "specifies"?

Also probably worth adding an empty line so that the short definition is in a separate paragraph.

Comment thread README.md Outdated
@@ -240,7 +240,9 @@ arrays of real numbers; `w` is an iterator/array of parameters (like weights or
periods); `k` and `l` are iterators/arrays of distinct elements of
any kind; `a` and `b` are iterators/arrays of Bools; and finally, `p` and `q` are

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"finally" isn't appropriate anymore. Also below better continue the same sentence with a semicolon for consistency.

Comment thread README.md Outdated
Comment on lines +244 to +245
enters the bilinear form directly: to obtain the classical Mahalanobis distance,
pass the inverse of the covariance matrix `S`, i.e. `mahalanobis(x, y, inv(S))`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to specific to mention here (we can't detail all methods), people can look at the docstring if needed.

Suggested change
enters the bilinear form directly: to obtain the classical Mahalanobis distance,
pass the inverse of the covariance matrix `S`, i.e. `mahalanobis(x, y, inv(S))`.
specifies the bilinear form (inverse of covariance matrix).

Made the readme explanation of the mahalanobis distance parameter
simpler.
Clarify that `Q` is the precision matrix, which is the inverse of the
covariance matrix `S`.
@davide-grheco

Copy link
Copy Markdown
Author

Sorry for the delay. I modified the documentation, now it clarifies that Q is the precision matrix (https://en.wikipedia.org/wiki/Precision_(statistics)) which is the inverse of the Covariance Matrix S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants