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.RestorableUniformRandomProvider;
20 import org.apache.commons.rng.UniformRandomProvider;
21 import org.apache.commons.rng.sampling.RandomAssert;
22 import org.apache.commons.rng.simple.RandomSource;
23 import org.junit.Assert;
24 import org.junit.Test;
25
26
27
28
29 public class ChengBetaSamplerTest {
30
31
32
33 @Test(expected = IllegalArgumentException.class)
34 public void testConstructorThrowsWithZeroAlpha() {
35 final RestorableUniformRandomProvider rng =
36 RandomSource.create(RandomSource.SPLIT_MIX_64);
37 final double alpha = 0;
38 final double beta = 1;
39 ChengBetaSampler.of(rng, alpha, beta);
40 }
41
42
43
44
45 @Test(expected = IllegalArgumentException.class)
46 public void testConstructorThrowsWithZeroBeta() {
47 final RestorableUniformRandomProvider rng =
48 RandomSource.create(RandomSource.SPLIT_MIX_64);
49 final double alpha = 1;
50 final double beta = 0;
51 ChengBetaSampler.of(rng, alpha, beta);
52 }
53
54
55
56
57 @Test
58 public void testSharedStateSamplerWithAlphaAndBetaAbove1AndAlphaBelowBeta() {
59 testSharedStateSampler(1.23, 4.56);
60 }
61
62
63
64
65 @Test
66 public void testSharedStateSamplerWithAlphaAndBetaAbove1AndAlphaAboveBeta() {
67 testSharedStateSampler(4.56, 1.23);
68 }
69
70
71
72
73 @Test
74 public void testSharedStateSamplerWithAlphaOrBetaBelow1AndAlphaBelowBeta() {
75 testSharedStateSampler(0.23, 4.56);
76 }
77
78
79
80
81 @Test
82 public void testSharedStateSamplerWithAlphaOrBetaBelow1AndAlphaAboveBeta() {
83 testSharedStateSampler(4.56, 0.23);
84 }
85
86
87
88
89
90
91
92 private static void testSharedStateSampler(double alpha, double beta) {
93 final UniformRandomProvider rng1 = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
94 final UniformRandomProvider rng2 = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
95
96 final SharedStateContinuousSampler sampler1 =
97 new ChengBetaSampler(rng1, alpha, beta);
98 final SharedStateContinuousSampler sampler2 = sampler1.withUniformRandomProvider(rng2);
99 RandomAssert.assertProduceSameSequence(sampler1, sampler2);
100 }
101
102
103
104
105
106 @Test
107 public void testToString() {
108 final UniformRandomProvider rng = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
109 Assert.assertTrue(new ChengBetaSampler(rng, 1.0, 2.0).toString()
110 .toLowerCase().contains("beta"));
111 }
112 }