# Gaussian processes, sampling and marginalization

In this tutorial we will experiment with sampling and marginalization of Gaussian processes. Furthermore, we will learn how to compare the likelihood of instances of our model.

##### Related resources

The following resources from our online course may provide some helpful context for this tutorial:

##### Preparation

As in the previous tutorials, we start by importing some commonly used objects and initializing the system.

#### Discrete and Continuous Gaussian processes

We have seen in the last tutorial that a Point Distribution Model (PDM) is represented in Scalismo as a (discrete) Gaussian process over deformation fields, defined on a reference mesh.

To continue our exploration of Gaussian processes, we therefore start by loading (and visualizing) an existing PDM and retrieve its underlying Gaussian process

We can retrieve random samples from the Gaussian process by calling `sample`

on the `gp`

object:

Note that the sampled vector field is **discrete**; I.e. is
defined over a **discrete set of points**.
This is due to the fact that our Gaussian Process is stored in a file
and was therefore discretized over the points of the reference mesh.

As seen in the previous tutorial, we could interpolate the
sample `sampleDf`

to obtain a continuous version of the deformation field.
A more convenient approach is, however, to interpolate the
Gaussian process directly:

When we sample now from the continuous GP, we obtain a vector-valued function, which is defined on the entire 3D Space:

*Attention: While the interpolated Gaussian process is now defined on the entire 3D Space, the interpolation really only makes sense close to the mesh points*.

## From continuous to discrete: marginalization

In practice, we will never work with a continuous Gaussian process directly.
We are always interested in the distribution on a finite set of points.
The real advantage of having a continuous Gaussian process is, that we can
get samples at *any* finite set of points and thereby choosing the discretization
according to the needs of our application.

To illustrate this, we could, for example obtain a sample, which is defined on all the points of the original reference mesh.

We can also obtain samples which are defined only at a single point:

(This should show a vector at the tip of the nose, which, could also be behind the face)

The marginalization property of a Gaussian process makes it possible not only
to obtain samples at an arbitrary set of points, but also the
distribution at these points. We can
obtain this distribution, by calling the method `marginal`

on the Gaussian process instance:

The result of marginalization is again a discrete Gaussian process. Sampling from this new Gaussian process yields a discrete deformation field, which is defined only at the two points over which we marginalized:

It seems that we are back where we started. But note that we have now choosen a completly different set of points on which the Gaussian process is defined. This is important, as we can choose for any application that discretization of the Gaussian process, which is most useful.

## Marginal of a statistical mesh model

Given that a *StatisticalMeshModel* is in reality just a wrapper
around a GP, it naturally allows for marginalization as well:

Notice in this case, how the passed argument to the marginal function
is an indexed sequence of point **identifiers** instead of a discrete domain.
This is due to the fact that we are marginalizing a discrete Gaussian process.
Since the domain of the GP is already discrete, marginalization in this
case is done by selecting a subset of the discrete domain.
Hence the use of point identifiers instead of 3D coordinates.

Not surprisingly, we can again sample from this nose tip model:

Given that the marginal model is a *StatisticalMeshModel*, sampling from it
returns a `TriangleMesh`

. When inspecting the points of the
returned sample, we see that it contains only one point, the nose tip.

#### Nose marginal

Let's suppose that we have a full model of the face, but are only interested in the shape variations around the nose. Marginalization let's us achieve this easily. To do so, we extract all points which lie within a specified distance around the middle of the nose:

We can now use the point ids to marginalize our shape model:

## Probability of shapes and deformations:

It is often interesting to assess how probable a model instance is.
This can be done in Scalismo by means of the method `pdf `

(which stands for probability density function) of the class `GaussianProcess`

and `StatisticalMeshModel`

respectively.

The value of the *pdf* is often not interesting as such. But it allows us to compare the likelihood of different instances, by comparing their density value.
For numerical reasons, we usually work with the log probability: