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

Fix symmetrical case for hellinger distance. Fix #1854 #1860

Merged

Conversation

caiyulun
Copy link
Contributor

This changes fix the bugs that return different distance when we call hellinger(x, y) and hellinger(y,x) ( #1854)
The cause of this bug is that we compute the distance based on one distribution's index previously, but we should iterate all the index appears in two probability distributions.

This changes fix the bugs that return different distance when we
call hellinger(x, y) and hellinger(y, x).
The cause of this bug is that we compute the distance based on one
distribution's index previously, but we should iterate all the
index appears in two probability distributions.
@@ -897,10 +897,9 @@ def hellinger(vec1, vec2):
if isbow(vec1) and isbow(vec2):
# if it is a BoW format, instead of converting to dense we use dictionaries to calculate appropriate distance
vec1, vec2 = dict(vec1), dict(vec2)
if len(vec2) < len(vec1):
vec1, vec2 = vec2, vec1 # swap references so that we iterate over the shorter vector
indexs = set(vec1.keys() + vec2.keys())
Copy link
Contributor

Choose a reason for hiding this comment

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

indexs -> indices

sim = np.sqrt(
0.5 * sum((np.sqrt(value) - np.sqrt(vec2.get(index, 0.0)))**2 for index, value in iteritems(vec1))
0.5 * sum((np.sqrt(vec1.get(index, 0.0)) - np.sqrt(vec2.get(index, 0.0)))**2 for index in indexs)
Copy link
Contributor

Choose a reason for hiding this comment

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

Also please add a test for it (you can use an example from original issue).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I add a test in this function https://github.com/RaRe-Technologies/gensim/blob/develop/gensim/test/test_similarity_metrics.py#L106 or anywhere else ? And it seems that the current two different length BOW inputs test in Line109 has a wrong expected value, I'm tring to correct it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, please fix current test & add needed tests to TestHellinger class

@menshikh-iv menshikh-iv changed the title Fix #1854: return different hellinger distance when calling d(x,y) and d(y,x) Fix symmetrical case for hellinger distance. Fix #1854 Jan 26, 2018
* fix the currrent test for different length BOW inputs
* add a test for symmetrical inputs
@menshikh-iv menshikh-iv merged commit 1f357a7 into piskvorky:develop Jan 30, 2018
@menshikh-iv
Copy link
Contributor

Good work @caiyulun, congratz with first contribution 👍

sj29-innovate pushed a commit to sj29-innovate/gensim that referenced this pull request Feb 21, 2018
…vorky#1860)

* fix: fix bugs in hellinger distance computing

This changes fix the bugs that return different distance when we
call hellinger(x, y) and hellinger(y, x).
The cause of this bug is that we compute the distance based on one
distribution's index previously, but we should iterate all the
index appears in two probability distributions.

* rename variable + fix union indices error in python3

* fix and add tests for hellinger distance

* fix the currrent test for different length BOW inputs
* add a test for symmetrical inputs
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