Title: | Frequency Domain Based Analysis: Dynamic PCA |
---|---|
Description: | Implementation of dynamic principal component analysis (DPCA), simulation of VAR and VMA processes and frequency domain tools. These frequency domain methods for dimensionality reduction of multivariate time series were introduced by David Brillinger in his book Time Series (1974). We follow implementation guidelines as described in Hormann, Kidzinski and Hallin (2016), Dynamic Functional Principal Component <doi:10.1111/rssb.12076>. |
Authors: | Hormann S., Kidzinski L. |
Maintainer: | Kidzinski L. <[email protected]> |
License: | GPL-3 |
Version: | 2.0.5 |
Built: | 2024-12-03 06:58:58 UTC |
Source: | CRAN |
Implementation of dynamic principle component analysis (DPCA), simulation of VAR and VMA processes and frequency domain tools. The package also provides a toolset for developers simplifying construction of new frequency domain based methods for multivariate signals.
freqdom package allows you to manipulate time series objects in both time and frequency domains. We implement dynamic principal component analysis methods, enabling spectral decomposition of a stationary vector time series into uncorrelated components.
Dynamic principal component analysis enables estimation of temporal filters which transform a vector time series into another vector time series with uncorrelated components, maximizing the long run variance explained. There are two key differnces between classical PCA and dynamic PCA:
Components returned by the dynamic procedure are uncorrelated in time, i.e. for any
and
,
and
are uncorrelated,
The mapping maximizes the long run variance, which, in case of stationary vector time series, means
that the process reconstructed from and first dynamic principal components
better approximates your vector time series process than the first
classic principal components.
For details, please refer to literature below and to help pages of functions dpca
for estimating the components, dpca.scores
for estimating scores and
dpca.KLexpansion
for retrieving the signal from components.
Apart from frequency domain techniques for stationary vector time series,
freqdom provides a toolset of operators such as the vector Fourier Transform
(fourier.transform
) or a vector spectral density operator
(spectral.density
) as well as simulation of vector time series
models rar
, rma
generating vector
autoregressive and moving average respectively.
These functions enable developing new techniques based on the Frequency domain analysis.
Hormann Siegfried, Kidzinski Lukasz and Hallin Marc. Dynamic functional principal components. Journal of the Royal Statistical Society: Series B (Statistical Methodology) 77.2 (2015): 319-348.
Hormann Siegfried, Kidzinski Lukasz and Kokoszka Piotr. Estimation in functional lagged regression. Journal of Time Series Analysis 36.4 (2015): 541-561.
Hormann Siegfried and Kidzinski Lukasz. A note on estimation in Hilbertian linear models. Scandinavian journal of statistics 42.1 (2015): 43-62.
This function computes the empirical cross-covariance of two stationary multivariate time series. If only one time series is provided it determines the empirical autocovariance function.
cov.structure(X, Y = X, lags = 0)
cov.structure(X, Y = X, lags = 0)
X |
vector or matrix. If matrix, then each row corresponds to a timepoint of a vector time series. |
Y |
vector or matrix. If matrix, then each row corresponds to a timepoint of a vector time series. |
lags |
an integer-valued vector |
Let be a
matrix and
be a
matrix. We stack the vectors and
assume that
is a stationary multivariate time series
of dimension
. This function determines empirical lagged covariances between
the series
and
. More precisely it determines
for
lags,
where
is the empirical version of
.
For a sample of size
we set
and
and
and for
An object of class timedom
. The list contains
operators
an array. Element
[,,k]
contains the covariance matrix related to lag .
lags
returns the lags vector from the arguments.
Dynamic principal component analysis (DPCA) decomposes multivariate time series into uncorrelated components. Compared to classical principal components, DPCA decomposition outputs components which are uncorrelated in time, allowing simpler modeling of the processes and maximizing long run variance of the projection.
dpca(X, q = 30, freq = (-1000:1000/1000) * pi, Ndpc = dim(X)[2])
dpca(X, q = 30, freq = (-1000:1000/1000) * pi, Ndpc = dim(X)[2])
X |
a vector time series given as a |
q |
window size for the kernel estimator, i.e. a positive integer. |
freq |
a vector containing frequencies in |
Ndpc |
is the number of principal component filters to compute as in |
This convenience function applies the DPCA methodology and returns filters (dpca.filters
), scores
(dpca.scores
), the spectral density (spectral.density
), variances (dpca.var
) and
Karhunen-Leove expansion (dpca.KLexpansion
).
See the example for understanding usage, and help pages for details on individual functions.
A list containing
scores
DPCA scores (
dpca.scores
)
filters
DPCA filters (
dpca.filters
)
spec.density
spectral density of
X
(spectral.density
)
var
amount of variance explained by dynamic principal components (
dpca.var
)
Xhat
Karhunen-Loeve expansion using
Ndpc
dynamic principal components (dpca.KLexpansion
)
Hormann, S., Kidzinski, L., and Hallin, M. Dynamic functional principal components. Journal of the Royal Statistical Society: Series B (Statistical Methodology) 77.2 (2015): 319-348.
Brillinger, D. Time Series (2001), SIAM, San Francisco.
Shumway, R., and Stoffer, D. Time series analysis and its applications: with R examples (2010), Springer Science & Business Media
X = rar(100,3) # Compute DPCA with only one component res.dpca = dpca(X, q = 5, Ndpc = 1) # Compute PCA with only one component res.pca = prcomp(X, center = TRUE) res.pca$x[,-1] = 0 # Reconstruct the data var.dpca = (1 - sum( (res.dpca$Xhat - X)**2 ) / sum(X**2))*100 var.pca = (1 - sum( (res.pca$x %*% t(res.pca$rotation) - X)**2 ) / sum(X**2))*100 cat("Variance explained by DPCA:\t",var.dpca,"%\n") cat("Variance explained by PCA:\t",var.pca,"%\n")
X = rar(100,3) # Compute DPCA with only one component res.dpca = dpca(X, q = 5, Ndpc = 1) # Compute PCA with only one component res.pca = prcomp(X, center = TRUE) res.pca$x[,-1] = 0 # Reconstruct the data var.dpca = (1 - sum( (res.dpca$Xhat - X)**2 ) / sum(X**2))*100 var.pca = (1 - sum( (res.pca$x %*% t(res.pca$rotation) - X)**2 ) / sum(X**2))*100 cat("Variance explained by DPCA:\t",var.dpca,"%\n") cat("Variance explained by PCA:\t",var.pca,"%\n")
For a given spectral density matrix dynamic principal component filter sequences are computed.
dpca.filters(F, Ndpc = dim(F$operators)[1], q = 30)
dpca.filters(F, Ndpc = dim(F$operators)[1], q = 30)
F |
|
Ndpc |
an integer |
q |
a non-negative integer. DPCA filter coefficients at lags |
Dynamic principal components are linear filters ,
. They are defined as the Fourier coefficients of the dynamic eigenvector
of a spectral density matrix
:
The index is referring to the
-th #'largest dynamic eigenvalue. Since the
are
real, we have
For a given
spectral density (provided as on object of class freqdom
) the function
dpca.filters()
computes for
q
and
Ndpc
.
For more details we refer to Chapter 9 in Brillinger (2001), Chapter 7.8 in Shumway and Stoffer (2006) and to Hormann et al. (2015).
An object of class timedom
. The list has the following components:
operators
an array. Each matrix in this array has dimension
Ndpc
and is
assigned to a certain lag. For a given lag
, the rows of the matrix correpsond to
.
lags
a vector with the lags of the filter coefficients.
Hormann, S., Kidzinski, L., and Hallin, M. Dynamic functional principal components. Journal of the Royal Statistical Society: Series B (Statistical Methodology) 77.2 (2015): 319-348.
Brillinger, D. Time Series (2001), SIAM, San Francisco.
Shumway, R.H., and Stoffer, D.S. Time Series Analysis and Its Applications (2006), Springer, New York.
dpca.var
, dpca.scores
, dpca.KLexpansion
Computes the dynamic Karhunen-Loeve expansion of a vector time series up to a given order.
dpca.KLexpansion(X, dpcs)
dpca.KLexpansion(X, dpcs)
X |
a vector time series given as a |
dpcs |
an object of class |
We obtain the dynamic Karhnunen-Loeve expansion of order ,
. It is defined as
where are the dynamic PC filters as explained in
dpca.filters
and are dynamic scores as explained in
dpca.scores
. For the sample version the sum in extends over the range of lags for which the
are defined.
For more details we refer to Chapter 9 in Brillinger (2001), Chapter 7.8 in Shumway and Stoffer (2006) and to Hormann et al. (2015).
A -matix. The
-th column contains the
-th data point.
Hormann, S., Kidzinski, L., and Hallin, M. Dynamic functional principal components. Journal of the Royal Statistical Society: Series B (Statistical Methodology) 77.2 (2015): 319-348.
Brillinger, D. Time Series (2001), SIAM, San Francisco.
Shumway, R.H., and Stoffer, D.S. Time Series Analysis and Its Applications (2006), Springer, New York.
dpca.filters
, filter.process
, dpca.scores
Computes dynamic principal component score vectors of a vector time series.
dpca.scores(X, dpcs = dpca.filters(spectral.density(X)))
dpca.scores(X, dpcs = dpca.filters(spectral.density(X)))
X |
a vector time series given as a |
dpcs |
an object of class |
The -th dynamic principal components score sequence is defined by
where are the dynamic PC filters as explained in
dpca.filters
. For the sample version the sum extends
over the range of lags for which the are defined. The actual operation carried out is
filter.process(X, A = dpcs)
.
We for more details we refer to Chapter 9 in Brillinger (2001), Chapter 7.8 in Shumway and Stoffer (2006) and to Hormann et al. (2015).
A
Ndpc
-matix with Ndpc = dim(dpcs$operators)[1]
. The -th column contains the
-th dynamic principal component score sequence.
Hormann, S., Kidzinski, L., and Hallin, M. Dynamic functional principal components. Journal of the Royal Statistical Society: Series B (Statistical Methodology) 77.2 (2015): 319-348.
Brillinger, D. Time Series (2001), SIAM, San Francisco.
Shumway, R.H., and Stoffer, D.S. Time Series Analysis and Its Applications (2006), Springer, New York.
dpca.filters
, dpca.KLexpansion
, dpca.var
Computes the proportion of variance explained by a given dynamic principal component.
dpca.var(F)
dpca.var(F)
F |
|
Consider a spectral density matrix and let
by the
-th dynamic eigenvalue. The proportion of variance described by the
-th dynamic
principal component is given as
This function numerically computes the vectors .
For more details we refer to Chapter 9 in Brillinger (2001), Chapter 7.8 in Shumway and Stoffer (2006) and to Hormann et al. (2015).
A -dimensional vector containing the
.
Hormann, S., Kidzinski, L., and Hallin, M. Dynamic functional principal components. Journal of the Royal Statistical Society: Series B (Statistical Methodology) 77.2 (2015): 319-348.
Brillinger, D. Time Series (2001), SIAM, San Francisco.
Shumway, R.H., and Stoffer, D.S. Time Series Analysis and Its Applications (2006), Springer, New York.
dpca.filters
, dpca.KLexpansion
, dpca.scores
This function applies a linear filter to some vector time series.
filter.process(X, A) X %c% A
filter.process(X, A) X %c% A
X |
vector time series given in matrix form. Each row corresponds to a timepoint. |
A |
an object of class |
Let be a
matrix corresponding to a vector series
. This time series is transformed to the series
, where
The index of
is determined by the lags defined for the time domain object.
When index
falls outside the domain
we set
.
A matrix. Row corresponds to
.
filter.process()
: Multivariate convolution (filter) in the time domain
X %c% A
: Convenience operator for filter.process
function
Computes Fourier coefficients of some functional represented by an object of class freqdom
.
fourier.inverse(F, lags = 0)
fourier.inverse(F, lags = 0)
F |
an object of class |
lags |
lags of the Fourier coefficients to be computed. |
Consider a function . Its
-th Fourier
coefficient is given as
We represent the function by an object of class
freqdom
and approximate the integral via
for lags.
An object of class timedom
. The list has the following components:
operators
an array. The
-th matrix in this array corresponds to the
-th Fourier coefficient.
lags
the lags of the corresponding Fourier coefficients.
Y = rar(100) grid = c(pi*(1:2000) / 1000 - pi) #a dense grid on -pi, pi fourier.inverse(spectral.density(Y, q=2, freq=grid)) # compare this to cov.structure(Y)
Y = rar(100) grid = c(pi*(1:2000) / 1000 - pi) #a dense grid on -pi, pi fourier.inverse(spectral.density(Y, q=2, freq=grid)) # compare this to cov.structure(Y)
timedom
objectComputes the frequency response function of a linear filter and returns it as a freqdom
object.
fourier.transform(A, freq = pi * -100:100/100)
fourier.transform(A, freq = pi * -100:100/100)
A |
an object of class |
freq |
a vector of frequencies |
Consider a filter (a sequence of vectors or matrices) . Then this function computes
for all frequencies listed in the vector
freq
.
An object of class freqdom
.
# We compute the discrete Fourier transform (DFT) of a time series X_1,..., X_T. X = rar(100) T=dim(X)[1] tdX = timedom(X/sqrt(T),lags=1:T) DFT = fourier.transform(tdX, freq= pi*-1000:1000/1000)
# We compute the discrete Fourier transform (DFT) of a time series X_1,..., X_T. X = rar(100) T=dim(X)[1] tdX = timedom(X/sqrt(T),lags=1:T) DFT = fourier.transform(tdX, freq= pi*-1000:1000/1000)
Creates an object of class freqdom
. This object corresponds to a functional with domain and some complex vector space as codomain.
freqdom(F, freq)
freqdom(F, freq)
F |
a vector, a matrix or an array. For vectors |
freq |
a vector of dimension |
This class is used to describe a frequency domain functional (like a spectral density matrix, a discrete Fourier transform, an impulse response function, etc.)
on selected frequencies. Formally we consider a collection of complex-valued matrices
, all of which have the same dimension
. Moreover, we consider frequencies
. The object this function creates corresponds
to the mapping
, where
.
Consider, for example, the discrete Fourier transform of a vector time series :. It is defined as
We may choose and
. Then, the object
freqdom
creates, is corresponding to the function which associates and
.
Returns an object of class freqdom
. An object of class freqdom
is a list containing the following components:
operators
the array
F
as given in the argument.
freq
the vector
freq
as given in the argument.
i = complex(imaginary=1) OP = array(0, c(2, 2, 3)) OP[,,1] = diag(2) * exp(i)/2 OP[,,2] = diag(2) OP[,,3] = diag(2) * exp(-i)/2 freq = c(-pi/3, 0, pi/3) A = freqdom(OP, freq)
i = complex(imaginary=1) OP = array(0, c(2, 2, 3)) OP[,,1] = diag(2) * exp(i)/2 OP[,,2] = diag(2) OP[,,3] = diag(2) * exp(-i)/2 freq = c(-pi/3, 0, pi/3) A = freqdom(OP, freq)
Gives the eigendecomposition of objects of class freqdom
.
freqdom.eigen(F)
freqdom.eigen(F)
F |
an object of class freqdom. The matrices |
This function makes an eigendecomposition for each of the matrices F\$operator[,,k]
.
Returns a list. The list is containing the following components:
vectors
an array containing
matrices. The
-th matrix contains in its
-th row the conjugate transpose eigenvector belonging to the
-th largest eigenvalue of
F\$operator[,,i]
.
values
matrix containing in
-th column the eigenvalues of
F\$operator[,,k]
.
freq
vector of frequencies defining the object
F
.
Checks if an object belongs to the class freqdom
.
is.freqdom(X)
is.freqdom(X)
X |
some object |
TRUE
if X
is of type freqdom
, FALSE
otherwise
Checks if an object belongs to the class timedom
.
is.timedom(X)
is.timedom(X)
X |
some object |
TRUE
if X
is of type timedom
, FALSE
otherwise
Generates a zero mean vector autoregressive process of a given order.
rar( n, d = 2, Psi = NULL, burnin = 10, noise = c("mnormal", "mt"), sigma = NULL, df = 4 )
rar( n, d = 2, Psi = NULL, burnin = 10, noise = c("mnormal", "mt"), sigma = NULL, df = 4 )
n |
number of observations to generate. |
d |
dimension of the time series. |
Psi |
array of |
burnin |
an integer |
noise |
|
sigma |
covariance or scale matrix of the innovations. By default the identity matrix. |
df |
degrees of freedom if |
We simulate a vector autoregressive process
The innovation process is either multivariate normal or multivariate
with a predefined covariance/scale matrix sigma and zero mean. The noise is generated
with the package
mvtnorm
. For Gaussian noise we use rmvnorm
. For Student-t noise
we use rmvt
. The parameters sigma and df are imported as arguments, otherwise we use default
settings. To initialise the process we set
. When
burnin
is set
equal to then, n
observations are generated and the first
will be trashed.
A matrix with d
columns and n
rows. Each row corresponds to one time point.
Generates a zero mean vector moving average process.
rma(n, d = 2, Psi = NULL, noise = c("mnormal", "mt"), sigma = NULL, df = 4)
rma(n, d = 2, Psi = NULL, noise = c("mnormal", "mt"), sigma = NULL, df = 4)
n |
number of observations to generate. |
d |
dimension of the time series. |
Psi |
a |
noise |
|
sigma |
covariance or scale matrix of the innovations. If NULL then the identity matrix is used. |
df |
degrees of freedom if |
This simulates a vector moving average process
The innovation process is either multivariate normal or multivarite
with
a predefined covariance/scale matrix sigma and zero mean. The noise is generated with the
package
mvtnorm
. For Gaussian noise we use rmvnorm
. For Student-t noise we use
rmvt
. The parameters sigma
and df
are imported as arguments, otherwise we use default settings.
A matrix with d
columns and n
rows. Each row corresponds to one time point.
Estimates the spectral density and cross spectral density of vector time series.
spectral.density( X, Y = X, freq = (-1000:1000/1000) * pi, q = max(1, floor(dim(X)[1]^(1/3))), weights = c("Bartlett", "trunc", "Tukey", "Parzen", "Bohman", "Daniell", "ParzenCogburnDavis") )
spectral.density( X, Y = X, freq = (-1000:1000/1000) * pi, q = max(1, floor(dim(X)[1]^(1/3))), weights = c("Bartlett", "trunc", "Tukey", "Parzen", "Bohman", "Daniell", "ParzenCogburnDavis") )
X |
a vector or a vector time series given in matrix form. Each row corresponds to a timepoint. |
Y |
a vector or vector time series given in matrix form. Each row corresponds to a timepoint. |
freq |
a vector containing frequencies in |
q |
window size for the kernel estimator, i.e. a positive integer. |
weights |
kernel used in the spectral smoothing. By default the Bartlett kernel is chosen. |
Let be a
matrix and
be a
matrix. We stack the vectors and assume that
is a stationary multivariate time series of dimension
. The cross-spectral density between the two time series
and
is defined as
The function spectral.density
determines the empirical cross-spectral density between the two time series and
. The estimator is of form
with defined in
cov.structure
Here is a kernel of the specified type and
is the window size. By default the Bartlett kernel
is used.
See, e.g., Chapter 10 and 11 in Brockwell and Davis (1991) for details.
Returns an object of class freqdom
. The list is containing the following components:
operators
an array. The
-th matrix in this array corresponds to the spectral density matrix evaluated at the
-th frequency listed in
freq
.
freq
returns argument vector
freq
.
Peter J. Brockwell and Richard A. Davis Time Series: Theory and Methods Springer Series in Statistics, 2009
Creates an object of class timedom
. This object corresponds to a multivariate linear filter.
timedom(A, lags)
timedom(A, lags)
A |
a vector, matrix or array. If array, the elements |
lags |
a vector of increasing integers. It corresponds to the time lags of the filter. |
This class is used to describe a linear filter, i.e. a sequence of matrices, each of which correspond to a certain lag. Filters can, for example, be used to transform a sequence into a new sequence
by defining
See filter.process()
.
Formally we consider a collection of complex-valued matrices
, all of which have the same dimension
. Moreover, we consider lags
. The object this function creates corresponds to the mapping
, where
.
Returns an object of class timedom
. An object of class timedom
is a list containing the following components:
operators
returns the array
A
as given in the argument.
lags
returns the vector
lags
as given in the argument.
# In this example we apply the difference operator: Delta X_t= X_t-X_{t-1} to a time series X = rar(20) OP = array(0,c(2,2,2)) OP[,,1] = diag(2) OP[,,2] = -diag(2) A = timedom(OP, lags = c(0,1)) filter.process(X, A)
# In this example we apply the difference operator: Delta X_t= X_t-X_{t-1} to a time series X = rar(20) OP = array(0,c(2,2,2)) OP[,,1] = diag(2) OP[,,2] = -diag(2) A = timedom(OP, lags = c(0,1)) filter.process(X, A)
This function determines the norms of the matrices defining some linear filter.
timedom.norms(A, type = "2")
timedom.norms(A, type = "2")
A |
an object of class |
type |
matrix norm to be used as in |
Computes for
in the set of lags belonging to the object A. When type
is
2
then is the spectral radius of
. When type is
F
then is the Frobenius norm (or the Hilbert-Schmidt norm, or Schatten 2-norm) of
. Same options as for the function
norm
as in base package.
A list which contains the following components:
lags
a vector containing the lags of
A
.
norms
a vector containing the norms of the matrices defining
A
.
d = 2 A = array(0,c(d,d,2)) A[1,,] = 2 * diag(d:1)/d A[2,,] = 1.5 * diag(d:1)/d OP = timedom(A,c(-2,1)) timedom.norms(OP)
d = 2 A = array(0,c(d,d,2)) A[1,,] = 2 * diag(d:1)/d A[2,,] = 1.5 * diag(d:1)/d OP = timedom(A,c(-2,1)) timedom.norms(OP)
timedom
This function removes lags from a linear filter.
timedom.trunc(A, lags)
timedom.trunc(A, lags)
A |
an object of class |
lags |
a vector which contains a set of lags. These lags must be a subset of the lags defined for timedom object A. Only those lags will be kept, the other lags are removed. |
An object of class timedom
.