View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.rng.sampling.distribution;
18  
19  import org.apache.commons.rng.UniformRandomProvider;
20  import org.apache.commons.rng.sampling.RandomAssert;
21  import org.apache.commons.rng.sampling.distribution.LargeMeanPoissonSampler.LargeMeanPoissonSamplerState;
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.Test;
24  
25  /**
26   * This test checks the {@link LargeMeanPoissonSampler} can be created
27   * from a saved state.
28   */
29  class LargeMeanPoissonSamplerTest {
30  
31      // Edge cases for construction
32  
33      /**
34       * Test the constructor with a bad mean.
35       */
36      @Test
37      void testConstructorThrowsWithMeanLargerThanUpperBound() {
38          final UniformRandomProvider rng = RandomAssert.seededRNG();
39          final double mean = Integer.MAX_VALUE / 2 + 1;
40          Assertions.assertThrows(IllegalArgumentException.class,
41              () -> LargeMeanPoissonSampler.of(rng, mean));
42      }
43  
44      /**
45       * Test the constructor with a mean below 1.
46       */
47      @Test
48      void testConstructorThrowsWithMeanBelow1() {
49          final UniformRandomProvider rng = RandomAssert.seededRNG();
50          final double mean = Math.nextDown(1);
51          Assertions.assertThrows(IllegalArgumentException.class,
52              () -> LargeMeanPoissonSampler.of(rng, mean));
53      }
54  
55      /**
56       * Test the constructor using the state with a negative fractional mean.
57       */
58      @Test
59      void testConstructorThrowsWithStateAndNegativeFractionalMean() {
60          final UniformRandomProvider rng = RandomAssert.seededRNG();
61          final LargeMeanPoissonSamplerState state = new LargeMeanPoissonSampler(rng, 1).getState();
62          Assertions.assertThrows(IllegalArgumentException.class,
63              () -> new LargeMeanPoissonSampler(rng, state, -0.1));
64      }
65  
66      /**
67       * Test the constructor with a non-fractional mean.
68       */
69      @Test
70      void testConstructorThrowsWithStateAndNonFractionalMean() {
71          final UniformRandomProvider rng = RandomAssert.seededRNG();
72          final LargeMeanPoissonSamplerState state = new LargeMeanPoissonSampler(rng, 1).getState();
73          Assertions.assertThrows(IllegalArgumentException.class,
74              () -> new LargeMeanPoissonSampler(rng, state, 1.1));
75      }
76  
77      /**
78       * Test the constructor with fractional mean of 1.
79       */
80      @Test
81      void testConstructorThrowsWithStateAndFractionalMeanOne() {
82          final UniformRandomProvider rng = RandomAssert.seededRNG();
83          final LargeMeanPoissonSamplerState state = new LargeMeanPoissonSampler(rng, 1).getState();
84          Assertions.assertThrows(IllegalArgumentException.class,
85              () -> new LargeMeanPoissonSampler(rng, state, 1));
86      }
87  
88      // Sampling tests
89  
90      /**
91       * Test the {@link LargeMeanPoissonSampler} returns the same samples when it
92       * is created using the saved state.
93       */
94      @Test
95      void testCanComputeSameSamplesWhenConstructedWithState() {
96          // Two identical RNGs
97          final UniformRandomProvider rng1 = RandomAssert.seededRNG();
98          final UniformRandomProvider rng2 = RandomAssert.seededRNG();
99  
100         // The sampler is suitable for mean > 40
101         for (int i = 40; i < 44; i++) {
102             // Test integer mean (no SmallMeanPoissonSampler required)
103             testPoissonSamples(rng1, rng2, i);
104             // Test non-integer mean (SmallMeanPoissonSampler required)
105             testPoissonSamples(rng1, rng2, i + 0.5);
106         }
107     }
108 
109     /**
110      * Test the {@link LargeMeanPoissonSampler} returns the same samples when it
111      * is created using the saved state. The random providers must be
112      * identical (including state).
113      *
114      * @param rng1  the first random provider
115      * @param rng2  the second random provider
116      * @param mean  the mean
117      */
118     private static void testPoissonSamples(
119             final UniformRandomProvider rng1,
120             final UniformRandomProvider rng2,
121             double mean) {
122         final LargeMeanPoissonSampler s1 = new LargeMeanPoissonSampler(rng1, mean);
123         final int n = (int) Math.floor(mean);
124         final double lambdaFractional = mean - n;
125         final LargeMeanPoissonSamplerState state1 = s1.getState();
126         final LargeMeanPoissonSampler s2 = new LargeMeanPoissonSampler(rng2, state1, lambdaFractional);
127         final LargeMeanPoissonSamplerState state2 = s2.getState();
128         Assertions.assertEquals(state1.getLambda(), state2.getLambda(), "State lambdas are not equal");
129         Assertions.assertNotSame(state1, state2, "States are the same object");
130         RandomAssert.assertProduceSameSequence(s1, s2);
131     }
132 
133     /**
134      * Test the SharedStateSampler implementation.
135      */
136     @Test
137     void testSharedStateSamplerWithFractionalMean() {
138         testSharedStateSampler(34.5);
139     }
140 
141     /**
142      * Test the SharedStateSampler implementation with the edge case when there is no
143      * small mean sampler (i.e. no fraction part to the mean).
144      */
145     @Test
146     void testSharedStateSamplerWithIntegerMean() {
147         testSharedStateSampler(34.0);
148     }
149 
150     /**
151      * Test the SharedStateSampler implementation.
152      *
153      * @param mean Mean.
154      */
155     private static void testSharedStateSampler(double mean) {
156         final UniformRandomProvider rng1 = RandomAssert.seededRNG();
157         final UniformRandomProvider rng2 = RandomAssert.seededRNG();
158         final SharedStateDiscreteSampler sampler1 =
159             LargeMeanPoissonSampler.of(rng1, mean);
160         final SharedStateDiscreteSampler sampler2 = sampler1.withUniformRandomProvider(rng2);
161         RandomAssert.assertProduceSameSequence(sampler1, sampler2);
162     }
163 }