CUQIpy 1.1.1.post0.dev38__py3-none-any.whl → 1.1.1.post0.dev57__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.
Potentially problematic release.
This version of CUQIpy might be problematic. Click here for more details.
- {CUQIpy-1.1.1.post0.dev38.dist-info → CUQIpy-1.1.1.post0.dev57.dist-info}/METADATA +1 -1
- {CUQIpy-1.1.1.post0.dev38.dist-info → CUQIpy-1.1.1.post0.dev57.dist-info}/RECORD +7 -7
- cuqi/_version.py +3 -3
- cuqi/experimental/mcmc/_gibbs.py +78 -18
- {CUQIpy-1.1.1.post0.dev38.dist-info → CUQIpy-1.1.1.post0.dev57.dist-info}/LICENSE +0 -0
- {CUQIpy-1.1.1.post0.dev38.dist-info → CUQIpy-1.1.1.post0.dev57.dist-info}/WHEEL +0 -0
- {CUQIpy-1.1.1.post0.dev38.dist-info → CUQIpy-1.1.1.post0.dev57.dist-info}/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: CUQIpy
|
|
3
|
-
Version: 1.1.1.post0.
|
|
3
|
+
Version: 1.1.1.post0.dev57
|
|
4
4
|
Summary: Computational Uncertainty Quantification for Inverse problems in Python
|
|
5
5
|
Maintainer-email: "Nicolai A. B. Riis" <nabr@dtu.dk>, "Jakob S. Jørgensen" <jakj@dtu.dk>, "Amal M. Alghamdi" <amaal@dtu.dk>, Chao Zhang <chaz@dtu.dk>
|
|
6
6
|
License: Apache License
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
cuqi/__init__.py,sha256=LsGilhl-hBLEn6Glt8S_l0OJzAA1sKit_rui8h-D-p0,488
|
|
2
2
|
cuqi/_messages.py,sha256=fzEBrZT2kbmfecBBPm7spVu7yHdxGARQB4QzXhJbCJ0,415
|
|
3
|
-
cuqi/_version.py,sha256=
|
|
3
|
+
cuqi/_version.py,sha256=53dy_Qkm9OoHIFU5wgFEUHbx-_0QHgR5Hu1r9G4ga8c,509
|
|
4
4
|
cuqi/config.py,sha256=wcYvz19wkeKW2EKCGIKJiTpWt5kdaxyt4imyRkvtTRA,526
|
|
5
5
|
cuqi/diagnostics.py,sha256=5OrbJeqpynqRXOe5MtOKKhe7EAVdOEpHIqHnlMW9G_c,3029
|
|
6
6
|
cuqi/array/__init__.py,sha256=-EeiaiWGNsE3twRS4dD814BIlfxEsNkTCZUc5gjOXb0,30
|
|
@@ -39,7 +39,7 @@ cuqi/experimental/mcmc/_conjugate.py,sha256=VNPQkGity0mposcqxrx4UIeXm35EvJvZED4p
|
|
|
39
39
|
cuqi/experimental/mcmc/_conjugate_approx.py,sha256=uEnY2ea9su5ivcNagyRAwpQP2gBY98sXU7N0y5hTADo,3653
|
|
40
40
|
cuqi/experimental/mcmc/_cwmh.py,sha256=BQE7-4kbiC32TJ38rlVtfSQieoKBK5GDca3HCqHJ7zE,7135
|
|
41
41
|
cuqi/experimental/mcmc/_direct.py,sha256=9pQS_2Qk2-ybt6m8WTfPoKetcxQ00WaTRN85-Z0FrBY,777
|
|
42
|
-
cuqi/experimental/mcmc/_gibbs.py,sha256=
|
|
42
|
+
cuqi/experimental/mcmc/_gibbs.py,sha256=JwQQgKd-5IPVErUgMyxtG4sCVhf7gdzSr8ttn3trktY,12596
|
|
43
43
|
cuqi/experimental/mcmc/_hmc.py,sha256=h63KT0jYq86H541hQzhHHzuLEn7vrRE_2r1o1mTEcZo,19261
|
|
44
44
|
cuqi/experimental/mcmc/_langevin_algorithm.py,sha256=GKC_mlix3si_3J8E6I58ozh3ErB0XPmHMjV7sFyKLEQ,8200
|
|
45
45
|
cuqi/experimental/mcmc/_laplace_approximation.py,sha256=rdiE3cMQFq6FLQcOQwPpuGIxrTAp3aoGPxMDSdeopV0,5688
|
|
@@ -85,8 +85,8 @@ cuqi/testproblem/_testproblem.py,sha256=x769LwwRdJdzIiZkcQUGb_5-vynNTNALXWKato7s
|
|
|
85
85
|
cuqi/utilities/__init__.py,sha256=H7xpJe2UinjZftKvE2JuXtTi4DqtkR6uIezStAXwfGg,428
|
|
86
86
|
cuqi/utilities/_get_python_variable_name.py,sha256=QwlBVj2koJRA8s8pWd554p7-ElcI7HUwY32HknaR92E,1827
|
|
87
87
|
cuqi/utilities/_utilities.py,sha256=Jc4knn80vLoA7kgw9FzXwKVFGaNBOXiA9kgvltZU3Ao,11777
|
|
88
|
-
CUQIpy-1.1.1.post0.
|
|
89
|
-
CUQIpy-1.1.1.post0.
|
|
90
|
-
CUQIpy-1.1.1.post0.
|
|
91
|
-
CUQIpy-1.1.1.post0.
|
|
92
|
-
CUQIpy-1.1.1.post0.
|
|
88
|
+
CUQIpy-1.1.1.post0.dev57.dist-info/LICENSE,sha256=kJWRPrtRoQoZGXyyvu50Uc91X6_0XRaVfT0YZssicys,10799
|
|
89
|
+
CUQIpy-1.1.1.post0.dev57.dist-info/METADATA,sha256=K1z-0CsdGJFdgy_GUhJuf0G7lmo5QZKlVH6yM0xfYJI,18410
|
|
90
|
+
CUQIpy-1.1.1.post0.dev57.dist-info/WHEEL,sha256=cVxcB9AmuTcXqmwrtPhNK88dr7IR_b6qagTj0UvIEbY,91
|
|
91
|
+
CUQIpy-1.1.1.post0.dev57.dist-info/top_level.txt,sha256=AgmgMc6TKfPPqbjV0kvAoCBN334i_Lwwojc7HE3ZwD0,5
|
|
92
|
+
CUQIpy-1.1.1.post0.dev57.dist-info/RECORD,,
|
cuqi/_version.py
CHANGED
|
@@ -8,11 +8,11 @@ import json
|
|
|
8
8
|
|
|
9
9
|
version_json = '''
|
|
10
10
|
{
|
|
11
|
-
"date": "2024-09-
|
|
11
|
+
"date": "2024-09-10T22:51:38+0200",
|
|
12
12
|
"dirty": false,
|
|
13
13
|
"error": null,
|
|
14
|
-
"full-revisionid": "
|
|
15
|
-
"version": "1.1.1.post0.
|
|
14
|
+
"full-revisionid": "528481d0e961831e7e64f9dd6e48ddced1e10ae8",
|
|
15
|
+
"version": "1.1.1.post0.dev57"
|
|
16
16
|
}
|
|
17
17
|
''' # END VERSION_JSON
|
|
18
18
|
|
cuqi/experimental/mcmc/_gibbs.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
from cuqi.distribution import JointDistribution
|
|
2
2
|
from cuqi.experimental.mcmc import Sampler
|
|
3
3
|
from cuqi.samples import Samples
|
|
4
|
+
from cuqi.experimental.mcmc import NUTS
|
|
4
5
|
from typing import Dict
|
|
5
6
|
import numpy as np
|
|
6
7
|
import warnings
|
|
@@ -151,18 +152,50 @@ class HybridGibbs:
|
|
|
151
152
|
sampler.validate_target()
|
|
152
153
|
|
|
153
154
|
def sample(self, Ns) -> 'HybridGibbs':
|
|
154
|
-
""" Sample from the joint distribution using Gibbs sampling
|
|
155
|
+
""" Sample from the joint distribution using Gibbs sampling
|
|
156
|
+
|
|
157
|
+
Parameters
|
|
158
|
+
----------
|
|
159
|
+
Ns : int
|
|
160
|
+
The number of samples to draw.
|
|
161
|
+
|
|
162
|
+
"""
|
|
163
|
+
|
|
155
164
|
for _ in tqdm(range(Ns)):
|
|
165
|
+
|
|
156
166
|
self.step()
|
|
167
|
+
|
|
157
168
|
self._store_samples()
|
|
158
169
|
|
|
159
|
-
|
|
160
|
-
|
|
170
|
+
return self
|
|
171
|
+
|
|
172
|
+
def warmup(self, Nb, tune_freq=0.1) -> 'HybridGibbs':
|
|
173
|
+
""" Warmup (tune) the samplers in the Gibbs sampling scheme
|
|
174
|
+
|
|
175
|
+
Parameters
|
|
176
|
+
----------
|
|
177
|
+
Nb : int
|
|
178
|
+
The number of samples to draw during warmup.
|
|
179
|
+
|
|
180
|
+
tune_freq : float, optional
|
|
181
|
+
Frequency of tuning the samplers. Tuning is performed every tune_freq*Nb steps.
|
|
182
|
+
|
|
183
|
+
"""
|
|
184
|
+
|
|
185
|
+
tune_interval = max(int(tune_freq * Nb), 1)
|
|
186
|
+
|
|
161
187
|
for idx in tqdm(range(Nb)):
|
|
188
|
+
|
|
162
189
|
self.step()
|
|
163
|
-
|
|
190
|
+
|
|
191
|
+
# Tune the sampler at tuning intervals (matching behavior of Sampler class)
|
|
192
|
+
if (idx + 1) % tune_interval == 0:
|
|
193
|
+
self.tune(tune_interval, idx // tune_interval)
|
|
194
|
+
|
|
164
195
|
self._store_samples()
|
|
165
196
|
|
|
197
|
+
return self
|
|
198
|
+
|
|
166
199
|
def get_samples(self) -> Dict[str, Samples]:
|
|
167
200
|
samples_object = {}
|
|
168
201
|
for par_name in self.par_names:
|
|
@@ -182,38 +215,65 @@ class HybridGibbs:
|
|
|
182
215
|
# Get sampler
|
|
183
216
|
sampler = self.samplers[par_name]
|
|
184
217
|
|
|
185
|
-
#
|
|
186
|
-
# This
|
|
187
|
-
#
|
|
188
|
-
#
|
|
189
|
-
|
|
190
|
-
|
|
218
|
+
# Instead of simply changing the target of the sampler, we reinitialize it.
|
|
219
|
+
# This is to ensure that all internal variables are set to match the new target.
|
|
220
|
+
# To return the sampler to the old state and history, we first extract the state and history
|
|
221
|
+
# before reinitializing the sampler and then set the state and history back to the sampler
|
|
222
|
+
|
|
223
|
+
# Extract state and history from sampler
|
|
224
|
+
if isinstance(sampler, NUTS): # Special case for NUTS as it is not playing nice with get_state and get_history
|
|
225
|
+
sampler.initial_point = sampler.current_point
|
|
226
|
+
else:
|
|
227
|
+
sampler_state = sampler.get_state()
|
|
228
|
+
sampler_history = sampler.get_history()
|
|
191
229
|
|
|
192
230
|
# Reinitialize sampler
|
|
193
|
-
# This makes the sampler lose all of its state.
|
|
194
|
-
# This is only OK because we set the initial values above from the previous state
|
|
195
231
|
sampler.reinitialize()
|
|
196
232
|
|
|
233
|
+
# Set state and history back to sampler
|
|
234
|
+
if not isinstance(sampler, NUTS): # Again, special case for NUTS.
|
|
235
|
+
sampler.set_state(sampler_state)
|
|
236
|
+
sampler.set_history(sampler_history)
|
|
237
|
+
|
|
197
238
|
# Run pre_warmup and pre_sample methods for sampler
|
|
198
239
|
# TODO. Some samplers (NUTS) seem to require to run _pre_warmup before _pre_sample
|
|
199
240
|
self._pre_warmup_and_pre_sample_sampler(sampler)
|
|
200
241
|
|
|
201
|
-
#
|
|
242
|
+
# Allow for multiple sampling steps in each Gibbs step
|
|
202
243
|
for _ in range(self.num_sampling_steps[par_name]):
|
|
203
|
-
|
|
244
|
+
# Sampling step
|
|
245
|
+
acc = sampler.step()
|
|
246
|
+
|
|
247
|
+
# Store acceptance rate in sampler (matching behavior of Sampler class Sample method)
|
|
248
|
+
sampler._acc.append(acc)
|
|
204
249
|
|
|
205
250
|
# Extract samples (Ensure even 1-dimensional samples are 1D arrays)
|
|
206
|
-
|
|
251
|
+
if isinstance(sampler.current_point, np.ndarray):
|
|
252
|
+
self.current_samples[par_name] = sampler.current_point.reshape(-1)
|
|
253
|
+
else:
|
|
254
|
+
self.current_samples[par_name] = sampler.current_point
|
|
255
|
+
|
|
256
|
+
def tune(self, skip_len, update_count):
|
|
257
|
+
""" Run a single tuning step on each of the samplers in the Gibbs sampling scheme
|
|
258
|
+
|
|
259
|
+
Parameters
|
|
260
|
+
----------
|
|
261
|
+
skip_len : int
|
|
262
|
+
Defines the number of steps in between tuning (i.e. the tuning interval).
|
|
263
|
+
|
|
264
|
+
update_count : int
|
|
265
|
+
The number of times tuning has been performed. Can be used for internal bookkeeping.
|
|
207
266
|
|
|
208
|
-
|
|
209
|
-
""" Tune each of the samplers """
|
|
267
|
+
"""
|
|
210
268
|
for par_name in self.par_names:
|
|
211
|
-
self.samplers[par_name].tune(skip_len=
|
|
269
|
+
self.samplers[par_name].tune(skip_len=skip_len, update_count=update_count)
|
|
212
270
|
|
|
213
271
|
# ------------ Private methods ------------
|
|
214
272
|
def _initialize_samplers(self):
|
|
215
273
|
""" Initialize samplers """
|
|
216
274
|
for sampler in self.samplers.values():
|
|
275
|
+
if isinstance(sampler, NUTS):
|
|
276
|
+
print(f'Warning: NUTS sampler is not fully stateful in HybridGibbs. Sampler will be reinitialized in each Gibbs step.')
|
|
217
277
|
sampler.initialize()
|
|
218
278
|
|
|
219
279
|
def _initialize_num_sampling_steps(self):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|