Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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())
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)