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

Unit circle distribution #665

Closed
dhardy opened this issue Dec 12, 2018 · 1 comment
Closed

Unit circle distribution #665

dhardy opened this issue Dec 12, 2018 · 1 comment

Comments

@dhardy
Copy link
Member

dhardy commented Dec 12, 2018

@vks How come we never noticed in #567 that the second term is always positive?

[diff / sum, 2.*x1*x2 / sum]

Using the sin(2θ) / cos(2θ) trig identities, it turns out we're sampling from the upper half-circle (x2 >= 0), since when starting from one quadrant of the circle we have angle θ in 0..π/2. We could fix this in a few ways:

  • use an extra bit to randomly flip the sign of the second term (note that since we're sampling f64 from a u64 we are wasting a few bits which we could save as in the ziggurat function)

  • use the trig identities twice; this is a bit messy and yields the following by my calculation (r**2 = x1**2 + x2**2):

      cos(4θ) = (x1**4 + x2**4 - 6 * x1**2 * x2**2) / r**4
      sin(4θ) = 4*(x1**3 * x2 - x1 * x2**3) / r**4
    
  • start instead from the upper half-circle by taking x2 = 2 * sample - 1

@dhardy
Copy link
Member Author

dhardy commented Dec 12, 2018

Sorry, I missed that we are sampling both variables from [-1, 1). Theoretically one of them could be sampled from [0, 1) but according to my benchmark it's faster to sample both the same way.

However, I see no need to cache the Uniform distribution in UnitCircle (my bench is slightly faster without). Do you remember why you did this?

@dhardy dhardy closed this as completed Dec 12, 2018
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

No branches or pull requests

1 participant