-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathtest_probability.py
85 lines (66 loc) · 2.61 KB
/
test_probability.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from pyhf import probability
import numpy as np
def test_poisson(backend):
tb, _ = backend
result = probability.Poisson(tb.astensor([10.0])).log_prob(tb.astensor(2.0))
assert result.shape == (1,)
result = probability.Poisson(tb.astensor([10.0, 10.0])).log_prob(tb.astensor(2.0))
assert result.shape == (2,)
result = probability.Poisson(tb.astensor([10.0, 10.0])).log_prob(
tb.astensor([2.0, 3.0])
)
assert result.shape == (2,)
result = probability.Poisson(tb.astensor([10.0, 10.0])).log_prob(
tb.astensor([[2.0, 3.0]])
)
assert result.shape == (1, 2)
sample = probability.Poisson(tb.astensor([10.0, 10.0])).sample((10,))
assert sample.shape == (10, 2)
def test_normal(backend):
tb, _ = backend
result = probability.Normal(tb.astensor([10.0]), tb.astensor([1])).log_prob(
tb.astensor(2.0)
)
assert result.shape == (1,)
result = probability.Normal(
tb.astensor([10.0, 10.0]), tb.astensor([1, 1])
).log_prob(tb.astensor(2.0))
assert result.shape == (2,)
result = probability.Normal(
tb.astensor([10.0, 10.0]), tb.astensor([10.0, 10.0])
).log_prob(tb.astensor([2.0, 3.0]))
assert result.shape == (2,)
result = probability.Normal(
tb.astensor([10.0, 10.0]), tb.astensor([10.0, 10.0])
).log_prob(tb.astensor([[2.0, 3.0]]))
assert result.shape == (1, 2)
sample = probability.Normal(
tb.astensor([10.0, 10.0]), tb.astensor([10.0, 10.0])
).sample((10,))
assert sample.shape == (10, 2)
def test_joint(backend):
tb, _ = backend
p1 = probability.Poisson(tb.astensor([10.0])).log_prob(tb.astensor(2.0))
p2 = probability.Poisson(tb.astensor([10.0])).log_prob(tb.astensor(3.0))
assert tb.tolist(probability.Simultaneous._joint_logpdf([p1, p2])) == tb.tolist(
p1 + p2
)
def test_independent(backend):
tb, _ = backend
result = probability.Independent(
probability.Poisson(tb.astensor([10.0, 10.0]))
).log_prob(tb.astensor([2.0, 3.0]))
p1 = probability.Poisson(tb.astensor([10.0])).log_prob(tb.astensor(2.0))
p2 = probability.Poisson(tb.astensor([10.0])).log_prob(tb.astensor(3.0))
assert tb.tolist(probability.Simultaneous._joint_logpdf([p1, p2]))[0] == tb.tolist(
result
)
assert tb.tolist(probability.Simultaneous._joint_logpdf([p1, p2]))[0] == tb.tolist(
result
)
def test_simultaneous_list_ducktype():
myobjs = np.random.randint(100, size=10).tolist()
sim = probability.Simultaneous(myobjs, None)
assert sim[3] == myobjs[3]
for simobj, myobj in zip(sim, myobjs):
assert simobj == myobj