1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36
37
38
39
40
41 @Command(name = "visual",
42 description = "Show output from a tiny region of the sampler.")
43 class UniformSamplingVisualCheckCommand implements Callable<Void> {
44
45 @Mixin
46 private StandardOptions reusableOptions;
47
48
49 @Option(names = {"-l", "--low"},
50 description = "The lower bound (default: ${DEFAULT-VALUE}).")
51 private float lo = 0.1f;
52
53
54 @Option(names = {"-b", "--bands"},
55 description = "The number of bands for the range (default: ${DEFAULT-VALUE}).")
56 private int bands = 2;
57
58
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
64 private final UniformRandomProvider rng = RandomSource.create(RandomSource.XOR_SHIFT_1024_S_PHI);
65
66 private final ContinuousSampler[] samplers = new ContinuousSampler[] {
67 ZigguratNormalizedGaussianSampler.of(rng),
68 MarsagliaNormalizedGaussianSampler.of(rng),
69 BoxMullerNormalizedGaussianSampler.of(rng),
70 };
71
72
73
74
75
76
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
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 }