View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.rng.sampling;
18  
19  import org.junit.Assert;
20  import org.junit.Test;
21  import org.apache.commons.rng.simple.RandomSource;
22  import org.apache.commons.rng.UniformRandomProvider;
23  
24  /**
25   * Test for {@link UnitSphereSampler}.
26   */
27  public class UnitSphereSamplerTest {
28      /**
29       * Test the distribution of points in two dimensions.
30       */
31      @Test
32      public void testDistribution2D() {
33          UniformRandomProvider rng = RandomSource.create(RandomSource.XOR_SHIFT_1024_S, 17399225432L);
34          UnitSphereSampler generator = new UnitSphereSampler(2, rng);
35  
36          // In 2D, angles with a given vector should be uniformly distributed.
37          final int[] angleBuckets = new int[100];
38          final int steps = 1000000;
39          for (int i = 0; i < steps; ++i) {
40              final double[] v = generator.nextVector();
41              Assert.assertEquals(2, v.length);
42              Assert.assertEquals(1, length(v), 1e-10);
43              // Compute angle formed with vector (1, 0)?
44              // Cosine of angle is their dot product, because both are unit length.
45              // Dot product here is just the first element of the vector by construction.
46              final double angle = Math.acos(v[0]);
47              final int bucket = (int) (angle / Math.PI * angleBuckets.length);
48              ++angleBuckets[bucket];
49          }
50  
51          // Simplistic test for roughly even distribution.
52          final int expectedBucketSize = steps / angleBuckets.length;
53          for (int bucket : angleBuckets) {
54              Assert.assertTrue("Bucket count " + bucket + " vs expected " + expectedBucketSize,
55                                Math.abs(expectedBucketSize - bucket) < 350);
56          }
57      }
58  
59      /**
60       * @return the length (L2-norm) of given vector.
61       */
62      private static double length(double[] vector) {
63          double total = 0;
64          for (double d : vector) {
65              total += d * d;
66          }
67          return Math.sqrt(total);
68      }
69  }