1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.rng.sampling.distribution;
18
19 import org.apache.commons.rng.UniformRandomProvider;
20 import org.apache.commons.rng.core.source64.SplitMix64;
21 import org.apache.commons.rng.sampling.RandomAssert;
22 import org.junit.jupiter.api.Assertions;
23 import org.junit.jupiter.api.Test;
24
25
26
27
28 class LevySamplerTest {
29
30
31
32 @Test
33 void testConstructorThrowsWithNegativeScale() {
34 final UniformRandomProvider rng = RandomAssert.seededRNG();
35 final double location = 1;
36 final double scale = -1e-6;
37 Assertions.assertThrows(IllegalArgumentException.class,
38 () -> LevySampler.of(rng, location, scale));
39 }
40
41
42
43
44 @Test
45 void testConstructorThrowsWithZeroScale() {
46 final UniformRandomProvider rng = RandomAssert.seededRNG();
47 final double location = 1;
48 final double scale = 0;
49 Assertions.assertThrows(IllegalArgumentException.class,
50 () -> LevySampler.of(rng, location, scale));
51 }
52
53
54
55
56 @Test
57 void testSharedStateSampler() {
58 final UniformRandomProvider rng1 = RandomAssert.seededRNG();
59 final UniformRandomProvider rng2 = RandomAssert.seededRNG();
60 final double location = 4.56;
61 final double scale = 1.23;
62 final LevySampler sampler1 = LevySampler.of(rng1, location, scale);
63 final LevySampler sampler2 = sampler1.withUniformRandomProvider(rng2);
64 RandomAssert.assertProduceSameSequence(sampler1, sampler2);
65 }
66
67
68
69
70 @Test
71 void testSupport() {
72 final double location = 0.0;
73 final double scale = 1.0;
74
75 final LevySampler s1 = LevySampler.of(
76 new SplitMix64(0L) {
77 @Override
78 public long next() {
79 return 0L;
80 }
81 }, location, scale);
82 Assertions.assertEquals(Double.POSITIVE_INFINITY, s1.sample());
83
84
85
86
87
88
89
90
91
92
93
94 final long[] sequence = {
95
96 -1, -1,
97
98 -1, -1, -1, -1, -1, -1, -1, -1, 0,
99
100 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
101 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0,
102 };
103 final LevySampler s2 = LevySampler.of(
104 new SplitMix64(0L) {
105 private int i;
106 @Override
107 public long next() {
108 if (i++ < sequence.length) {
109 return sequence[i - 1];
110 }
111 return super.next();
112 }
113 }, location, scale);
114
115 final double s = 4 * 7.569274694148063 * 0.27502700159745347 + 3.6360066255009455861;
116
117
118
119 final double expected = 1 / (s * s);
120 Assertions.assertEquals(expected, s2.sample());
121 }
122 }