pymc-extras 0.2.1__py3-none-any.whl → 0.2.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tests/test_pathfinder.py CHANGED
@@ -18,12 +18,12 @@ import numpy as np
18
18
  import pymc as pm
19
19
  import pytest
20
20
 
21
+ pytestmark = pytest.mark.filterwarnings("ignore:compile_pymc was renamed to compile:FutureWarning")
22
+
21
23
  import pymc_extras as pmx
22
24
 
23
25
 
24
- @pytest.mark.skipif(sys.platform == "win32", reason="JAX not supported on windows.")
25
- def test_pathfinder():
26
- # Data of the Eight Schools Model
26
+ def eight_schools_model() -> pm.Model:
27
27
  J = 8
28
28
  y = np.array([28.0, 8.0, -3.0, 7.0, -1.0, 1.0, 18.0, 12.0])
29
29
  sigma = np.array([15.0, 10.0, 16.0, 11.0, 9.0, 11.0, 10.0, 18.0])
@@ -35,11 +35,139 @@ def test_pathfinder():
35
35
  theta = pm.Normal("theta", mu=0, sigma=1, shape=J)
36
36
  obs = pm.Normal("obs", mu=mu + tau * theta, sigma=sigma, shape=J, observed=y)
37
37
 
38
- idata = pmx.fit(method="pathfinder", random_seed=41)
38
+ return model
39
+
40
+
41
+ @pytest.fixture
42
+ def reference_idata():
43
+ model = eight_schools_model()
44
+ with model:
45
+ idata = pmx.fit(
46
+ method="pathfinder",
47
+ num_paths=50,
48
+ jitter=10.0,
49
+ random_seed=41,
50
+ inference_backend="pymc",
51
+ )
52
+ return idata
53
+
54
+
55
+ @pytest.mark.parametrize("inference_backend", ["pymc", "blackjax"])
56
+ def test_pathfinder(inference_backend, reference_idata):
57
+ if inference_backend == "blackjax" and sys.platform == "win32":
58
+ pytest.skip("JAX not supported on windows")
59
+
60
+ if inference_backend == "blackjax":
61
+ model = eight_schools_model()
62
+ with model:
63
+ idata = pmx.fit(
64
+ method="pathfinder",
65
+ num_paths=50,
66
+ jitter=10.0,
67
+ random_seed=41,
68
+ inference_backend=inference_backend,
69
+ )
70
+ else:
71
+ idata = reference_idata
72
+ np.testing.assert_allclose(idata.posterior["mu"].mean(), 5.0, atol=1.6)
73
+ np.testing.assert_allclose(idata.posterior["tau"].mean(), 4.15, atol=1.5)
39
74
 
40
75
  assert idata.posterior["mu"].shape == (1, 1000)
41
76
  assert idata.posterior["tau"].shape == (1, 1000)
42
77
  assert idata.posterior["theta"].shape == (1, 1000, 8)
