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.RandomProviderState;
20  import org.apache.commons.rng.RestorableUniformRandomProvider;
21  import org.apache.commons.rng.sampling.distribution.LargeMeanPoissonSampler.LargeMeanPoissonSamplerState;
22  import org.apache.commons.rng.simple.RandomSource;
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  /**
27   * This test checks the {@link LargeMeanPoissonSampler} can be created
28   * from a saved state.
29   */
30  public class LargeMeanPoissonSamplerTest {
31  
32      // Edge cases for construction
33  
34      /**
35       * Test the constructor with a bad mean.
36       */
37      @Test(expected=IllegalArgumentException.class)
38      public void testConstructorThrowsWithMeanLargerThanUpperBound() {
39          final RestorableUniformRandomProvider rng =
40                  RandomSource.create(RandomSource.SPLIT_MIX_64);
41          @SuppressWarnings("unused")
42          LargeMeanPoissonSampler sampler = new LargeMeanPoissonSampler(rng, Integer.MAX_VALUE / 2 + 1);
43      }
44  
45      /**
46       * Test the constructor with a bad mean.
47       */
48      @Test(expected=IllegalArgumentException.class)
49      public void testConstructorThrowsWithZeroMean() {
50          final RestorableUniformRandomProvider rng =
51                  RandomSource.create(RandomSource.SPLIT_MIX_64);
52          @SuppressWarnings("unused")
53          LargeMeanPoissonSampler sampler = new LargeMeanPoissonSampler(rng, 0);
54      }
55  
56      /**
57       * Test the constructor with a negative fractional mean.
58       */
59      @Test(expected=IllegalArgumentException.class)
60      public void testConstructorThrowsWithNegativeFractionalMean() {
61          final RestorableUniformRandomProvider rng =
62                  RandomSource.create(RandomSource.SPLIT_MIX_64);
63          final LargeMeanPoissonSamplerState state = new LargeMeanPoissonSampler(rng, 1).getState();
64          @SuppressWarnings("unused")
65          LargeMeanPoissonSampler sampler = new LargeMeanPoissonSampler(rng, state, -0.1);
66      }
67  
68      /**
69       * Test the constructor with a non-fractional mean.
70       */
71      @Test(expected=IllegalArgumentException.class)
72      public void testConstructorThrowsWithNonFractionalMean() {
73          final RestorableUniformRandomProvider rng =
74                  RandomSource.create(RandomSource.SPLIT_MIX_64);
75          final LargeMeanPoissonSamplerState state = new LargeMeanPoissonSampler(rng, 1).getState();
76          @SuppressWarnings("unused")
77          LargeMeanPoissonSampler sampler = new LargeMeanPoissonSampler(rng, state, 1.1);
78      }
79  
80      /**
81       * Test the constructor with fractional mean of 1.
82       */
83      @Test(expected=IllegalArgumentException.class)
84      public void testConstructorThrowsWithFractionalMeanOne() {
85          final RestorableUniformRandomProvider rng =
86                  RandomSource.create(RandomSource.SPLIT_MIX_64);
87          final LargeMeanPoissonSamplerState state = new LargeMeanPoissonSampler(rng, 1).getState();
88          @SuppressWarnings("unused")
89          LargeMeanPoissonSampler sampler = new LargeMeanPoissonSampler(rng, state, 1);
90      }
91  
92      // Sampling tests
93  
94      /**
95       * Test the {@link LargeMeanPoissonSampler} returns the same samples when it
96       * is created using the saved state.
97       */
98      @Test
99      public void testCanComputeSameSamplesWhenConstructedWithState() {
100         // Two identical RNGs
101         final RestorableUniformRandomProvider rng1 =
102                 RandomSource.create(RandomSource.MWC_256);
103         final RandomProviderState state = rng1.saveState();
104         final RestorableUniformRandomProvider rng2 =
105                 RandomSource.create(RandomSource.MWC_256);
106         rng2.restoreState(state);
107 
108         // The sampler is suitable for mean > 40
109         for (int i = 40; i < 44; i++) {
110             // Test integer mean (no SmallMeanPoissonSampler required)
111             testPoissonSamples(rng1, rng2, i);
112             // Test non-integer mean (SmallMeanPoissonSampler required)
113             testPoissonSamples(rng1, rng2, i + 0.5);
114         }
115     }
116 
117     /**
118      * Test the {@link LargeMeanPoissonSampler} returns the same samples when it
119      * is created using the saved state. The random providers must be
120      * identical (including state).
121      *
122      * @param rng1  the first random provider
123      * @param rng2  the second random provider
124      * @param cache the cache
125      * @param mean  the mean
126      */
127     private static void testPoissonSamples(
128             final RestorableUniformRandomProvider rng1,
129             final RestorableUniformRandomProvider rng2,
130             double mean) {
131         final DiscreteSampler s1 = new LargeMeanPoissonSampler(rng1, mean);
132         final int n = (int) Math.floor(mean);
133         final double lambdaFractional = mean - n;
134         final LargeMeanPoissonSamplerState state1 = ((LargeMeanPoissonSampler)s1).getState();
135         final DiscreteSampler s2 = new LargeMeanPoissonSampler(rng2, state1, lambdaFractional);
136         final LargeMeanPoissonSamplerState state2 = ((LargeMeanPoissonSampler)s2).getState();
137         Assert.assertEquals("State lambdas are not equal", state1.getLambda(), state2.getLambda());
138         Assert.assertNotSame("States are the same object", state1, state2);
139         for (int j = 0; j < 10; j++)
140             Assert.assertEquals("Not the same sample", s1.sample(), s2.sample());
141     }
142 }