epinowcast
Loading...
Searching...
No Matches
gaussian_process.stan File Reference

Go to the source code of this file.

Functions

vector diagSPD_EQ (real alpha, real rho, real L, int M)
 
vector matern_indices (int M, real L)
 
vector diagSPD_Matern12 (real alpha, real rho, real L, int M)
 
vector diagSPD_Matern32 (real alpha, real rho, real L, int M)
 
vector diagSPD_Matern52 (real alpha, real rho, real L, int M)
 
vector diagSPD_Periodic (real alpha, real rho, int M)
 
vector update_gp (matrix PHI, int M, real L, real alpha, real rho, vector eta, int type, real nu)
 
vector apply_gp_term (vector base, int present, int T, int G, int M, real L, int type, real nu, int d, matrix PHI, matrix eta, array[] real rho, array[] real alpha, array[] int flat_idx)
 

Function Documentation

◆ apply_gp_term()

vector apply_gp_term ( vector base,
int present,
int T,
int G,
int M,
real L,
int type,
real nu,
int d,
matrix PHI,
matrix eta,
array[]real rho,
array[]real alpha,
array[]int flat_idx )

Add an approximate Gaussian process latent term to a per-observation predictor, with optional integer-order differencing.

Encapsulates the spectral update, per-group evaluation, optional integration, and per-observation gather so module call sites are a single line. When present is 0 the input is returned unchanged. Each group shares the length scale rho and magnitude alpha; per-group spectral coefficients are stacked column-wise in eta.

The differencing order d integrates each group's realisation d times before it enters the predictor:

  • d = 0: stationary deviations of length T (the basis matrix is T x M). This is the original behaviour.
  • d >= 1: the basis matrix has T - d rows, so the spectral update yields T - d free values. These fill positions (d + 1):T of a length-T vector whose first d entries are zero; applying cumulative_sum() d times then integrates the series. Because the leading d entries start at zero, they stay zero through every integration pass, anchoring the first d values of the realisation to zero. This leaves the free level (for d = 1) and additionally the free slope (for d >= 2) to the module's fixed effects rather than the GP, which is the identifiability fix. d = 1 matches EpiNow2's non-stationary reproduction-number GP (gp[2:(gp_n + 1)] = noise; cumulative_sum).
Parameters
basePredictor to which the latent process is added.
present0 = inert, 1 = active.
TNumber of time points in the integrated series.
GNumber of groups.
MNumber of basis functions.
LBoundary factor.
typeKernel type (0 SE, 1 periodic, 2 Matern).
nuMatern smoothness.
dDifferencing (integration) order, d >= 0.
PHIBasis matrix ((T - d) x M, or (T - d) x 2M periodic).
etaSpectral coefficients ((2M or M) x G matrix; empty when not present).
rhoLength scale (length-1 array, empty when inert).
alphaMagnitude (length-1 array, empty when inert).
flat_idxPer-observation column-major index into the (T x G) latent matrix.
Returns
base + gathered Gaussian process contribution.

Definition at line 161 of file gaussian_process.stan.

◆ diagSPD_EQ()

vector diagSPD_EQ ( real alpha,
real rho,
real L,
int M )

Hilbert-space reduced-rank (spectral) approximate Gaussian process.

Adapted from EpiNow2 (https://github.com/epiforecasts/EpiNow2, inst/stan/functions/gaussian_process.stan, MIT licensed, copyright EpiForecasts). Lightly modified for epinowcast: array syntax, naming, and an apply_gp_term() wrapper that matches the apply_arima_residual() per-observation gather used elsewhere in this package.

The approximation follows:

A latent series f over T time points is approximated as f = PHI * (sqrt(S(rho, alpha)) .* eta), where PHI is a fixed (data) matrix of basis functions, S is the spectral density of the chosen kernel evaluated at the basis eigenvalues, and eta are unit-normal spectral coefficients. The per-observation contribution is gathered from the (T x G) latent matrix with a flat column-major index, mirroring the ARIMA path. Spectral density for the squared exponential kernel.

Definition at line 28 of file gaussian_process.stan.

◆ diagSPD_Matern12()

vector diagSPD_Matern12 ( real alpha,
real rho,
real L,
int M )

Spectral density for the 1/2 Matern (Ornstein-Uhlenbeck) kernel.

Definition at line 46 of file gaussian_process.stan.

◆ diagSPD_Matern32()

vector diagSPD_Matern32 ( real alpha,
real rho,
real L,
int M )

Spectral density for the 3/2 Matern kernel.

Definition at line 54 of file gaussian_process.stan.

◆ diagSPD_Matern52()

vector diagSPD_Matern52 ( real alpha,
real rho,
real L,
int M )

Spectral density for the 5/2 Matern kernel.

Definition at line 63 of file gaussian_process.stan.

◆ diagSPD_Periodic()

vector diagSPD_Periodic ( real alpha,
real rho,
int M )

Spectral density for the periodic kernel.

Definition at line 72 of file gaussian_process.stan.

◆ matern_indices()

vector matern_indices ( int M,
real L )

Squared spectral indices shared by the Matern kernels.

Definition at line 38 of file gaussian_process.stan.

◆ update_gp()

vector update_gp ( matrix PHI,
int M,
real L,
real alpha,
real rho,
vector eta,
int type,
real nu )

Update a Gaussian process using the spectral densities.

Parameters
PHIBasis function matrix (T x M, or T x 2M for periodic).
MNumber of basis functions.
LBoundary factor.
alphaMagnitude (marginal standard deviation).
rhoLength scale.
etaSpectral coefficients (M, or 2M for periodic).
type0 = squared exponential, 1 = periodic, 2 = Matern.
nuMatern smoothness; one of 0.5, 1.5, 2.5.
Returns
The latent process values (length = rows of PHI).

Definition at line 95 of file gaussian_process.stan.