|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +from multinomial import MultinomialDistribution |
| 4 | + |
| 5 | + |
| 6 | +def test_init_without_rso(): |
| 7 | + """Initialize without rso""" |
| 8 | + p = np.array([0.1, 0.5, 0.3, 0.1]) |
| 9 | + dist = MultinomialDistribution(p) |
| 10 | + assert (dist.p == p).all() |
| 11 | + assert (dist.logp == np.log(p)).all() |
| 12 | + assert dist.rso is None |
| 13 | + assert dist._sample_func == np.random.multinomial |
| 14 | + |
| 15 | + |
| 16 | +def test_init_with_rso(): |
| 17 | + """Initialize with rso""" |
| 18 | + p = np.array([0.1, 0.5, 0.3, 0.1]) |
| 19 | + rso = np.random.RandomState(29348) |
| 20 | + dist = MultinomialDistribution(p, rso=rso) |
| 21 | + assert (dist.p == p).all() |
| 22 | + assert (dist.logp == np.log(p)).all() |
| 23 | + assert dist.rso == rso |
| 24 | + assert dist._sample_func == rso.multinomial |
| 25 | + |
| 26 | + |
| 27 | +def test_init_bad_probabilities(): |
| 28 | + """Initialize with probabilities that don't sum to 1""" |
| 29 | + p = np.array([0.1, 0.5, 0.3, 0.0]) |
| 30 | + rso = np.random.RandomState(29348) |
| 31 | + with pytest.raises(ValueError): |
| 32 | + MultinomialDistribution(p, rso=rso) |
| 33 | + |
| 34 | + |
| 35 | +def test_pmf_1(): |
| 36 | + """Test PMF with only one possible event""" |
| 37 | + p = np.array([1.0]) |
| 38 | + rso = np.random.RandomState(29348) |
| 39 | + dist = MultinomialDistribution(p, rso=rso) |
| 40 | + assert dist.pmf(np.array([1])) == 1.0 |
| 41 | + assert dist.pmf(np.array([2])) == 1.0 |
| 42 | + assert dist.pmf(np.array([10])) == 1.0 |
| 43 | + |
| 44 | + |
| 45 | +def test_pmf_2(): |
| 46 | + """Test PMF with two possible events, one with zero probability""" |
| 47 | + p = np.array([1.0, 0.0]) |
| 48 | + rso = np.random.RandomState(29348) |
| 49 | + dist = MultinomialDistribution(p, rso=rso) |
| 50 | + assert dist.pmf(np.array([1, 0])) == 1.0 |
| 51 | + assert dist.pmf(np.array([0, 1])) == 0.0 |
| 52 | + assert dist.pmf(np.array([2, 0])) == 1.0 |
| 53 | + assert dist.pmf(np.array([2, 2])) == 0.0 |
| 54 | + assert dist.pmf(np.array([10, 0])) == 1.0 |
| 55 | + assert dist.pmf(np.array([10, 3])) == 0.0 |
| 56 | + |
| 57 | + p = np.array([0.0, 1.0]) |
| 58 | + rso = np.random.RandomState(29348) |
| 59 | + dist = MultinomialDistribution(p, rso=rso) |
| 60 | + assert dist.pmf(np.array([0, 1])) == 1.0 |
| 61 | + assert dist.pmf(np.array([1, 0])) == 0.0 |
| 62 | + assert dist.pmf(np.array([0, 2])) == 1.0 |
| 63 | + assert dist.pmf(np.array([2, 2])) == 0.0 |
| 64 | + assert dist.pmf(np.array([0, 10])) == 1.0 |
| 65 | + assert dist.pmf(np.array([3, 10])) == 0.0 |
| 66 | + |
| 67 | + |
| 68 | +def test_pmf_3(): |
| 69 | + """Test PMF with two possible events, both with nonzero probability""" |
| 70 | + p = np.array([0.5, 0.5]) |
| 71 | + rso = np.random.RandomState(29348) |
| 72 | + dist = MultinomialDistribution(p, rso=rso) |
| 73 | + assert dist.pmf(np.array([1, 0])) == 0.5 |
| 74 | + assert dist.pmf(np.array([0, 1])) == 0.5 |
| 75 | + assert dist.pmf(np.array([2, 0])) == 0.25 |
| 76 | + assert dist.pmf(np.array([0, 2])) == 0.25 |
| 77 | + assert dist.pmf(np.array([1, 1])) == 0.5 |
| 78 | + |
| 79 | + |
| 80 | +def test_sample_1(): |
| 81 | + """Test sampling with only one possible event""" |
| 82 | + p = np.array([1.0]) |
| 83 | + rso = np.random.RandomState(29348) |
| 84 | + dist = MultinomialDistribution(p, rso=rso) |
| 85 | + samples = np.array([dist.sample(1) for i in xrange(100)]) |
| 86 | + assert samples.shape == (100, 1) |
| 87 | + assert (samples == 1).all() |
| 88 | + samples = np.array([dist.sample(3) for i in xrange(100)]) |
| 89 | + assert samples.shape == (100, 1) |
| 90 | + assert (samples == 3).all() |
| 91 | + |
| 92 | + |
| 93 | +def test_sample_2(): |
| 94 | + """Test sampling with two possible events, one with zero probability""" |
| 95 | + p = np.array([1.0, 0.0]) |
| 96 | + rso = np.random.RandomState(29348) |
| 97 | + dist = MultinomialDistribution(p, rso=rso) |
| 98 | + samples = np.array([dist.sample(1) for i in xrange(100)]) |
| 99 | + assert samples.shape == (100, 2) |
| 100 | + assert (samples == np.array([1, 0])).all() |
| 101 | + samples = np.array([dist.sample(3) for i in xrange(100)]) |
| 102 | + assert samples.shape == (100, 2) |
| 103 | + assert (samples == np.array([3, 0])).all() |
| 104 | + |
| 105 | + p = np.array([0.0, 1.0]) |
| 106 | + rso = np.random.RandomState(29348) |
| 107 | + dist = MultinomialDistribution(p, rso=rso) |
| 108 | + samples = np.array([dist.sample(1) for i in xrange(100)]) |
| 109 | + assert samples.shape == (100, 2) |
| 110 | + assert (samples == np.array([0, 1])).all() |
| 111 | + samples = np.array([dist.sample(3) for i in xrange(100)]) |
| 112 | + assert samples.shape == (100, 2) |
| 113 | + assert (samples == np.array([0, 3])).all() |
| 114 | + |
| 115 | + |
| 116 | +def test_sample_3(): |
| 117 | + """Test sampling with two possible events, both with nonzero probability""" |
| 118 | + p = np.array([0.5, 0.5]) |
| 119 | + rso = np.random.RandomState(29348) |
| 120 | + dist = MultinomialDistribution(p, rso=rso) |
| 121 | + samples = np.array([dist.sample(1) for i in xrange(100)]) |
| 122 | + assert samples.shape == (100, 2) |
| 123 | + assert ((samples == np.array([1, 0])) | |
| 124 | + (samples == np.array([0, 1]))).all() |
| 125 | + samples = np.array([dist.sample(3) for i in xrange(100)]) |
| 126 | + assert samples.shape == (100, 2) |
| 127 | + assert ((samples == np.array([3, 0])) | |
| 128 | + (samples == np.array([2, 1])) | |
| 129 | + (samples == np.array([1, 2])) | |
| 130 | + (samples == np.array([0, 3]))).all() |
0 commit comments