.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "user/_auto_howtos/deconvolution_2d.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_user__auto_howtos_deconvolution_2d.py: 2D Deconvolution ================ In this example we show how to quantify the uncertainty of a solution to a 2D deconvolution problem. .. GENERATED FROM PYTHON SOURCE LINES 8-9 First we import the modules needed. .. GENERATED FROM PYTHON SOURCE LINES 9-14 .. code-block:: Python import numpy as np from cuqi.testproblem import Deconvolution2D from cuqi.distribution import Gaussian, LMRF from cuqi.problem import BayesianProblem .. GENERATED FROM PYTHON SOURCE LINES 15-32 Step 1: Deterministic model --------------------------- Consider the deterministic inverse problem .. math:: \mathbf{y} = \mathbf{A} \mathbf{x} where :math:`\mathbf{A}` is a matrix representing a 2D convolution operation and :math:`\mathbf{y}` and :math:`\mathbf{x}` are the data and unknown (solution to the inverse problem) respectively. A linear forward model like :math:`\mathbf{A}` is represented by a :class:`~cuqi.model.LinearModel` and any data (like some observed data :math:`\mathbf{y}^\mathrm{obs}`) as a :class:`~cuqi.array.CUQIarray`. The easiest way to get these two components is to use the built-in testproblems. Let us extract the model and data for a 2D deconvolution. .. GENERATED FROM PYTHON SOURCE LINES 32-35 .. code-block:: Python A, y_obs, info = Deconvolution2D().get_components() .. GENERATED FROM PYTHON SOURCE LINES 36-53 Step 2: Prior model ------------------- Now we aim to represent our prior knowledge of the unknown image. In this case, let us assume that the unknown is piecewise constant. This can be modelled by assuming a Laplace difference prior. The Laplace difference prior can be defined as .. math:: \mathbf{x}_{i,j}-\mathbf{x}_{i',j} \sim \mathrm{Laplace}(0, \delta),\\ \mathbf{x}_{i,j}-\mathbf{x}_{i,j'} \sim \mathrm{Laplace}(0, \delta), where :math:`\delta` is the scale parameter defining how likely jumps from one pixel value to another are in the horizontal and vertical directions. This distribution comes pre-defined in CUQIpy as the :class:`cuqi.distribution.LMRF`. Notice we have to specify the geometry of the unknown. .. GENERATED FROM PYTHON SOURCE LINES 53-56 .. code-block:: Python x = LMRF(location=0, scale=0.1, geometry=A.domain_geometry) .. GENERATED FROM PYTHON SOURCE LINES 57-70 Step 3: Likelihood model ------------------------ Suppose our data is corrupted by a Gaussian noise so our observational model is .. math:: \mathbf{y}\mid \mathbf{x} \sim \mathcal{N}(\mathbf{A} \mathbf{x}, \sigma^2), where :math:`\sigma^2` is a noise variance that we know. We can represent :math:`\mathbf{y}\mid \mathbf{x}` as a :class:`cuqi.distribution.Distribution` object. We often call the distribution of :math:`\mathbf{y}\mid \mathbf{x}` the data distribution. .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: Python y = Gaussian(mean=A@x, cov=0.01) .. GENERATED FROM PYTHON SOURCE LINES 74-86 Step 4: Posterior sampling -------------------------- In actuality we are interested in conditioning on the observed data :math:`\mathbf{y}^\mathrm{obs}`, to obtain the posterior distribution .. math:: p(\mathbf{x}|\mathbf{y}^\mathrm{obs}) \propto p(\mathbf{y}^\mathrm{obs}|\mathbf{x})p(\mathbf{x}), and then sampling from this posterior distribution. In CUQIpy, we the easiest way to do this is to use the :class:`cuqi.problem.BayesianProblem` class. .. GENERATED FROM PYTHON SOURCE LINES 86-90 .. code-block:: Python # Create Bayesian problem and set observed data (conditioning) BP = BayesianProblem(y, x).set_data(y=y_obs) .. GENERATED FROM PYTHON SOURCE LINES 91-93 After setting the data, we can sample from the posterior using the :meth:`cuqi.problem.BayesianProblem.sample_posterior` method. Notice that a well-suited sampler is automatically chosen based on the model, likelihood and prior chosen. .. GENERATED FROM PYTHON SOURCE LINES 93-97 .. code-block:: Python # Sample posterior samples = BP.sample_posterior(200) .. rst-class:: sphx-glr-script-out .. code-block:: none !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!! Automatic sampler selection is a work-in-progress. !!! !!! Always validate the computed results. !!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Using cuqi.sampler Unadjusted Gaussian Laplace Approximation (UGLA) sampler. burn-in: 20% Warmup: 0%| | 0/40 [00:00` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: deconvolution_2d.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: deconvolution_2d.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_