eryn 1.2.0__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.
- eryn/CMakeLists.txt +51 -0
- eryn/__init__.py +35 -0
- eryn/backends/__init__.py +20 -0
- eryn/backends/backend.py +1150 -0
- eryn/backends/hdfbackend.py +819 -0
- eryn/ensemble.py +1690 -0
- eryn/git_version.py.in +7 -0
- eryn/model.py +18 -0
- eryn/moves/__init__.py +42 -0
- eryn/moves/combine.py +135 -0
- eryn/moves/delayedrejection.py +229 -0
- eryn/moves/distgen.py +104 -0
- eryn/moves/distgenrj.py +222 -0
- eryn/moves/gaussian.py +190 -0
- eryn/moves/group.py +281 -0
- eryn/moves/groupstretch.py +120 -0
- eryn/moves/mh.py +193 -0
- eryn/moves/move.py +703 -0
- eryn/moves/mtdistgen.py +137 -0
- eryn/moves/mtdistgenrj.py +190 -0
- eryn/moves/multipletry.py +776 -0
- eryn/moves/red_blue.py +333 -0
- eryn/moves/rj.py +388 -0
- eryn/moves/stretch.py +231 -0
- eryn/moves/tempering.py +649 -0
- eryn/pbar.py +56 -0
- eryn/prior.py +452 -0
- eryn/state.py +775 -0
- eryn/tests/__init__.py +0 -0
- eryn/tests/test_eryn.py +1246 -0
- eryn/utils/__init__.py +10 -0
- eryn/utils/periodic.py +134 -0
- eryn/utils/stopping.py +164 -0
- eryn/utils/transform.py +226 -0
- eryn/utils/updates.py +69 -0
- eryn/utils/utility.py +329 -0
- eryn-1.2.0.dist-info/METADATA +167 -0
- eryn-1.2.0.dist-info/RECORD +39 -0
- eryn-1.2.0.dist-info/WHEEL +4 -0
eryn/moves/mtdistgen.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from eryn.moves.multipletry import MultipleTryMove
|
|
4
|
+
from eryn.moves import MHMove
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class MTDistGenMove(MultipleTryMove, MHMove):
|
|
8
|
+
"""Perform a multiple try MH move based on a distribution.
|
|
9
|
+
|
|
10
|
+
Distribution must be independent of the current point.
|
|
11
|
+
|
|
12
|
+
This is effectively an example of the mutliple try class inheritance structure.
|
|
13
|
+
|
|
14
|
+
Args:
|
|
15
|
+
generate_dist (dict): Keys are branch names and values are :class:`ProbDistContainer` objects
|
|
16
|
+
that have ``logpdf`` and ``rvs`` methods. If you
|
|
17
|
+
*args (tuple, optional): Additional arguments to pass to parent classes.
|
|
18
|
+
**kwargs (dict, optional): Keyword arguments passed to parent classes.
|
|
19
|
+
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
def __init__(self, generate_dist, **kwargs):
|
|
23
|
+
|
|
24
|
+
MultipleTryMove.__init__(self, **kwargs)
|
|
25
|
+
MHMove.__init__(self, **kwargs)
|
|
26
|
+
|
|
27
|
+
self.generate_dist = generate_dist
|
|
28
|
+
|
|
29
|
+
def special_generate_logpdf(self, generated_coords):
|
|
30
|
+
"""Get logpdf of generated coordinates.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
generated_coords (np.ndarray): Current coordinates of walkers.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
np.ndarray: logpdf of generated points.
|
|
37
|
+
|
|
38
|
+
"""
|
|
39
|
+
return self.generate_dist.logpdf(generated_coords)
|
|
40
|
+
|
|
41
|
+
def special_generate_func(
|
|
42
|
+
self, coords, random, size=1, fill_tuple=None, fill_values=None, **kwargs
|
|
43
|
+
):
|
|
44
|
+
"""Generate samples and calculate the logpdf of their proposal function.
|
|
45
|
+
|
|
46
|
+
Args:
|
|
47
|
+
coords (np.ndarray): Current coordinates of walkers.
|
|
48
|
+
random (obj): Random generator.
|
|
49
|
+
*args (tuple, optional): additional arguments passed by overwriting the
|
|
50
|
+
``get_proposal`` function and passing ``args_generate`` keyword argument.
|
|
51
|
+
size (int, optional): Number of tries to generate.
|
|
52
|
+
fill_tuple (tuple, optional): Length 2 tuple with the indexing of which values to fill
|
|
53
|
+
when generating. Can be used for auxillary proposals or reverse RJ proposals. First index is the index into walkers and the second index is
|
|
54
|
+
the index into the number of tries. (default: ``None``)
|
|
55
|
+
fill_values (np.ndarray): values to fill associated with ``fill_tuple``. Should
|
|
56
|
+
have size ``(len(fill_tuple[0]), ndim)``. (default: ``None``).
|
|
57
|
+
**kwargs (tuple, optional): additional keyword arguments passed by overwriting the
|
|
58
|
+
``get_proposal`` function and passing ``kwargs_generate`` keyword argument.
|
|
59
|
+
|
|
60
|
+
Returns:
|
|
61
|
+
tuple: (generated points, logpdf of generated points).
|
|
62
|
+
|
|
63
|
+
"""
|
|
64
|
+
nwalkers = coords.shape[0]
|
|
65
|
+
|
|
66
|
+
if not isinstance(size, int):
|
|
67
|
+
raise ValueError("size must be an int.")
|
|
68
|
+
|
|
69
|
+
# generate coordinates
|
|
70
|
+
generated_coords = self.generate_dist.rvs(size=(nwalkers, size))
|
|
71
|
+
|
|
72
|
+
# fill special coordinates
|
|
73
|
+
if fill_values is not None:
|
|
74
|
+
generated_coords[fill_tuple] = fill_values
|
|
75
|
+
|
|
76
|
+
# get logpdf
|
|
77
|
+
generated_logpdf = self.special_generate_logpdf(
|
|
78
|
+
generated_coords.reshape(nwalkers * size, -1)
|
|
79
|
+
).reshape(nwalkers, size)
|
|
80
|
+
|
|
81
|
+
return generated_coords, generated_logpdf
|
|
82
|
+
|
|
83
|
+
def set_coords_and_inds(self, generated_coords):
|
|
84
|
+
"""Setup coordinates for prior and Likelihood
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
generated_coords (np.ndarray): Generated coordinates with shape ``(number of independent walkers, num_try)``.
|
|
88
|
+
|
|
89
|
+
Returns:
|
|
90
|
+
dict: Coordinates for Likelihood and Prior.
|
|
91
|
+
|
|
92
|
+
"""
|
|
93
|
+
ndim = self.current_state.branches[self.key_in].shape[-1]
|
|
94
|
+
|
|
95
|
+
coords_in_dict = {}
|
|
96
|
+
for key in self.current_state.branches.keys():
|
|
97
|
+
if key == self.key_in:
|
|
98
|
+
coords_in_dict[key] = generated_coords.reshape(-1, 1, ndim)[None, :]
|
|
99
|
+
|
|
100
|
+
else:
|
|
101
|
+
coords_in_dict[key] = self.current_state.branches[key].coords.reshape(
|
|
102
|
+
(1, -1) + self.current_state.branches[key].shape[-2:]
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
return coords_in_dict
|
|
106
|
+
|
|
107
|
+
def special_like_func(self, generated_coords, **kwargs):
|
|
108
|
+
"""Calculate the Likelihood for sampled points.
|
|
109
|
+
|
|
110
|
+
Args:
|
|
111
|
+
generated_coords (np.ndarray): Generated coordinates with shape ``(number of independent walkers, num_try)``.
|
|
112
|
+
**kwargs (dict, optional): For compatibility.
|
|
113
|
+
|
|
114
|
+
Returns:
|
|
115
|
+
np.ndarray: Likelihood values with shape ``(generated_coords.shape[0], num_try).``
|
|
116
|
+
|
|
117
|
+
"""
|
|
118
|
+
coords_in = self.set_coords_and_inds(generated_coords)
|
|
119
|
+
ll = self.current_model.compute_log_like_fn(coords_in)[0]
|
|
120
|
+
ll = ll[0].reshape(-1, self.num_try)
|
|
121
|
+
return ll
|
|
122
|
+
|
|
123
|
+
def special_prior_func(self, generated_coords, **kwargs):
|
|
124
|
+
"""Calculate the Prior for sampled points.
|
|
125
|
+
|
|
126
|
+
Args:
|
|
127
|
+
generated_coords (np.ndarray): Generated coordinates with shape ``(number of independent walkers, num_try)``.
|
|
128
|
+
**kwargs (dict, optional): For compatibility.
|
|
129
|
+
|
|
130
|
+
Returns:
|
|
131
|
+
np.ndarray: Prior values with shape ``(generated_coords.shape[0], num_try).``
|
|
132
|
+
|
|
133
|
+
"""
|
|
134
|
+
coords_in = self.set_coords_and_inds(generated_coords)
|
|
135
|
+
lp = self.current_model.compute_log_prior_fn(coords_in)
|
|
136
|
+
return lp.reshape(-1, self.num_try)
|
|
137
|
+
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
from eryn.moves.multipletry import MultipleTryMoveRJ
|
|
4
|
+
from eryn.moves import DistributionGenerateRJ
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class MTDistGenMoveRJ(MultipleTryMoveRJ, DistributionGenerateRJ):
|
|
8
|
+
def __init__(self, generate_dist, *args, **kwargs):
|
|
9
|
+
"""Perform a reversible-jump multiple try move based on a distribution.
|
|
10
|
+
|
|
11
|
+
Distribution must be independent of the current point.
|
|
12
|
+
|
|
13
|
+
This is effectively an example of the mutliple try class inheritance structure.
|
|
14
|
+
|
|
15
|
+
Args:
|
|
16
|
+
generate_dist (dict): Keys are branch names and values are :class:`ProbDistContainer` objects
|
|
17
|
+
that have ``logpdf`` and ``rvs`` methods. If you
|
|
18
|
+
*args (tuple, optional): Additional arguments to pass to parent classes.
|
|
19
|
+
**kwargs (dict, optional): Keyword arguments passed to parent classes.
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
"""
|
|
23
|
+
kwargs["rj"] = True
|
|
24
|
+
MultipleTryMoveRJ.__init__(self, **kwargs)
|
|
25
|
+
DistributionGenerateRJ.__init__(self, generate_dist, *args, **kwargs)
|
|
26
|
+
|
|
27
|
+
self.generate_dist = generate_dist
|
|
28
|
+
|
|
29
|
+
def special_generate_logpdf(self, generated_coords):
|
|
30
|
+
"""Get logpdf of generated coordinates.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
generated_coords (np.ndarray): Current coordinates of walkers.
|
|
34
|
+
|
|
35
|
+
Returns:
|
|
36
|
+
np.ndarray: logpdf of generated points.
|
|
37
|
+
"""
|
|
38
|
+
return self.generate_dist[self.key_in].logpdf(generated_coords)
|
|
39
|
+
|
|
40
|
+
def special_generate_func(
|
|
41
|
+
self, coords, random, size=1, fill_tuple=None, fill_values=None
|
|
42
|
+
):
|
|
43
|
+
"""Generate samples and calculate the logpdf of their proposal function.
|
|
44
|
+
|
|
45
|
+
Args:
|
|
46
|
+
coords (np.ndarray): Current coordinates of walkers.
|
|
47
|
+
random (obj): Random generator.
|
|
48
|
+
*args (tuple, optional): additional arguments passed by overwriting the
|
|
49
|
+
``get_proposal`` function and passing ``args_generate`` keyword argument.
|
|
50
|
+
size (int, optional): Number of tries to generate.
|
|
51
|
+
fill_tuple (tuple, optional): Length 2 tuple with the indexing of which values to fill
|
|
52
|
+
when generating. Can be used for auxillary proposals or reverse RJ proposals. First index is the index into walkers and the second index is
|
|
53
|
+
the index into the number of tries. (default: ``None``)
|
|
54
|
+
fill_values (np.ndarray): values to fill associated with ``fill_tuple``. Should
|
|
55
|
+
have size ``(len(fill_tuple[0]), ndim)``. (default: ``None``).
|
|
56
|
+
**kwargs (tuple, optional): additional keyword arguments passed by overwriting the
|
|
57
|
+
``get_proposal`` function and passing ``kwargs_generate`` keyword argument.
|
|
58
|
+
|
|
59
|
+
Returns:
|
|
60
|
+
tuple: (generated points, logpdf of generated points).
|
|
61
|
+
|
|
62
|
+
"""
|
|
63
|
+
nwalkers = coords.shape[0]
|
|
64
|
+
|
|
65
|
+
if not isinstance(size, int):
|
|
66
|
+
raise ValueError("size must be an int.")
|
|
67
|
+
|
|
68
|
+
generated_coords = self.generate_dist[self.key_in].rvs(size=(nwalkers, size))
|
|
69
|
+
|
|
70
|
+
if fill_values is not None:
|
|
71
|
+
generated_coords[fill_tuple] = fill_values
|
|
72
|
+
|
|
73
|
+
generated_logpdf = self.special_generate_logpdf(
|
|
74
|
+
generated_coords.reshape(nwalkers * size, -1)
|
|
75
|
+
).reshape(nwalkers, size)
|
|
76
|
+
|
|
77
|
+
return generated_coords, generated_logpdf
|
|
78
|
+
|
|
79
|
+
def set_coords_and_inds(self, generated_coords, inds_leaves_rj=None):
|
|
80
|
+
"""Setup coordinates for prior and Likelihood
|
|
81
|
+
|
|
82
|
+
Args:
|
|
83
|
+
generated_coords (np.ndarray): Generated coordinates with shape ``(number of independent walkers, num_try)``.
|
|
84
|
+
inds_leaves_rj (np.ndarray): Index into each individual walker giving the
|
|
85
|
+
leaf index associated with this proposal. Should only be used if ``self.rj is True``. (default: ``None``)
|
|
86
|
+
|
|
87
|
+
Returns:
|
|
88
|
+
dict: Coordinates for Likelihood and Prior.
|
|
89
|
+
|
|
90
|
+
"""
|
|
91
|
+
coords_in = np.repeat(
|
|
92
|
+
self.current_state.branches[self.key_in].coords.reshape(
|
|
93
|
+
(1, -1,) + self.current_state.branches[self.key_in].coords.shape[-2:]
|
|
94
|
+
),
|
|
95
|
+
self.num_try,
|
|
96
|
+
axis=1,
|
|
97
|
+
)
|
|
98
|
+
coords_in[
|
|
99
|
+
(
|
|
100
|
+
np.zeros(coords_in.shape[0], dtype=int),
|
|
101
|
+
np.arange(coords_in.shape[1]),
|
|
102
|
+
np.repeat(inds_leaves_rj, self.num_try),
|
|
103
|
+
)
|
|
104
|
+
] = generated_coords.reshape(-1, coords_in.shape[-1])
|
|
105
|
+
inds_in = np.repeat(
|
|
106
|
+
self.current_state.branches[self.key_in].inds.reshape(
|
|
107
|
+
(1, -1,) + self.current_state.branches[self.key_in].inds.shape[-1:]
|
|
108
|
+
),
|
|
109
|
+
self.num_try,
|
|
110
|
+
axis=1,
|
|
111
|
+
)
|
|
112
|
+
inds_in[
|
|
113
|
+
(
|
|
114
|
+
np.zeros(coords_in.shape[0], dtype=int),
|
|
115
|
+
np.arange(inds_in.shape[1]),
|
|
116
|
+
np.repeat(inds_leaves_rj, self.num_try),
|
|
117
|
+
)
|
|
118
|
+
] = True
|
|
119
|
+
|
|
120
|
+
coords_in_dict = {}
|
|
121
|
+
inds_in_dict = {}
|
|
122
|
+
for key in self.current_state.branches.keys():
|
|
123
|
+
if key == self.key_in:
|
|
124
|
+
coords_in_dict[key] = coords_in
|
|
125
|
+
inds_in_dict[key] = inds_in
|
|
126
|
+
|
|
127
|
+
else:
|
|
128
|
+
coords_in_dict[key] = self.current_state.branches[key].coords.reshape(
|
|
129
|
+
(1, -1) + self.current_state.branches[key].shape[-2:]
|
|
130
|
+
)
|
|
131
|
+
# expand to express multiple tries
|
|
132
|
+
coords_in_dict[key] = np.tile(
|
|
133
|
+
coords_in_dict[key], (1, self.num_try, 1)
|
|
134
|
+
).reshape(
|
|
135
|
+
coords_in_dict[key].shape[0],
|
|
136
|
+
coords_in_dict[key].shape[1] * self.num_try,
|
|
137
|
+
coords_in_dict[key].shape[2],
|
|
138
|
+
coords_in_dict[key].shape[3],
|
|
139
|
+
)
|
|
140
|
+
inds_in_dict[key] = self.current_state.branches[key].inds.reshape(
|
|
141
|
+
(1, -1) + self.current_state.branches[key].shape[-2:-1]
|
|
142
|
+
)
|
|
143
|
+
# expand to express multiple tries
|
|
144
|
+
inds_in_dict[key] = np.tile(
|
|
145
|
+
inds_in_dict[key], (1, self.num_try)
|
|
146
|
+
).reshape(
|
|
147
|
+
inds_in_dict[key].shape[0],
|
|
148
|
+
inds_in_dict[key].shape[1] * self.num_try,
|
|
149
|
+
inds_in_dict[key].shape[2],
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
return coords_in_dict, inds_in_dict
|
|
153
|
+
|
|
154
|
+
def special_like_func(self, generated_coords, inds_leaves_rj=None, **kwargs):
|
|
155
|
+
"""Calculate the Likelihood for sampled points.
|
|
156
|
+
|
|
157
|
+
Args:
|
|
158
|
+
generated_coords (np.ndarray): Generated coordinates with shape ``(number of independent walkers, num_try)``.
|
|
159
|
+
inds_leaves_rj (np.ndarray): Index into each individual walker giving the
|
|
160
|
+
leaf index associated with this proposal. Should only be used if ``self.rj is True``. (default: ``None``)
|
|
161
|
+
|
|
162
|
+
Returns:
|
|
163
|
+
np.ndarray: Likelihood values with shape ``(generated_coords.shape[0], num_try).``
|
|
164
|
+
|
|
165
|
+
"""
|
|
166
|
+
coords_in, inds_in = self.set_coords_and_inds(
|
|
167
|
+
generated_coords, inds_leaves_rj=inds_leaves_rj
|
|
168
|
+
)
|
|
169
|
+
ll = self.current_model.compute_log_like_fn(coords_in, inds=inds_in)[0]
|
|
170
|
+
ll = ll[0].reshape(-1, self.num_try)
|
|
171
|
+
return ll
|
|
172
|
+
|
|
173
|
+
def special_prior_func(self, generated_coords, inds_leaves_rj=None, **kwargs):
|
|
174
|
+
"""Calculate the Prior for sampled points.
|
|
175
|
+
|
|
176
|
+
Args:
|
|
177
|
+
generated_coords (np.ndarray): Generated coordinates with shape ``(number of independent walkers, num_try)``.
|
|
178
|
+
inds_leaves_rj (np.ndarray): Index into each individual walker giving the
|
|
179
|
+
leaf index associated with this proposal. Should only be used if ``self.rj is True``. (default: ``None``)
|
|
180
|
+
|
|
181
|
+
Returns:
|
|
182
|
+
np.ndarray: Prior values with shape ``(generated_coords.shape[0], num_try).``
|
|
183
|
+
|
|
184
|
+
"""
|
|
185
|
+
coords_in, inds_in = self.set_coords_and_inds(
|
|
186
|
+
generated_coords, inds_leaves_rj=inds_leaves_rj
|
|
187
|
+
)
|
|
188
|
+
lp = self.current_model.compute_log_prior_fn(coords_in, inds=inds_in)
|
|
189
|
+
return lp.reshape(-1, self.num_try)
|
|
190
|
+
|