-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into sprint19_MetropolishHastings_renaming
- Loading branch information
Showing
34 changed files
with
164 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,88 @@ | ||
from cuqi.distribution import Distribution | ||
from cuqi.operator import FirstOrderFiniteDifference | ||
import numpy as np | ||
from cuqi.geometry import _DefaultGeometry, Image2D | ||
from cuqi.operator import FirstOrderFiniteDifference | ||
from cuqi.distribution import Distribution | ||
|
||
class LMRF(Distribution): | ||
""" | ||
Parameters | ||
---------- | ||
partition_size : int | ||
The dimension of the distribution in one physical dimension. | ||
"""Laplace distribution on the difference between neighboring nodes. | ||
For 1D `(physical_dim=1)`, the Laplace difference distribution assumes that | ||
.. math:: | ||
x_i-x_{i-1} \sim \mathrm{Laplace}(0, b), | ||
where :math:`b` is the scale parameter. | ||
For 2D `(physical_dim=2)` the differences are defined in both horizontal and vertical directions. | ||
It is possible to define boundary conditions using the `bc_type` parameter. | ||
The location parameter is a shift of the :math:`\mathbf{x}`. | ||
physical_dim : int | ||
The physical dimension of what the distribution represents (can take the values 1 or 2). | ||
Parameters | ||
---------- | ||
location : scalar or ndarray | ||
The location parameter of the distribution. | ||
scale : scalar | ||
The scale parameter of the distribution. | ||
bc_type : string | ||
The boundary conditions of the difference operator. | ||
physical_dim : int | ||
The physical dimension of what the distribution represents (can take the values 1 or 2). | ||
Example | ||
------- | ||
.. code-block:: python | ||
import cuqi | ||
import numpy as np | ||
prior = cuqi.distribution.LMRF(location=np.zeros(128), scale=0.1) | ||
""" | ||
def __init__(self, mean, prec, partition_size, physical_dim, bc_type, **kwargs): | ||
super().__init__(**kwargs) | ||
self.mean = mean.reshape(len(mean), 1) | ||
self.prec = prec | ||
self._partition_size = partition_size # partition size | ||
self._bc_type = bc_type # boundary conditions | ||
def __init__(self, location, scale, bc_type="zero", physical_dim=1, **kwargs): | ||
# Init from abstract distribution class | ||
super().__init__(**kwargs) | ||
|
||
self.location = location | ||
self.scale = scale | ||
self._bc_type = bc_type | ||
self._physical_dim = physical_dim | ||
if physical_dim == 1: | ||
num_nodes = (partition_size,) | ||
|
||
if physical_dim == 2: | ||
N = int(np.sqrt(self.dim)) | ||
num_nodes = (N, N) | ||
if isinstance(self.geometry, _DefaultGeometry): | ||
self.geometry = Image2D(num_nodes) | ||
|
||
elif physical_dim == 1: | ||
num_nodes = self.dim | ||
else: | ||
num_nodes = (partition_size,partition_size) | ||
raise ValueError("Only physical dimension 1 or 2 supported.") | ||
|
||
self._diff_op = FirstOrderFiniteDifference(num_nodes=num_nodes, bc_type=bc_type) | ||
|
||
self._diff_op = FirstOrderFiniteDifference( num_nodes, bc_type= bc_type) | ||
# Check if location parameter is non-zero vector (not supported) | ||
if callable(location) or np.linalg.norm(location) > 0: | ||
raise ValueError("Non-zero location parameter not supported.") | ||
|
||
@property | ||
def dim(self): | ||
return self._diff_op.dim | ||
def pdf(self, x): | ||
Dx = self._diff_op @ (x-self.location) # np.diff(X) | ||
return (1/(2*self.scale))**(len(Dx)) * np.exp(-np.linalg.norm(Dx, ord=1, axis=0)/self.scale) | ||
|
||
def logpdf(self, x): | ||
Dx = self._diff_op @ (x-self.location) | ||
return len(Dx)*(-(np.log(2)+np.log(self.scale))) - np.linalg.norm(Dx, ord=1, axis=0)/self.scale | ||
|
||
if self._physical_dim == 1 or self._physical_dim == 2: | ||
const = self.dim *(np.log(self.prec)-np.log(2)) | ||
y = const - self.prec*(np.linalg.norm(self._diff_op@x, ord=1)) | ||
else: | ||
raise NotImplementedError | ||
return y | ||
def _sample(self,N=1,rng=None): | ||
raise NotImplementedError("'LMRF.sample' is not implemented. Sampling can be performed with the 'sampler' module.") | ||
|
||
# def cdf(self, x): # TODO | ||
# return 1/2 + 1/2*np.sign(x-self.loc)*(1-np.exp(-np.linalg.norm(x, ord=1, axis=0)/self.scale)) | ||
|
||
def _sample(self, N): | ||
raise NotImplementedError | ||
# def sample(self): # TODO | ||
# p = np.random.rand(self.dim) | ||
# return self.loc - self.scale*np.sign(p-1/2)*np.log(1-2*abs(p-1/2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.