-
Notifications
You must be signed in to change notification settings - Fork 423
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
Optimize vMF sampling #1162
Optimize vMF sampling #1162
Conversation
Codecov Report
@@ Coverage Diff @@
## master #1162 +/- ##
==========================================
- Coverage 79.91% 79.89% -0.03%
==========================================
Files 115 115
Lines 5905 5899 -6
==========================================
- Hits 4719 4713 -6
Misses 1186 1186
Continue to review full report at Codecov.
|
Some (rough) Benchmarks:master (e127e9d):
|
This PR (48c4245):
|
Added a couple more benchmarks for p=2 and p=4. You can see that there's a dip in the computation time for p=3, since we avoid rejection sampling there. Hence the reduction in computation time for p=3 doesn't just come from the improved rotation operation. Additionally, I noticed that the vMF distribution errors for p=1. In this case the distribution technically reduces to a discrete distribution on {+1, -1} (basically a post-processed Bernoulli), should vMF have a special case for that, or should the constructor error out and tell the user to use a Bernoulli distribution instead? I'd assume that anyone using the vMF distribution likely won't make use of this case, but it's probably a good idea for the sampling function to not error out in that scenario regardless. There's a similar issue for kappa = 0 (vMF becomes a uniform spherical distribution), though currently the vMF constructor rejects that value. |
Thanks for this! Looks good to me. My only suggestion is to take this opportunity to make the unit tests more convincing. Might include:
I am in favor of things collapsing to the special cases when |
Codecov Report
@@ Coverage Diff @@
## master #1162 +/- ##
==========================================
+ Coverage 79.91% 81.80% +1.88%
==========================================
Files 115 116 +1
Lines 5905 6550 +645
==========================================
+ Hits 4719 5358 +639
- Misses 1186 1192 +6
Continue to review full report at Codecov.
|
I've added the tests for the w sampler. It now compares the sample statistics of the p=3 case against those of the rejection sampling method. In addition, the sample statistics for the p=3 case are compared against analytical formulas for the mean/variance.
I'm having trouble reading this notation, do you mean construct the Householder matrix explicitly and apply it to the vector |
I agree with dealing with those special cases in a separate PR, particularly the Another optimization I believe should be possible would be to defer the sampling for the p=2 case to the univariate |
Also, quick question, is the temporary variable |
Exactly. I believe the correctness of it, but no reason not to document it.
Yeah, if
Not sure. |
Co-authored-by: John Zito <[email protected]>
Nice, once this gets merged I'll get started on the UniformSpherical/UniformBall distributions, and then the special cases for vMF will come after. |
I'll merge this in 24 hours if there is no other feedback. |
Resolves #1161
Had some free time so I just implemented it right away lol