43
- # FIXME: pathfinder doesn't find a reasonable mean! Fix bug or choose model pathfinder can handle
44
- # np.testing.assert_allclose(idata.posterior["mu"].mean(), 5.0)
45
- np.testing.assert_allclose(idata.posterior["tau"].mean(), 4.15, atol=0.5)
78
+
79
+
80
+ @pytest.mark.parametrize("concurrent", ["thread", "process"])
81
+ def test_concurrent_results(reference_idata, concurrent):
82
+ model = eight_schools_model()
83
+ with model:
84
+ idata_conc = pmx.fit(
85
+ method="pathfinder",
86
+ num_paths=50,
87
+ jitter=10.0,
88
+ random_seed=41,
89
+ inference_backend="pymc",
90
+ concurrent=concurrent,
91
+ )
92
+
93
+ np.testing.assert_allclose(
94
+ reference_idata.posterior.mu.data.mean(),
95
+ idata_conc.posterior.mu.data.mean(),
96
+ atol=0.4,
97
+ )
98
+
99
+ np.testing.assert_allclose(
100
+ reference_idata.posterior.tau.data.mean(),
101
+ idata_conc.posterior.tau.data.mean(),
102
+ atol=0.4,
103
+ )
104
+
105
+
106
+ def test_seed(reference_idata):
107
+ model = eight_schools_model()
108
+ with model:
109
+ idata_41 = pmx.fit(
110
+ method="pathfinder",
111
+ num_paths=50,
112
+ jitter=10.0,
113
+ random_seed=41,
114
+ inference_backend="pymc",
115
+ )
116
+
117
+ idata_123 = pmx.fit(
118
+ method="pathfinder",
119
+ num_paths=50,
120
+ jitter=10.0,
121
+ random_seed=123,
122
+ inference_backend="pymc",
123
+ )
124
+
125
+ assert not np.allclose(idata_41.posterior.mu.data.mean(), idata_123.posterior.mu.data.mean())
126
+
127
+ assert np.allclose(idata_41.posterior.mu.data.mean(), idata_41.posterior.mu.data.mean())
128
+
129
+
130
+ def test_bfgs_sample():
131
+ import pytensor.tensor as pt
132
+
133
+ from pymc_extras.inference.pathfinder.pathfinder import (
134
+ alpha_recover,
135
+ bfgs_sample,
136
+ inverse_hessian_factors,
137
+ )
138
+
139
+ """test BFGS sampling"""
140
+ Lp1, N = 8, 10
141
+ L = Lp1 - 1
142
+ J = 6
143
+ num_samples = 1000
144
+
145
+ # mock data
146
+ x_data = np.random.randn(Lp1, N)
147
+ g_data = np.random.randn(Lp1, N)
148
+
149
+ # get factors
150
+ x_full = pt.as_tensor(x_data, dtype="float64")
151
+ g_full = pt.as_tensor(g_data, dtype="float64")
152
+ epsilon = 1e-11
153
+
154
+ x = x_full[1:]
155
+ g = g_full[1:]
156
+ alpha, S, Z, update_mask = alpha_recover(x_full, g_full, epsilon)
157
+ beta, gamma = inverse_hessian_factors(alpha, S, Z, update_mask, J)
158
+
159
+ # sample
160
+ phi, logq = bfgs_sample(
161
+ num_samples=num_samples,
162
+ x=x,
163
+ g=g,
164
+ alpha=alpha,
165
+ beta=beta,
166
+ gamma=gamma,
167
+ )
168
+
169
+ # check shapes
170
+ assert beta.eval().shape == (L, N, 2 * J)
171
+ assert gamma.eval().shape == (L, 2 * J, 2 * J)
172
+ assert phi.eval().shape == (L, num_samples, N)
173
+ assert logq.eval().shape == (L, num_samples)
@@ -1,134 +0,0 @@
1
- # Copyright 2022 The PyMC Developers
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- import collections
16
- import sys
17
-
18
- import arviz as az
19
- import blackjax
20
- import jax
21
- import numpy as np
22
- import pymc as pm
23
-
24
- from packaging import version
25
- from pymc.backends.arviz import coords_and_dims_for_inferencedata
26
- from pymc.blocking import DictToArrayBijection, RaveledVars
27
- from pymc.model import modelcontext
28
- from pymc.sampling.jax import get_jaxified_graph
29
- from pymc.util import RandomSeed, _get_seeds_per_chain, get_default_varnames
30
-
31
-
32
- def convert_flat_trace_to_idata(
33
- samples,
34
- include_transformed=False,
35
- postprocessing_backend="cpu",
36
- model=None,
37
- ):
38
- model = modelcontext(model)
39
- ip = model.initial_point()
40
- ip_point_map_info = pm.blocking.DictToArrayBijection.map(ip).point_map_info
41
- trace = collections.defaultdict(list)
42
- for sample in samples:
43
- raveld_vars = RaveledVars(sample, ip_point_map_info)
44
- point = DictToArrayBijection.rmap(raveld_vars, ip)
45
- for p, v in point.items():
46
- trace[p].append(v.tolist())
47
-
48
- trace = {k: np.asarray(v)[None, ...] for k, v in trace.items()}
49
-
50
- var_names = model.unobserved_value_vars
51
- vars_to_sample = list(get_default_varnames(var_names, include_transformed=include_transformed))
52
- print("Transforming variables...", file=sys.stdout)
53
- jax_fn = get_jaxified_graph(inputs=model.value_vars, outputs=vars_to_sample)
54
- result = jax.vmap(jax.vmap(jax_fn))(
55
- *jax.device_put(list(trace.values()), jax.devices(postprocessing_backend)[0])
56
- )
57
- trace = {v.name: r for v, r in zip(vars_to_sample, result)}
58
- coords, dims = coords_and_dims_for_inferencedata(model)
59
- idata = az.from_dict(trace, dims=dims, coords=coords)
60
-
61
- return idata
62
-
63
-
64
- def fit_pathfinder(
65
- samples=1000,
66
- random_seed: RandomSeed | None = None,
67
- postprocessing_backend="cpu",
68
- model=None,
69
- **pathfinder_kwargs,
70
- ):
71
- """
72
- Fit the pathfinder algorithm as implemented in blackjax
73
-
74
- Requires the JAX backend
75
-
76
- Parameters
77
- ----------
78
- samples : int
79
- Number of samples to draw from the fitted approximation.
80
- random_seed : int
81
- Random seed to set.
82
- postprocessing_backend : str
83
- Where to compute transformations of the trace.
84
- "cpu" or "gpu".
85
- pathfinder_kwargs:
86
- kwargs for blackjax.vi.pathfinder.approximate
87
-
88
- Returns
89
- -------
90
- arviz.InferenceData
91
-
92
- Reference
93
- ---------
94
- https://arxiv.org/abs/2108.03782
95
- """
96
- # Temporarily helper
97
- if version.parse(blackjax.__version__).major < 1:
98
- raise ImportError("fit_pathfinder requires blackjax 1.0 or above")
99
-
100
- model = modelcontext(model)
101
-
102
- ip = model.initial_point()
103
- ip_map = DictToArrayBijection.map(ip)
104
-
105
- new_logprob, new_input = pm.pytensorf.join_nonshared_inputs(
106
- ip, (model.logp(),), model.value_vars, ()
107
- )
108
-
109
- logprob_fn_list = get_jaxified_graph([new_input], new_logprob)
110
-
111
- def logprob_fn(x):
112
- return logprob_fn_list(x)[0]
113
-
114
- [pathfinder_seed, sample_seed] = _get_seeds_per_chain(random_seed, 2)
115
-
116
- print("Running pathfinder...", file=sys.stdout)
117
- pathfinder_state, _ = blackjax.vi.pathfinder.approximate(
118
- rng_key=jax.random.key(pathfinder_seed),
119
- logdensity_fn=logprob_fn,
120
- initial_position=ip_map.data,
121
- **pathfinder_kwargs,
122
- )
123
- samples, _ = blackjax.vi.pathfinder.sample(
124
- rng_key=jax.random.key(sample_seed),
125
- state=pathfinder_state,
126
- num_samples=samples,
127
- )
128
-
129
- idata = convert_flat_trace_to_idata(
130
- samples,
131
- postprocessing_backend=postprocessing_backend,
132
- model=model,
133
- )
134
- return idata