Movatterモバイル変換


[0]ホーム

URL:


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 as⁠O(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:

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-N or⁠N x p⁠ matrix of residuals.

acf

Length-N autocorrelation vector of the Toeplitz variance matrix.

X

Length-N or⁠N x p⁠ matrix of observations.

Details

IfC == t(chol(toeplitz(acf))), thencholZX() computesC %*% Z andcholZX() computessolve(C, X). Both functions use the Durbin-Levinson algorithm.

Value

Size⁠N 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
N

Size of Circulant matrix.

uacf

Optional vector ofNu = floor(N/2)+1 unique elements of the autocorrelation.

upsd

Optional vector ofNu = floor(N/2)+1 unique 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
uacf

Vector ofNu = floor(N/2)+1 unique 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
upsd

Vector ofNu = floor(N/2)+1 unique 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
x

Vector or matrix withN rows.

Returns

The matrix productCt %*% x.


Methodsolve()

Solve a Circulant system of equations.

Usage
Circulant$solve(x)
Arguments
x

Optional vector or matrix withN rows.

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
deep

Whether 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
N

Size 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
z

Density argument. A vector of lengthN or an⁠N x n_obs⁠ matrix where each column is anN-dimensional observation.

uacf

A vector of lengthNu = 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
z

Density argument. A vector of lengthN.

uacf

A vector of lengthNu = floor(N/2) containing the first half of the autocorrelation (i.e., first row/column) of the Circulant variance matrix.

calc_dldz

Whether or not to calculate the gradient with respect toz.

calc_dldu

Whether or not to calculate the gradient with respect touacf.

Returns

A list with elements:

ldens

The log-density evaluated atz anduacf.

dldz

The length-N gradient vector with respect toz, ifcalc_dldz = TRUE.

dldu

The length-Nu = floor(N/2)+1 gradient vector with respect touacf, ifcalc_dldu = TRUE.


Methodclone()

The objects of this class are cloneable with this method.

Usage
NormalCirculant$clone(deep = FALSE)
Arguments
deep

Whether 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
N

Size 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
z

Density argument. A vector of lengthN or an⁠N x n_obs⁠ matrix where each column is anN-dimensional observation.

acf

A vector of lengthN containing 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
z

Density argument. A vector of lengthN.

dz

An⁠N x n_theta⁠ matrix containing the gradientdz/dtheta.

acf

A vector of lengthN containing the autocorrelation of the Toeplitz variance matrix.

dacf

An⁠N x n_theta⁠ matrix containing the gradientdacf/dtheta.

full_out

IfTRUE, 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
z

Density argument. A vector of lengthN.

dz

An⁠N x n_theta⁠ matrix containing the gradientdz/dtheta.

d2z

An⁠N x n_theta x n_theta⁠ array containing the Hessian⁠d^2z/dtheta^2⁠.

acf

A vector of lengthN containing the autocorrelation of the Toeplitz variance matrix.

dacf

An⁠N x n_theta⁠ matrix containing the gradientdacf/dtheta.

d2acf

An⁠N x n_theta x n_theta⁠ array containing the Hessiandacf^2/dtheta^2.

full_out

IfTRUE, returns the log-density and its gradient as well (see 'Value').

Returns

An⁠n_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
z

Density argument. A vector of lengthN.

acf

A vector of lengthN containing the autocorrelation of the Toeplitz variance matrix.

calc_dldz

Whether or not to calculate the gradient with respect toz.

calc_dlda

Whether or not to calculate the gradient with respect toacf.

Returns

A list with elements:

ldens

The log-density evaluated atz andacf.

dldz

The length-N gradient vector with respect toz, ifcalc_dldz = TRUE.

dlda

The length-N gradient vector with respect toacf, ifcalc_dlda = TRUE.


Methodclone()

The objects of this class are cloneable with this method.

Usage
NormalToeplitz$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.


Defunct functions inSuperGauss.

Description

Defunct functions inSuperGauss.

The following functions have been removed from theSuperGauss package

rSnorm()

Please usernormtz() instead.

dSnorm()

Please usednormtz() instead.

Snorm.grad()

Please use thegrad() method in theNormalToeplitz class.

Snorm.hess()

Please use thehess() 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

An⁠N 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
N

Size of Toeplitz matrix.

acf

Autocorrelation vector of lengthN.

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
acf

Autocorrelation vector of lengthN.


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
x

Vector or matrix withN rows.

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
x

Optional vector or matrix withN rows.

method

Solve method to use. Choices are:gschur for a modified version of the Generalized Schur algorithm of Ammar & Gragg (1988), orpcg for 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.

tol

Tolerance level for thepcg method.

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
acf2

Length-N autocorrelation 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
acf2

Length-N autocorrelation vector of the second Toeplitz matrix. This matrix must be symmetric but not necessarily positive definite.

acf3

Length-N autocorrelation 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
deep

Whether 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-determinant

Convert position autocorrelations to increment autocorrelations.

Description

Convert the autocorrelation of a stationary sequence⁠x = (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-N vector of position autocorrelations.

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 sequence⁠dx = (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-N autocorrelation vector of a stationary increment sequence.

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 lengthN or⁠N x n⁠ matrix, of which each column is a multivariate observation.

mu

Vector or matrix of mean values of compatible dimensions withX. Defaults to all zeros.

acf

Vector of lengthN containing the first column of the Toeplitz variance matrix.

log

Logical; whether to return the multivariate normal density on the log scale.

method

Which calculation method to use. Choices are:gschur for a modified version of the Generalized Schur algorithm of Ammar & Gragg (1988), orltz for the Levinson-Trench-Zohar method. The former scales as⁠O(N log^2 N)⁠ whereas the latter scales asO(N^2) and should only be used forN < 300.

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-N vector of timepoints.

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 is⁠MSD(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 ofN time points at which the autocorrelation is to be calculated.

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 sequence⁠x = (x_0, x_1, ..., x_N)⁠ positions to the autocorrelation of the corresponding increments⁠dx = (x_1 - x_0, ..., x_N - x_(N-1))⁠.

Usage

msd2acf(msd)

Arguments

msd

Length-N MSD vector, i.e., excludingx_0 which is assumed to be zero.

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 ofN time points at which the autocorrelation is to be calculated.

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-N vector giving the autocorrelation of the series.

Z

Optional size⁠(2N-2) x n⁠ or⁠N x n⁠ matrix of iid standard normals, to use in the FFT and Durbin-Levinson methods, respectively.

fft

Logical; whether or not to use the⁠O(N log N)⁠ FFT-based algorithm of Wood and Chan (1994) or the more stableO(N^2) Durbin-Levinson algorithm. See Details.

nkeep

Length of time series. Defaults toN = length(acf). See Details.

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:

  1. Roundoff error can make tiny eigenvalues appear negative. For this purpose, argumenttol can be used to replace all negative eigenvalues bytol * ev_max, whereev_max is the largest eigenvalue.

  2. The autocorrelation is decaying too slowly on the given timescale. To mitigate this, argumentnkeep can be used to supply a longeracf than is required, and keep only the firstnkeep time series observations. For consistency,nkeep also applies to Durbin-Levinson method.

Value

Length-nkeep vector or size⁠nkeep 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-N vector giving the first column (or row) of the Toeplitz matrix.

X

Vector or matrix of compatible dimensions withacf.

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)

[8]ページ先頭

©2009-2025 Movatter.jp