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 AhrensDieterMarsagliaTsangGammaSamplerTest {
30
31
32
33 @Test(expected = IllegalArgumentException.class)
34 public void testConstructorThrowsWithZeroAlpha() {
35 final RestorableUniformRandomProvider rng =
36 RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
37 final double alpha = 0;
38 final double theta = 1;
39 AhrensDieterMarsagliaTsangGammaSampler.of(rng, alpha, theta);
40 }
41
42
43
44
45 @Test(expected = IllegalArgumentException.class)
46 public void testConstructorThrowsWithZeroTheta() {
47 final RestorableUniformRandomProvider rng =
48 RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
49 final double alpha = 1;
50 final double theta = 0;
51 AhrensDieterMarsagliaTsangGammaSampler.of(rng, alpha, theta);
52 }
53
54
55
56
57 @Test
58 public void testSharedStateSamplerWithAlphaBelowOne() {
59 testSharedStateSampler(0.5, 3.456);
60 }
61
62
63
64
65 @Test
66 public void testSharedStateSamplerWithAlphaAboveOne() {
67 testSharedStateSampler(3.5, 3.456);
68 }
69
70
71
72
73
74
75
76 private static void testSharedStateSampler(double alpha, double theta) {
77 final UniformRandomProvider rng1 = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
78 final UniformRandomProvider rng2 = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
79
80 final AhrensDieterMarsagliaTsangGammaSampler sampler1 =
81 new AhrensDieterMarsagliaTsangGammaSampler(rng1, alpha, theta);
82 final SharedStateContinuousSampler sampler2 = sampler1.withUniformRandomProvider(rng2);
83 RandomAssert.assertProduceSameSequence(sampler1, sampler2);
84 }
85
86
87
88
89
90 @Test
91 public void testToString() {
92 final UniformRandomProvider rng = RandomSource.create(RandomSource.SPLIT_MIX_64, 0L);
93 Assert.assertTrue(new AhrensDieterMarsagliaTsangGammaSampler(rng, 1.0, 2.0).toString()
94 .toLowerCase().contains("gamma"));
95 }
96 }