Post · 72 min read· pp. 3335–3422

Robust Gaussian Process Modeling

Gaussian processes are a powerful approach for modeling functional behavior, but as with any tool that power can be wielded responsibly only with deliberate care. In this case study I introduce the basic usage of Gaussian processes in modeling applications, and how to implement them in Stan, before considering some of their subtle but ubiquitous inferential pathologies and the techniques for addressing those pathologies with principled domain expertise.

1 Modeling Functional Relationships

A common problem in probabilistic modeling is capturing the impact of some continuous variable, x∈Xx \in X, such as the known time or spatial location at which a measurement is made. Even once we've chosen an appropriate observational model, for example π(y;θ,ϕ)\pi(y; \theta, \phi), and identified which parameter is influenced by the covariate, for example θ\theta, we still have to model the functional relationship between that parameter and the covariates, θ=f(x)\theta = f(x).

For example consider a normal observation model where the covariates influence only the location parameter,

y∼normal(f(x),σ).y \sim \text{normal} \left( f(x), \sigma \right).
In this special case of modeling the structure of the location parameter we can interpret the observed data (yn,xn)(y_{n}, x_{n}) as points scattered around the location function f(x)f(x) and overlay them on the same plot, even though they technically live in different spaces.

In some cases this functional relationship is motivated by explicit generative models, but when we aren't privileged with that depth of understanding we have to consider more heuristic models. For example if θ\theta takes values on the entire real line then we can consider a linear approximation where we assume a linear relationship between the covariate xx and the parameter,

f(x)=α+xT⋅β.f(x) = \alpha + x^{T} \cdot \boldsymbol{\beta}.

This approximation, however, is often not rich enough to capture the functional behavior that manifests in a given system. A more sophisticated heuristic model might consider a higher-order polynomial approximation that captures more complex functional behaviors than the linear model.

As the order of the polynomial grows, however, the space of polynomial functions starts to be dominated by more extreme functional relationships that usually conflict with our own domain expertise.

Unfortunately this behavior is somewhat inevitable. In practice it is incredibly difficult, if not outright impossible, to engineer function spaces that are rich enough to capture complex behaviors of interest without also being so large that those desired behaviors are overwhelmed by less desirable behaviors. To compensate we need to ensure that our probabilistic model for the functional behavior provides sufficient regularization, concentrating on the desired behaviors while suppressing the undesirable behaviors.

Principled regularization, however, requires not only understanding what behaviors are consistent with our domain expertise and which are not but also how these behaviors are related to our representation of the function space. For example robust use of the polynomial model requires understanding how all of the polynomial coefficients interact to control the overall function behavior, which quickly becomes unmanageable as the order of the polynomial grows. Interpretable regularization requires much more than just shrinking all of the coefficients towards zero! Polynomial models are problematic not because they're too flexible but rather because it's challenging to regularize that flexibility in a principled way.

Gaussian processes define probabilisitic models of functional behavior not through any finite-dimensional parametric model but rather by defining probability distributions over function spaces directly. This more direct approach can drastically improve the interpretability of the model and hence facilitate its principled use.

1.1 Gaussian Processes In Theory

Gaussian processes are built on top of a sophisticated, and honestly a bit overwhelming, mathematical foundation [1, 2]. In this case study we will consider only a relatively simple abstraction of that theory that will be sufficient for their basic use with one-dimensional covariate spaces. The deeper theory becomes much more relevant when approximations are concerned; see Section 4.3 for a limited discussion.

A Gaussian process defines a probability distribution over functions; in other words every sample from a Gaussian process is an entire function from the covariate space XX to the real-valued output space.

These realizations are said to be infinite-dimensional in the sense that in order to specify an entire function we need to specify the function value at every one of the infinite covariate values in XX.

1.1.1 Visualizing Gaussian Processes

We have a few natural ways to visualize a Gaussian process in one-dimension.

Spaghetti plots, for example, visualize some typical functional behaviors, including most importantly the typical correlations between function values, by overlaying an ensemble of function samples. Unfortunately spaghetti plots are not without some fundamental limitations. A small ensemble allows us to readily resolve individual function behavior but only poorly quantifies the entire Gaussian process, especially in the tails. On the other hand a large ensembles better characterizes the full breadth of typical behavior at the expense of obfuscating the individual functional behavior in the bulk of the distribution.

Judicious use of color and transparency, as well as animation, can reduce these limitations somewhat. Because ensemble visualizations are not estimating any particular expectation value, however, we cannot quantify exactly how faithful of a picture they're providing.

Alternatively we can consider ribbon plots, sometimes known as fan plots, that visualize the ensemble with marginal quantile intervals at each covariate value. Here I use ribbons covering the 10%-90%, 20%-80%, 30%-70%, and 40%-60% quantile intervals as well as one for the 50% medians.

Ribbon plots quantify the ensemble behavior of the distribution of function behaviors at each covariate value, but only at the expense of ignoring the correlations between different covariate values. In particular by visualizing only the marginal quantiles the ribbons will make the ensemble appear narrower than it actually is and the joint coverage will be underestimated.

Ultimately no visualization of a space with more than a few dimensions will be without tradeoffs, but complementing spaghetti plots with ribbon plots can perform reasonably well for understanding the basic behavior of a Gaussian process.

1.1.2 Parameterizing Gaussian Processes

The space of all Gaussian processes is parameterized by two functions. A mean function m:X→Rm : X \rightarrow \mathbb{R} defines the baseline function around which the Gaussian process concentrates while the covariance function k:X×X→R+k : X \times X \rightarrow \mathbb{R}^{+} quantifies the variation around that baseline.

Because every Gaussian process can be recovered by adding the mean function to a corresponding zero-mean Gaussian process,

f∼GP(0,k)m+f∼GP(m,k),\begin{align*} f &\sim \text{GP}(0, k) \\ m + f &\sim \text{GP}(m, k), \end{align*}
we can focus our attention on zero-mean Gaussian processes, and the influence of the covariance function, without any loss of generality. Indeed this perspective is quite natural from the modeling perspective where Gaussian processes are often added to some presumed functional relationship, say one motivated by a simple mechanistic model, to capture residual behavior. See Section 4.1 for some additional discussion.

For a zero-mean Gaussian process the behavior of the sampled function is completely determined by the covariance function.

The marginal variation of function values at any given covariate value is determined by the diagonal evaluations of the covariance function where the same value is used for both arguments, k(x1,x1)k(x_{1}, x_{1}) The larger the covariance function output the more the function values f(x1)f(x_{1}) will vary around the zero baseline.

Similarly the correlation between function values at different inputs is determined by the off-diagonal evaluations of the covariance function where different values are used for each input. The larger the normalized covariance function output,

k(x1,x2)k(x1,x1)⋅k(x2,x2),\frac{k(x_{1}, x_{2})}{ \sqrt{ k(x_{1}, x_{1}) \cdot k(x_{2}, x_{2})} },
the more correlated f(x1)f(x_{1}) and f(x2)f(x_{2}) will be with each other. In other words if we fix f(x1)f(x_{1}) to a particular value, or equivalently filter all of the possible functions except those that happen to have the right value of f(x1)f(x_{1}), then the larger the normalized covariance function the less f(x2)f(x_{2}) will vary around f(x1)f(x_{1}).

In applications we often consider stationary covariance functions that depend on only the relative distance between covariate values,

Δx=∣x1−x2∣.\Delta x = | x_{1} - x_{2} |.
In this case we can visualize the marginal and correlation structure of the covariance function by plotting its values verses distance in a covariogram, or its normalized values verse distance in a correlogram.

The marginal variation, which is constant for all covariate values

k(x,x)=k(0),k(x, x) = k(0),
is given by the initial value while the decay of the covariance function with covariate distance determines for long correlations persist. A stationary covariance function that decays slowly will generate functions that are rigid while faster decay will lead to functions that fluctuate much more strongly. If the covariance function decays quickly enough then the realized functions may not even have well-defined derivatives!

1.1.3 Gaussian Process Modeling

Given an observational model π(y;f(x),ϕ)\pi(y; f(x), \phi) a Gaussian process can be used to define a prior model for the functional relationship ff. Conditioned on observed data the posterior distribution for ff will quantify the functional behavior compatible with both the observed likelihood function and the Gaussian process prior model.

When modeling a location function we can visualize the influence of the likelihood function by overlaying the data and the posterior function realizations.

That posterior distribution, however, may not itself be a Gaussian process. In fact the posterior distribution for ff will be a Gaussian process if and only if the observational model is specified by a family of normal probability density functions,

