/
test_distributions_util.py
158 lines (130 loc) · 4.54 KB
/
test_distributions_util.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Copyright Contributors to the Pyro project.
# SPDX-License-Identifier: Apache-2.0
from numbers import Number
import numpy as onp
from numpy.testing import assert_allclose
import pytest
from jax import lax, random, vmap
import jax.numpy as np
from jax.scipy.special import expit, xlog1py, xlogy
from numpyro.distributions.util import (
binary_cross_entropy_with_logits,
categorical,
cholesky_update,
multinomial,
poisson,
vec_to_tril_matrix,
binomial)
@pytest.mark.parametrize('x, y', [
(0.2, 10.),
(0.6, -10.),
])
def test_binary_cross_entropy_with_logits(x, y):
actual = -y * np.log(expit(x)) - (1 - y) * np.log(expit(-x))
expect = binary_cross_entropy_with_logits(x, y)
assert_allclose(actual, expect, rtol=1e-6)
@pytest.mark.parametrize('prim', [
xlogy,
xlog1py,
])
def test_binop_batch_rule(prim):
bx = np.array([1., 2., 3.])
by = np.array([2., 3., 4.])
x = np.array(1.)
y = np.array(2.)
actual_bx_by = vmap(lambda x, y: prim(x, y))(bx, by)
for i in range(3):
assert_allclose(actual_bx_by[i], prim(bx[i], by[i]))
actual_x_by = vmap(lambda y: prim(x, y))(by)
for i in range(3):
assert_allclose(actual_x_by[i], prim(x, by[i]))
actual_bx_y = vmap(lambda x: prim(x, y))(bx)
for i in range(3):
assert_allclose(actual_bx_y[i], prim(bx[i], y))
@pytest.mark.parametrize('p, shape', [
(np.array([0.1, 0.9]), ()),
(np.array([0.2, 0.8]), (2,)),
(np.array([[0.1, 0.9], [0.2, 0.8]]), ()),
(np.array([[0.1, 0.9], [0.2, 0.8]]), (3, 2)),
])
def test_categorical_shape(p, shape):
rng_key = random.PRNGKey(0)
expected_shape = lax.broadcast_shapes(p.shape[:-1], shape)
assert np.shape(categorical(rng_key, p, shape)) == expected_shape
@pytest.mark.parametrize("p", [
np.array([0.2, 0.3, 0.5]),
np.array([0.8, 0.1, 0.1]),
])
def test_categorical_stats(p):
rng_key = random.PRNGKey(0)
n = 10000
z = categorical(rng_key, p, (n,))
_, counts = onp.unique(z, return_counts=True)
assert_allclose(counts / float(n), p, atol=0.01)
@pytest.mark.parametrize('p, shape', [
(np.array([0.1, 0.9]), ()),
(np.array([0.2, 0.8]), (2,)),
(np.array([[0.1, 0.9], [0.2, 0.8]]), ()),
(np.array([[0.1, 0.9], [0.2, 0.8]]), (3, 2)),
])
def test_multinomial_shape(p, shape):
rng_key = random.PRNGKey(0)
n = 10000
expected_shape = lax.broadcast_shapes(p.shape[:-1], shape) + p.shape[-1:]
assert np.shape(multinomial(rng_key, p, n, shape)) == expected_shape
@pytest.mark.parametrize("p", [
np.array([0.2, 0.3, 0.5]),
np.array([0.8, 0.1, 0.1]),
])
@pytest.mark.parametrize("n", [
10000,
np.array([10000, 20000]),
])
def test_multinomial_stats(p, n):
rng_key = random.PRNGKey(0)
z = multinomial(rng_key, p, n)
n = float(n) if isinstance(n, Number) else np.expand_dims(n.astype(p.dtype), -1)
p = np.broadcast_to(p, z.shape)
assert_allclose(z / n, p, atol=0.01)
def test_poisson():
mu = rate = 1000
N = 2 ** 18
key = random.PRNGKey(64)
B = poisson(key, rate=rate, shape=(N,))
assert_allclose(B.mean(), mu, rtol=0.001)
@pytest.mark.parametrize("shape", [
(6,),
(5, 10),
(3, 4, 3),
])
@pytest.mark.parametrize("diagonal", [
0,
-1,
-2,
])
def test_vec_to_tril_matrix(shape, diagonal):
rng_key = random.PRNGKey(0)
x = random.normal(rng_key, shape)
actual = vec_to_tril_matrix(x, diagonal)
expected = onp.zeros(shape[:-1] + actual.shape[-2:])
tril_idxs = onp.tril_indices(expected.shape[-1], diagonal)
expected[..., tril_idxs[0], tril_idxs[1]] = x
assert_allclose(actual, expected)
@pytest.mark.parametrize("chol_batch_shape", [(), (3,)])
@pytest.mark.parametrize("vec_batch_shape", [(), (3,)])
@pytest.mark.parametrize("dim", [1, 4])
@pytest.mark.parametrize("coef", [1, -1])
def test_cholesky_update(chol_batch_shape, vec_batch_shape, dim, coef):
A = random.normal(random.PRNGKey(0), chol_batch_shape + (dim, dim))
A = A @ np.swapaxes(A, -2, -1) + np.eye(dim)
x = random.normal(random.PRNGKey(0), vec_batch_shape + (dim,)) * 0.1
xxt = x[..., None] @ x[..., None, :]
expected = np.linalg.cholesky(A + coef * xxt)
actual = cholesky_update(np.linalg.cholesky(A), x, coef)
assert_allclose(actual, expected, atol=1e-4, rtol=1e-4)
@pytest.mark.parametrize("n", [10, 100, 1000])
@pytest.mark.parametrize("p", [0., 0.01, 0.05, 0.3, 0.5, 0.7, 0.95, 1.])
def test_binomial_mean(n, p):
samples = binomial(random.PRNGKey(1), p, n, shape=(100, 100))
expected_mean = n * p
assert_allclose(np.mean(samples), expected_mean, rtol=0.05)