示例#1
0
 def test_reset(self):
     p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])
     seed = np.random.default_rng(15286971436105697578659603487027011470)
     engine = qmc.MultinomialQMC(p, seed=seed)
     samples = engine.random(2)
     engine.reset()
     samples_reset = engine.random(2)
     assert_array_equal(samples, samples_reset)
示例#2
0
 def test_other_engine(self):
     # same as test_MultinomialBasicDraw with different engine
     seed = np.random.RandomState(12345)
     p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])
     expected = np.array([12, 25, 6, 35, 22])
     base_engine = qmc.Sobol(1, scramble=True, seed=seed)
     engine = qmc.MultinomialQMC(p, engine=base_engine, seed=seed)
     assert_array_equal(engine.random(100), expected)
示例#3
0
 def test_other_engine(self):
     # same as test_MultinomialBasicDraw with different engine
     seed = np.random.default_rng(283753519042773243071753037669078065412)
     p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])
     expected = np.array([12, 25, 5, 36, 22])
     base_engine = qmc.Sobol(1, scramble=True, seed=seed)
     engine = qmc.MultinomialQMC(p, engine=base_engine, seed=seed)
     assert_array_equal(engine.random(100), expected)
示例#4
0
 def test_reset(self):
     p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])
     seed = np.random.RandomState(12345)
     engine = qmc.MultinomialQMC(p, seed=seed)
     samples = engine.random(2)
     engine.reset()
     samples_reset = engine.random(2)
     assert_array_equal(samples, samples_reset)
示例#5
0
    def test_validations(self):
        # negative Ps
        p = np.array([0.12, 0.26, -0.05, 0.35, 0.22])
        with pytest.raises(ValueError, match=r"Elements of pvals must "
                                             r"be non-negative."):
            qmc.MultinomialQMC(p)

        # sum of P too large
        p = np.array([0.12, 0.26, 0.1, 0.35, 0.22])
        message = r"Elements of pvals must sum to 1."
        with pytest.raises(ValueError, match=message):
            qmc.MultinomialQMC(p)

        p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])

        message = r"Dimension of `engine` must be 1."
        with pytest.raises(ValueError, match=message):
            qmc.MultinomialQMC(p, engine=qmc.Sobol(d=2))

        message = r"`engine` must be an instance of..."
        with pytest.raises(ValueError, match=message):
            qmc.MultinomialQMC(p, engine=np.random.default_rng())
示例#6
0
 def test_MultinomialDistribution(self):
     seed = np.random.default_rng(77797854505813727292048130876699859000)
     p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])
     engine = qmc.MultinomialQMC(p, seed=seed)
     draws = engine.random(8192)
     assert_array_almost_equal(draws / np.sum(draws), p, decimal=4)
示例#7
0
 def test_MultinomialBasicDraw(self):
     seed = np.random.default_rng(6955663962957011631562466584467607969)
     p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])
     expected = np.array([13, 24, 6, 35, 22])
     engine = qmc.MultinomialQMC(p, seed=seed)
     assert_array_equal(engine.random(100), expected)
示例#8
0
 def test_MultinomialDistribution(self):
     seed = np.random.RandomState(12345)
     p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])
     engine = qmc.MultinomialQMC(p, seed=seed)
     draws = engine.random(8192)
     assert_array_almost_equal(draws / np.sum(draws), p, decimal=4)
示例#9
0
 def test_MultinomialBasicDraw(self):
     seed = np.random.RandomState(12345)
     p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])
     expected = np.array([12, 25, 6, 35, 22])
     engine = qmc.MultinomialQMC(p, seed=seed)
     assert_array_equal(engine.random(100), expected)
示例#10
0
 def test_MultinomialSumOfPTooLarge(self):
     p = np.array([0.12, 0.26, 0.1, 0.35, 0.22])
     with pytest.raises(ValueError,
                        match=r"Elements of pvals must sum "
                        r"to 1."):
         qmc.MultinomialQMC(p)
示例#11
0
 def test_MultinomialNegativePs(self):
     p = np.array([0.12, 0.26, -0.05, 0.35, 0.22])
     with pytest.raises(ValueError,
                        match=r"Elements of pvals must "
                        r"be non-negative."):
         qmc.MultinomialQMC(p)
示例#12
0
 def test_MultinomialDistribution(self):
     seed = np.random.default_rng(77797854505813727292048130876699859000)
     p = np.array([0.12, 0.26, 0.05, 0.35, 0.22])
     engine = qmc.MultinomialQMC(p, n_trials=8192, seed=seed)
     draws = engine.random(1)
     assert_allclose(draws / np.sum(draws), np.atleast_2d(p), atol=1e-4)