y∼normal(f(x),σ).y \sim \mathrm{normal} \left( f(x), \sigma \right).
In this case the posterior Gaussian process is defined by the modified covariance function
kpost(x1,x2)=kprior(x1,x2)+σ2δx1,x2,k_{\text{post}}(x_{1}, x_{2}) = k_{\text{prior}}(x_{1}, x_{2}) + \sigma^{2} \delta_{x_{1}, x_{2}},
where
δx1,x2={1,x1=x20,x1≠x2.\delta_{x_{1}, x_{2}} = \left\{ \begin{array}{rr} 1, & x_{1} = x_{2} \\ 0, & x_{1} \ne x_{2} \end{array} \right. .

1.2 Gaussian Processes In Practice

Although the theory of Gaussian processes can be compelling, in practice we can never completely realize it. The problem is that with only finite computational resources -- both in terms of instruction processing and memory -- we'll never be able to manipulate an entire sampled function.

Fortunately the practical implementation of a Gaussian process becomes infinitely easier -- literally -- if we consider not all function values but rather only those at a finite number of covariate values which I will denote as a grid. In this case the function values over the grid follow a distribution specified by a multivariate normal density function completely determined by the Gaussian process.

First consider the behavior of a single function value at the covariate point x1x_{1},

f1=f(x1).f_{1} = f(x_{1}).
Each sampled function reduces to single value of that function evaluated at x1x_{1}, and the variation of those function values is completely specified by a normal density function with location parameter given by evaluating the mean function at x1x_{1} and variance given by evaluating the covariance function at x1x_{1} in both arguments,
π(f1)=normal(f1∣m(x1),k(x1,x1)).\pi(f_{1}) = \text{normal} \left( f_{1} \mid m(x_{1}), \sqrt{ k(x_{1}, x_{1}) } \right).

The distribution of multiple function values proceeds with a vector of location parameters given by evaluating the mean function at each covariate value,

mn=m(xn),m_{n} = m(x_{n}),
and a covariance matrix of parameters given by evaluating the covariance function at each pair of covariate values,
Knm=k(xn,xm).K_{nm} = k(x_{n}, x_{m}).
This realized covariance matrix is also known as the Gram matrix.

Once we've constructed a multivariate normal density function the implementation of a Gaussian process reduces to the manipulation of multivariate normal density functions which themselves consist of a series of well-known linear algebraic operations.

For example to simulate sampling a function from a Gaussian process and then evaluating that sampled function at the grid of covariate values we can just directly sample from a multivariate normal random number generator. Indeed this is how I have been making the function realizations plots this entire time. In each plot I discretize the covariate space into 551 equally-spaced points, sample from the corresponding 551-dimensional multivariate normal random number generator, and then let gnuplot interpolate between sampled values with lines. If we zoom into any of the plots then the deception becomes a little bit more evident.

We can also consider predictions by taking advantage of the conditional structure of a multivariate normal density function. Consider a grid of observed covariate values

{x1obs,…,xNobsobs}\{ x^{\text{obs}}_{1}, \ldots, x^{\text{obs}}_{N_{\text{obs}}} \}
where we know the function values f(xnobs)f(x^{\text{obs}}_{n}), and a grid of unobserved covariate values where we want to predict the function values,
{x1pred,…,xNpredpred}.\{ x^{\text{pred}}_{1}, \ldots, x^{\text{pred}}_{N_{\text{pred}}} \}.
The parameters of the multivariate normal density function of the combined covariate values decomposes into the parameters of the multivariate normal density function for each component grid plus the mixed covariate function evaluations.

More compactly

m=(mobsmpred)\mathbf{m} = \begin{pmatrix} \mathbf{m}_{\text{obs}} \\ \mathbf{m}_{\text{pred}} \end{pmatrix}
and
K=(KobsKmix(Kmix)TKpred).\mathbf{K} = \begin{pmatrix} \mathbf{K}_{\text{obs}} & \mathbf{K}_{\text{mix}} \\ ( \mathbf{K}_{\text{mix}} )^{T} & \mathbf{K}_{\text{pred}} \end{pmatrix}.

The conditional probability density function for the unobserved function values (fpred)n=f(xnpred)(f_{\text{pred}})_{n} = f(x^{\text{pred}}_{n}) given the observed function values (fobs)n=f(xnobs)(f_{\text{obs}})_{n} = f(x^{\text{obs}}_{n}) falls into the multivariate normal family,

π(fpred∣fobs)=multi-normal(μ,Σ)\pi(\mathbf{f}_{\text{pred}} \mid \mathbf{f}_{\text{obs}}) = \text{multi-normal} (\boldsymbol{\mu}, \boldsymbol{\Sigma})
with the location parameters
μ=mpred+(Kmix)T⋅(Kobs)−1⋅(fobs−mobs)\boldsymbol{\mu} = \mathbf{m}_{\text{pred}} + ( \mathbf{K}_{\text{mix}} )^{T} \cdot ( \mathbf{K}_{\text{obs}} )^{-1} \cdot (\mathbf{f}_{\text{obs}} - \mathbf{m}_{\text{obs}})
and the covariance matrix parameters
Σ=Kpred−(Kmix)T⋅(Kobs)−1⋅Kmix.\boldsymbol{\Sigma} = \mathbf{K}_{\text{pred}} - ( \mathbf{K}_{\text{mix}} )^{T} \cdot ( \mathbf{K}_{\text{obs}} )^{-1} \cdot \mathbf{K}_{\text{mix}}.

Inference with Gaussian process priors proceeds similarly. Once we've identified the covariate values at which we have observations or will want to make predictions we can specify the marginalized prior model with the corresponding multivariate normal density function.

f∼GP(m,k)→fn∼multi-normal(m,K)π(yn;f(xn),ϕ)→π(yn;fn,ϕ).\begin{align*} f \sim \text{GP}(m, k) & \quad \rightarrow \quad f_{n} \sim \text{multi-normal}(\mathbf{m}, \mathbf{K}) \\ \pi(y_{n}; f(x_{n}), \phi) & \quad \rightarrow \quad \pi(y_{n}; f_{n}, \phi). \end{align*}

If the observational model is normal then we can generate predictions analytically with the conditioning operations for the posterior covariance function we introduced in Section 1.1.3. Calculating the conditional location parameters in this way is also known as kriging, especially in the geological literature.

By working directly with a finite-dimensional projection of a Gaussian process we avoid all of the impracticalities of infinite-dimensional spaces. At the same time because we never actually realize the latent infinite-dimensional function space I hesitate calling Gaussian processes "infinite-dimensional" or "non-parametric". In practice a Gaussian process model will always manifest in a finite number of parameters!

Working with finite-dimensional projections, however, is not without its limitations. Once we've projected a Gaussian process onto a finite grid we cannot investigate functional behavior elsewhere; contrast this for example with a polynomial model that allows the possible functional behavior to be evaluated at any input point. In order to investigate functional behavior at new covariate points we have to create a larger grid and project again, constructing an entirely new multivariate normal density function and repeating all of the subsequent linear algebra. Unfortunately Gaussian processes are awkward when we can't specify all of the relevant covariate values ahead of time.

Moreover while the manipulations of a projected Gaussian process are technically possible, they are definitely not cheap. The cost of all of these operations scales with the cube of the total number of grid points being considered due to the required matrix inversions. This unfortunate scaling limits the size of grids with which we can work given the fixed computational resources typical in practice.

1.3 Covariance Functions and You

The ultimate utility of a Gaussian process model depends on the specific choice of covariance function; the better a chosen covariance function captures the functional behavior consistent with our domain expertise the better behaved our inferences will be.

From this perspective we are fortunate that the breadth of covariance functions is absolutely massive. That breadth can be expanded even further with operations that combine individual covariance functions into new, well-defined covariances functions. For example we can define new covariance functions with point-wise addition of existing covariance functions,

k(x1,x2)=kα(x1,x2)+kβ(x1,x2)k(x_{1}, x_{2}) = k_{\alpha}(x_{1}, x_{2}) + k_{\beta}(x_{1}, x_{2})
and point-wise multiplication,
k(x1,x2)=kα(x1,x2)⋅kβ(x1,x2).k(x_{1}, x_{2}) = k_{\alpha}(x_{1}, x_{2}) \cdot k_{\beta}(x_{1}, x_{2}).

At the same time that breadth can be overwhelming, especially when first learning about Gaussian processes. For a thorough but readable review of common families of covariances functions and their properties I recommend [3]. In this case study, however, we will focus on a single family of covariance functions: the exponentiated quadratic family.

The covariance functions in the exponentiated quadratic family are stationary, decaying exponentially fast with the squared distance between the covariates being compared,

k(x1,x2)=α2exp⁡(−12(∣x1−x2∣ρ)2).k(x_{1}, x_{2}) = \alpha^{2} \exp \left( - \frac{1}{2} \left( \frac{ |x_{1} - x_{2} |}{\rho} \right)^{2} \right).

Because Gaussian processes have been developed independently in different fields this family is also known by a few different names, for example the squared exponential family and the radial basis function family. The one name we shouldn't be tempted to call it a "Gaussian" family despite the similarity of the covariance function to a normal probability density function. The problem is that similarity is only superficial and using "Gaussian" here only confuses the covariance function, the Gaussian process that it specifies, and the normal probability density function given by the finite dimensional projection of that Gaussian process. As awkward as "exponentiated quadratic" may be to say it's one of the better options!

The parameter α\alpha is known as the marginal deviation as it controls the marginal variability of function values at all covariate values,

k(x1,x1)=α2exp⁡(−12(∣x1−x1∣ρ)2)=α2exp⁡(−12(0ρ)2)=α2exp⁡(−0)=α2.\begin{align*} k(x_{1}, x_{1}) &= \alpha^{2} \exp \left( - \frac{1}{2} \left( \frac{ |x_{1} - x_{1} |}{\rho} \right)^{2} \right) \\ &= \alpha^{2} \exp \left( - \frac{1}{2} \left( \frac{0}{\rho} \right)^{2} \right) \\ &= \alpha^{2} \exp \left( - 0 \right) \\ &= \alpha^{2}. \end{align*}
Consequently when α\alpha is small the variation in the function values at every covariate input are suppressed, and when α\alpha is large the variations are amplified.

The parameter ρ\rho is known as the length scale as it controls how quickly the correlations between function values decay,

k(x1,x2)k(x1,x1)⋅k(x2,x2)=α2exp⁡(−12(∣x1−x2∣ρ)2)α2⋅α2=α2α2exp⁡(−12(∣x1−x2∣ρ)2)=exp⁡(−12(∣x1−x2∣ρ)2).\begin{align*} \frac{ k(x_{1}, x_{2}) }{ \sqrt{ k(x_{1}, x_{1}) \cdot k(x_{2}, x_{2})} } &= \frac{ \alpha^{2} \exp \left( - \frac{1}{2} \left( \frac{ |x_{1} - x_{2} |}{\rho} \right)^{2} \right) } { \sqrt{ \alpha^{2} \cdot \alpha^{2} }} \\ &= \frac{ \alpha^{2} }{ \alpha^{2} } \exp \left( - \frac{1}{2} \left( \frac{ |x_{1} - x_{2} |}{\rho} \right)^{2} \right) \\ &= \exp \left( - \frac{1}{2} \left( \frac{ |x_{1} - x_{2} |}{\rho} \right)^{2} \right). \end{align*}
When ρ\rho is small the correlations between function values at even close covariate inputs are weak and the realized functions are more flexible and "wiggly". As ρ\rho increases, however, the correlations persist to longer and longer distances resulting in more rigid functions.

To build intuition for how these parameters interact with each other it helps to examine the behavior of the Gaussian processes as both α\alpha and ρ\rho are varied from small to large values. Translation into an alignment chart is left as an exercise for the reader.

Another way of interpreting the exponentiated quadratic parameters is as scalings of the function input and output values. Multiplying α\alpha by a constant, for example, is mathematically equivalent to multiplying all of the realized functions that same constant. Similarly multiplying ρ\rho by a constant is equivalent to multiplying the absolute covariate values, and hence also their relative distances, by that same constant. In other words we can quantify the behavior of every exponentiated quadratic covariance function by using α\alpha and ρ\rho as the units for the yy and xx axes, respectively.

Although we won't be using it in this case study I do want to mention one more family of covariance functions. The Matérn family generalizes the exponentiated quadratic family with the introduction of a smoothness parameter ν\nu,

k(x1,x2)=21−νΓ(ν)(2ν ∣x1−x2∣ρ)νKν(2ν ∣x1−x2∣ρ),k(x_{1}, x_{2}) = \frac{ 2^{1 - \nu}}{\Gamma(\nu)} \left( \sqrt{2 \nu} \, \frac{ |x_{1} - x_{2} |}{\rho} \right)^{\nu} K_{\nu} \left( \sqrt{2 \nu} \, \frac{ |x_{1} - x_{2} |}{\rho} \right),
whre KνK_{\nu} is a modified Bessel function of the second kind.

The smoothness parameter controls, well, the smoothness of the realized functions by quantifying how many higher-order derivatives will be well-defined. Formally for any integer k≥νk \ge \nu the kk-th order derivatives of the realized functions will not exist. For example if ν=2.5\nu = 2.5 then the realized functions will have well-defined derivatives at only zeroth, first, and second-order where as if ν=0.5\nu = 0.5 then the realized functions will have only well-defined zeroth-order derivatives which ensures continuity but not much else.

In the limit ν→∞\nu \rightarrow \infty, when derivatives of all orders are well-defined, the Matérn covariance functions reduce to the exponentiated quadratic covariance functions. While this infinite smoothness is natural in many applications it may not be appropriate when modeling functions that feature occasional, sudden changes such as natural and political boundaries that are common in spatial statistics.

2 Implementing Gaussian Processes in Stan

With a reasonable understanding of Gaussian processes and their practical realizations we can now consider how to implement them in Stan. In this section we'll consider Gaussian processes operations given a fixed Gaussian process, in particular a fixed element of the exponentiated quadratic family.

2.1 Simulating From A Gaussian Process

Simulating from the finite projection of Gaussian process prior is straightforward once we've specified the covariate grid.

First let's set up our R environment.

text
library(rstan)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
parallel:::setDefaultClusterOptions(setup_strategy = "sequential")

util <- new.env()

par(family="CMU Serif", las=1, bty="l", cex.axis=1, cex.lab=1, cex.main=1,
    xaxs="i", yaxs="i", mar = c(5, 5, 3, 5))

Now we can define the covariate grid,

text
N = 551
x <- 22 * (0:(N - 1)) / (N - 1) - 11

the parameters that specify our particular Gaussian process,

text
alpha_true <- 3
rho_true <- 5.5

and then pack everything together,

text
simu_data <- list(alpha=alpha_true, rho=rho_true, N=N, x=x)

In order to sample from the Gaussian process projected onto our covariate grid we just need to construct the Gram matrix and then sample from the corresponding multivariate normal random number generator.

text
writeLines(readLines("stan_programs/simu.stan"))
text
data {
  int<lower=1> N;
  real x[N];

  real<lower=0> rho;
  real<lower=0> alpha;
}

transformed data {
  matrix[N, N] cov =   cov_exp_quad(x, alpha, rho)
                     + diag_matrix(rep_vector(1e-10, N));
  matrix[N, N] L_cov = cholesky_decompose(cov);
}

parameters {}
model {}

generated quantities {
  vector[N] f = multi_normal_cholesky_rng(rep_vector(0, N), L_cov);
}

Here we use Stan's builtin cov_exp_quad function for evaluating the Gram matrix and then add a nugget or jitter of 10−1010^{-10} to the diagonals elements in order to stabilize the subsequent numeral linear algebra calculations. Often the nugget is taking to be square root of the floating point precision, which would be 10−810^{-8} for double precision calculations, but for this particular example we can be a bit more aggressive.

We can generate exact samples by running the Fixed_param sampler in Stan, which just evaluates the generated quantities block that calls the multivariate normal random number generator.

text
simu_fit <- stan(file='stan_programs/simu.stan', data=simu_data,
                 warmup=0, iter=4000, chains=1, seed=494838,
                 algorithm="Fixed_param", refresh=4000)
text
SAMPLING FOR MODEL 'simu' NOW (CHAIN 1).
Chain 1: Iteration:    1 / 4000 [  0%]  (Sampling)
Chain 1: Iteration: 4000 / 4000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0 seconds (Warm-up)
Chain 1:                0.319468 seconds (Sampling)
Chain 1:                0.319468 seconds (Total)
Chain 1: 

To visualize the sampled functions we'll use some visualization code in the auxiliary gp_utility.R script.

text
source('gp_utility.R', local=util)

For example we can overlay some of the functions in a spaghetti plot,

text
util$plot_gp_prior_realizations(simu_fit, x, "Realizations")

or plot marginal quantiles to summarize the aggregate behavior,

text
util$plot_gp_prior_quantiles(simu_fit, x, "Marginal Quantiles")

2.2 Simulating From A Gaussian Process Model

Incorporating this Gaussian process into a generative model is straightforward.

Consider for example using this Gaussian process as a prior model for the location function in a normal observational model,

f∼GP(0,keq)yn∼normal(f(xn),σ).\begin{align*} f &\sim \text{GP}(0, k_{\text{eq}}) \\ y_{n} &\sim \text{normal}(f(x_{n}), \sigma). \end{align*}

Once we've specified a true measurement variability we can simulate observations by sequentially sampling the function values along the covariate grid and then the observations at each of those points.

text
writeLines(readLines("stan_programs/simu_normal.stan"))
text
data {
  int<lower=1> N;
  real x[N];

  real<lower=0> rho;
  real<lower=0> alpha;
  real<lower=0> sigma;
}

transformed data {
  matrix[N, N] cov =   cov_exp_quad(x, alpha, rho)
                     + diag_matrix(rep_vector(1e-10, N));
  matrix[N, N] L_cov = cholesky_decompose(cov);
}

parameters {}
model {}

generated quantities {
  vector[N] f = multi_normal_cholesky_rng(rep_vector(0, N), L_cov);
  real y[N] = normal_rng(f, sigma);
}
text
sigma_true <- 2
simu_data$sigma <- sigma_true

set.seed(2595)
simu_fit <- stan(file='stan_programs/simu_normal.stan', data=simu_data,
                 warmup=0, iter=4000, chains=1, seed=494838,
                 algorithm="Fixed_param", refresh=4000)
text
SAMPLING FOR MODEL 'simu_normal' NOW (CHAIN 1).
Chain 1: Iteration:    1 / 4000 [  0%]  (Sampling)
Chain 1: Iteration: 4000 / 4000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0 seconds (Warm-up)
Chain 1:                0.507205 seconds (Sampling)
Chain 1:                0.507205 seconds (Total)
Chain 1: 

Now in addition the plotting some of the functions sampled from the Gaussian process prior,

text
util$plot_gp_prior_realizations(simu_fit, x, "Prior Realizations")

and the marginal quantile ribbons,

text
util$plot_gp_prior_quantiles(simu_fit, x, "Prior Marginal Quantiles")

we can also plot the marginal quantile ribbons of the prior predictive distribution that convolves the Gaussian process prior with the observational model.

text
util$plot_gp_prior_pred_quantiles(simu_fit, x, "Prior Predictive Marginal Quantiles")

Let's grab the first simulation for closer inspection.

text
f <- extract(simu_fit)$f[1,]
y <- extract(simu_fit)$y[1,]

c_mid_teal="#487575"

plot(x, f, type="l", lwd=2, xlab="x", ylab="y",
     xlim=c(-11, 11), ylim=c(-10, 10))
points(x, y, col="white", pch=16, cex=0.6)
points(x, y, col=c_mid_teal, pch=16, cex=0.4)

In practice we are often interested in the functional behavior beyond just the observed covariate values. To emulate this circumstance let's define a measurement by the observations at just eleven evenly spaced points along the covariate grid.

text
observed_idx <- c(50 * (0:10) + 26)
N_obs = length(observed_idx)
x_obs <- x[observed_idx]
y_obs <- y[observed_idx]

The entire grid, however, will be considered for predictions.

text
N_predict <- N
x_predict <- x
y_predict <- y

plot(x, f, type="l", lwd=2, xlab="x", ylab="y",
     xlim=c(-11, 11), ylim=c(-10, 10))
points(x_predict, y_predict, col="white", pch=16, cex=0.6)
points(x_predict, y_predict, col=c_mid_teal, pch=16, cex=0.4)
points(x_obs, y_obs, col="white", pch=16, cex=1.2)
points(x_obs, y_obs, col="black", pch=16, cex=0.8)

In order to use this simulation in the next section we'll put everything in a safe place.

text
stan_rdump(c("N_obs", "x_obs", "y_obs",
             "N_predict", "x_predict", "y_predict",
             "observed_idx"), file="output/normal.data.R")

stan_rdump(c("f", "x"), file="output/gp.truth.R")

The same construction works for more sophisticated models, as well. For example consider using the selected Gaussian process as a prior model for the log intensity in a Poisson observational model,

f∼GP(0,keq)yn∼Poisson(exp⁡(f(xn))).\begin{align*} f &\sim \text{GP}(0, k_{\text{eq}}) \\ y_{n} &\sim \text{Poisson}(\exp(f(x_{n}))). \end{align*}
The log link function here ensures that the unbounded Gaussian process is compatible with the positivity of the Poisson intensity parameter.

Simulating an observation from this model proceeds just as for the normal model.

text
writeLines(readLines("stan_programs/simu_poisson.stan"))
text
data {
  int<lower=1> N;
  real x[N];

  real<lower=0> rho;
  real<lower=0> alpha;
}

transformed data {
  matrix[N, N] cov =   cov_exp_quad(x, alpha, rho)
                     + diag_matrix(rep_vector(1e-10, N));
  matrix[N, N] L_cov = cholesky_decompose(cov);
}

parameters {}
model {}

generated quantities {
  vector[N] f = multi_normal_cholesky_rng(rep_vector(0, N), L_cov);
  int y[N] = poisson_log_rng(f);
}

Note that by setting the R seed again here we ensure the same permutation of the generated samples in RStan as above so that the first iteration, which we'll use for our simulated data, will use the exact same function realization.

text
set.seed(2595)
simu_fit <- stan(file='stan_programs/simu_poisson.stan', data=simu_data,
                 warmup=0, iter=4000, chains=1, seed=494838,
                 algorithm="Fixed_param", refresh=4000)
text
SAMPLING FOR MODEL 'simu_poisson' NOW (CHAIN 1).
Chain 1: Iteration:    1 / 4000 [  0%]  (Sampling)
Chain 1: Iteration: 4000 / 4000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0 seconds (Warm-up)
Chain 1:                0.469286 seconds (Sampling)
Chain 1:                0.469286 seconds (Total)
Chain 1: 
text
util$plot_gp_prior_realizations(simu_fit, x, "Prior Realizations")

text
util$plot_gp_prior_quantiles(simu_fit, x, "Prior Marginal Quantiles")

text
util$plot_gp_prior_pred_quantiles_disc(simu_fit, x, "Prior Predictive Marginal Quantiles")

text
f <- extract(simu_fit)$f[1,]
y <- extract(simu_fit)$y[1,]

N_obs = length(observed_idx)
x_obs <- x[observed_idx]
y_obs <- y[observed_idx]

N_predict <- N
x_predict <- x
y_predict <- y

plot(x, exp(f), type="l", lwd=2, xlab="x", ylab="y",
     xlim=c(-11, 11), ylim=c(0, 8))
points(x_predict, y_predict, col="white", pch=16, cex=0.6)
points(x_predict, y_predict, col=c_mid_teal, pch=16, cex=0.4)
points(x_obs, y_obs, col="white", pch=16, cex=1.2)
points(x_obs, y_obs, col="black", pch=16, cex=0.8)

text
stan_rdump(c("N_obs", "x_obs", "y_obs",
             "N_predict", "x_predict", "y_predict",
             "observed_idx"), file="output/poisson.data.R")

2.3 Fitting A General Gaussian Process Posterior

Fitting the posterior of a Gaussian process model is also straightforward using the Markov chain Monte Carlo capabilities of Stan. Here we include all of the function values of interest in the parameters block and then specify the joint probability density function, including the contributions from the projection of the Gaussian process prior model and the observational model at the observed covariate values, in the model block.

Keep in mind that this model is using a fixed Gaussian process specified by the exponentiated quadratic covariance function parameters passed in through the data block.

text
writeLines(readLines("stan_programs/fit_normal.stan"))
text
data {
  int<lower=1> N_predict;
  real x_predict[N_predict];

  int<lower=1> N_obs;
  real y_obs[N_obs];
  int<lower=1, upper=N_predict> observed_idx[N_obs];

  real<lower=0> rho;
  real<lower=0> alpha;
  real<lower=0> sigma;
}

transformed data {
  matrix[N_predict, N_predict] cov =   cov_exp_quad(x_predict, alpha, rho)
                                     + diag_matrix(rep_vector(1e-10, N_predict));
  matrix[N_predict, N_predict] L_cov = cholesky_decompose(cov);
}

parameters {
  vector[N_predict] f_predict;
}

model {
  f_predict ~ multi_normal_cholesky(rep_vector(0, N_predict), L_cov);
  y_obs ~ normal(f_predict[observed_idx], sigma);
}

generated quantities {
  real y_predict[N_predict] = normal_rng(f_predict, sigma);
}
text
truth <- read_rdump("output/gp.truth.R")

data <- read_rdump("output/normal.data.R")
data$alpha <- alpha_true
data$rho <- rho_true
data$sigma <- sigma_true
text
normal_fit <- stan(file='stan_programs/fit_normal.stan', data=data,
                   seed=5838299, refresh=1000)

To check the fit let's load my stan_utility.R script for its diagnostic functions.

text
source('stan_utility.R', local=util)

writeLines(capture.output(util$check_n_eff(normal_fit))[1:5])
text
[1] "n_eff / iter for parameter f_predict[1] is 0.000508264922280805!"
[1] "n_eff / iter for parameter f_predict[2] is 0.000506151400707279!"
[1] "n_eff / iter for parameter f_predict[3] is 0.000504407086451579!"
[1] "n_eff / iter for parameter f_predict[4] is 0.000503015022370476!"
[1] "n_eff / iter for parameter f_predict[5] is 0.000501949846345331!"
text
writeLines(capture.output(util$check_rhat(normal_fit))[1:5])
text
[1] "Rhat for parameter f_predict[1] is 16.1347274181213!"
[1] "Rhat for parameter f_predict[2] is 18.7836878382283!"
[1] "Rhat for parameter f_predict[3] is 22.3719027952987!"
[1] "Rhat for parameter f_predict[4] is 27.4425826506464!"
[1] "Rhat for parameter f_predict[5] is 35.0491614882217!"
text
util$check_div(normal_fit)
text
[1] "0 of 4000 iterations ended with a divergence (0%)"
text
util$check_treedepth(normal_fit)
text
[1] "0 of 4000 iterations saturated the maximum tree depth of 10 (0%)"
text
util$check_energy(normal_fit)
text
[1] "Chain 1: E-FMI = 0.000463403313143136"
[1] "Chain 2: E-FMI = 4.44862801442144e-05"
[1] "Chain 3: E-FMI = 5.41286435807885e-05"
[1] "Chain 4: E-FMI = 0.000227585804643942"
[1] "  E-FMI below 0.2 indicates you may need to reparameterize your model"

Unfortunately the diagnostics clearly indicate that Stan is not able to accurately quantify the posterior for the projected function values. We may be able to moderate the computational problems, however, by employing a non-centered parameterization of the function values. A non-centered parameterization takes advantage of the fact that

f∼multi-normal(μ,Σ)\mathbf{f} \sim \text{multi-normal} ( \boldsymbol{\mu}, \boldsymbol{\Sigma})
is equivalent to
f~∼multi-normal(0,I)f=μ+L⋅f~\begin{align*} \tilde{\mathbf{f}} &\sim \text{multi-normal} (\mathbf{0}, \mathbf{I}) \\ \mathbf{f} &= \boldsymbol{\mu} + \mathbf{L} \cdot \tilde{\mathbf{f}} \end{align*}
where L\mathbf{L} is the Cholesky factor of the covariance matrix,
Σ=LLT.\boldsymbol{\Sigma} = \mathbf{L} \mathbf{L}^{T}.
Parameterizing the latent functions directly or through the non-centered parameterization specifies the same probability distribution, but the latter enjoys a nicer posterior geometry when the likelihood function is relatively diffuse.

The non-centered parameterization, and the recovery of the centered function values, is straightforward to implement in Stan.

text
writeLines(readLines("stan_programs/fit_normal_ncp.stan"))
text
data {
  int<lower=1> N_predict;
  real x_predict[N_predict];

  int<lower=1> N_obs;
  real y_obs[N_obs];
  int<lower=1, upper=N_predict> observed_idx[N_obs];

  real<lower=0> rho;
  real<lower=0> alpha;
  real<lower=0> sigma;
}

transformed data {
  matrix[N_predict, N_predict] cov =   cov_exp_quad(x_predict, alpha, rho)
                                     + diag_matrix(rep_vector(1e-10, N_predict));
  matrix[N_predict, N_predict] L_cov = cholesky_decompose(cov);
}

parameters {
  vector[N_predict] f_tilde;
}

transformed parameters {
  vector[N_predict] f_predict = L_cov * f_tilde;
}

model {
  f_tilde ~ normal(0, 1);
  y_obs ~ normal(f_predict[observed_idx], sigma);
}

generated quantities {
  real y_predict[N_predict] = normal_rng(f_predict, sigma);
}
text
normal_fit <- stan(file='stan_programs/fit_normal_ncp.stan', data=data,
                   seed=5838298, refresh=1000)

Not only does this run significantly faster but also there are no indications of inaccurate computation.

text
util$check_all_diagnostics(normal_fit)
text
[1] "n_eff / iter looks reasonable for all parameters"
[1] "Rhat looks reasonable for all parameters"
[1] "0 of 4000 iterations ended with a divergence (0%)"
[1] "0 of 4000 iterations saturated the maximum tree depth of 10 (0%)"
[1] "E-FMI indicated no pathological behavior"

Confident in our computation we can analyze the posterior, first with a spaghetti plot. Note that the true function doesn't look out of place compared to the included posterior realizations.

text
util$plot_gp_post_realizations(normal_fit, data, truth,
                               "Posterior Realizations")

We can also consider a ribbon plot, although we have to be a bit more careful with the interpretation. Because we're looking only at marginal quantiles the ribbons won't properly capture correlations between the function values and consequently will appear narrow than they should be. The fact that our ribbons span only 10% to 90% quantiles also visually suppresses the tails a bit; estimating more extreme quantiles accurately would require running longer Markov chains. In any case when judging the posterior fit through these ribbons we have to take these considerations into account not expect that the true function be exactly contained within the ribbons.

text
util$plot_gp_post_quantiles(normal_fit, data, truth,
                            "Posterior Marginal Quantiles")

Finally we can consider a ribbon plot of the posterior predictive distribution. Comparing these ribbons to the observed data in black implements a posterior retrodictive check while comparing the ribbons to the held out data in teal implements a posterior predictive check.

Because posterior predictive intervals have no coverage guarantees in general for either retrodictions or predictions, we can't quantify either check by counting how many observations are within the quantile ribbons. Instead this visualization provides a qualitative check for any systematic deviations that might indicate limitations of our Gaussian process model.

text
util$plot_gp_post_pred_quantiles(normal_fit, data, truth,
                                 "Posterior Predictive Marginal Quantiles")

The advantage of probabilistic programming is now that we know how to implement a Gaussian process in one generative model it becomes straightforward to implement in another. Here let's consider using a Gaussian process to model the log intensity of a Poisson observational model.

text
writeLines(readLines("stan_programs/fit_poisson_ncp.stan"))
text
data {
  int<lower=1> N_predict;
  real x_predict[N_predict];

  int<lower=1> N_obs;
  int y_obs[N_obs];
  int<lower=1, upper=N_predict> observed_idx[N_obs];

  real<lower=0> rho;
  real<lower=0> alpha;
}

transformed data {
  matrix[N_predict, N_predict] cov =   cov_exp_quad(x_predict, alpha, rho)
                                     + diag_matrix(rep_vector(1e-10, N_predict));
  matrix[N_predict, N_predict] L_cov = cholesky_decompose(cov);
}

parameters {
  vector[N_predict] f_tilde;
}

transformed parameters {
  vector[N_predict] f_predict = L_cov * f_tilde;
}

model {
  f_tilde ~ normal(0, 1);
  y_obs ~ poisson_log(f_predict[observed_idx]);
}

generated quantities {
  int y_predict[N_predict] = poisson_log_rng(f_predict);
}
text
data <- read_rdump("output/poisson.data.R")
data$alpha <- alpha_true
data$rho <- rho_true
text
poisson_fit <- stan(file='stan_programs/fit_poisson_ncp.stan', data=data,
                    seed=5838298, refresh=1000)

The fit looks good.

text
util$check_all_diagnostics(poisson_fit)
text
[1] "n_eff / iter looks reasonable for all parameters"
[1] "Rhat looks reasonable for all parameters"
[1] "0 of 4000 iterations ended with a divergence (0%)"
[1] "0 of 4000 iterations saturated the maximum tree depth of 10 (0%)"
[1] "E-FMI indicated no pathological behavior"

We can visualize the posterior over functions, which is no longer a Gaussian process, using spaghetti plots

text
util$plot_gp_post_realizations(poisson_fit, data, truth,
                               "Posterior Realizations")

or ribbon plots.

text
util$plot_gp_post_quantiles(poisson_fit, data, truth,
                            "Posterior Marginal Quantiles")

Visual posterior predictive and retrodictive checks proceed in exactly the same was as before.

text
util$plot_gp_post_pred_quantiles_disc(poisson_fit, data, truth,
                                      "Posterior Predictive Marginal Quantiles")

2.4 Simulating From An Analytic Gaussian Process Posterior

When the observational model is specified by a family of normal probability density functions we can also take advantage of the analytic Gaussian process posterior covariance function and conditional linear algebra operations to exactly sample function values conditioned on the observed data.

Instead of adding a nugget to the diagonal elements of the Gram matrix we add the square of the measurement variability and then perform all of the necessary linear algebra in the gp_pred_rng function.

text
writeLines(readLines("stan_programs/fit_normal_anal.stan"))
text
functions {
  vector gp_pred_rng(real[] x2,
                     vector y1, real[] x1,
                     real alpha, real rho, real sigma, real delta) {
    int N1 = rows(y1);
    int N2 = size(x2);
    vector[N2] f2;
    {
      matrix[N1, N1] K =   cov_exp_quad(x1, alpha, rho)
                         + diag_matrix(rep_vector(square(sigma), N1));
      matrix[N1, N1] L_K = cholesky_decompose(K);

      vector[N1] L_K_div_y1 = mdivide_left_tri_low(L_K, y1);
      vector[N1] K_div_y1 = mdivide_right_tri_low(L_K_div_y1', L_K)';
      matrix[N1, N2] k_x1_x2 = cov_exp_quad(x1, x2, alpha, rho);
      vector[N2] f2_mu = (k_x1_x2' * K_div_y1);
      matrix[N1, N2] v_pred = mdivide_left_tri_low(L_K, k_x1_x2);
      matrix[N2, N2] cov_f2 =   cov_exp_quad(x2, alpha, rho) - v_pred' * v_pred
                              + diag_matrix(rep_vector(delta, N2));
      f2 = multi_normal_rng(f2_mu, cov_f2);
    }
    return f2;
  }
}

data {
  int<lower=1> N_predict;
  real x_predict[N_predict];

  int<lower=1> N_obs;
  real x_obs[N_obs];
  vector[N_obs] y_obs;

  real<lower=0> rho;
  real<lower=0> alpha;
  real<lower=0> sigma;
}

generated quantities {
  vector[N_predict] f_predict = gp_pred_rng(x_predict, y_obs, x_obs, alpha, rho, sigma, 1e-10);
  real y_predict[N_predict] = normal_rng(f_predict, sigma);
}

Because we can sample the posterior function values exactly we no longer need to run Markov chain Monte Carlo and can instead use the Fixed_param sampler.

text
data <- read_rdump("output/normal.data.R")
data$alpha <- alpha_true
data$rho <- rho_true
data$sigma <- sigma_true

normal_fit_anal <- stan(file='stan_programs/fit_normal_anal.stan', data=data,
                        iter=4000, warmup=0, chains=1, seed=5838298,
                        algorithm="Fixed_param", refresh=1000)
text
SAMPLING FOR MODEL 'fit_normal_anal' NOW (CHAIN 1).
Chain 1: Iteration:    1 / 4000 [  0%]  (Sampling)
Chain 1: Iteration: 1000 / 4000 [ 25%]  (Sampling)
Chain 1: Iteration: 2000 / 4000 [ 50%]  (Sampling)
Chain 1: Iteration: 3000 / 4000 [ 75%]  (Sampling)
Chain 1: Iteration: 4000 / 4000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0 seconds (Warm-up)
Chain 1:                21.6444 seconds (Sampling)
Chain 1:                21.6444 seconds (Total)
Chain 1: 

This yields equivalent results,

text
par(mfrow=c(1, 2))
util$plot_gp_post_quantiles(normal_fit, data, truth,
                            "MCMC Posterior Quantiles")
util$plot_gp_post_quantiles(normal_fit_anal, data, truth,
                            "MC Posterior Quantiles")

but at much lower computational cost.

3 Inferring A Gaussian Process

In the previous section we inferred functional behavior assuming a fixed Gaussian process, in particular a fixed Gaussian process specified by particular exponentiated quadratic parameter values. For most applications, however, we won't have enough domain expertise to specify any single Gaussian process a priori. Instead we will need to infer a Gaussian process within an appropriate family. More practically we will need to infer the covariance function parameters that specify a unique Gaussian process, for example the α\alpha and ρ\rho that specify an element of the exponentiated quadratic family. Here we will consider a normal observational model where we will also have to infer the measurement variability σ\sigma.

Unfortunately inference within a family of Gaussian processes is frustrated by degeneracies inherent to the flexibility that make Gaussian processes so useful in practice. In order to enable practical inference we need to consider principled regularization that moderate these degeneracies without compromising the utility of the resulting inferences. Fortunately if we look carefully enough we'll see that the structure of Gaussian processes naturally guides this regularization.

In this section we'll see how these degeneracies manifest in the fragility of point estimation before using full Bayesian inference to investigate the nature of these pathologies. Through that investigation we'll learn a little bit about how Gaussian process inference interacts with the observed covariates and then motivate prior modeling techniques that allow us to take full advantage of exponentiated quadratic Gaussian processes in practice.

3.1 Maximum Marginal Likelihood Estimation

Let's begin with point estimation where we endeavor to find and then use only a single Gaussian process in our inferences. In particular we'll use a maximum marginal likelihood estimator given by the exponentiated quadratic parameters that optimize the likelihood of the observed data after the functional behavior has been integrated out.

Note that maximum marginal likelihood estimators are subtly different from maximum a posterior estimators that optimize the marginal posterior density function of the exponentiated quadratic parameters. The key difference arises in the presence of constraints, such as the positivity of α\alpha and ρ\rho. While the maximum marginal likelihood, and hence maximum marginal likelihood estimators, is invariant to unconstraining transformations the marginal posterior density function, and hence maximum a posteriori estimators, is not.

We already implemented the marginal likelihood analytically in the fit_normal_anal.stan program. In order to fit the posterior covariance function parameters all we have to do is move them from the data block to the parameters block.

text
writeLines(readLines("stan_programs/fit_covar1.stan"))
text
data {
  int<lower=1> N_obs;
  real x_obs[N_obs];
  vector[N_obs] y_obs;
}

parameters {
  real<lower=0> rho;
  real<lower=0> alpha;
  real<lower=0> sigma;
}

model {
  matrix[N_obs, N_obs] cov =   cov_exp_quad(x_obs, alpha, rho)
                             + diag_matrix(rep_vector(square(sigma), N_obs));
  matrix[N_obs, N_obs] L_cov = cholesky_decompose(cov);

  y_obs ~ multi_normal_cholesky(rep_vector(0, N_obs), L_cov);
}

The maximum marginal likelihood estimator, or an approximation thereof, is then given by running Stan's optimizer.

text
gp_opt1 <- stan_model(file='stan_programs/fit_covar1.stan')
opt_fit <- optimizing(gp_opt1, data=data, seed=5838298, hessian=FALSE)

alpha <- opt_fit$par[2]
rho <- opt_fit$par[1]
sigma <- opt_fit$par[3]

sprintf('alpha = %s', alpha)
text
[1] "alpha = 3.18109889787607"
text
sprintf('rho = %s', rho)
text
[1] "rho = 1.13658239614758"
text
sprintf('sigma = %s', sigma)
text
[1] "sigma = 0.196539418782145"

The small σ\sigma suggests that the optimal Gaussian process supports functions flexible enough to capture most of the variation in the observed data. In order to investigate exactly how flexible those functions are we can generate posterior samples conditioned on the maximum marginal likelihood parameters.

text
pred_data <- list(alpha=alpha, rho=rho, sigma=sigma,
                  N_obs=data$N_obs, x_obs=data$x_obs, y_obs=data$y_obs,
                  N_predict=data$N_predict, x_predict=data$x_predict)
pred_opt_fit <- stan(file='stan_programs/fit_normal_anal.stan', data=pred_data,
                     iter=1000, warmup=0, chains=1, seed=5838298, refresh=1000,
                     algorithm="Fixed_param")
text
SAMPLING FOR MODEL 'fit_normal_anal' NOW (CHAIN 1).
Chain 1: Iteration:   1 / 1000 [  0%]  (Sampling)
Chain 1: Iteration: 1000 / 1000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0 seconds (Warm-up)
Chain 1:                6.66322 seconds (Sampling)
Chain 1:                6.66322 seconds (Total)
Chain 1: 

Unfortunately the resulting functional posterior concentrates around functions that strongly fluctuate away from the true functional behavior.

text
par(mfrow=c(1, 1))

util$plot_gp_post_realizations(pred_opt_fit, data, truth,
                               "Posterior Realizations")

text
util$plot_gp_post_quantiles(pred_opt_fit, data, truth,
                            "Posterior Marginal Quantiles")

Comparing the observed data in black to the posterior predictive distribution we can immediately see that the posterior retrodictive, or in sample, performance is quite good. At the same time, however, the large deviations between the unobserved data in teal and the posterior predictive distribution indicate terrible posterior predictive, or out of sample, performance. In other words the strong fluctuations in the functional posterior are characteristic of overfitting to the observed data.

text
util$plot_gp_post_pred_quantiles(pred_opt_fit, data, truth,
                                 "Posterior Predictive Marginal Quantiles")

One thing that we always have to consider when using point estimates is their stability. Unstable point estimates suggests a degenerate marginal likelihood function that is poorly quantified by any single point.

Let's try the fit again, only this time with a different random number generator seed that will select a different starting point for the parameters.

text
opt_fit <- optimizing(gp_opt1, data=data, seed=2384853, hessian=FALSE)

alpha <- opt_fit$par[2]
rho <- opt_fit$par[1]
sigma <- opt_fit$par[3]

sprintf('alpha = %s', alpha)
text
[1] "alpha = 2.17988348121825"
text
sprintf('rho = %s', rho)
text
[1] "rho = 0.234581345618208"
text
sprintf('sigma = %s', sigma)
text
[1] "sigma = 2.39639088164647"

Although the estimated value for α\alpha is similar, the values for ρ\rho and σ\sigma have almost switched. The larger value of σ\sigma suggests that the functional posterior will more closely resemble the Gaussian process prior which will concentrate on wiggly functions due to the small value of ρ\rho. To confirm this hypothesis we sample from the functional posterior as before.

text
pred_data <- list(alpha=alpha, rho=rho, sigma=sigma,
                  N_obs=data$N_obs, x_obs=data$x_obs, y_obs=data$y_obs,
                  N_predict=data$N_predict, x_predict=data$x_predict)
pred_opt_fit <- stan(file='stan_programs/fit_normal_anal.stan', data=pred_data,
                     iter=1000, warmup=0, chains=1, seed=5838298, refresh=1000,
                     algorithm="Fixed_param")
text
SAMPLING FOR MODEL 'fit_normal_anal' NOW (CHAIN 1).
Chain 1: Iteration:   1 / 1000 [  0%]  (Sampling)
Chain 1: Iteration: 1000 / 1000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0 seconds (Warm-up)
Chain 1:                8.47186 seconds (Sampling)
Chain 1:                8.47186 seconds (Total)
Chain 1: 

Indeed the typical functions fluctuate more quickly and don't seem to be strongly influenced by the observed data.

text
util$plot_gp_post_realizations(pred_opt_fit, data, truth,
                               "Posterior Realizations")

The weak influence of the observed data is more evident in the ribbon plot. Because of the large σ\sigma and small ρ\rho the posterior concentrates on functions that are only locally influenced by the observed data, returning to behavior more typical of the Gaussian process prior inbetween.

text
util$plot_gp_post_quantiles(pred_opt_fit, data, truth,
                            "Posterior Marginal Quantiles")

text
util$plot_gp_post_pred_quantiles(pred_opt_fit, data, truth,
                                 "Posterior Predictive Marginal Quantiles")

The strong sensitivity of the maximum marginal likelihood estimator to the initial condition suggests that the marginal marginal likelihood function itself may be degenerate. While we might be able to investigate that degeneracy from a theory perspective we won't be able to investigate it empirically without moving beyond point estimation. To investigate the entire breadth of the maximum marginal likelihood function we'll need Markov chain Monte Carlo.

3.2 Exploring the Marginal Likelihood Function

As I discuss in my degeneracy case study, Markov chain Monte Carlo, in particular the stubborn effectiveness of Hamiltonian Monte Carlo, is a powerful tool for investigating the features of a likelihood function even if we aren't necessarily interested in Bayesian inference. Let's see what we can learn about the marginal likelihood function of our Gaussian process model and why maximum marginal likelihood was so unstable.

3.2.1 Uniform Prior Model

Consider the Stan program that we used above which, without an explicitly specified prior model, presumes a prior model specified by improper uniform prior density functions.

text
writeLines(readLines("stan_programs/fit_covar1.stan"))
text
data {
  int<lower=1> N_obs;
  real x_obs[N_obs];
  vector[N_obs] y_obs;
}

parameters {
  real<lower=0> rho;
  real<lower=0> alpha;
  real<lower=0> sigma;
}

model {
  matrix[N_obs, N_obs] cov =   cov_exp_quad(x_obs, alpha, rho)
                             + diag_matrix(rep_vector(square(sigma), N_obs));
  matrix[N_obs, N_obs] L_cov = cholesky_decompose(cov);

  y_obs ~ multi_normal_cholesky(rep_vector(0, N_obs), L_cov);
}

We now let Hamiltonian Monte Carlo take the program for a spin.

text
fit <- stan(file='stan_programs/fit_covar1.stan', data=data,
            seed=5838298, refresh=1000)

Innocent and naive in our hopes for the fit, let's take a look at the diagnostics.

text
util$check_all_diagnostics(fit)
text
[1] "n_eff for parameter rho is NaN!"
[1] "n_eff / iter for parameter alpha is 0.000632264802915665!"
[1] "n_eff / iter for parameter sigma is 0.000979017173201156!"
[1] "n_eff / iter for parameter lp__ is 0.000500257586161989!"
[1] "  n_eff / iter below 0.001 indicates that the effective sample size has likely been overestimated"
[1] "Rhat for parameter rho is NaN!"
[1] "Rhat for parameter alpha is 2.14560690403923!"
[1] "Rhat for parameter sigma is 1.45226062135406!"
[1] "Rhat for parameter lp__ is 254.265701275626!"
[1] "  Rhat above 1.1 indicates that the chains very likely have not mixed"
[1] "3327 of 4000 iterations ended with a divergence (83.175%)"
[1] "  Try running with larger adapt_delta to remove the divergences"
[1] "257 of 4000 iterations saturated the maximum tree depth of 10 (6.425%)"
[1] "  Run again with max_treedepth set to a larger value to avoid saturation"
[1] "E-FMI indicated no pathological behavior"

Oooph, something really unpleasant is going on.

Let's examine the posterior scatter plots, being careful to separate the divergent and non-divergent transitions.

text
c_dark_trans <- c("#8F272780")
c_dark_highlight_trans <- c("#7C000080")
c_green_trans <- c("#00FF0080")

partition <- util$partition_div(fit)
div_params <- partition[[1]]
nondiv_params <- partition[[2]]

par(mfrow=c(1, 3))

plot(log(nondiv_params$rho), log(nondiv_params$alpha),
    col=c_dark_trans, pch=16, cex=0.8, xlab="log(rho)", ylab="log(alpha)")
points(log(div_params$rho), log(div_params$alpha),
      col=c_green_trans, pch=16, cex=0.8)

plot(log(nondiv_params$rho), log(nondiv_params$sigma),
    col=c_dark_trans, pch=16, cex=0.8, xlab="log(rho)", ylab="log(sigma)")
points(log(div_params$rho), log(div_params$sigma),
      col=c_green_trans, pch=16, cex=0.8)

plot(log(nondiv_params$alpha), log(nondiv_params$sigma),
    col=c_dark_trans, pch=16, cex=0.8, xlab="log(alpha)", ylab="log(sigma)")
points(log(div_params$alpha), log(div_params$sigma),
      col=c_green_trans, pch=16, cex=0.8)

While the values of σ\sigma look reasonable, the values of α\alpha are unusually large and the values of ρ\rho are absolutely massive. In fact

exp⁡(700)≈10304\exp(700) \approx 10^{304}
just about saturates the maximum value of the double precision floating point numbers used by Stan. In other words the Markov chains are concentrating around infinity as much as the floating point numbers will allow them!

This behavior is typical of uniform density functions -- uniform is better thought of as" "concentrating around infinity" rather that "flat" -- but the fact that this behavior manifests in the posterior density function indicates that the marginal likelihood function is itself flat, at least for large enough values of ρ\rho.

In order to understand why the marginal likelihood function is degenerate for large values of ρ\rho let's take another look at the observed data.

text
par(mfrow=c(1, 1))

plot(1, type="n", xlab="x", ylab="y", main="", xlim=c(-11, 11), ylim=c(-10, 10))
points(data$x_obs, data$y_obs, col="white", pch=16, cex=1.2)
points(data$x_obs, data$y_obs, col="black", pch=16, cex=0.8)
grid <- sapply(0:10, function(n) abline(v=2 * n - 10, col="gray80", lwd=2, lty=3))

Recall that because the exponentiated quadratic covariance function is stationary the correlations between function values are determined entirely by the distances between covariate values. In particular we can learn about the length scale ρ\rho only through the distances between the covariates in our observed data. Because our covariate grid spans only the range −10≤x≤10-10 \le x \le 10 our observed data cannot inform length scales above 20!

In other words the degeneracy in the marginal likelihood function that we've encountered is a fundamental limitation of our experimental design. There is simply no information about length scales larger than the maximum distance between the observed covariates.

3.2.2 Containment Prior Model

To ensure a well-defined posterior density function we need a prior model that can suppress infinity and compensate for the degeneracy of the marginal likelihood function. The modeling question is exactly how strongly our domain expertise allows us to suppress infinity along each parameter direction.

One particularly tempting strategy is to design a prior model that suppresses length scales larger than the maximum distance in the observed data, exactly compensating for the inherent degeneracy of the marginal likelihood function. The problem with this approach, however, is that in general there is no reason why our domain expertise would be related to the observed covariate values. In particular there is no reason why length scales larger than the the maximum distance that we happen to observe are inconsistent with our domain expertise.

A principled prior model can consider only how the covariates are observed rather than any particular values. For example consider a fixed covariate grid that has been carefully designed to be sensitive to the length scales of interest. This deliberate experimental design implies than length scales larger than the span of the covariate grid actually are inconsistent with the domain expertise that motivated the design, in which case we can use that span to set the scale of our prior model for ρ\rho.

Without that relationship a prior model based on the span of the covariates will overfit to the initial data and prematurely limit the scope of future inferences. Even though the span of the initially observed covariates does not inform larger length scales future observations may very well introduce more distant covariates that do. Without any generative process relating the observed covariates to the length scale we will have to focus on eliciting domain expertise about ρ\rho directly.

Understanding the fundamental degeneracy of the exponentiated quadratic marginal likelihood function tells us what information we need in our prior model to ensure well-behaved inferences. We cannot, however, always assume that the needed information can be elicited from our domain expertise. In particular sometimes we just don't have enough information to inform a sufficiently well-behaved posterior distribution.

Here let's assume that the covariate grid was deliberately designed based on our knowledge of the functional length scales so that a prior that suppresses length scales larger than 20 is compatible with our domain expertise. Specifically let's take a half-normal probability density function with scale

ρ∼half-normal ⁣(0,Δx3)=half-normal ⁣(0,203),\rho \sim \text{half-normal} \! \left( 0, \frac{\Delta x}{3} \right) = \text{half-normal} \! \left( 0, \frac{20}{3} \right),
which puts around 1% of the prior mass above ρ=20\rho = 20.

While we're considering more careful prior models let's also add prior density functions for α\alpha and σ\sigma that contain them around zero with scales that are consistent with our hypothetical domain expertise,

α∼half-normal(0,2)σ∼half-normal(0,1).\begin{align*} \alpha &\sim \text{half-normal}(0, 2) \\ \sigma &\sim \text{half-normal}(0, 1). \end{align*}

This more thoughtful prior model is straightforward to incorporate into our previous Stan program. Here we'll also include simulations of the posterior functional behavior.

text
writeLines(readLines("stan_programs/fit_covar2.stan"))
text
functions {
  vector gp_pred_rng(real[] x2,
                     vector y1, real[] x1,
                     real alpha, real rho, real sigma, real delta) {
    int N1 = rows(y1);
    int N2 = size(x2);
    vector[N2] f2;
    {
      matrix[N1, N1] K =   cov_exp_quad(x1, alpha, rho)
                         + diag_matrix(rep_vector(square(sigma), N1));
      matrix[N1, N1] L_K = cholesky_decompose(K);

      vector[N1] L_K_div_y1 = mdivide_left_tri_low(L_K, y1);
      vector[N1] K_div_y1 = mdivide_right_tri_low(L_K_div_y1', L_K)';
      matrix[N1, N2] k_x1_x2 = cov_exp_quad(x1, x2, alpha, rho);
      vector[N2] f2_mu = (k_x1_x2' * K_div_y1);
      matrix[N1, N2] v_pred = mdivide_left_tri_low(L_K, k_x1_x2);
      matrix[N2, N2] cov_f2 =   cov_exp_quad(x2, alpha, rho) - v_pred' * v_pred
                              + diag_matrix(rep_vector(delta, N2));
      f2 = multi_normal_rng(f2_mu, cov_f2);
    }
    return f2;
  }
}

data {
  int<lower=1> N_obs;
  real x_obs[N_obs];
  vector[N_obs] y_obs;

  int<lower=1> N_predict;
  real x_predict[N_predict];
}

parameters {
  real<lower=0> rho;
  real<lower=0> alpha;
  real<lower=0> sigma;
}

model {
  matrix[N_obs, N_obs] cov =   cov_exp_quad(x_obs, alpha, rho)
                             + diag_matrix(rep_vector(square(sigma), N_obs));
  matrix[N_obs, N_obs] L_cov = cholesky_decompose(cov);

  rho ~ normal(0, 20.0 / 3);
  alpha ~ normal(0, 2);
  sigma ~ normal(0, 1);
  y_obs ~ multi_normal_cholesky(rep_vector(0, N_obs), L_cov);
}

generated quantities {
  vector[N_predict] f_predict = gp_pred_rng(x_predict, y_obs, x_obs, alpha, rho, sigma, 1e-10);
  real y_predict[N_predict] = normal_rng(f_predict, sigma);
}
text
fit <- stan(file='stan_programs/fit_covar2.stan', data=data,
            seed=5838298, refresh=1000)

Unfortunately the diagnostics indicate divergent iterations and incomplete exploration of the posterior density function. Our better prior model resolved one issue only to uncover another, which unfortunately is a fact of life in many analyses.

text
util$check_all_diagnostics(fit)
text
[1] "n_eff / iter looks reasonable for all parameters"
[1] "Rhat looks reasonable for all parameters"
[1] "11 of 4000 iterations ended with a divergence (0.275%)"
[1] "  Try running with larger adapt_delta to remove the divergences"
[1] "0 of 4000 iterations saturated the maximum tree depth of 10 (0%)"
[1] "E-FMI indicated no pathological behavior"

Let's investigate the problem with scatter plots between the unconstrained parameters separated by divergent and non-divergent transitions.

text
partition <- util$partition_div(fit)
div_params <- partition[[1]]
nondiv_params <- partition[[2]]

par(mfrow=c(1, 3))

plot(log(nondiv_params$rho), log(nondiv_params$alpha),
    col=c_dark_trans, pch=16, cex=0.8, xlab="log(rho)", ylab="log(alpha)")
points(log(div_params$rho), log(div_params$alpha),
      col=c_green_trans, pch=16, cex=0.8)

plot(log(nondiv_params$rho), log(nondiv_params$sigma),
    col=c_dark_trans, pch=16, cex=0.8, xlab="log(rho)", ylab="log(sigma)")
points(log(div_params$rho), log(div_params$sigma),
      col=c_green_trans, pch=16, cex=0.8)

plot(log(nondiv_params$alpha), log(nondiv_params$sigma),
    col=c_dark_trans, pch=16, cex=0.8, xlab="log(alpha)", ylab="log(sigma)")
points(log(div_params$alpha), log(div_params$sigma),
      col=c_green_trans, pch=16, cex=0.8)

While neither the ρ\rho-α\alpha nor α\alpha-σ\sigma scatter plots look ideal, the ρ\rho-σ\sigma scatter plot looks particularly pathological. Once log⁡(ρ)\log(\rho) drops below 0.7 or so the posterior samples suddenly change behavior and disperse across the values of σ\sigma consistent with the prior model. Moreover the divergent transitions concentrate on the edge of that plateau.

text
par(mfrow=c(1, 1))

plot(log(nondiv_params$rho), log(nondiv_params$sigma),
    col=c_dark_trans, pch=16, cex=0.8, xlab="log(rho)", ylab="log(sigma)")
points(log(div_params$rho), log(div_params$sigma),
      col=c_green_trans, pch=16, cex=0.8)
abline(v=log(2), col="gray80", lwd=2, lty=3)

To better understand what's going on in that small length scale plateau let's investigate some of the functional behavior in that region.

text
util$plot_low_sigma_gp_post_realizations(fit, data, truth,
                                         "Posterior Realizations with sigma < 0.5")

The Gaussian processes in this plateau support functions that very closely, if not exactly, interpolate through the observed data in which case the measurement variability σ\sigma is completely uninformed by the marginal likelihood function.

One of the advantages of Bayesian inference is that the posterior distribution aggregates both the more reasonable functional behavior above ρ=2\rho = 2 with the overfitting behavior below.

text
util$plot_gp_post_realizations(fit, data, truth,
                               "Posterior Realizations")

In particular the better behaving configurations moderate the influence of the overfitting model configurations, limiting the corruption of any well-posed posterior expectation value.

text
util$plot_gp_post_quantiles(fit, data, truth,
                            "Posterior Marginal Quantiles")

text
util$plot_gp_post_pred_quantiles(fit, data, truth,
                                 "Posterior Predictive Marginal Quantiles")

Why does this overfitting behavior suddenly appear at log⁡(ρ)≈0.7\log(\rho) \approx 0.7, or ρ≈2\rho \approx 2? Let's look at our observed data again.

text
plot(1, type="n", xlab="x", ylab="y", main="", xlim=c(-11, 11), ylim=c(-10, 10))
points(data$x_obs, data$y_obs, col="white", pch=16, cex=1.2)
points(data$x_obs, data$y_obs, col="black", pch=16, cex=0.8)
grid <- sapply(0:10, function(n) abline(v=2 * n - 10, col="gray80", lwd=2, lty=3))

The threshold ρ=2\rho = 2 is exactly the minimal distance between the observed covariates. Just as the data don't inform length scales larger than the maximum covariate distance they also don't inform length scales shorter than the minimum covariate distance. Gaussian processes with length scales shorter than the minimal covariate distance support functions that are pinned near the observed data but are poorly constrained elsewhere.

In other words we've encountered another limitation of the experimental design, and all of the considerations of the previous section immediately apply. To prevent this degeneracy of the marginal likelihood function from propagating to the posterior density function we need a prior model that suppresses Gaussian processes with smaller length scales. Whether or not we can do this in a principled way depends on what domain expertise we can elicit.

3.2.3 Informative Prior Model

Let's presume that the covariate grid was chosen deliberately and motivated by domain expertise about both large and small length scales. In other words we can reasonably build a prior model that suppresses both length scales above 20 and below 2.

While there are multiple families of prior density functions that suppress both zero and infinity here we'll use an inverse gamma family. The probability density functions in the inverse gamma family all feature particularly strong suppression towards zero which is well-suited to moderating the threshold behavior of the small length scale degeneracy.

We can select a particular element of the inverse gamma family by specifying quantile constraints consistent with our domain expertise, for example

0.01=P[0,l]=∫0ldρ inv-gamma(ρ∣a,b)0.01=P[u,∞]=1−P[0,u]=1−∫0udρ inv-gamma(ρ∣a,b).\begin{align*} 0.01 = \mathbb{P}[0, l] &= \int_{0}^{l} \mathrm{d}\rho \, \text{inv-gamma}(\rho \mid a, b) \\ 0.01 = \mathbb{P}[u, \infty] = 1 - \mathbb{P}[0, u] &= 1 - \int_{0}^{u} \mathrm{d}\rho \, \text{inv-gamma}(\rho \mid a, b). \end{align*}
To solve for the parameters that identify the inverse gamma density function with these tail quantiles we appeal to Stan's algebraic solver and a careful initialization.

text
writeLines(readLines("stan_programs/prior_tune.stan"))
text
functions {
  vector tail_delta(vector y, vector theta, real[] x_r, int[] x_i) {
    vector[2] deltas;
    deltas[1] = inv_gamma_cdf(theta[1], exp(y[1]), exp(y[2])) - 0.01;
    deltas[2] = 1 - inv_gamma_cdf(theta[2], exp(y[1]), exp(y[2])) - 0.01;
    return deltas;
  }
}

transformed data {
  real l = 2;
  real u = 20;
  vector[2] theta = [l, u]';
    
  real delta = 1;
  real a = square(delta * (u + l) / (u - l)) + 2;
  real b =  ((u + l) / 2) * ( square(delta * (u + l) / (u - l)) + 1);
  vector[2] y_guess = [log(a), log(b)]';
  
  real x_r[0];
  int x_i[0];
  
  vector[2] y = algebra_solver(tail_delta, y_guess, theta, x_r, x_i);

  print("a = ", exp(y[1]));
  print("b = ", exp(y[2]));
}
text
fit <- stan(file='stan_programs/prior_tune.stan', iter=1, warmup=0, chains=1,
            seed=5838298, algorithm="Fixed_param")
text
a = 4.62909
b = 22.0673

SAMPLING FOR MODEL 'prior_tune' NOW (CHAIN 1).
Chain 1: Iteration: 1 / 1 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0 seconds (Warm-up)
Chain 1:                1.1e-05 seconds (Sampling)
Chain 1:                1.1e-05 seconds (Total)
Chain 1: 

We don't have to be precious about the exact values so I'll round to the first decimal place.

text
c_dark <- c("#8F2727")
c_dark_highlight <- c("#7C0000")

lambda <- seq(0, 25, 0.001)
dens <- lapply(lambda, function(l) dgamma(1 / l, 4.6, rate=22.1) * (1 / l**2))
plot(lambda, dens, type="l", col=c_dark_highlight, lwd=2,
     xlab="lambda", ylab="Prior Density", yaxt='n')

lambda98 <- seq(2, 20, 0.001)
dens <- lapply(lambda98, function(l) dgamma(1 / l, 4.6, rate=22.1) * (1 / l**2))
lambda98 <- c(lambda98, 20, 2)
dens <- c(dens, 0, 0)

polygon(lambda98, dens, col=c_dark, border=NA)

Now let's update the prior contributions in our Stan program and see how we do.

text
writeLines(readLines("stan_programs/fit_covar3.stan"))
text
functions {
  vector gp_pred_rng(real[] x2,
                     vector y1, real[] x1,
                     real alpha, real rho, real sigma, real delta) {
    int N1 = rows(y1);
    int N2 = size(x2);
    vector[N2] f2;
    {
      matrix[N1, N1] K =   cov_exp_quad(x1, alpha, rho)
                         + diag_matrix(rep_vector(square(sigma), N1));
      matrix[N1, N1] L_K = cholesky_decompose(K);

      vector[N1] L_K_div_y1 = mdivide_left_tri_low(L_K, y1);
      vector[N1] K_div_y1 = mdivide_right_tri_low(L_K_div_y1', L_K)';
      matrix[N1, N2] k_x1_x2 = cov_exp_quad(x1, x2, alpha, rho);
      vector[N2] f2_mu = (k_x1_x2' * K_div_y1);
      matrix[N1, N2] v_pred = mdivide_left_tri_low(L_K, k_x1_x2);
      matrix[N2, N2] cov_f2 =   cov_exp_quad(x2, alpha, rho) - v_pred' * v_pred
                              + diag_matrix(rep_vector(delta, N2));
      f2 = multi_normal_rng(f2_mu, cov_f2);
    }
    return f2;
  }
}

data {
  int<lower=1> N_obs;
  real x_obs[N_obs];
  vector[N_obs] y_obs;

  int<lower=1> N_predict;
  real x_predict[N_predict];
}

parameters {
  real<lower=0> rho;
  real<lower=0> alpha;
  real<lower=0> sigma;
}

model {
  matrix[N_obs, N_obs] cov =   cov_exp_quad(x_obs, alpha, rho)
                             + diag_matrix(rep_vector(square(sigma), N_obs));
  matrix[N_obs, N_obs] L_cov = cholesky_decompose(cov);

  rho ~ inv_gamma(4.6, 22.1); // P[rho < 2.0] \approx 0.01, P[rho > 20] \approx 0.01
  alpha ~ normal(0, 2);
  sigma ~ normal(0, 1);

  y_obs ~ multi_normal_cholesky(rep_vector(0, N_obs), L_cov);
}

generated quantities {
  vector[N_predict] f_predict = gp_pred_rng(x_predict, y_obs, x_obs, alpha, rho, sigma, 1e-10);
  real y_predict[N_predict] = normal_rng(f_predict, sigma);
}
text
fit <- stan(file='stan_programs/fit_covar3.stan', data=data,
            seed=5838298, refresh=1000)

Thankfully all of the diagnostic checks now pass without any indication of fitting problems.

text
util$check_all_diagnostics(fit)
text
[1] "n_eff / iter looks reasonable for all parameters"
[1] "Rhat looks reasonable for all parameters"
[1] "0 of 4000 iterations ended with a divergence (0%)"
[1] "0 of 4000 iterations saturated the maximum tree depth of 10 (0%)"
[1] "E-FMI indicated no pathological behavior"

By suppressing the degenerate plateaus at both small and large length scales we're left with a reasonably well-behaved posterior density function that doesn't frustrate the exploration of Hamiltonian Monte Carlo.

text
partition <- util$partition_div(fit)
nondiv_params <- partition[[2]]

par(mfrow=c(1, 3))

plot(log(nondiv_params$rho), log(nondiv_params$alpha),
    col=c_dark_trans, pch=16, cex=0.8, xlab="log(rho)", ylab="log(alpha)")

plot(log(nondiv_params$rho), log(nondiv_params$sigma),
    col=c_dark_trans, pch=16, cex=0.8, xlab="log(rho)", ylab="log(sigma)")

plot(log(nondiv_params$alpha), log(nondiv_params$sigma),
    col=c_dark_trans, pch=16, cex=0.8, xlab="log(alpha)", ylab="log(sigma)")

The posterior distribution for the Gaussian process and observational model parameters also capture the true values from which the observed data were simulated.

text
params <- extract(fit)

c_light <- c("#DCBCBC")

par(mfrow=c(1, 3))

alpha_breaks=10 * (0:50) / 50 - 5
hist(params$alpha, main="", xlab="alpha", ylab="",
     col=c_dark, border=c_dark_highlight, yaxt='n')
abline(v=alpha_true, col=c_light, lty=1, lwd=3)

beta_breaks=10 * (0:50) / 50 - 5
hist(params$rho, main="", xlab="rho", ylab="",
     col=c_dark, border=c_dark_highlight, yaxt='n')
abline(v=rho_true, col=c_light, lty=1, lwd=3)

sigma_breaks=5 * (0:50) / 50
hist(params$sigma, main="", xlab="sigma", ylab="",
     col=c_dark, border=c_dark_highlight, yaxt='n')
abline(v=sigma_true, col=c_light, lty=1, lwd=3)

Similarly the functional posterior captures the true functional behavior.

text
par(mfrow=c(1, 1))

util$plot_gp_post_realizations(fit, data, truth,
                               "Posterior Realizations")

text
util$plot_gp_post_quantiles(fit, data, truth,
                            "Posterior Marginal Quantiles")

This all manifests in a posterior predictive data that covers both the observed data and the unobserved data exceptionally well.

text
util$plot_gp_post_pred_quantiles(fit, data, truth,
                                 "Posterior Predictive Marginal Quantiles")

By heeding our computational diagnostics we were able to not only identify the degeneracies in the marginal likelihood function but also confirm their origin and work out an appropriate resolution with prior modeling.

3.3 Back To The House That Karhunen–Loève Built

With a better understanding of the degeneracies inherent to inference over the exponentiated quadratic family of Gaussian processes let's return to maximum marginal likelihood and see if we can't do any better.

The plateaus in the marginal likelihood function at small and large length scales help to explain why the optimization that we employed performed so poorly. Because the marginal likelihood function is constant in these regions the gradients will vanish, and any gradient-based optimizer will either freeze or continue on based on any inertia inherent to the algorithm. Where the optimizer encounters either plateau is highly sensitive to where the optimizer is initialized, which manifests as the instability we saw above.

We can avoid these degenerate plateaus, however, by using our final prior density function above as a penalty function in the optimization. In Stan this corresponds to using the same model block but switching the fitting algorithm to optimization. The only change we'll make to the Stan program is to remove the predictions in the generated quantities for convenience.

text
writeLines(readLines("stan_programs/fit_covar3_no_pred.stan"))
text
data {
  int<lower=1> N_obs;
  real x_obs[N_obs];
  vector[N_obs] y_obs;
}

parameters {
  real<lower=0> rho;
  real<lower=0> alpha;
  real<lower=0> sigma;
}

model {
  matrix[N_obs, N_obs] cov =   cov_exp_quad(x_obs, alpha, rho)
                             + diag_matrix(rep_vector(square(sigma), N_obs));
  matrix[N_obs, N_obs] L_cov = cholesky_decompose(cov);

  rho ~ inv_gamma(4.6, 22.1); // P[rho < 2.0] \approx 0.01, P[rho > 20] \approx 0.01
  alpha ~ normal(0, 2);
  sigma ~ normal(0, 1);

  y_obs ~ multi_normal_cholesky(rep_vector(0, N_obs), L_cov);
}
text
gp_opt2<- stan_model(file='stan_programs/fit_covar3_no_pred.stan')
opt_fit <- optimizing(gp_opt2, data=data, seed=5838298, hessian=FALSE)


alpha <- opt_fit$par[2]
rho <- opt_fit$par[1]
sigma <- opt_fit$par[3]

sprintf('alpha = %s', alpha)
text
[1] "alpha = 1.84195949538493"
text
sprintf('rho = %s', rho)
text
[1] "rho = 3.70531131785434"
text
sprintf('sigma = %s', sigma)
text
[1] "sigma = 2.00517559625945"

While both α\alpha and ρ\rho are underestimated relative to the true values of 33 and 5.55.5, respectively, σ\sigma is quite close to the true value of 22.

More encouraging these estimates appear to be stable relative to random initializations.

text
opt_fit <- optimizing(gp_opt2, data=data, seed=95848338, hessian=FALSE)

alpha <- opt_fit$par[2]
rho <- opt_fit$par[1]
sigma <- opt_fit$par[3]

sprintf('alpha = %s', alpha)
text
[1] "alpha = 1.8420795684571"
text
sprintf('rho = %s', rho)
text
[1] "rho = 3.70507531169028"
text
sprintf('sigma = %s', sigma)
text
[1] "sigma = 2.00514275911595"
text
opt_fit <- optimizing(gp_opt2, data=data, seed=2485813, hessian=FALSE)

alpha <- opt_fit$par[2]
rho <- opt_fit$par[1]
sigma <- opt_fit$par[3]

sprintf('alpha = %s', alpha)
text
[1] "alpha = 1.8417845260413"
text
sprintf('rho = %s', rho)
text
[1] "rho = 3.70499509458416"
text
sprintf('sigma = %s', sigma)
text
[1] "sigma = 2.00519003719586"

To see how well this Gaussian process captures the true functional behavior we can generate posterior realizations as before.

text
pred_data <- list(alpha=alpha, rho=rho, sigma=sigma,
                  N_obs=data$N_obs, x_obs=data$x_obs, y_obs=data$y_obs,
                  N_predict=data$N_predict, x_predict=data$x_predict)
pred_opt_fit <- stan(file='stan_programs/fit_normal_anal.stan', data=pred_data,
                     iter=1000, warmup=0, chains=1, seed=5838298, refresh=1000,
                     algorithm="Fixed_param")
text
SAMPLING FOR MODEL 'fit_normal_anal' NOW (CHAIN 1).
Chain 1: Iteration:   1 / 1000 [  0%]  (Sampling)
Chain 1: Iteration: 1000 / 1000 [100%]  (Sampling)
Chain 1: 
Chain 1:  Elapsed Time: 0 seconds (Warm-up)
Chain 1:                7.04352 seconds (Sampling)
Chain 1:                7.04352 seconds (Total)
Chain 1: 

Overall the functional posterior captures the true functional behavior reasonably well.

text
util$plot_gp_post_realizations(pred_opt_fit, data, truth,
                               "Posterior Realizations")

text
util$plot_gp_post_quantiles(pred_opt_fit, data, truth,
                            "Posterior Marginal Quantiles")

That said we can see a mild pull away from the true functional behavior and towards the observed data.

text
util$plot_gp_post_pred_quantiles(pred_opt_fit, data, truth,
                                 "Posterior Predictive Marginal Quantiles")

While a strongly-informative penalty function does drastically improves maximum marginal likelihood estimation, we have to be careful with the practicality of this result. Without having already quantified the entire posterior distribution we never would have ben able to motivate the needed penalty function in the first place. Once we've quantified the posterior distribution what additional utility does a point estimate offer?

3.4 One Singular Fluctuation

Taking the derivation of principled regularization for granted we can still ask what advantage, if any, Bayesian inference over the exponentiated quadratic family has over properly-penalized point estimation. Bayesian inference inherently provides better uncertainty quantification the exponentiated quadratic and observational model parameters than the maximum marginal likelihood estimate, but what difference is there in the inference of the functional behavior?

Let's compare the two results side by side.

text
par(mfrow=c(1, 2))

util$plot_gp_post_realizations(fit, data, truth,
                               "Posterior Realizations (Bayes)")

util$plot_gp_post_realizations(pred_opt_fit, data, truth,
                               "Posterior Realizations (MML)")

text
util$plot_gp_post_quantiles(fit, data, truth,
                            "Posterior Marginal Quantiles (Bayes)")
                      
util$plot_gp_post_quantiles(pred_opt_fit, data, truth,
                            "Posterior Marginal Quantiles (MML)")

text
util$plot_gp_post_pred_quantiles(fit, data, truth,
                                 "Posterior Predictive (Bayes)")
                           
util$plot_gp_post_pred_quantiles(pred_opt_fit, data, truth,
                                 "Posterior Predictive (MML)")

Although the differences are relatively small in this case the full Bayesian inference captures more functional behavior that better contains the true functional behavior, as well as providing more accurate predictions for both the observed and unobserved data. The superiority of averaging over the Gaussian processes in the squared exponential instead of resorting to a single element is no accident, but rather a critical consequence of Gaussian processes themselves.

To help understand why let's first consider the concept of the support of a probability distribution in a given parameterization of the ambient space. The support is defined as the smallest subset of the ambient space with probability one. Because conditions like "smallest" depend on the choice of parameterization so too will the definition of support! The support of a probability distribution within a given parameterization also defines the range of samples from that distribution.

For example consider a probability distribution specified by a normal probability density function within a given parameterization of the real line. The support in that parameterization is the entire real line, and a draw from the corresponding normal random number generator can take values anywhere on the real line. If we reparameterize with an exponential transformation then the distribution is specified by a log normal probability density function while the support, and range of possible sample values, shrinks to the positive real line.

Similarly we can ask what the support of a Gaussian process might be. By definition the support has to be contained within the infinite-dimensional space of all real-valued functions, but it's not clear how many functions the support of any one Gaussian process actually encapsulates. Remarkably the support of any single Gaussian process is only an infinitesimal slice of the space of all functions. Although that slice is still technically infinite-dimensional -- infinities are weird -- it encodes only a limited amount of possible functional behaviors.

A related question is how much the support of different elements of the exponentiated quadratic family overlap with each other. The stronger the overlap between Gaussian processes with similar marginal variation and length scale parameters the less sensitive our inferences will be to the choice of any single element, and the better point estimation will perform. Unfortunately the overlap is exactly zero, even for Gaussian processes with infinitesimally close parameters.

In other words every exponentiated quadratic Gaussian process supports its own distinct set of functional behaviors. Consequently if we cannot infer the true element exactly then any inference of the exponentiated quadratic family will never be able to capture the true functional behavior.

Although this sounds dire the consequences are not quite as dramatic in practice. The actual differences in functional behavior between different exponentiated quadratic Gaussian processes with similar parameters is often microscopic and unresolvable without nearly infinite amounts of data. When we average over the entire family of Gaussian processes, however, we aggregate all of these microscopic differences into a significant increase in the complexity of functional behavior captured in the model.

The utility of full Bayesian inference becomes particularly evident when the regularization of the prior model isn't as strong as we might want it to be and overfitting model configurations propagate to the posterior distribution like we saw in Section 3.2.2. While a point estimate might be vulnerable to these overfitting model configurations any posterior expectation will weight them against better behaved model configurations, moderating their influence and yielding much more robust inferences.

4 Advanced Topics

Although this case study is meant to serve as only a basic introduction to Gaussian processes and their use in probabilistic modeling, I do want to touch on a few advanced topics that one will eventually encounter when moving beyond these basic applications.

4.1 Additive Functional Models

One of the most common uses of Gaussian processes in probabilistic modeling is not to model the entirety of a functional behavior but rather just the residual behavior around simpler model. Given the parametric function f0(x,ϕ))f_{0}(x, \phi)) we might model the functional behavior as

f(x)=f0(x,ϕ)+f1(x)f(x) = f_{0}(x, \phi) + f_{1}(x)
where
f1∼GP(0,k1).f_{1} \sim \text{GP}(0, k_{1}).
If the residual behavior is particularly sophisticated we might add multiple Gaussian processes,
f(x)=f0(x,ϕ)+∑nfn(x)fn∼GP(0,kn),\begin{align*} f(x) &= f_{0}(x, \phi) + \sum_{n} f_{n}(x) \\ f_{n} &\sim \text{GP}(0, k_{n}), \end{align*}
where each component Gaussian process is responsible for modeling a particular kind of functional behavior. Because adding zero-mean Gaussian processes is equivalent to adding a single zero-mean Gaussian process with an additive covariance function,
fn∼GP(0,kn)∑nfn∼GP ⁣(0,∑nkn)\begin{align*} f_{n} &\sim \text{GP}(0, k_{n}) \\ \sum_{n} f_{n} &\sim \text{GP} \! \left( 0, \sum_{n} k_{n} \right) \end{align*}
these additive models are sometimes also referred to as additive covariance function models.

For example consider modeling counts arising from some source whose intensity is increasing with time, such as events in a growing population. A simple model might consider a Poisson observational model whose log intensity depends linearly on time,

pi(y;α,β,t)=Poisson(y;exp⁡(α+β⋅t)).pi(y; \alpha, \beta, t) = \text{Poisson}(y; \exp(\alpha + \beta \cdot t)).
We can model a richer time dependence by adding a Gaussian process to the log intensity,
log⁡λ=α+β⋅t+f(t)f∼GP(0,k).\begin{align*} \log \lambda &= \alpha + \beta \cdot t + f(t) \\ f &\sim \text{GP}(0, k). \end{align*}
If we want to model a more structured residual then we can add multiple Gaussian processes,
log⁡λ=α+β⋅t+∑nfn(t)fn∼GP(0,kn),\begin{align*} \log \lambda &= \alpha + \beta \cdot t + \sum_{n} f_{n}(t) \\ f_{n} &\sim \text{GP}(0, k_{n}), \end{align*}
for example with each component Gaussian process responsible for modeling residuals at different time scales.

The challenge with these models -- and really any additive model -- is ensuring that the components do not support overlapping functional behavior. If two components can capture the same behavior then posterior inference will have to explore both possibilities, inducing degenerate if not outright multimodal posterior density functions.

In the Poisson model with

log⁡λ=α+β⋅t+f(t)f∼GP(0,k).\begin{align*} \log \lambda &= \alpha + \beta \cdot t + f(t) \\ f &\sim \text{GP}(0, k). \end{align*}
this requires that the Gaussian process specified by the covariance function kk assigns little if any probability around functions that are approximately linear in the span of the observed times. When adding multiple Gaussian processes together we also have to ensure that the functions supported by the component Gaussian processes are sufficiently different.

If we don't know the covariance function for the residual Gaussian processes exactly and instead want to infer them from within a family of Gaussian processes then the only way that maintain the needed separation is through the prior model over the family. For example if we add multiple exponentiated quadratic Gaussian processes together then we need to ensure that the prior models for the marginal variation or length scale parameters have negligible overlap. In the Poisson time series example this might be achieved with the prior model that concentrates each length scale parameter around a distinct scale, such as

P[0.5 s<ρ1<5 s]=0.99P[0.5 min<ρ2<30 min]=0.99P[0.5 hr<ρ3<∞]=0.99.\begin{align*} \mathbb{P}[0.5 \, \mathrm{s} < \rho_{1} < 5 \, \mathrm{s}] &= 0.99 \\ \mathbb{P}[0.5 \, \mathrm{min} < \rho_{2} < 30 \, \mathrm{min} ] &= 0.99 \\ \mathbb{P}[0.5 \, \mathrm{hr} < \rho_{3} < \infty ] &= 0.99. \end{align*}
Ultimately Gaussian processes are like any other modeling technique -- they perform best when used generatively rather than as a black box!

4.2 High-Dimensional Covariate Spaces

Inferring functional behavior with Gaussian process priors can be considered a form of interpolation. The observational model constrains the functional behavior at the observed covariate values while the structure of the covariance function constrains how that behavior propagates between the observed covariate values.

The less distance between the observed covariates the less work the Gaussian process prior has to do and, generally, the more precise the inferred interpolation will be. When the observed covariates are far apart, however, the interpolations become more uncertain unless the Gaussian process prior compensates by concentrating on a smaller set of functional behaviors.

Unfortunately large distances are typical as the dimension of the covariate space increases. As I discuss in my probabilistic computation case study_is_the_loneliest_number) even if we can maintain the distance along each dimension, the total distance between any set of points in the covariate space will grow with the increasing dimension. Consequently for a fixed number of observed covariates values and a fixed Gaussian process prior the interpolation uncertainty will also grow.

In order to maintain the same predictive power between the covariate values we have to increase either the number of observed points or the domain expertise encoded in the Gaussian process prior model, typically exponentially so in both cases. Because of the costly scaling of Gaussian process computations, we can increase the number of covariate only so much in practice. At the same time we rarely have exponentially more domain expertise about the functional behavior between the observed covariates just waiting to be exploited.

These challenges are not unique to Gaussian processes but are instead inherent to any probabilistic interpolation method. Interpolation of functional behavior between observed points simply becomes a poorly-posed problem beyond a few dimensions. Ultimately interpolation methods like Gaussian processes are just mechanisms to convert domain expertise into prior models for functional behavior. If we don't provide enough useful domain expertise then the resulting prior models will be poorly informed no matter the details of that mechanism. Unfortunately we rarely have much domain expertise about the behavior of high-dimensional functions.

This has critical implications for emulation methods that try to model the behavior of expensive functions from a few costly evaluations. In low dimensions prior information about the functional behavior encoded in the Gaussian process can lead to reasonably precise inferences both around and far away from the evaluated points, but the model becomes less and less useful as the dimension of the input space increases. Probabilistic interpolation with Gaussian processes might perform better than other interpolation methods against this scaling, but ultimately there's only so much information that can be exploited, and questions about the unknown function that can be answered.

4.3 Approximating Gaussian Processes

Because the practical implementation of Gaussian process is so expensive there is a vast literature on Gaussian process approximations that compromise some of the structure of the covariance function in exchange for drastically reduced cost. The challenge with employing these approximations is ensuring that they don't compromise so much of the structure that we loose the interpretability that makes Gaussian process prior models so useful in modeling.

Principled approximations of Gaussian processes require a deeper understanding of the theoretical foundations of Gaussian processes than what I have introduced here. Most of them use the theory to pivot from constructing the covariance matrix of any finite dimensional projection to directly constructing the corresponding precision matrix. This dual approach not only can be cheaper but also is better suited to sparse matrix approximations; the precision matrix can often be well-approximated by a sparse matrix even when the covariance matrix cannot.

The precision matrix can also be directly related to alternative representations of Gaussian processes, such as spectral densities and stochastic differential equations. Although these are more mathematically sophisticated representations they can offer a wealth of understanding about the functional behavior encoded within a Gaussian process, complementing the interpretability of the covariance function representation.

For an example of the machinery required to project Matérn Gaussian processes onto a set of finite basis functions, such as splines, see [4].

One helpful way of evaluating Gaussian process approximations is through backwards error analysis. Instead of trying to quantify the approximation error directly we can sometimes identify the Gaussian process that is exactly specified by an approximation and then compare that Gaussian process to the desired Gaussian process that we are trying to approximate. These comparisons often provide more information about how much the structure of the Gaussian process is compromised than many direct error quantifications.

5 Conclusion

Gaussian processes provide a powerful yet principled technique for modeling functional behavior, with applications spanning relatively simple regression models to sophisticated generative models. Incorporating entire families of interpretable Gaussian processes offers even more modeling capabilities, but only at the expense of degenerates once we condition on any finite set of observations. Fortunately principled prior modeling of these families can suppress these pathologies, leaving us with rich models spanning a massive space of well-behaved functional behaviors.

6 Acknowledgements

I thank Dan Simpson, Aki Vehtari, Rob Trangucci, Maggie Lieu, Finn Lindgren, Jeremy Silver, and Dom Di Francesco for helpful comments.

A very special thanks to everyone supporting me on Patreon: Aapeli Nevala, abcnap, Abhinav Katoch, Abhishek Vasu, Adam Bartonicek, Al King, Alan O'Donnell, Alex ANDORRA, Alexander Bartik, Alexander Noll, Allison M. Campbell, Anders Valind, Andrea Serafino, Andrew Rouillard, Annie Chen, Anthony Wuersch, Antoine Soubret, Arya, asif zubair, Austin Rochford, Austin Rochford, Avraham Adler, Bao Tin Hoang, Ben Matthews, Ben Swallow, Benjamin Phillabaum, Benoit Essiambre, Bo Schwartz Madsen, Brian Callander, Brian Hartley, Bryan Yu, Cameron Smith, Canaan Breiss, Cat Shark, Chad Scherrer, Charles Naylor, Chase Dwelle, Chibuikem Nwizu, Chris Zawora, Christopher Mehrvarzi, Cole Monnahan, Colin Carroll, Colin McAuliffe, D, Damien Mannion, dan mackinlay, Dan W Joyce, Daniel Hocking, Daniel Rowe, Darshan Pandit, David Burdelski, David Humeau, David Pascall, Derek G Miller, Diogo Melo, Doug Rivers, Ed Berry, Ed Cashin, Elizaveta Semenova, Erik Banek, Ero Carrera, Ethan Goan, Evan Russek, Fabio Zottele, Federico Carrone, Felipe González, Fergus Chadwick, Filipe Dias, Finn Lindgren, Florian Wellmann, Francesco Corona, Garrett Mooney, Gene K, Geoff Rollins, George Ho, Georgia S, Granville Matheson, Guido Biele, Guilherme Marthe, Hamed Bastan-Hagh, Hany Abdulsamad, Haonan Zhu, Ian , Ilaria Prosdocimi, Isaac S, J Michael Burgess, Jair Andrade Ortiz, James McInerney, James Wade, JamesU, Janek Berger, Jeff Dotson, Jeff Helzner, Jeffrey Arnold, Jeffrey Burnett, Jessica Graves, Joel Kronander, John Flournoy, John Zito, Jonathan Sedar, Jonathan St-onge, Jonathon Vallejo, Jose Pereira, Josh Weinstock, Joshua Mayer, Josué Mendoza, jsdenain , Justin Bois, Juuso Parkkinen, Karim Naguib, Karim Osman, Kazuki Yoshida, Kejia Shi, Kádár András, Lars Barquist, Leo Burgy, lizzie , Luiz Carvalho, Lukas Neugebauer, Marc Dotson, Marc Trunjer Kusk Nielsen, Mark Donoghoe, Markus P., Martin Modrák, Martin Rosecky, Matt Wescott, Matthew Kay, Matthew Quick, Matthew T Stern, Maurits van der Meer, Maxim Kesin, Michael Colaresi, Michael DeWitt, Michael Dillon, Michael Griffiths, Michael Grosskopf, Michael Redman, Michael Tracy, Mikael Elhouar, Mike Lawrence, Mikhail Popov, mikko heikkilä, MisterMentat , Márton Vaitkus, N Sanders, Nathan Rutenbeck, Nerf-Shepherd , Nicholas Cowie, Nicholas Erskine, Nicholas Knoblauch, Nicholas Ursa, Nick S, Nicolas Frisby, Noah Guzman, Okada, Ole Rogeberg, Oliver Clark, Oliver Crook, Olivier Ma, Omri Har Shemesh, Pablo León Villagrá, Patrick Boehnke, Pau Pereira Batlle, Paul Oreto, Peter Smits, Pieter van den Berg , Pintaius Pedilici, Ravin Kumar, Reed Harder, Riccardo Fusaroli, Richard Jiang, Richard Price, Robert Frost, Robert Goldman, Robert kohn, Robin Taylor, Roofwalker, Scott Block, Sean Talts, Seth Axen, Shira, sid phani, Simon Dirmeier, Simon Duane, Simon Lilburn, Srivatsa Srinath, Stephanie Fitzgerald, Stephen Lienhard, Stephen Oates, Steve Bertolani, Stijn, Stone Chen, Sue Marquez, Sus, Susan Holmes, Suyog Chandramouli, Sören Berg, Teddy Groves, Teresa Ortiz, Thomas Littrell, Thomas Siegert, Thomas Vladeck, Tiago Cabaço, Tim Howes, Tim Radtke, Tom McEwen, Tommy Naugle, Trey Spiller, Tuan Nguyen Anh, Tyrel Stokes, Vanda Inacio de Carvalho, Vince, Virginia Webber, vittorio trotta, Will Dearden, Will Farr, William Grosso, yolhaj , yureq , Z, and ZAKH.

References

[1] Cramér, H. and Leadbetter, M. R. (2004). Stationary and related stochastic processes. Dover Publications, Inc., Mineola, NY.

[2] Rasmussen, C. E. and Williams, C. K. I. (2006). Gaussian processes for machine learning. MIT Press, Cambridge, MA.

[3] Abrahamsen, P. (1997). A review of gaussian random fields and correlation functions.

[4] Lindgren, F., Rue, H. and Lindström, J. (2011). An explicit link between Gaussian fields and Gaussian Markov random fields: The stochastic partial differential equation approach. J. R. Stat. Soc. Ser. B Stat. Methodol. 73 423–98.

License

A repository containing the material used in this case study is available on GitHub.

The code in this case study is copyrighted by Michael Betancourt and licensed under the new BSD (3-clause) license:

https://opensource.org/licenses/BSD-3-Clause

The text and figures in this case study are copyrighted by Michael Betancourt and licensed under the CC BY-NC 4.0 license:

https://creativecommons.org/licenses/by-nc/4.0/

Original Computing Environment

text
writeLines(readLines(file.path(Sys.getenv("HOME"), ".R/Makevars")))
text
CC=clang

CXXFLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function -Wno-macro-redefined -Wno-unneeded-internal-declaration
CXX=clang++ -arch x86_64 -ftemplate-depth-256

CXX14FLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function -Wno-macro-redefined -Wno-unneeded-internal-declaration -Wno-unknown-pragmas
CXX14=clang++ -arch x86_64 -ftemplate-depth-256
text
sessionInfo()
text
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.7

Matrix products: default
BLAS:   /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRblas.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] colormap_0.1.4       rstan_2.19.3         ggplot2_3.3.1       
[4] StanHeaders_2.21.0-3

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.4.6       pillar_1.4.4       compiler_4.0.2     prettyunits_1.1.1 
 [5] tools_4.0.2        digest_0.6.25      pkgbuild_1.0.8     jsonlite_1.6.1    
 [9] evaluate_0.14      lifecycle_0.2.0    tibble_3.0.1       gtable_0.3.0      
[13] pkgconfig_2.0.3    rlang_0.4.6        cli_2.0.2          curl_4.3          
[17] parallel_4.0.2     yaml_2.2.1         xfun_0.16          loo_2.2.0         
[21] gridExtra_2.3      withr_2.2.0        stringr_1.4.0      dplyr_1.0.0       
[25] knitr_1.29         generics_0.0.2     vctrs_0.3.0        stats4_4.0.2      
[29] grid_4.0.2         tidyselect_1.1.0   glue_1.4.1         inline_0.3.15     
[33] R6_2.4.1           processx_3.4.2     fansi_0.4.1        rmarkdown_2.2     
[37] purrr_0.3.4        callr_3.4.3        magrittr_1.5       codetools_0.2-16  
[41] matrixStats_0.56.0 ps_1.3.3           scales_1.1.1       ellipsis_0.3.1    
[45] htmltools_0.4.0    assertthat_0.2.1   colorspace_1.4-1   V8_3.2.0          
[49] stringi_1.4.6      munsell_0.5.0      crayon_1.3.4