8000 BUG: Fixed Von Mises distribution for big values of kappa by bashtage · Pull Request #18498 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Fixed Von Mises distribution for big values of kappa #18498

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

Merged
merged 5 commits into from
Feb 26, 2021

Conversation

bashtage
Copy link
Contributor

Extends branch in #17378 to prevent fix applying to RandomState and changing its stream.

@bashtage bashtage force-pushed the Androp0v-vonmises-fix branch 3 times, most recently from 82ca662 to 8e4ac94 Compare February 26, 2021 14:47
Apply vonmises fix only to Generator
Add tests for correctness

closes numpy#17378
closes numpy#17275
@bashtage bashtage force-pushed the Androp0v-vonmises-fix branch from 8e4ac94 to 79c3030 Compare February 26, 2021 14:50
return result - 2*M_PI;
}
return result;
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Indentation needs to be 4 spaces, else on new line.

Copy link
Member

Choose a reason for hiding this comment

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

Style needs fixing all round, let's leave that to a later cleanup.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

While I don't mind changing the commit, the entire file is formatted using these conventions (2 spaces, single like else). IIRC think this is Google C format as formatted using clang-format.

double rho = (r - sqrt(2 * r)) / (2 * kappa);
s = (1 + rho * rho) / (2 * rho);
#ifndef NP_RANDOM_LEGACY
/* Fallback to normal distribution for big values of kappa
Copy link
Member

Choose a reason for hiding this comment

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

First comment line should be blank.

@rkern
Copy link
Member
rkern commented Feb 26, 2021

I think that it would be safer/more readable/more convenient to do the #ifdef NP_RANDOM_LEGACY switch between two copies of the whole function. Then we have free reign in the non-legacy copy of the function. Since we're not maintaining the legacy version, there's no DRY benefit to sharing the (thus-far) common parts of the function. I think it would make the task of verifying that the legacy variant remains unchanged much, much simpler. And it would be an easy convention that new contributors can follow, so we don't have to police them with followup PRs like this.

@charris
Copy link
Member
charris commented Feb 26, 2021

The code coverage failure should probably be fixed as it seems a common case.

@bashtage
< 8000 div class="timeline-comment-header clearfix d-flex" data-morpheus-enabled="false">
Copy link
Contributor Author

I think that it would be safer/more readable/more convenient to do the #ifdef NP_RANDOM_LEGACY switch between two copies of the whole function. Then we have free reign in the non-legacy copy of the function. Since we're not maintaining the legacy version, there's no DRY benefit to sharing the (thus-far) common parts of the function. I think it would make the task of verifying that the legacy variant remains unchanged much, much simpler. And it would be an easy convention that new contributors can follow, so we don't have to police them with followup PRs like this.

I moved it to legacy-distributions.c as legacy_vonmises. This has the advantage of clarifying that these functions should not be altered except under special circumstances.

Comment on lines +1752 to +1757
@pytest.mark.parametrize("mu", [-7., -np.pi, -3.1, np.pi, 3.2])
@pytest.mark.parametrize("kappa", [1e-9, 1e-6, 1, 1e3, 1e15])
def test_vonmises_large_kappa_range(self, mu, kappa):
r = random.vonmises(mu, kappa, 50)
assert_(np.all(r > -np.pi) and np.all(r <= np.pi))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that coverage is a bit suspicious. I added this test to ensure that all code paths are hit.

Avoid conditional compilation and move old version to legacy_vonmises
Small clean up
Additional comments
@bashtage bashtage force-pushed the Androp0v-vonmises-fix branch from 8ec65ca to aa52959 Compare February 26, 2021 18:43
@rkern
Copy link
Member
rkern commented Feb 26, 2021

I moved it to legacy-distributions.c as legacy_vonmises.

Agreed.

@charris
Copy link
Member
charris commented Feb 26, 2021

The azure failure is unrelated, I didn't rerun the test as the output would be lost.

@charris charris merged commit 7d3b555 into numpy:master Feb 26, 2021
@charris
Copy link
Member
charris commented Feb 26, 2021

Thanks @bashtage .

@bashtage bashtage deleted the Androp0v-vonmises-fix branch April 21, 2021 14:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0