| Type: | Package |
| Title: | Superfast Likelihood Inference for Stationary Gaussian TimeSeries |
| Version: | 2.0.4 |
| Date: | 2025-09-09 |
| Description: | Likelihood evaluations for stationary Gaussian time series are typically obtained via the Durbin-Levinson algorithm, which scales as O(n^2) in the number of time series observations. This package provides a "superfast" O(n log^2 n) algorithm written in C++, crossing over with Durbin-Levinson around n = 300. Efficient implementations of the score and Hessian functions are also provided, leading to superfast versions of inference algorithms such as Newton-Raphson and Hamiltonian Monte Carlo. The C++ code provides a Toeplitz matrix class packaged as a header-only library, to simplify low-level usage in other packages and outside of R. |
| URL: | https://github.com/mlysy/SuperGauss |
| BugReports: | https://github.com/mlysy/SuperGauss/issues |
| License: | GPL-3 |
| Depends: | R (≥ 3.0.0) |
| Imports: | stats, methods, R6, Rcpp (≥ 0.12.7), fftw |
| LinkingTo: | Rcpp, RcppEigen |
| Suggests: | knitr, rmarkdown, testthat, mvtnorm, numDeriv |
| VignetteBuilder: | knitr |
| RoxygenNote: | 7.3.2 |
| Encoding: | UTF-8 |
| SystemRequirements: | fftw3 (>= 3.1.2) |
| NeedsCompilation: | yes |
| Packaged: | 2025-09-09 15:53:15 UTC; mlysy |
| Author: | Yun Ling [aut], Martin Lysy [aut, cre] |
| Maintainer: | Martin Lysy <mlysy@uwaterloo.ca> |
| Repository: | CRAN |
| Date/Publication: | 2025-09-10 07:51:19 UTC |
Superfast inference for stationary Gaussian time series.
Description
Likelihood evaluations for stationary Gaussian time series are typically obtained via the Durbin-Levinson algorithm, which scales as O(n^2) in the number of time series observations. This package provides a "superfast" O(n log^2 n) algorithm written in C++, crossing over with Durbin-Levinson around n = 300. Efficient implementations of the score and Hessian functions are also provided, leading to superfast versions of inference algorithms such as Newton-Raphson and Hamiltonian Monte Carlo. The C++ code provides a Toeplitz matrix class packaged as a header-only library, to simplify low-level usage in other packages and outside of R.
Details
While likelihood calculations with stationary Gaussian time series generally scale asO(N^2) in the number of observations, this package implements an algorithm which scales asO(N log^2 N). "Superfast" algorithms for loglikelihood gradients and Hessians are also provided. The underlying C++ code is distributed through a header-only library found in the installed package'sinclude directory.
Author(s)
Maintainer: Martin Lysymlysy@uwaterloo.ca
Authors:
Yun Ling
See Also
Useful links:
Examples
# Superfast inference for the timescale parameter# of the exponential autocorrelation functionexp_acf <- function(lambda) exp(-(1:N-1)/lambda)# simulate datalambda0 <- 1N <- 1000X <- rnormtz(n = 1, acf = exp_acf(lambda0))# loglikelihood function# allocate memory for a NormalToeplitz distribution objectNTz <- NormalToeplitz$new(N)loglik <- function(lambda) { NTz$logdens(z = X, acf = exp_acf(lambda)) ## dSnorm(X = X, acf = Toep, log = TRUE)}# maximum likelihood estimationoptimize(f = loglik, interval = c(.2, 5), maximum = TRUE)Cholesky multiplication with Toeplitz variance matrices.
Description
Multiplies the Cholesky decomposition of the Toeplitz matrix with another matrix, or solves a system of equations with the Cholesky factor.
Usage
cholZX(Z, acf)cholXZ(X, acf)Arguments
Z | Length- |
acf | Length- |
X | Length- |
Details
IfC == t(chol(toeplitz(acf))), thencholZX() computesC %*% Z andcholZX() computessolve(C, X). Both functions use the Durbin-Levinson algorithm.
Value
SizeN x p residual or observation matrix.
Examples
N <- 10p <- 2acf <- exp(-(1:N - 1))Z <- matrix(rnorm(N * p), N, p)cholZX(Z = Z, acf = acf) - (t(chol(toeplitz(acf))) %*% Z)X <- matrix(rnorm(N * p), N, p)cholXZ(X = X, acf = acf) - solve(t(chol(toeplitz(acf))), X)Constructor and methods for Circulant matrix objects.
Description
Constructor and methods for Circulant matrix objects.
Methods
Public methods
Methodnew()
Class constructor.
Usage
Circulant$new(N, uacf, upsd)
Arguments
NSize of Circulant matrix.
uacfOptional vector of
Nu = floor(N/2)+1unique elements of the autocorrelation.upsdOptional vector of
Nu = floor(N/2)+1unique elements of the PSD.
Returns
ACirculant object.
Methodsize()
Get the size of the Circulant matrix.
Usage
Circulant$size()
Returns
Size of the Circulant matrix.
Methodset_acf()
Set the autocorrelation of the Circulant matrix.
Usage
Circulant$set_acf(uacf)
Arguments
uacfVector of
Nu = floor(N/2)+1unique elements of the autocorrelation.
Methodget_acf()
Get the autocorrelation of the Circulant matrix.
Usage
Circulant$get_acf()
Returns
The complete autocorrelation vector of lengthN.
Methodset_psd()
Set the PSD of the Circulant matrix.
The power spectral density (PSD) of a Circulant matrixCt = Circulant(acf) is defined aspsd = iFFT(acf).
Usage
Circulant$set_psd(upsd)
Arguments
upsdVector of
Nu = floor(N/2)+1unique elements of the psd.
Methodget_psd()
Get the PSD of the Circulant matrix.
Usage
Circulant$get_psd()
Returns
The complete PSD vector of lengthN.
Methodhas_acf()
Check whether the autocorrelation of the Circulant matrix has been set.
Usage
Circulant$has_acf()
Returns
Logical;TRUE ifCirculant$set_acf() has been called.
Methodprod()
Circulant matrix-matrix product.
Usage
Circulant$prod(x)
Arguments
xVector or matrix with
Nrows.
Returns
The matrix productCt %*% x.
Methodsolve()
Solve a Circulant system of equations.
Usage
Circulant$solve(x)
Arguments
xOptional vector or matrix with
Nrows.
Returns
The solution inz to the system of equationsCt %*% z = x. Ifx is missing, returns the inverse ofCt.
Methodlog_det()
Calculate the log-determinant of the Circulant matrix.
Usage
Circulant$log_det()
Returns
The log-determinantlog(det(Ct)).
Methodclone()
The objects of this class are cloneable with this method.
Usage
Circulant$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Multivariate normal with Circulant variance matrix.
Description
Provides methods for the Normal-Circulant (NCt) distribution, which for a random vectorz of lengthN is defined as
z ~ NCt(uacf) <=> z ~ Normal(0, toeplitz(acf)),
whereuacf are theNu = floor(N/2)+1 unique elements of the autocorrelation vectoracf, i.e.,
acf = (uacf, rev(uacf[2:(Nu-1)]), N even, = (uacf, rev(uacf[2:Nu])), N odd.
Methods
Public methods
Methodnew()
Class constructor.
Usage
NormalCirculant$new(N)
Arguments
NSize of the NCt random vector.
Returns
ANormalCirculant object.
Methodsize()
Get the size of the NCt random vector.
Usage
NormalCirculant$size()
Returns
Size of the NCt random vector.
Methodlogdens()
Log-density function.
Usage
NormalCirculant$logdens(z, uacf)
Arguments
zDensity argument. A vector of length
Nor anN x n_obsmatrix where each column is anN-dimensional observation.uacfA vector of length
Nu = floor(N/2)containing the first half of the autocorrelation (i.e., first row/column) of the Circulant variance matrix.
Returns
A scalar or vector of lengthn_obs containing the log-density of the NCt evaluated at its arguments.
Methodgrad_full()
Full gradient of log-density function.
Usage
NormalCirculant$grad_full(z, uacf, calc_dldz = TRUE, calc_dldu = TRUE)
Arguments
zDensity argument. A vector of length
N.uacfA vector of length
Nu = floor(N/2)containing the first half of the autocorrelation (i.e., first row/column) of the Circulant variance matrix.calc_dldzWhether or not to calculate the gradient with respect to
z.calc_dlduWhether or not to calculate the gradient with respect to
uacf.
Returns
A list with elements:
ldensThe log-density evaluated at
zanduacf.dldzThe length-
Ngradient vector with respect toz, ifcalc_dldz = TRUE.dlduThe length-
Nu = floor(N/2)+1gradient vector with respect touacf, ifcalc_dldu = TRUE.
Methodclone()
The objects of this class are cloneable with this method.
Usage
NormalCirculant$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Multivariate normal with Toeplitz variance matrix.
Description
Provides methods for the Normal-Toeplitz (NTz) distribution defined as
z ~ NTz(acf) <=> z ~ Normal(0, toeplitz(acf)),
i.e., for a multivariate normal with mean zero and varianceTz = toeplitz(acf).
Methods
Public methods
Methodnew()
Class constructor.
Usage
NormalToeplitz$new(N)
Arguments
NSize of the NTz random vector.
Returns
ANormalToeplitz object.
Methodsize()
Get the size of the NTz random vector.
Usage
NormalToeplitz$size()
Returns
Size of the NTz random vector.
Methodlogdens()
Log-density function.
Usage
NormalToeplitz$logdens(z, acf)
Arguments
zDensity argument. A vector of length
Nor anN x n_obsmatrix where each column is anN-dimensional observation.acfA vector of length
Ncontaining the autocorrelation (i.e., first row/column) of the Toeplitz variance matrix.
Returns
A scalar or vector of lengthn_obs containing the log-density of the NTz evaluated at its arguments.
Methodgrad()
Gradient of the log-density with respect to parameters.
Usage
NormalToeplitz$grad(z, dz, acf, dacf, full_out = FALSE)
Arguments
zDensity argument. A vector of length
N.dzAn
N x n_thetamatrix containing the gradientdz/dtheta.acfA vector of length
Ncontaining the autocorrelation of the Toeplitz variance matrix.dacfAn
N x n_thetamatrix containing the gradientdacf/dtheta.full_outIf
TRUE, returns the log-density as well (see 'Value').
Returns
A vector of lengthn_theta containing the gradient of the NTz log-density with respect totheta, or a list with elementsldens andgrad consisting of the log-density and the gradient vector.
Methodhess()
Hessian of log-density with respect to parameters.
Usage
NormalToeplitz$hess(z, dz, d2z, acf, dacf, d2acf, full_out = FALSE)
Arguments
zDensity argument. A vector of length
N.dzAn
N x n_thetamatrix containing the gradientdz/dtheta.d2zAn
N x n_theta x n_thetaarray containing the Hessiand^2z/dtheta^2.acfA vector of length
Ncontaining the autocorrelation of the Toeplitz variance matrix.dacfAn
N x n_thetamatrix containing the gradientdacf/dtheta.d2acfAn
N x n_theta x n_thetaarray containing the Hessiandacf^2/dtheta^2.full_outIf
TRUE, returns the log-density and its gradient as well (see 'Value').
Returns
Ann_theta x n_theta matrix containing the Hessian of the NTz log-density with respect totheta, or a list with elementsldens,grad, andhess consisting of the log-density, its gradient (a vector of sizen_theta), and the Hessian matrix, respectively.
Methodgrad_full()
Full gradient of log-density function.
Usage
NormalToeplitz$grad_full(z, acf, calc_dldz = TRUE, calc_dlda = TRUE)
Arguments
zDensity argument. A vector of length
N.acfA vector of length
Ncontaining the autocorrelation of the Toeplitz variance matrix.calc_dldzWhether or not to calculate the gradient with respect to
z.calc_dldaWhether or not to calculate the gradient with respect to
acf.
Returns
A list with elements:
ldensThe log-density evaluated at
zandacf.dldzThe length-
Ngradient vector with respect toz, ifcalc_dldz = TRUE.dldaThe length-
Ngradient vector with respect toacf, ifcalc_dlda = TRUE.
Methodclone()
The objects of this class are cloneable with this method.
Usage
NormalToeplitz$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Defunct functions inSuperGauss.
Description
Defunct functions inSuperGauss.
The following functions have been removed from theSuperGauss package
rSnorm()Please use
rnormtz()instead.dSnorm()Please use
dnormtz()instead.Snorm.grad()Please use the
grad()method in theNormalToeplitz class.Snorm.hess()Please use the
hess()method in theNormalToeplitz class.
Constructor and methods for Toeplitz matrix objects.
Description
TheToeplitz class contains efficient methods for linear algebra with symmetric positive definite (i.e., variance) Toeplitz matrices.
Usage
is.Toeplitz(x)as.Toeplitz(x)## S3 method for class 'Toeplitz'dim(x)Arguments
x | An R object. |
Details
AnN x N Toeplitz matrixTz is defined by its length-N "autocorrelation" vectoracf, i.e., first row/columnTz. Thus, for the functionstats::toeplitz(), we haveTz = toeplitz(acf).
It is assumed thatacf defines a valid (i.e., positive definite) variance matrix. The matrix multiplication methods still work when this is not the case but the other methods do not (return values typically containNaNs).
as.Toeplitz(x) attempts to convert its argument to aToeplitz object by callingToeplitz$new(acf = x).is.Toeplitz(x) checks whether its argument is aToeplitz object.
Methods
Public methods
Methodnew()
Class constructor.
Usage
Toeplitz$new(N, acf)
Arguments
NSize of Toeplitz matrix.
acfAutocorrelation vector of length
N.
Returns
AToeplitz object.
Methodprint()
Print method.
Usage
Toeplitz$print()
Methodsize()
Get the size of the Toeplitz matrix.
Usage
Toeplitz$size()
Returns
Size of the Toeplitz matrix.ncol(),nrow(), anddim() methods forToeplitz objects also work as expected.
Methodset_acf()
Set the autocorrelation of the Toeplitz matrix.
Usage
Toeplitz$set_acf(acf)
Arguments
acfAutocorrelation vector of length
N.
Methodget_acf()
Get the autocorrelation of the Toeplitz matrix.
Usage
Toeplitz$get_acf()
Returns
The autocorrelation vector of lengthN.
Methodhas_acf()
Check whether the autocorrelation of the Toeplitz matrix has been set.
Usage
Toeplitz$has_acf()
Returns
Logical;TRUE ifToeplitz$set_acf() has been called.
Methodprod()
Toeplitz matrix-matrix product.
Usage
Toeplitz$prod(x)
Arguments
xVector or matrix with
Nrows.
Returns
The matrix productTz %*% x.Tz %*% x andx %*% Tz also work as expected.
Methodsolve()
Solve a Toeplitz system of equations.
Usage
Toeplitz$solve(x, method = c("gschur", "pcg"), tol = 1e-10)Arguments
xOptional vector or matrix with
Nrows.methodSolve method to use. Choices are:
gschurfor a modified version of the Generalized Schur algorithm of Ammar & Gragg (1988), orpcgfor the preconditioned conjugate gradient method of Chen et al (2006). The former is faster and obtains the log-determinant as a direct biproduct. The latter is more numerically stable for long-memory autocorrelations.tolTolerance level for the
pcgmethod.
Returns
The solution inz to the system of equationsTz %*% z = x. Ifx is missing, returns the inverse ofTz.solve(Tz, x) andsolve(Tz, x, method, tol) also work as expected.
Methodlog_det()
Calculate the log-determinant of the Toeplitz matrix.
Usage
Toeplitz$log_det()
Returns
The log-determinantlog(det(Tz)).determinant(Tz) also works as expected.
Methodtrace_grad()
Computes the trace-gradient with respect to Toeplitz matrices.
Usage
Toeplitz$trace_grad(acf2)
Arguments
acf2Length-
Nautocorrelation vector of the second Toeplitz matrix. This matrix must be symmetric but not necessarily positive definite.
Returns
Computes the trace of
solve(Tz, toeplitz(acf2)).
This is used in the computation of the gradient oflog(det(Tz(theta))) with respect totheta.
Methodtrace_hess()
Computes the trace-Hessian with respect to Toeplitz matrices.
Usage
Toeplitz$trace_hess(acf2, acf3)
Arguments
acf2Length-
Nautocorrelation vector of the second Toeplitz matrix. This matrix must be symmetric but not necessarily positive definite.acf3Length-
Nautocorrelation vector of the third Toeplitz matrix. This matrix must be symmetric but not necessarily positive definite.
Returns
Computes the trace of
solve(Tz, toeplitz(acf2)) %*% solve(Tz, toeplitz(acf3)).
This is used in the computation of the Hessian oflog(det(Tz(theta))) with respect totheta.
Methodclone()
The objects of this class are cloneable with this method.
Usage
Toeplitz$clone(deep = FALSE)
Arguments
deepWhether to make a deep clone.
Examples
# construct a Toeplitz matrixacf <- exp(-(1:5))Tz <- Toeplitz$new(acf = acf)# alternatively, can allocate space firstTz <- Toeplitz$new(N = length(acf))Tz$set_acf(acf = acf)# basic methodsTz$get_acf() # extract the acfdim(Tz) # == c(nrow(Tz), ncol(Tz))Tz # print method# linear algebra methodsX <- matrix(rnorm(10), 5, 2)Tz %*% Xt(X) %*% Tzsolve(Tz, X)determinant(Tz) # log-determinantConvert position autocorrelations to increment autocorrelations.
Description
Convert the autocorrelation of a stationary sequencex = (x_1, ..., x_N) to that of its increments,dx = (x_2 - x_1, ..., x_N - x_(N-1)).
Usage
acf2incr(acf)Arguments
acf | Length- |
Value
LengthN-1 vector of increment autocorrelations.
Examples
acf2incr(acf = exp(-(0:10)))Convert autocorrelation of stationary increments to mean squared displacement of posititions.
Description
Converts the autocorrelation of a stationary increment sequencedx = (x_1 - x_0, ..., x_N - x_(N-1)) to the mean squared displacement (MSD) of the corresponding positions, i.e.,MSD_i = E[(x_i - x_0)^2].
Usage
acf2msd(acf)Arguments
acf | Length- |
Value
Length-N MSD vector of the corresponding positions.
Examples
acf2msd(acf = exp(-(0:10)))Density of a multivariate normal with Toeplitz variance matrix.
Description
Density of a multivariate normal with Toeplitz variance matrix.
Usage
dnormtz(X, mu, acf, log = FALSE, method = c("gschur", "ltz"))Arguments
X | Vector of length |
mu | Vector or matrix of mean values of compatible dimensions with |
acf | Vector of length |
log | Logical; whether to return the multivariate normal density on the log scale. |
method | Which calculation method to use. Choices are: |
Value
Vector ofn (log-)densities, one for each column ofX.
Examples
# simulate dataN <- 10 # length of each time seriesn <- 3 # number of time seriestheta <- 0.1lambda <- 2mu <- theta^2 * rep(1, N)acf <- exp(-lambda * (1:N - 1))X <- rnormtz(n, acf = acf) + mu# evaluate log-densitydnormtz(X, mu, acf, log = TRUE)Mean square displacement of fractional Brownian motion.
Description
Mean square displacement of fractional Brownian motion.
Usage
fbm_msd(tseq, H)Arguments
tseq | Length- |
H | Hurst parameter (between 0 and 1). |
Details
The mean squared displacement (MSD) of a stochastic processX_t is defined as
MSD(t) = E[(X_t - X_0)^2].
Fractional Brownian motion (fBM) is a continuous Gaussian process with stationary increments, such that its covariance function is entirely defined the MSD, which in this case isMSD(t) = |t|^(2H).
Value
Length-N vector of mean square displacements.
Examples
fbm_msd(tseq = 1:10, H = 0.4)Matern autocorrelation function.
Description
Matern autocorrelation function.
Usage
matern_acf(tseq, lambda, nu)Arguments
tseq | Vector of |
lambda | Timescale parameter. |
nu | Smoothness parameter. |
Details
The Matern autocorrelation is given by
\mathrm{\scriptsize ACF}(t) = \frac{2^{1-\nu}}{\Gamma(\nu)} \left(\sqrt{2\nu}\frac{t}{\lambda}\right)^\nu K_\nu\left(\sqrt{2\nu} \frac{t}{\lambda}\right),
whereK_\nu(x) is the modified Bessel function of second kind.
Value
An autocorrelation vector of lengthN.
Examples
matern_acf(tseq = 1:10, lambda = 1, nu = 3/2)Convert mean square displacement of positions to autocorrelation of increments.
Description
Converts the mean squared displacement (MSD) of a stationary increments sequencex = (x_0, x_1, ..., x_N) positions to the autocorrelation of the corresponding incrementsdx = (x_1 - x_0, ..., x_N - x_(N-1)).
Usage
msd2acf(msd)Arguments
msd | Length- |
Value
Length-N autocorrelation vector.
Examples
# autocorrelation of fBM incrementsmsd2acf(msd = fbm_msd(tseq = 0:10, H = .3))Power-exponential autocorrelation function.
Description
Power-exponential autocorrelation function.
Usage
pex_acf(tseq, lambda, rho)Arguments
tseq | Vector of |
lambda | Timescale parameter. |
rho | Power parameter. |
Details
The power-exponential autocorrelation function is given by:
\mathrm{\scriptsize ACF}(t) = \exp \left\{-(t/\lambda)^\rho\right\}.
Value
An autocorrelation vector of lengthN.
Examples
pex_acf(tseq = 1:10, lambda = 1, rho = 2)Simulate a stationary Gaussian time series.
Description
Simulate a stationary Gaussian time series.
Usage
rnormtz(n = 1, acf, Z, fft = TRUE, nkeep, tol = 1e-06)Arguments
n | Number of time series to generate. |
acf | Length- |
Z | Optional size |
fft | Logical; whether or not to use the |
nkeep | Length of time series. Defaults to |
tol | Relative tolerance on negative eigenvalues. See Details. |
Details
The FFT method fails when the embedding circulant matrix is not positive definite. This is typically due to one of two things:
Roundoff error can make tiny eigenvalues appear negative. For this purpose, argument
tolcan be used to replace all negative eigenvalues bytol * ev_max, whereev_maxis the largest eigenvalue.The autocorrelation is decaying too slowly on the given timescale. To mitigate this, argument
nkeepcan be used to supply a longeracfthan is required, and keep only the firstnkeeptime series observations. For consistency,nkeepalso applies to Durbin-Levinson method.
Value
Length-nkeep vector or sizenkeep x n matrix with time series as columns.
Examples
N <- 10acf <- exp(-(1:N - 1)/N)rnormtz(n = 3, acf = acf)Toeplitz matrix multiplication.
Description
Efficient matrix multiplication with Toeplitz matrix and arbitrary matrix or vector.
Usage
toep.mult(acf, X)Arguments
acf | Length- |
X | Vector or matrix of compatible dimensions with |
Value
AnN-row matrix corresponding totoeplitz(acf) %*% X.
Examples
N <- 20d <- 3acf <- exp(-(1:N))X <- matrix(rnorm(N*d), N, d)toep.mult(acf, X)