[
https://issues.apache.org/jira/browse/RNG-128?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17318595#comment-17318595
]
Alex Herbert commented on RNG-128:
----------------------------------
Unit tests with a check for uniformity show that the implementation of the ball
point picking is not uniform through the n-ball. I do not know what is
incorrect in my implementation so I searched for alternative methods. The
following are based on sampling from a hypersphere as described in a
MathOverflow post: [Sampling a uniformly distributed point INSIDE a
hypersphere?|https://mathoverflow.net/questions/309567/sampling-a-uniformly-distributed-point-inside-a-hypersphere]:
||Method||Description||
|HypersphereInternal|Take a point on the unit hypersphere then multiply it by
U^1/n (with U in [0, 1)).
This requires n samples from a normal distribution and 1 uniform deviate for
an n-ball, plus a sqrt, cbrt or pow operation from Math.|
|HypersphereDiscard|Take a random point on the (n+1)-dimensional hypersphere
and drop two coordinates. (Note that the (n+1)-hypersphere is the unit sphere
of R^(n+2).)
This requires n+2 samples from a normal distribution for an n-ball.|
Both of these methods pass the same unit test for uniformity in 3, 4 and 5
dimensions. These methods are faster than the ball point method as they do not
require an exponential deviate.
Here are updated performance results:
|Method|dimension|type|size|Score|Relative|
|create2D| |Baseline|100|911.248|1.00|
|create2D| |Rejection|100|2001.437|2.20|
|create2D| |DiskPoint|100|4881.344|5.36|
|create2D| |HypersphereInternal|100|2128.195|2.34|
|create2D| |HypersphereDiscard|100|2960.067|3.25|
|create3D| |Baseline|100|1200.417|1.00|
|create3D| |Rejection|100|4089.504|3.41|
|create3D| |BallPoint|100|4931.618|4.11|
|create3D| |HypersphereInternal|100|7374.794|6.14|
|create3D| |HypersphereDiscard|100|3630.02|3.02|
|createND|3|Baseline|100|1615.134|1.00|
|createND|3|Rejection|100|4397.874|2.72|
|createND|3|BallPoint|100|6013.344|3.72|
|createND|3|HypersphereInternal|100|6018.201|3.73|
|createND|3|HypersphereDiscard|100|4419.303|2.74|
|createND|4|Baseline|100|1943.102|1.00|
|createND|4|Rejection|100|8230.632|4.24|
|createND|4|BallPoint|100|7024.477|3.62|
|createND|4|HypersphereInternal|100|6823.26|3.51|
|createND|4|HypersphereDiscard|100|5260.748|2.71|
|createND|5|Baseline|100|2037.261|1.00|
|createND|5|Rejection|100|13756.556|6.75|
|createND|5|BallPoint|100|7599.781|3.73|
|createND|5|HypersphereInternal|100|7486.163|3.67|
|createND|5|HypersphereDiscard|100|6012.527|2.95|
For 2D the Rejection method is still the fastest and the HypersphereInternal
method is a close second.
For 3D, using the array based methods, the Rejection method and the
HypersphereDiscard method are approximately equal. The slowness of the
HypersphereInternal in 3D compared to 2D where it is fast may be due to the use
of Math.cbrt verses Math.sqrt. Math.pow is used in higher dimensions.
Non-array based 3D versions show a useful speed increase for a common use case
of 3D sampling. In this case a non-array based version of the
HypersphereDiscard method is 1.1x faster than the Rejection method and 1.2x
faster than the array based version of the same method. It is unclear why the
Rejection method is not the same speed as the HypersphereDiscard method when
non-array based.
For higher dimensions the HypersphereDiscard method is fastest and should scale
linearly with the number of dimensions.
I propose to create a sampler using the Rejection method for 2D and the
HypersphereDiscard method for higher dimensions.
> UnitBallSampler
> ---------------
>
> Key: RNG-128
> URL: https://issues.apache.org/jira/browse/RNG-128
> Project: Commons RNG
> Issue Type: Improvement
> Components: sampling
> Reporter: Alex Herbert
> Priority: Minor
>
> The *UnitSphereSampler* can generate isotropically distributed coordinates
> *on the surface* of a hypersphere of a given dimension.
> I propose to create a *UnitBallSampler* to generate isotropically distributed
> coordinates *within* a hypersphere of a given dimension.
> Wolfram describes a method that will work with any dimension and a method for
> 2 dimensions:
> [Ball Point Picking|https://mathworld.wolfram.com/BallPointPicking.html]
> [Disk Point Picking|https://mathworld.wolfram.com/DiskPointPicking.html]
> It also suggests that a simple rejection method when sampling inside an
> n-cube of length 2 may be faster for small dimensions.
> Creating an abstract class with a factory method would allow the best
> performing sampler to be created for each dimension:
> {code:java}
> public abstract class UnitBallSampler implements
> SharedStateSampler<UnitBallSampler> {
> /**
> * @return a random Cartesian coordinate within the unit n-ball.
> */
> public double[] next() {
> // ...
> }
> /**
> * Create a unit n-ball sampler for the given dimension.
> *
> * @param dimension Space dimension.
> * @param rng Generator for the individual components of the coordinates.
> * A shallow copy will be stored in this instance.
> * @throws IllegalArgumentException If {@code dimension <= 0}
> */
> public static UnitBallSampler of(int dimension,
> UniformRandomProvider rng) {
> // ...
> }
> }
> {code}
> The UnitSphereSampler sample method is named {{nextVector}}. The
> corresponding method here could be:
> * next
> * nextVector
> * nextCoordinate
> * nextPoint
> * sample
> * ...
--
This message was sent by Atlassian Jira
(v8.3.4#803005)