-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathtest_approximations.py
176 lines (143 loc) · 6.76 KB
/
test_approximations.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright 2024 - present The PyMC Developers
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import numpy as np
import pytensor
import pytest
import pymc as pm
from pymc.variational.approximations import Empirical, MeanField
from tests import models
def test_empirical_does_not_support_inference_data():
with models.another_simple_model():
step = pm.Metropolis()
trace = pm.sample(100, step=step, chains=1, tune=0, return_inferencedata=True)
with pytest.raises(NotImplementedError, match="return_inferencedata=False"):
Empirical(trace)
def test_empirical_from_trace():
with models.another_simple_model():
step = pm.Metropolis()
trace = pm.sample(100, step=step, chains=1, tune=0, return_inferencedata=False)
emp = Empirical(trace)
assert emp.histogram.shape[0].eval() == 100
trace = pm.sample(100, step=step, chains=4, tune=0, return_inferencedata=False)
emp = Empirical(trace)
assert emp.histogram.shape[0].eval() == 400
def test_elbo():
mu0 = 1.5
sigma = 1.0
y_obs = np.array([1.6, 1.4])
post_mu = np.array([1.88], dtype=pytensor.config.floatX)
post_sigma = np.array([1], dtype=pytensor.config.floatX)
# Create a model for test
with pm.Model() as model:
mu = pm.Normal("mu", mu=mu0, sigma=sigma)
pm.Normal("y", mu=mu, sigma=1, observed=y_obs)
# Create variational gradient tensor
mean_field = MeanField(model=model)
with pytensor.config.change_flags(compute_test_value="off"):
elbo = -pm.operators.KL(mean_field)()(10000)
mean_field.shared_params["mu"].set_value(post_mu)
mean_field.shared_params["rho"].set_value(np.log(np.exp(post_sigma) - 1))
f = pytensor.function([], elbo)
elbo_mc = f()
# Exact value
elbo_true = -0.5 * (
3
+ 3 * post_mu**2
- 2 * (y_obs[0] + y_obs[1] + mu0) * post_mu
+ y_obs[0] ** 2
+ y_obs[1] ** 2
+ mu0**2
+ 3 * np.log(2 * np.pi)
) + 0.5 * (np.log(2 * np.pi) + 1)
np.testing.assert_allclose(elbo_mc, elbo_true, rtol=0, atol=1e-1)
@pytest.mark.parametrize("aux_total_size", range(2, 10, 3))
def test_scale_cost_to_minibatch_works(aux_total_size):
mu0 = 1.5
sigma = 1.0
y_obs = np.array([1.6, 1.4])
beta = len(y_obs) / float(aux_total_size)
with pytensor.config.change_flags(floatX="float64", warn_float64="ignore"):
assert pytensor.config.floatX == "float64"
assert pytensor.config.warn_float64 == "ignore"
post_mu = np.array([1.88], dtype=pytensor.config.floatX)
post_sigma = np.array([1], dtype=pytensor.config.floatX)
with pm.Model():
mu = pm.Normal("mu", mu=mu0, sigma=sigma)
pm.Normal("y", mu=mu, sigma=1, observed=y_obs, total_size=aux_total_size)
# Create variational gradient tensor
mean_field_1 = MeanField()
assert mean_field_1.scale_cost_to_minibatch
mean_field_1.shared_params["mu"].set_value(post_mu)
mean_field_1.shared_params["rho"].set_value(np.log(np.exp(post_sigma) - 1))
with pytensor.config.change_flags(compute_test_value="off"):
elbo_via_total_size_scaled = -pm.operators.KL(mean_field_1)()(10000)
with pm.Model():
mu = pm.Normal("mu", mu=mu0, sigma=sigma)
pm.Normal("y", mu=mu, sigma=1, observed=y_obs, total_size=aux_total_size)
# Create variational gradient tensor
mean_field_2 = MeanField()
assert mean_field_1.scale_cost_to_minibatch
mean_field_2.scale_cost_to_minibatch = False
assert not mean_field_2.scale_cost_to_minibatch
mean_field_2.shared_params["mu"].set_value(post_mu)
mean_field_2.shared_params["rho"].set_value(np.log(np.exp(post_sigma) - 1))
with pytensor.config.change_flags(compute_test_value="off"):
elbo_via_total_size_unscaled = -pm.operators.KL(mean_field_2)()(10000)
np.testing.assert_allclose(
elbo_via_total_size_unscaled.eval(),
elbo_via_total_size_scaled.eval() * pm.floatX(1 / beta),
rtol=0.02,
atol=1e-1,
)
@pytest.mark.parametrize("aux_total_size", range(2, 10, 3))
def test_elbo_beta_kl(aux_total_size):
mu0 = 1.5
sigma = 1.0
y_obs = np.array([1.6, 1.4])
beta = len(y_obs) / float(aux_total_size)
with pytensor.config.change_flags(floatX="float64", warn_float64="ignore"):
post_mu = np.array([1.88], dtype=pytensor.config.floatX)
post_sigma = np.array([1], dtype=pytensor.config.floatX)
with pm.Model():
mu = pm.Normal("mu", mu=mu0, sigma=sigma)
pm.Normal("y", mu=mu, sigma=1, observed=y_obs, total_size=aux_total_size)
# Create variational gradient tensor
mean_field_1 = MeanField()
mean_field_1.scale_cost_to_minibatch = True
mean_field_1.shared_params["mu"].set_value(post_mu)
mean_field_1.shared_params["rho"].set_value(np.log(np.exp(post_sigma) - 1))
with pytensor.config.change_flags(compute_test_value="off"):
elbo_via_total_size_scaled = -pm.operators.KL(mean_field_1)()(10000)
with pm.Model():
mu = pm.Normal("mu", mu=mu0, sigma=sigma)
pm.Normal("y", mu=mu, sigma=1, observed=y_obs)
# Create variational gradient tensor
mean_field_3 = MeanField()
mean_field_3.shared_params["mu"].set_value(post_mu)
mean_field_3.shared_params["rho"].set_value(np.log(np.exp(post_sigma) - 1))
with pytensor.config.change_flags(compute_test_value="off"):
elbo_via_beta_kl = -pm.operators.KL(mean_field_3, beta=beta)()(10000)
np.testing.assert_allclose(
elbo_via_total_size_scaled.eval(), elbo_via_beta_kl.eval(), rtol=0, atol=1e-1
)
def test_seeding_advi_fit():
with pm.Model():
x = pm.Normal("x", 0, 10, initval="prior")
approx1 = pm.fit(
random_seed=42, n=10, method="advi", obj_optimizer=pm.adagrad_window, progressbar=False
)
approx2 = pm.fit(
random_seed=42, n=10, method="advi", obj_optimizer=pm.adagrad_window, progressbar=False
)
np.testing.assert_allclose(approx1.mean.eval(), approx2.mean.eval())