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  
18  package org.apache.commons.rng.examples.sampling;
19  
20  import org.apache.commons.rng.UniformRandomProvider;
21  import org.apache.commons.rng.simple.RandomSource;
22  
23  import picocli.CommandLine.Command;
24  import picocli.CommandLine.Mixin;
25  import picocli.CommandLine.Option;
26  
27  import java.util.concurrent.Callable;
28  
29  import org.apache.commons.rng.sampling.distribution.ZigguratNormalizedGaussianSampler;
30  import org.apache.commons.rng.sampling.distribution.MarsagliaNormalizedGaussianSampler;
31  import org.apache.commons.rng.sampling.distribution.BoxMullerNormalizedGaussianSampler;
32  import org.apache.commons.rng.sampling.distribution.ContinuousSampler;
33  
34  /**
35   * Creates 2D plot of sampling output.
36   * It is a "manual" check that could help ensure that no artifacts
37   * exist in some tiny region of the expected range, due to loss of
38   * accuracy, e.g. when porting C code based on 32-bits "float" to
39   * "Commons RNG" that uses Java "double" (64-bits).
40   */
41  @Command(name = "visual",
42           description = "Show output from a tiny region of the sampler.")
43  class UniformSamplingVisualCheckCommand implements Callable<Void> {
44      /** The standard options. */
45      @Mixin
46      private StandardOptions reusableOptions;
47  
48      /** The lower bound of the tiny range. */
49      @Option(names = {"-l", "--low"},
50              description = "The lower bound (default: ${DEFAULT-VALUE}).")
51      private float lo = 0.1f;
52  
53      /** The number of bands of the tiny range. */
54      @Option(names = {"-b", "--bands"},
55              description = "The number of bands for the range (default: ${DEFAULT-VALUE}).")
56      private int bands = 2;
57  
58      /** Number of samples to be generated. */
59      @Option(names = {"-s", "--samples"},
60          description = "The number of samples in the tiny range (default: ${DEFAULT-VALUE}).")
61      private int numSamples = 50;
62  
63      /** RNG. */
64      private final UniformRandomProvider rng = RandomSource.create(RandomSource.XOR_SHIFT_1024_S_PHI);
65      /** Samplers. */
66      private final ContinuousSampler[] samplers = new ContinuousSampler[] {
67          ZigguratNormalizedGaussianSampler.of(rng),
68          MarsagliaNormalizedGaussianSampler.of(rng),
69          BoxMullerNormalizedGaussianSampler.of(rng),
70      };
71  
72      // Allow System.out
73      // CHECKSTYLE: stop RegexpCheck
74  
75      /**
76       * Prints a template generators list to stdout.
77       */
78      @Override
79      public Void call() {
80          float hi = lo;
81          for (int i = 0; i < bands; i++) {
82              hi = Math.nextUp(hi);
83          }
84          System.out.printf("# lower=%.16e%n", lo);
85          System.out.printf("# upper=%.16e%n", hi);
86  
87          for (int i = 0; i < samplers.length; i++) {
88              System.out.printf("# [%d] %s%n", i, samplers[i].getClass().getSimpleName());
89          }
90  
91          for (int n = 0; n < numSamples; n++) {
92              System.out.printf("[%d]", n, rng.nextDouble());
93  
94              for (final ContinuousSampler s : samplers) {
95                  double r = s.sample();
96                  while (r < lo || r > hi) {
97                      // Discard numbers outside the tiny region.
98                      r = s.sample();
99                  }
100                 System.out.printf("\t%.16e", r);
101             }
102 
103             System.out.println();
104         }
105 
106         return null;
107     }
108 }