1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.statistics.examples.jmh.distribution;
19
20 import java.util.SplittableRandom;
21 import java.util.concurrent.ThreadLocalRandom;
22 import java.util.concurrent.TimeUnit;
23 import java.util.function.DoubleUnaryOperator;
24 import org.apache.commons.numbers.rootfinder.BrentSolver;
25 import org.apache.commons.statistics.distribution.BetaDistribution;
26 import org.apache.commons.statistics.distribution.ChiSquaredDistribution;
27 import org.apache.commons.statistics.distribution.ContinuousDistribution;
28 import org.apache.commons.statistics.distribution.FDistribution;
29 import org.apache.commons.statistics.distribution.GammaDistribution;
30 import org.apache.commons.statistics.distribution.NakagamiDistribution;
31 import org.apache.commons.statistics.distribution.TDistribution;
32 import org.openjdk.jmh.annotations.Benchmark;
33 import org.openjdk.jmh.annotations.BenchmarkMode;
34 import org.openjdk.jmh.annotations.Fork;
35 import org.openjdk.jmh.annotations.Measurement;
36 import org.openjdk.jmh.annotations.Mode;
37 import org.openjdk.jmh.annotations.OutputTimeUnit;
38 import org.openjdk.jmh.annotations.Param;
39 import org.openjdk.jmh.annotations.Scope;
40 import org.openjdk.jmh.annotations.Setup;
41 import org.openjdk.jmh.annotations.State;
42 import org.openjdk.jmh.annotations.Warmup;
43
44
45
46
47
48 @BenchmarkMode(Mode.AverageTime)
49 @OutputTimeUnit(TimeUnit.NANOSECONDS)
50 @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
51 @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
52 @State(Scope.Benchmark)
53 @Fork(value = 1, jvmArgs = {"-server", "-Xms512M", "-Xmx512M"})
54 public class InverseProbabilityPerformance {
55
56 private static final String NOOP = "Noop";
57
58 private static final String UNKNOWN_FUNCTION = "unknown function: ";
59
60 private static final String UNKNOWN_DISTRIBUTION = "unknown distrbution: ";
61
62
63
64
65
66 private static final long SEED = ThreadLocalRandom.current().nextLong();
67
68
69
70
71 @State(Scope.Benchmark)
72 public static class InverseData {
73
74 @Param({NOOP,
75
76 "Beta:4:0.1",
77 "ChiSquared:0.1",
78 "F:5:6",
79 "Gamma:4:2",
80 "Nakagami:0.33333333333:1",
81 "T:5",
82 })
83 private String implementation;
84
85
86 @Param({
87
88 "1e-14",
89
90 "1.1102230246251565E-16"})
91 private double relEps;
92
93
94 @Param({
95
96 "1e-9",
97
98 "4.9e-324"})
99 private double absEps;
100
101
102 @Param({"cdf", "sf"})
103 private String invert;
104
105
106 private SplittableRandom rng;
107
108
109 private DoubleUnaryOperator function;
110
111
112
113
114
115
116 public double next() {
117 return function.applyAsDouble(rng.nextDouble());
118 }
119
120
121
122
123 @Setup
124 public void setup() {
125
126
127
128 rng = new SplittableRandom(SEED);
129 function = createFunction(implementation, relEps, absEps, invert);
130 }
131
132
133
134
135
136
137
138
139
140
141 private static DoubleUnaryOperator createFunction(String implementation,
142 double relativeAccuracy,
143 double absoluteAccuracy,
144 String invert) {
145 if (implementation.startsWith(NOOP)) {
146 return x -> x;
147 }
148
149
150 final ContinuousDistribution dist = createDistribution(implementation);
151
152
153 final ContinuousDistributionInverter inverter =
154 new ContinuousDistributionInverter(dist, relativeAccuracy, absoluteAccuracy);
155
156 if ("cdf".equals(invert)) {
157 return inverter::inverseCumulativeProbability;
158 } else if ("sf".equals(invert)) {
159 return inverter::inverseSurvivalProbability;
160 }
161 throw new IllegalStateException(UNKNOWN_FUNCTION + invert);
162 }
163
164
165
166
167
168
169
170 private static ContinuousDistribution createDistribution(String implementation) {
171
172
173 final String[] parts = implementation.split(":");
174 if ("Beta".equals(parts[0])) {
175 return BetaDistribution.of(Double.parseDouble(parts[1]), Double.parseDouble(parts[2]));
176 } else if ("ChiSquared".equals(parts[0])) {
177 return ChiSquaredDistribution.of(Double.parseDouble(parts[1]));
178 } else if ("F".equals(parts[0])) {
179 return FDistribution.of(Double.parseDouble(parts[1]), Double.parseDouble(parts[2]));
180 } else if ("Gamma".equals(parts[0])) {
181 return GammaDistribution.of(Double.parseDouble(parts[1]), Double.parseDouble(parts[2]));
182 } else if ("Nakagami".equals(parts[0])) {
183 return NakagamiDistribution.of(Double.parseDouble(parts[1]), Double.parseDouble(parts[2]));
184 } else if ("T".equals(parts[0])) {
185 return TDistribution.of(Double.parseDouble(parts[1]));
186 }
187 throw new IllegalStateException(UNKNOWN_DISTRIBUTION + implementation);
188 }
189
190
191
192
193
194
195 static class ContinuousDistributionInverter {
196
197
198
199 private static final double SOLVER_FUNCTION_VALUE_ACCURACY = Double.MIN_VALUE;
200
201
202
203 private final double relativeAccuracy;
204
205 private final double absoluteAccuracy;
206
207 private final ContinuousDistribution dist;
208
209
210
211
212
213
214 ContinuousDistributionInverter(ContinuousDistribution dist,
215 double relativeAccuracy,
216 double absoluteAccuracy) {
217 this.dist = dist;
218 this.relativeAccuracy = relativeAccuracy;
219 this.absoluteAccuracy = absoluteAccuracy;
220 }
221
222
223
224
225
226
227
228 private static boolean isFiniteStrictlyPositive(double x) {
229 return x > 0 && x < Double.POSITIVE_INFINITY;
230 }
231
232
233
234
235
236
237
238 private static void checkProbability(double p) {
239 if (p >= 0 && p <= 1) {
240 return;
241 }
242
243 throw new IllegalArgumentException("Invalid p: " + p);
244 }
245
246
247
248
249
250
251
252
253 public double inverseCumulativeProbability(double p) {
254 checkProbability(p);
255 return inverseProbability(p, 1 - p, false);
256 }
257
258
259
260
261
262
263
264
265 public double inverseSurvivalProbability(double p) {
266 checkProbability(p);
267 return inverseProbability(1 - p, p, true);
268 }
269
270
271
272
273
274
275
276
277
278 private double inverseProbability(final double p, final double q, boolean complement) {
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310 double lowerBound = dist.getSupportLowerBound();
311 if (p == 0) {
312 return lowerBound;
313 }
314 double upperBound = dist.getSupportUpperBound();
315 if (q == 0) {
316 return upperBound;
317 }
318
319 final double mu = dist.getMean();
320 final double sig = Math.sqrt(dist.getVariance());
321 final boolean chebyshevApplies = Double.isFinite(mu) &&
322 isFiniteStrictlyPositive(sig);
323
324 if (lowerBound == Double.NEGATIVE_INFINITY) {
325 lowerBound = createFiniteLowerBound(p, q, complement, upperBound, mu, sig, chebyshevApplies);
326 }
327
328 if (upperBound == Double.POSITIVE_INFINITY) {
329 upperBound = createFiniteUpperBound(p, q, complement, lowerBound, mu, sig, chebyshevApplies);
330 }
331
332
333
334
335 if (upperBound == Double.MAX_VALUE) {
336 if (complement) {
337 if (dist.survivalProbability(upperBound) > q) {
338 return dist.getSupportUpperBound();
339 }
340 } else if (dist.cumulativeProbability(upperBound) < p) {
341 return dist.getSupportUpperBound();
342 }
343 }
344 if (lowerBound == -Double.MAX_VALUE) {
345 if (complement) {
346 if (dist.survivalProbability(lowerBound) < q) {
347 return dist.getSupportLowerBound();
348 }
349 } else if (dist.cumulativeProbability(lowerBound) > p) {
350 return dist.getSupportLowerBound();
351 }
352 }
353
354 final DoubleUnaryOperator fun = complement ?
355 arg -> dist.survivalProbability(arg) - q :
356 arg -> dist.cumulativeProbability(arg) - p;
357
358
359 final double x = new BrentSolver(relativeAccuracy,
360 absoluteAccuracy,
361 SOLVER_FUNCTION_VALUE_ACCURACY)
362 .findRoot(fun,
363 lowerBound,
364 lowerBound + 0.5 * (upperBound - lowerBound),
365 upperBound);
366
367 return x;
368 }
369
370
371
372
373
374
375
376
377
378
379
380
381
382 private double createFiniteLowerBound(final double p, final double q, boolean complement,
383 double upperBound, final double mu, final double sig, final boolean chebyshevApplies) {
384 double lowerBound;
385 if (chebyshevApplies) {
386 lowerBound = mu - sig * Math.sqrt(q / p);
387 } else {
388 lowerBound = Double.NEGATIVE_INFINITY;
389 }
390
391 if (lowerBound == Double.NEGATIVE_INFINITY) {
392 lowerBound = Math.min(-1, upperBound);
393 if (complement) {
394 while (dist.survivalProbability(lowerBound) < q) {
395 lowerBound *= 2;
396 }
397 } else {
398 while (dist.cumulativeProbability(lowerBound) >= p) {
399 lowerBound *= 2;
400 }
401 }
402
403 lowerBound = Math.max(lowerBound, -Double.MAX_VALUE);
404 }
405 return lowerBound;
406 }
407
408
409
410
411
412
413
414
415
416
417
418
419
420 private double createFiniteUpperBound(final double p, final double q, boolean complement,
421 double lowerBound, final double mu, final double sig, final boolean chebyshevApplies) {
422 double upperBound;
423 if (chebyshevApplies) {
424 upperBound = mu + sig * Math.sqrt(p / q);
425 } else {
426 upperBound = Double.POSITIVE_INFINITY;
427 }
428
429 if (upperBound == Double.POSITIVE_INFINITY) {
430 upperBound = Math.max(1, lowerBound);
431 if (complement) {
432 while (dist.survivalProbability(upperBound) >= q) {
433 upperBound *= 2;
434 }
435 } else {
436 while (dist.cumulativeProbability(upperBound) < p) {
437 upperBound *= 2;
438 }
439 }
440
441 upperBound = Math.min(upperBound, Double.MAX_VALUE);
442 }
443 return upperBound;
444 }
445 }
446 }
447
448
449
450
451
452
453
454 @Benchmark
455 public double inverse(InverseData data) {
456 return data.next();
457 }
458 }