Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Bhatthacharayya distance #4111

Merged
merged 5 commits into from
Nov 1, 2019
Merged

Conversation

AndrejaKovacic
Copy link
Contributor

Description of changes

A new metric is introduced in distances widget. Bhatthacharayya is used to measure the distance between distributions. Currently, the user has to already have the data that they see as a distribution. In the future, pivot could be extended to have aggregation of all features, filtered by column values, so distributions could be generated in Orange itself.

Includes
  • Code changes
  • Tests
  • Documentation

@codecov
Copy link

codecov bot commented Oct 17, 2019

Codecov Report

Merging #4111 into master will increase coverage by 0.01%.
The diff coverage is 100%.

@@            Coverage Diff            @@
##           master   #4111      +/-   ##
=========================================
+ Coverage   85.68%   85.7%   +0.01%     
=========================================
  Files         390     389       -1     
  Lines       69727   69813      +86     
=========================================
+ Hits        59744   59830      +86     
  Misses       9983    9983

@janezd janezd self-assigned this Oct 18, 2019
@@ -644,6 +644,36 @@ class PearsonRAbsolute(CorrelationDistance):
def fit(self, _):
return PearsonModel(True, self.axis, self.impute)

def _prob_dist(a):
# Makes the vector sum to one, as to mimick probability distribution.
return a/np.sum(a)
Copy link
Contributor

Choose a reason for hiding this comment

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

Add spaces around /.

b = _prob_dist(b)
if sp.issparse(a):
return -np.log(np.sum(np.sqrt(a.multiply(b))))
return -np.log(np.sum(np.sqrt(a*b)))
Copy link
Contributor

Choose a reason for hiding this comment

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

Same. :)

Copy link
Contributor

@janezd janezd left a comment

Choose a reason for hiding this comment

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

Apologies for pestering you. I have a few comments, and we can talk about this on Friday, but if you'd prefer to be done with it, we can merge it as it is.

Orange/distance/distance.py Outdated Show resolved Hide resolved
Orange/distance/distance.py Outdated Show resolved Hide resolved
@@ -157,6 +160,9 @@ def _fix_missing():
_fix_discrete, _fix_missing, _fix_nonbinary):
if not check():
return None
if (METRICS[self.metric_idx][0] == 'Bhattacharyya') and _min(data.X) < 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

It is in general a bad idea to use string literals like this. Somebody will rename it ... and there we go. I'd be happier with METRICS[self.metric_idx][1] is distance.Bhattacharyya (and, by the way, you can remove the parentheses).

On the other hand, why wouldn't the distance itself (i.e. function distance. _bhattacharyya) test the values and raise ValueError("Bhattcharyya distance requires non-negative values")? The widget already catches and shows ValueError exceptions.

As I proposed on Friday, this is better because it gives a reasonable error also to anybody that would call this distance from a script. With current code, if somebody calls Bhattacharyya with negative values, (s)he will get just a RuntimeWarning: invalid value encountered in sqrt, the result will be nan ... and this nan will propagate until an unrelated function crashes down the road. Testing and raising an exception there is better because it helps debugging.

This is open for discussion -- on Friday.

Copy link
Contributor Author

@AndrejaKovacic AndrejaKovacic Oct 25, 2019

Choose a reason for hiding this comment

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

You are right, I moved it.

@AndrejaKovacic AndrejaKovacic force-pushed the bhatthacharayya branch 2 times, most recently from 7766bd9 to 0bc5bb0 Compare October 25, 2019 08:40
# Raise an exception for infinities, nans and negative values
check_array(a,
accept_sparse=True, accept_large_sparse=True, ensure_2d=False)
if a.min() < 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

@AndrejaKovacic, would this be OK, too?

Dense arrays also have a method min, so there's no need for if. Also, I think it is better to not change the exception message raised by check_array so the caller is informed that, for instance, there are nan values in the data.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's more informative this way.

@janezd janezd merged commit 0c0a1e9 into biolab:master Nov 1, 2019
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