pymc-extras 0.2.1__py3-none-any.whl → 0.2.2__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.
- pymc_extras/inference/fit.py +0 -4
- pymc_extras/inference/pathfinder/__init__.py +3 -0
- pymc_extras/inference/pathfinder/importance_sampling.py +139 -0
- pymc_extras/inference/pathfinder/lbfgs.py +190 -0
- pymc_extras/inference/pathfinder/pathfinder.py +1746 -0
- pymc_extras/model/model_api.py +18 -2
- pymc_extras/statespace/core/statespace.py +79 -36
- pymc_extras/version.txt +1 -1
- {pymc_extras-0.2.1.dist-info → pymc_extras-0.2.2.dist-info}/METADATA +13 -2
- {pymc_extras-0.2.1.dist-info → pymc_extras-0.2.2.dist-info}/RECORD +16 -13
- {pymc_extras-0.2.1.dist-info → pymc_extras-0.2.2.dist-info}/WHEEL +1 -1
- tests/model/test_model_api.py +9 -0
- tests/statespace/test_statespace.py +54 -0
- tests/test_pathfinder.py +135 -7
- pymc_extras/inference/pathfinder.py +0 -134
- {pymc_extras-0.2.1.dist-info → pymc_extras-0.2.2.dist-info}/LICENSE +0 -0
- {pymc_extras-0.2.1.dist-info → pymc_extras-0.2.2.dist-info}/top_level.txt +0 -0
|
@@ -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
|
|
File without changes
|
|
File without changes
|