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
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed style and added check for bounds in [-pi,pi] interval.
  • Loading branch information
Androp0v authored and bashtage committed Feb 26, 2021
commit f26a7d202a7136c0750356f7ec6c148d67a5927e
23 changes: 17 additions & 6 deletions numpy/random/src/distributions/distributions.c
Original file line number Diff line number Diff line change
Expand Up @@ -844,19 +844,30 @@ double random_vonmises(bitgen_t *bitgen_state, double mu, double kappa) {
}
if (kappa < 1e-8) {
return M_PI * (2 * next_double(bitgen_state) - 1);
} else {
}
else {
/* with double precision rho is zero until 1.4e-8 */
if (kappa < 1e-5) {
/*
* second order taylor expansion around kappa = 0
* precise until relatively large kappas as second order is 0
*/
s = (1. / kappa + kappa);
} else {
/* Fallback to normal distribution for big values of kappa*/
if (kappa > 1e6){
return mu + sqrt(1/kappa) * random_standard_normal(bitgen_state);
}else{
}
else {
/* Fallback to normal distribution for big values of kappa */
if (kappa > 1e6) {
result = mu + sqrt(1. / kappa) * random_standard_normal(bitgen_state);
/* Check if result is within bounds */
if (result < -M_PI) {
return result + 2*M_PI;
}
if (result > M_PI) {
return result - 2*M_PI;
}
return result;
}
else {
double r = 1 + sqrt(1 + 4 * kappa * kappa);
double rho = (r - sqrt(2 * r)) / (2 * kappa);
s = (1 + rho * rho) / (2 * rho);
Expand Down
0