CUQIpy 1.4.0.post0.dev13__py3-none-any.whl → 1.4.0.post0.dev45__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.
- cuqi/__init__.py +1 -0
- cuqi/_version.py +3 -3
- cuqi/experimental/__init__.py +1 -2
- cuqi/experimental/_recommender.py +4 -4
- cuqi/legacy/__init__.py +2 -0
- cuqi/legacy/sampler/__init__.py +11 -0
- cuqi/legacy/sampler/_conjugate.py +55 -0
- cuqi/legacy/sampler/_conjugate_approx.py +52 -0
- cuqi/legacy/sampler/_cwmh.py +196 -0
- cuqi/legacy/sampler/_gibbs.py +231 -0
- cuqi/legacy/sampler/_hmc.py +335 -0
- cuqi/legacy/sampler/_langevin_algorithm.py +198 -0
- cuqi/legacy/sampler/_laplace_approximation.py +184 -0
- cuqi/legacy/sampler/_mh.py +190 -0
- cuqi/legacy/sampler/_pcn.py +244 -0
- cuqi/legacy/sampler/_rto.py +284 -0
- cuqi/legacy/sampler/_sampler.py +182 -0
- cuqi/problem/_problem.py +87 -80
- cuqi/sampler/__init__.py +120 -8
- cuqi/sampler/_conjugate.py +376 -35
- cuqi/sampler/_conjugate_approx.py +40 -16
- cuqi/sampler/_cwmh.py +132 -138
- cuqi/{experimental/mcmc → sampler}/_direct.py +1 -1
- cuqi/sampler/_gibbs.py +269 -130
- cuqi/sampler/_hmc.py +328 -201
- cuqi/sampler/_langevin_algorithm.py +282 -98
- cuqi/sampler/_laplace_approximation.py +87 -117
- cuqi/sampler/_mh.py +47 -157
- cuqi/sampler/_pcn.py +56 -211
- cuqi/sampler/_rto.py +206 -140
- cuqi/sampler/_sampler.py +540 -135
- {cuqipy-1.4.0.post0.dev13.dist-info → cuqipy-1.4.0.post0.dev45.dist-info}/METADATA +1 -1
- {cuqipy-1.4.0.post0.dev13.dist-info → cuqipy-1.4.0.post0.dev45.dist-info}/RECORD +36 -35
- cuqi/experimental/mcmc/__init__.py +0 -122
- cuqi/experimental/mcmc/_conjugate.py +0 -396
- cuqi/experimental/mcmc/_conjugate_approx.py +0 -76
- cuqi/experimental/mcmc/_cwmh.py +0 -190
- cuqi/experimental/mcmc/_gibbs.py +0 -366
- cuqi/experimental/mcmc/_hmc.py +0 -462
- cuqi/experimental/mcmc/_langevin_algorithm.py +0 -382
- cuqi/experimental/mcmc/_laplace_approximation.py +0 -154
- cuqi/experimental/mcmc/_mh.py +0 -80
- cuqi/experimental/mcmc/_pcn.py +0 -89
- cuqi/experimental/mcmc/_rto.py +0 -350
- cuqi/experimental/mcmc/_sampler.py +0 -582
- {cuqipy-1.4.0.post0.dev13.dist-info → cuqipy-1.4.0.post0.dev45.dist-info}/WHEEL +0 -0
- {cuqipy-1.4.0.post0.dev13.dist-info → cuqipy-1.4.0.post0.dev45.dist-info}/licenses/LICENSE +0 -0
- {cuqipy-1.4.0.post0.dev13.dist-info → cuqipy-1.4.0.post0.dev45.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import scipy as sp
|
|
2
|
+
import numpy as np
|
|
3
|
+
import cuqi
|
|
4
|
+
from cuqi.distribution import Normal
|
|
5
|
+
from cuqi.solver import CGLS
|
|
6
|
+
from cuqi.legacy.sampler import Sampler
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class UGLA(Sampler):
|
|
10
|
+
""" Unadjusted (Gaussian) Laplace Approximation sampler
|
|
11
|
+
|
|
12
|
+
Samples an approximate posterior where the prior is approximated
|
|
13
|
+
by a Gaussian distribution. The likelihood must be Gaussian.
|
|
14
|
+
|
|
15
|
+
Currently only works for LMRF priors.
|
|
16
|
+
|
|
17
|
+
The inner solver is Conjugate Gradient Least Squares (CGLS) solver.
|
|
18
|
+
|
|
19
|
+
For more details see: Uribe, Felipe, et al. "A hybrid Gibbs sampler for edge-preserving
|
|
20
|
+
tomographic reconstruction with uncertain view angles." arXiv preprint arXiv:2104.06919 (2021).
|
|
21
|
+
|
|
22
|
+
Parameters
|
|
23
|
+
----------
|
|
24
|
+
target : `cuqi.distribution.Posterior`
|
|
25
|
+
The target posterior distribution to sample.
|
|
26
|
+
|
|
27
|
+
x0 : ndarray
|
|
28
|
+
Initial parameters. *Optional*
|
|
29
|
+
|
|
30
|
+
maxit : int
|
|
31
|
+
Maximum number of inner iterations for solver when generating one sample.
|
|
32
|
+
|
|
33
|
+
tol : float
|
|
34
|
+
Tolerance for inner solver. Will stop before maxit if the inner solvers convergence check reaches tol.
|
|
35
|
+
|
|
36
|
+
beta : float
|
|
37
|
+
Smoothing parameter for the Gaussian approximation of the Laplace distribution. Larger beta is easier to sample but is a worse approximation.
|
|
38
|
+
|
|
39
|
+
rng : np.random.RandomState
|
|
40
|
+
Random number generator used for sampling. *Optional*
|
|
41
|
+
|
|
42
|
+
callback : callable, *Optional*
|
|
43
|
+
If set this function will be called after every sample.
|
|
44
|
+
The signature of the callback function is `callback(sample, sample_index)`,
|
|
45
|
+
where `sample` is the current sample and `sample_index` is the index of the sample.
|
|
46
|
+
An example is shown in demos/demo31_callback.py.
|
|
47
|
+
|
|
48
|
+
Returns
|
|
49
|
+
-------
|
|
50
|
+
cuqi.samples.Samples
|
|
51
|
+
Samples from the posterior distribution.
|
|
52
|
+
|
|
53
|
+
"""
|
|
54
|
+
|
|
55
|
+
def __init__(self, target, x0=None, maxit=50, tol=1e-4, beta=1e-5, rng=None, **kwargs):
|
|
56
|
+
|
|
57
|
+
super().__init__(target, x0=x0, **kwargs)
|
|
58
|
+
|
|
59
|
+
# Check target type
|
|
60
|
+
if not isinstance(self.target, cuqi.distribution.Posterior):
|
|
61
|
+
raise ValueError(f"To initialize an object of type {self.__class__}, 'target' need to be of type 'cuqi.distribution.Posterior'.")
|
|
62
|
+
|
|
63
|
+
# Check Affine model
|
|
64
|
+
if not isinstance(self.target.likelihood.model, cuqi.model.AffineModel):
|
|
65
|
+
raise TypeError("Model needs to be affine or linear")
|
|
66
|
+
|
|
67
|
+
# Check Gaussian likelihood
|
|
68
|
+
if not hasattr(self.target.likelihood.distribution, "sqrtprec"):
|
|
69
|
+
raise TypeError("Distribution in Likelihood must contain a sqrtprec attribute")
|
|
70
|
+
|
|
71
|
+
# Check that prior is LMRF
|
|
72
|
+
if not isinstance(self.target.prior, cuqi.distribution.LMRF):
|
|
73
|
+
raise ValueError('Unadjusted Gaussian Laplace approximation (UGLA) requires LMRF prior')
|
|
74
|
+
|
|
75
|
+
# Modify initial guess since Sampler sets it to ones.
|
|
76
|
+
if x0 is not None:
|
|
77
|
+
self.x0 = x0
|
|
78
|
+
else:
|
|
79
|
+
self.x0 = np.zeros(self.target.prior.dim)
|
|
80
|
+
|
|
81
|
+
# Store internal parameters
|
|
82
|
+
self.maxit = maxit
|
|
83
|
+
self.tol = tol
|
|
84
|
+
self.beta = beta
|
|
85
|
+
self.rng = rng
|
|
86
|
+
|
|
87
|
+
def _sample_adapt(self, Ns, Nb):
|
|
88
|
+
return self._sample(Ns, Nb)
|
|
89
|
+
|
|
90
|
+
def _sample(self, Ns, Nb):
|
|
91
|
+
""" Sample from the approximate posterior.
|
|
92
|
+
|
|
93
|
+
Parameters
|
|
94
|
+
----------
|
|
95
|
+
Ns : int
|
|
96
|
+
Number of samples to draw.
|
|
97
|
+
|
|
98
|
+
Nb : int
|
|
99
|
+
Number of burn-in samples to discard.
|
|
100
|
+
|
|
101
|
+
Returns
|
|
102
|
+
-------
|
|
103
|
+
samples : ndarray
|
|
104
|
+
Samples from the approximate posterior.
|
|
105
|
+
|
|
106
|
+
target_eval : ndarray
|
|
107
|
+
Log-likelihood of each sample.
|
|
108
|
+
|
|
109
|
+
acc : ndarray
|
|
110
|
+
Acceptance rate of each sample.
|
|
111
|
+
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
# Extract diff_op from target prior
|
|
115
|
+
D = self.target.prior._diff_op
|
|
116
|
+
n = D.shape[0]
|
|
117
|
+
|
|
118
|
+
# Gaussian approximation of LMRF prior as function of x_k
|
|
119
|
+
def Lk_fun(x_k):
|
|
120
|
+
dd = 1/np.sqrt((D @ x_k)**2 + self.beta*np.ones(n))
|
|
121
|
+
W = sp.sparse.diags(dd)
|
|
122
|
+
return W.sqrt() @ D
|
|
123
|
+
|
|
124
|
+
# Now prepare "LinearRTO" type sampler. TODO: Use LinearRTO for this instead
|
|
125
|
+
self._shift = 0
|
|
126
|
+
|
|
127
|
+
# Pre-computations
|
|
128
|
+
self._model = self.target.likelihood.model
|
|
129
|
+
self._data = self.target.likelihood.data - self.target.model._shift
|
|
130
|
+
self._m = len(self._data)
|
|
131
|
+
self._L1 = self.target.likelihood.distribution.sqrtprec
|
|
132
|
+
|
|
133
|
+
# If prior location is scalar, repeat it to match dimensions
|
|
134
|
+
if len(self.target.prior.location) == 1:
|
|
135
|
+
self._priorloc = np.repeat(self.target.prior.location, self.dim)
|
|
136
|
+
else:
|
|
137
|
+
self._priorloc = self.target.prior.location
|
|
138
|
+
|
|
139
|
+
# Initial Laplace approx
|
|
140
|
+
self._L2 = Lk_fun(self.x0)
|
|
141
|
+
self._L2mu = self._L2@self._priorloc
|
|
142
|
+
self._b_tild = np.hstack([self._L1@self._data, self._L2mu])
|
|
143
|
+
|
|
144
|
+
#self.n = len(self.x0)
|
|
145
|
+
|
|
146
|
+
# Least squares form
|
|
147
|
+
def M(x, flag):
|
|
148
|
+
if flag == 1:
|
|
149
|
+
out1 = self._L1 @ self._model._forward_func_no_shift(x) # Use forward function which excludes shift
|
|
150
|
+
out2 = np.sqrt(1/self.target.prior.scale)*(self._L2 @ x)
|
|
151
|
+
out = np.hstack([out1, out2])
|
|
152
|
+
elif flag == 2:
|
|
153
|
+
idx = int(self._m)
|
|
154
|
+
out1 = self._model._adjoint_func_no_shift(self._L1.T@x[:idx])
|
|
155
|
+
out2 = np.sqrt(1/self.target.prior.scale)*(self._L2.T @ x[idx:])
|
|
156
|
+
out = out1 + out2
|
|
157
|
+
return out
|
|
158
|
+
|
|
159
|
+
# Initialize samples
|
|
160
|
+
N = Ns+Nb # number of simulations
|
|
161
|
+
samples = np.empty((self.target.dim, N))
|
|
162
|
+
|
|
163
|
+
# initial state
|
|
164
|
+
samples[:, 0] = self.x0
|
|
165
|
+
for s in range(N-1):
|
|
166
|
+
|
|
167
|
+
# Update Laplace approximation
|
|
168
|
+
self._L2 = Lk_fun(samples[:, s])
|
|
169
|
+
self._L2mu = self._L2@self._priorloc
|
|
170
|
+
self._b_tild = np.hstack([self._L1@self._data, self._L2mu])
|
|
171
|
+
|
|
172
|
+
# Sample from approximate posterior
|
|
173
|
+
e = Normal(mean=np.zeros(len(self._b_tild)), std=1).sample(rng=self.rng)
|
|
174
|
+
y = self._b_tild + e # Perturb data
|
|
175
|
+
sim = CGLS(M, y, samples[:, s], self.maxit, self.tol, self._shift)
|
|
176
|
+
samples[:, s+1], _ = sim.solve()
|
|
177
|
+
|
|
178
|
+
self._print_progress(s+2,N) #s+2 is the sample number, s+1 is index assuming x0 is the first sample
|
|
179
|
+
self._call_callback(samples[:, s+1], s+1)
|
|
180
|
+
|
|
181
|
+
# remove burn-in
|
|
182
|
+
samples = samples[:, Nb:]
|
|
183
|
+
|
|
184
|
+
return samples, None, None
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import cuqi
|
|
3
|
+
from cuqi.legacy.sampler import ProposalBasedSampler
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class MH(ProposalBasedSampler):
|
|
7
|
+
"""Metropolis Hastings sampler.
|
|
8
|
+
|
|
9
|
+
Allows sampling of a target distribution by random-walk sampling of a proposal distribution along with an accept/reject step.
|
|
10
|
+
|
|
11
|
+
Parameters
|
|
12
|
+
----------
|
|
13
|
+
|
|
14
|
+
target : `cuqi.distribution.Distribution` or lambda function
|
|
15
|
+
The target distribution to sample. Custom logpdfs are supported by using a :class:`cuqi.distribution.UserDefinedDistribution`.
|
|
16
|
+
|
|
17
|
+
proposal : `cuqi.distribution.Distribution` or callable method
|
|
18
|
+
The proposal to sample from. If a callable method it should provide a single independent sample from proposal distribution. Defaults to a Gaussian proposal. *Optional*.
|
|
19
|
+
|
|
20
|
+
scale : float
|
|
21
|
+
Scale parameter used to define correlation between previous and proposed sample in random-walk. *Optional*.
|
|
22
|
+
|
|
23
|
+
x0 : ndarray
|
|
24
|
+
Initial parameters. *Optional*
|
|
25
|
+
|
|
26
|
+
dim : int
|
|
27
|
+
Dimension of parameter space. Required if target and proposal are callable functions. *Optional*.
|
|
28
|
+
|
|
29
|
+
callback : callable, *Optional*
|
|
30
|
+
If set this function will be called after every sample.
|
|
31
|
+
The signature of the callback function is `callback(sample, sample_index)`,
|
|
32
|
+
where `sample` is the current sample and `sample_index` is the index of the sample.
|
|
33
|
+
An example is shown in demos/demo31_callback.py.
|
|
34
|
+
|
|
35
|
+
Example
|
|
36
|
+
-------
|
|
37
|
+
.. code-block:: python
|
|
38
|
+
|
|
39
|
+
# Parameters
|
|
40
|
+
dim = 5 # Dimension of distribution
|
|
41
|
+
mu = np.arange(dim) # Mean of Gaussian
|
|
42
|
+
std = 1 # standard deviation of Gaussian
|
|
43
|
+
|
|
44
|
+
# Logpdf function
|
|
45
|
+
logpdf_func = lambda x: -1/(std**2)*np.sum((x-mu)**2)
|
|
46
|
+
|
|
47
|
+
# Define distribution from logpdf as UserDefinedDistribution (sample and gradients also supported)
|
|
48
|
+
target = cuqi.distribution.UserDefinedDistribution(dim=dim, logpdf_func=logpdf_func)
|
|
49
|
+
|
|
50
|
+
# Set up sampler
|
|
51
|
+
sampler = cuqi.legacy.sampler.MH(target, scale=1)
|
|
52
|
+
|
|
53
|
+
# Sample
|
|
54
|
+
samples = sampler.sample(2000)
|
|
55
|
+
|
|
56
|
+
"""
|
|
57
|
+
#target, proposal=None, scale=1, x0=None, dim=None
|
|
58
|
+
# super().__init__(target, proposal=proposal, scale=scale, x0=x0, dim=dim)
|
|
59
|
+
def __init__(self, target, proposal=None, scale=None, x0=None, dim=None, **kwargs):
|
|
60
|
+
""" Metropolis-Hastings (MH) sampler. Default (if proposal is None) is random walk MH with proposal that is Gaussian with identity covariance"""
|
|
61
|
+
super().__init__(target, proposal=proposal, scale=scale, x0=x0, dim=dim, **kwargs)
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@ProposalBasedSampler.proposal.setter
|
|
65
|
+
def proposal(self, value):
|
|
66
|
+
fail_msg = "Proposal should be either None, symmetric cuqi.distribution.Distribution or a lambda function."
|
|
67
|
+
|
|
68
|
+
if value is None:
|
|
69
|
+
self._proposal = cuqi.distribution.Gaussian(np.zeros(self.dim), 1)
|
|
70
|
+
elif not isinstance(value, cuqi.distribution.Distribution) and callable(value):
|
|
71
|
+
raise NotImplementedError(fail_msg)
|
|
72
|
+
elif isinstance(value, cuqi.distribution.Distribution) and value.is_symmetric:
|
|
73
|
+
self._proposal = value
|
|
74
|
+
else:
|
|
75
|
+
raise ValueError(fail_msg)
|
|
76
|
+
self._proposal.geometry = self.target.geometry
|
|
77
|
+
|
|
78
|
+
def _sample(self, N, Nb):
|
|
79
|
+
if self.scale is None:
|
|
80
|
+
raise ValueError("Scale must be set to sample without adaptation. Consider using sample_adapt instead.")
|
|
81
|
+
|
|
82
|
+
Ns = N+Nb # number of simulations
|
|
83
|
+
|
|
84
|
+
# allocation
|
|
85
|
+
samples = np.empty((self.dim, Ns))
|
|
86
|
+
target_eval = np.empty(Ns)
|
|
87
|
+
acc = np.zeros(Ns, dtype=int)
|
|
88
|
+
|
|
89
|
+
# initial state
|
|
90
|
+
samples[:, 0] = self.x0
|
|
91
|
+
target_eval[0] = self.target.logd(self.x0)
|
|
92
|
+
acc[0] = 1
|
|
93
|
+
|
|
94
|
+
# run MCMC
|
|
95
|
+
for s in range(Ns-1):
|
|
96
|
+
# run component by component
|
|
97
|
+
samples[:, s+1], target_eval[s+1], acc[s+1] = self.single_update(samples[:, s], target_eval[s])
|
|
98
|
+
self._print_progress(s+2,Ns) #s+2 is the sample number, s+1 is index assuming x0 is the first sample
|
|
99
|
+
self._call_callback(samples[:, s+1], s+1)
|
|
100
|
+
|
|
101
|
+
# remove burn-in
|
|
102
|
+
samples = samples[:, Nb:]
|
|
103
|
+
target_eval = target_eval[Nb:]
|
|
104
|
+
accave = acc[Nb:].mean()
|
|
105
|
+
print('\nAverage acceptance rate:', accave, '\n')
|
|
106
|
+
#
|
|
107
|
+
return samples, target_eval, accave
|
|
108
|
+
|
|
109
|
+
def _sample_adapt(self, N, Nb):
|
|
110
|
+
# Set intial scale if not set
|
|
111
|
+
if self.scale is None:
|
|
112
|
+
self.scale = 0.1
|
|
113
|
+
|
|
114
|
+
Ns = N+Nb # number of simulations
|
|
115
|
+
|
|
116
|
+
# allocation
|
|
117
|
+
samples = np.empty((self.dim, Ns))
|
|
118
|
+
target_eval = np.empty(Ns)
|
|
119
|
+
acc = np.zeros(Ns)
|
|
120
|
+
|
|
121
|
+
# initial state
|
|
122
|
+
samples[:, 0] = self.x0
|
|
123
|
+
target_eval[0] = self.target.logd(self.x0)
|
|
124
|
+
acc[0] = 1
|
|
125
|
+
|
|
126
|
+
# initial adaptation params
|
|
127
|
+
Na = int(0.1*N) # iterations to adapt
|
|
128
|
+
hat_acc = np.empty(int(np.floor(Ns/Na))) # average acceptance rate of the chains
|
|
129
|
+
lambd = self.scale
|
|
130
|
+
star_acc = 0.234 # target acceptance rate RW
|
|
131
|
+
i, idx = 0, 0
|
|
132
|
+
|
|
133
|
+
# run MCMC
|
|
134
|
+
for s in range(Ns-1):
|
|
135
|
+
# run component by component
|
|
136
|
+
samples[:, s+1], target_eval[s+1], acc[s+1] = self.single_update(samples[:, s], target_eval[s])
|
|
137
|
+
|
|
138
|
+
# adapt prop spread using acc of past samples
|
|
139
|
+
if ((s+1) % Na == 0):
|
|
140
|
+
# evaluate average acceptance rate
|
|
141
|
+
hat_acc[i] = np.mean(acc[idx:idx+Na])
|
|
142
|
+
|
|
143
|
+
# d. compute new scaling parameter
|
|
144
|
+
zeta = 1/np.sqrt(i+1) # ensures that the variation of lambda(i) vanishes
|
|
145
|
+
lambd = np.exp(np.log(lambd) + zeta*(hat_acc[i]-star_acc))
|
|
146
|
+
|
|
147
|
+
# update parameters
|
|
148
|
+
self.scale = min(lambd, 1)
|
|
149
|
+
|
|
150
|
+
# update counters
|
|
151
|
+
i += 1
|
|
152
|
+
idx += Na
|
|
153
|
+
|
|
154
|
+
# display iterations
|
|
155
|
+
self._print_progress(s+2,Ns) #s+2 is the sample number, s+1 is index assuming x0 is the first sample
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
# remove burn-in
|
|
159
|
+
samples = samples[:, Nb:]
|
|
160
|
+
target_eval = target_eval[Nb:]
|
|
161
|
+
accave = acc[Nb:].mean()
|
|
162
|
+
print('\nAverage acceptance rate:', accave, 'MCMC scale:', self.scale, '\n')
|
|
163
|
+
|
|
164
|
+
return samples, target_eval, accave
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def single_update(self, x_t, target_eval_t):
|
|
168
|
+
# propose state
|
|
169
|
+
xi = self.proposal.sample(1) # sample from the proposal
|
|
170
|
+
x_star = x_t + self.scale*xi.flatten() # MH proposal
|
|
171
|
+
|
|
172
|
+
# evaluate target
|
|
173
|
+
target_eval_star = self.target.logd(x_star)
|
|
174
|
+
|
|
175
|
+
# ratio and acceptance probability
|
|
176
|
+
ratio = target_eval_star - target_eval_t # proposal is symmetric
|
|
177
|
+
alpha = min(0, ratio)
|
|
178
|
+
|
|
179
|
+
# accept/reject
|
|
180
|
+
u_theta = np.log(np.random.rand())
|
|
181
|
+
if (u_theta <= alpha):
|
|
182
|
+
x_next = x_star
|
|
183
|
+
target_eval_next = target_eval_star
|
|
184
|
+
acc = 1
|
|
185
|
+
else:
|
|
186
|
+
x_next = x_t
|
|
187
|
+
target_eval_next = target_eval_t
|
|
188
|
+
acc = 0
|
|
189
|
+
|
|
190
|
+
return x_next, target_eval_next, acc
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import cuqi
|
|
3
|
+
from cuqi.legacy.sampler import Sampler
|
|
4
|
+
|
|
5
|
+
class pCN(Sampler):
|
|
6
|
+
#Samples target*proposal
|
|
7
|
+
#TODO. Check proposal, needs to be Gaussian and zero mean.
|
|
8
|
+
"""Preconditioned Crank-Nicolson sampler
|
|
9
|
+
|
|
10
|
+
Parameters
|
|
11
|
+
----------
|
|
12
|
+
target : `cuqi.distribution.Posterior` or tuple of likelihood and prior objects
|
|
13
|
+
If target is of type cuqi.distribution.Posterior, it represents the posterior distribution.
|
|
14
|
+
If target is a tuple of (cuqi.likelihood.Likelihood, cuqi.distribution.Distribution) objects,
|
|
15
|
+
the first element is considered the likelihood and the second is considered the prior.
|
|
16
|
+
|
|
17
|
+
scale : int
|
|
18
|
+
|
|
19
|
+
x0 : `np.ndarray`
|
|
20
|
+
Initial point for the sampler
|
|
21
|
+
|
|
22
|
+
callback : callable, *Optional*
|
|
23
|
+
If set this function will be called after every sample.
|
|
24
|
+
The signature of the callback function is `callback(sample, sample_index)`,
|
|
25
|
+
where `sample` is the current sample and `sample_index` is the index of the sample.
|
|
26
|
+
An example is shown in demos/demo31_callback.py.
|
|
27
|
+
|
|
28
|
+
Example
|
|
29
|
+
-------
|
|
30
|
+
|
|
31
|
+
This uses a custom logpdf and sample function.
|
|
32
|
+
|
|
33
|
+
.. code-block:: python
|
|
34
|
+
|
|
35
|
+
# Parameters
|
|
36
|
+
dim = 5 # Dimension of distribution
|
|
37
|
+
mu = np.arange(dim) # Mean of Gaussian
|
|
38
|
+
std = 1 # standard deviation of Gaussian
|
|
39
|
+
|
|
40
|
+
# Logpdf function of likelihood
|
|
41
|
+
logpdf_func = lambda x: -1/(std**2)*np.sum((x-mu)**2)
|
|
42
|
+
|
|
43
|
+
# sample function of prior N(0,I)
|
|
44
|
+
sample_func = lambda : 0 + 1*np.random.randn(dim,1)
|
|
45
|
+
|
|
46
|
+
# Define as UserDefinedDistributions
|
|
47
|
+
likelihood = cuqi.likelihood.UserDefinedLikelihood(dim=dim, logpdf_func=logpdf_func)
|
|
48
|
+
prior = cuqi.distribution.UserDefinedDistribution(dim=dim, sample_func=sample_func)
|
|
49
|
+
|
|
50
|
+
# Set up sampler
|
|
51
|
+
sampler = cuqi.legacy.sampler.pCN((likelihood,prior), scale = 0.1)
|
|
52
|
+
|
|
53
|
+
# Sample
|
|
54
|
+
samples = sampler.sample(5000)
|
|
55
|
+
|
|
56
|
+
Example
|
|
57
|
+
-------
|
|
58
|
+
|
|
59
|
+
This uses CUQIpy distributions.
|
|
60
|
+
|
|
61
|
+
.. code-block:: python
|
|
62
|
+
|
|
63
|
+
# Parameters
|
|
64
|
+
dim = 5 # Dimension of distribution
|
|
65
|
+
mu = np.arange(dim) # Mean of Gaussian
|
|
66
|
+
std = 1 # standard deviation of Gaussian
|
|
67
|
+
|
|
68
|
+
# Define as UserDefinedDistributions
|
|
69
|
+
model = cuqi.model.Model(lambda x: x, range_geometry=dim, domain_geometry=dim)
|
|
70
|
+
likelihood = cuqi.distribution.Gaussian(mean=model, cov=np.ones(dim)).to_likelihood(mu)
|
|
71
|
+
prior = cuqi.distribution.Gaussian(mean=np.zeros(dim), cov=1)
|
|
72
|
+
|
|
73
|
+
target = cuqi.distribution.Posterior(likelihood, prior)
|
|
74
|
+
|
|
75
|
+
# Set up sampler
|
|
76
|
+
sampler = cuqi.legacy.sampler.pCN(target, scale = 0.1)
|
|
77
|
+
|
|
78
|
+
# Sample
|
|
79
|
+
samples = sampler.sample(5000)
|
|
80
|
+
|
|
81
|
+
"""
|
|
82
|
+
def __init__(self, target, scale=None, x0=None, **kwargs):
|
|
83
|
+
super().__init__(target, x0=x0, dim=None, **kwargs)
|
|
84
|
+
self.scale = scale
|
|
85
|
+
|
|
86
|
+
@property
|
|
87
|
+
def prior(self):
|
|
88
|
+
if isinstance(self.target, cuqi.distribution.Posterior):
|
|
89
|
+
return self.target.prior
|
|
90
|
+
elif isinstance(self.target,tuple) and len(self.target)==2:
|
|
91
|
+
return self.target[1]
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def likelihood(self):
|
|
95
|
+
if isinstance(self.target, cuqi.distribution.Posterior):
|
|
96
|
+
return self.target.likelihood
|
|
97
|
+
elif isinstance(self.target,tuple) and len(self.target)==2:
|
|
98
|
+
return self.target[0]
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
@Sampler.target.setter
|
|
102
|
+
def target(self, value):
|
|
103
|
+
if isinstance(value, cuqi.distribution.Posterior):
|
|
104
|
+
self._target = value
|
|
105
|
+
self._loglikelihood = lambda x : self.likelihood.logd(x)
|
|
106
|
+
elif isinstance(value,tuple) and len(value)==2 and \
|
|
107
|
+
(isinstance(value[0], cuqi.likelihood.Likelihood) or isinstance(value[0], cuqi.likelihood.UserDefinedLikelihood)) and \
|
|
108
|
+
isinstance(value[1], cuqi.distribution.Distribution):
|
|
109
|
+
self._target = value
|
|
110
|
+
self._loglikelihood = lambda x : self.likelihood.logd(x)
|
|
111
|
+
else:
|
|
112
|
+
raise ValueError(f"To initialize an object of type {self.__class__}, 'target' need to be of type 'cuqi.distribution.Posterior'.")
|
|
113
|
+
|
|
114
|
+
#TODO:
|
|
115
|
+
#if not isinstance(self.prior,(cuqi.distribution.Gaussian, cuqi.distribution.Normal)):
|
|
116
|
+
# raise ValueError("The prior distribution of the target need to be Gaussian")
|
|
117
|
+
|
|
118
|
+
@property
|
|
119
|
+
def dim(self):
|
|
120
|
+
if hasattr(self,'target') and hasattr(self.target,'dim'):
|
|
121
|
+
self._dim = self.target.dim
|
|
122
|
+
elif hasattr(self,'target') and isinstance(self.target,tuple) and len(self.target)==2:
|
|
123
|
+
self._dim = self.target[0].dim
|
|
124
|
+
return self._dim
|
|
125
|
+
|
|
126
|
+
def _sample(self, N, Nb):
|
|
127
|
+
if self.scale is None:
|
|
128
|
+
raise ValueError("Scale must be set to sample without adaptation. Consider using sample_adapt instead.")
|
|
129
|
+
|
|
130
|
+
Ns = N+Nb # number of simulations
|
|
131
|
+
|
|
132
|
+
# allocation
|
|
133
|
+
samples = np.empty((self.dim, Ns))
|
|
134
|
+
loglike_eval = np.empty(Ns)
|
|
135
|
+
acc = np.zeros(Ns, dtype=int)
|
|
136
|
+
|
|
137
|
+
# initial state
|
|
138
|
+
samples[:, 0] = self.x0
|
|
139
|
+
loglike_eval[0] = self._loglikelihood(self.x0)
|
|
140
|
+
acc[0] = 1
|
|
141
|
+
|
|
142
|
+
# run MCMC
|
|
143
|
+
for s in range(Ns-1):
|
|
144
|
+
# run component by component
|
|
145
|
+
samples[:, s+1], loglike_eval[s+1], acc[s+1] = self.single_update(samples[:, s], loglike_eval[s])
|
|
146
|
+
|
|
147
|
+
self._print_progress(s+2,Ns) #s+2 is the sample number, s+1 is index assuming x0 is the first sample
|
|
148
|
+
self._call_callback(samples[:, s+1], s+1)
|
|
149
|
+
|
|
150
|
+
# remove burn-in
|
|
151
|
+
samples = samples[:, Nb:]
|
|
152
|
+
loglike_eval = loglike_eval[Nb:]
|
|
153
|
+
accave = acc[Nb:].mean()
|
|
154
|
+
print('\nAverage acceptance rate:', accave, '\n')
|
|
155
|
+
#
|
|
156
|
+
return samples, loglike_eval, accave
|
|
157
|
+
|
|
158
|
+
def _sample_adapt(self, N, Nb):
|
|
159
|
+
# Set intial scale if not set
|
|
160
|
+
if self.scale is None:
|
|
161
|
+
self.scale = 0.1
|
|
162
|
+
|
|
163
|
+
Ns = N+Nb # number of simulations
|
|
164
|
+
|
|
165
|
+
# allocation
|
|
166
|
+
samples = np.empty((self.dim, Ns))
|
|
167
|
+
loglike_eval = np.empty(Ns)
|
|
168
|
+
acc = np.zeros(Ns)
|
|
169
|
+
|
|
170
|
+
# initial state
|
|
171
|
+
samples[:, 0] = self.x0
|
|
172
|
+
loglike_eval[0] = self._loglikelihood(self.x0)
|
|
173
|
+
acc[0] = 1
|
|
174
|
+
|
|
175
|
+
# initial adaptation params
|
|
176
|
+
Na = int(0.1*N) # iterations to adapt
|
|
177
|
+
hat_acc = np.empty(int(np.floor(Ns/Na))) # average acceptance rate of the chains
|
|
178
|
+
lambd = self.scale
|
|
179
|
+
star_acc = 0.44 # target acceptance rate RW
|
|
180
|
+
i, idx = 0, 0
|
|
181
|
+
|
|
182
|
+
# run MCMC
|
|
183
|
+
for s in range(Ns-1):
|
|
184
|
+
# run component by component
|
|
185
|
+
samples[:, s+1], loglike_eval[s+1], acc[s+1] = self.single_update(samples[:, s], loglike_eval[s])
|
|
186
|
+
|
|
187
|
+
# adapt prop spread using acc of past samples
|
|
188
|
+
if ((s+1) % Na == 0):
|
|
189
|
+
# evaluate average acceptance rate
|
|
190
|
+
hat_acc[i] = np.mean(acc[idx:idx+Na])
|
|
191
|
+
|
|
192
|
+
# d. compute new scaling parameter
|
|
193
|
+
zeta = 1/np.sqrt(i+1) # ensures that the variation of lambda(i) vanishes
|
|
194
|
+
lambd = np.exp(np.log(lambd) + zeta*(hat_acc[i]-star_acc))
|
|
195
|
+
|
|
196
|
+
# update parameters
|
|
197
|
+
self.scale = min(lambd, 1)
|
|
198
|
+
|
|
199
|
+
# update counters
|
|
200
|
+
i += 1
|
|
201
|
+
idx += Na
|
|
202
|
+
|
|
203
|
+
# display iterations
|
|
204
|
+
if ((s+1) % (max(Ns//100,1))) == 0 or (s+1) == Ns-1:
|
|
205
|
+
print("\r",'Sample', s+1, '/', Ns, end="")
|
|
206
|
+
|
|
207
|
+
self._call_callback(samples[:, s+1], s+1)
|
|
208
|
+
|
|
209
|
+
print("\r",'Sample', s+2, '/', Ns)
|
|
210
|
+
|
|
211
|
+
# remove burn-in
|
|
212
|
+
samples = samples[:, Nb:]
|
|
213
|
+
loglike_eval = loglike_eval[Nb:]
|
|
214
|
+
accave = acc[Nb:].mean()
|
|
215
|
+
print('\nAverage acceptance rate:', accave, 'MCMC scale:', self.scale, '\n')
|
|
216
|
+
|
|
217
|
+
return samples, loglike_eval, accave
|
|
218
|
+
|
|
219
|
+
def single_update(self, x_t, loglike_eval_t):
|
|
220
|
+
# propose state
|
|
221
|
+
xi = self.prior.sample(1).flatten() # sample from the prior
|
|
222
|
+
x_star = np.sqrt(1-self.scale**2)*x_t + self.scale*xi # pCN proposal
|
|
223
|
+
|
|
224
|
+
# evaluate target
|
|
225
|
+
loglike_eval_star = self._loglikelihood(x_star)
|
|
226
|
+
|
|
227
|
+
# ratio and acceptance probability
|
|
228
|
+
ratio = loglike_eval_star - loglike_eval_t # proposal is symmetric
|
|
229
|
+
alpha = min(0, ratio)
|
|
230
|
+
|
|
231
|
+
# accept/reject
|
|
232
|
+
u_theta = np.log(np.random.rand())
|
|
233
|
+
if (u_theta <= alpha):
|
|
234
|
+
x_next = x_star
|
|
235
|
+
loglike_eval_next = loglike_eval_star
|
|
236
|
+
acc = 1
|
|
237
|
+
else:
|
|
238
|
+
x_next = x_t
|
|
239
|
+
loglike_eval_next = loglike_eval_t
|
|
240
|
+
acc = 0
|
|
241
|
+
|
|
242
|
+
return x_next, loglike_eval_next, acc
|
|
243
|
+
|
|
244
|
+
|