S3
method to compute the sample covariance and cross-covariance
functions for a set of functional data.
cov_fun(X, Y = NULL)
# S3 method for fData
cov_fun(X, Y = NULL)
# S3 method for mfData
cov_fun(X, Y = NULL)
is the (eventually first) functional dataset, i.e. either an object
of class fData
or an object of class mfData
;
is the (optional) second functional dataset to be used to compute the
cross-covariance function, either NULL
or an fData
or
mfData
object (see the Value section for details).
The following cases are given:
if X
is of class fData
and Y
is NULL
, then
the covariance function of X
is returned;
if X
is of class fData
and Y
is of
class fData
,
the cross-covariance function of the two datasets is returned;
if X
is of class mfData
and Y
is NULL
,
the upper-triangular blocks of the covariance function of X
are returned (in form of list and by row, i.e. in the sequence 1_1, 1_2, ...,
1_L, 2_2, ... - have a look at the labels of the list with str
);
if X
is of class mfData
and Y
is of
class fData
,
the cross-covariances of X
's components and Y
are
returned (in form of list);
if X
is of class mfData
and Y
is of
class mfData
,
the upper-triangular blocks of the cross-covariance of X
's and
Y
's components are returned (in form of list and by row, i.e. in the
sequence 1_1, 1_2, ..., 1_L, 2_2, ... - have a look at the labels
of the list with str
));
In any case, the return type is either an instance of the S3
class Cov
or a list of instances of such class (for the case of multivariate
functional data).
Given a univariate random function X, defined over the grid \(I = [a,b]\), the covariance function is defined as:
$$C(s,t) = Cov( X(s), X(t) ), \qquad s,t \in I.$$
Given another random function, Y, defined over the same grid as X, the cross- covariance function of X and Y is:
$$C^{X,Y}( s,t ) = Cov( X(s), Y(t) ), \qquad s, t \in I.$$
For a generic L-dimensional random function X, i.e. an L-dimensional multivariate functional datum, the covariance function is defined as the set of blocks:
$$C_{i,j}(s,t) = Cov( X_i(s), X_j(t)), i,j = 1, ...,L, s,t \in I,$$
while the cross-covariance function is defined by the blocks:
$$C^{X,Y}_{i,j}(s,t) = Cov( X_i(s), Y_j(t))$$
The method cov_fun
provides the sample estimator of the covariance or
cross-covariance functions for univariate or multivariate functional datasets.
The class of X
(fData
or mfData
) is used to dispatch the
correct implementation of the method.
# Generating a univariate functional dataset
N = 1e2
P = 1e2
t0 = 0
t1 = 1
time_grid = seq( t0, t1, length.out = P )
Cov = exp_cov_function( time_grid, alpha = 0.3, beta = 0.4 )
D1 = generate_gauss_fdata( N, centerline = sin( 2 * pi * time_grid ), Cov = Cov )
D2 = generate_gauss_fdata( N, centerline = sin( 2 * pi * time_grid ), Cov = Cov )
fD1 = fData( time_grid, D1 )
fD2 = fData( time_grid, D2 )
# Computing the covariance function of fD1
C = cov_fun( fD1 )
str( C )
#> List of 5
#> $ t0 : num 0
#> $ tP : num 1
#> $ h : num 0.0101
#> $ P : int 100
#> $ values: num [1:100, 1:100] 0.287 0.287 0.286 0.284 0.285 ...
#> - attr(*, "class")= chr "Cov"
# Computing the cross-covariance function of fD1 and fD2
CC = cov_fun( fD1, fD2 )
str( CC )
#> List of 5
#> $ t0 : num 0
#> $ tP : num 1
#> $ h : num 0.0101
#> $ P : int 100
#> $ values: num [1:100, 1:100] -0.0153 -0.0152 -0.0141 -0.017 -0.0191 ...
#> - attr(*, "class")= chr "Cov"
# Generating a multivariate functional dataset
L = 3
C1 = exp_cov_function( time_grid, alpha = 0.1, beta = 0.2 )
C2 = exp_cov_function( time_grid, alpha = 0.2, beta = 0.5 )
C3 = exp_cov_function( time_grid, alpha = 0.3, beta = 1 )
centerline = matrix( c( sin( 2 * pi * time_grid ),
sqrt( time_grid ),
10 * ( time_grid - 0.5 ) * time_grid ),
nrow = 3, byrow = TRUE )
D3 = generate_gauss_mfdata( N, L, centerline,
correlations = c( 0.5, 0.5, 0.5 ),
listCov = list( C1, C2, C3 ) )
# adding names for better readability of BC3's labels
names( D3 ) = c( 'comp1', 'comp2', 'comp3' )
mfD3 = mfData( time_grid, D3 )
D1 = generate_gauss_fdata( N, centerline = sin( 2 * pi * time_grid ),
Cov = Cov )
fD1 = fData( time_grid, D1 )
# Computing the block covariance function of mfD3
BC3 = cov_fun( mfD3 )
str( BC3 )
#> List of 6
#> $ comp1_comp1:List of 5
#> ..$ t0 : num 0
#> ..$ tP : num 1
#> ..$ h : num 0.0101
#> ..$ P : int 100
#> ..$ values: num [1:100, 1:100] 0.101 0.101 0.101 0.102 0.103 ...
#> ..- attr(*, "class")= chr "Cov"
#> $ comp1_comp2:List of 5
#> ..$ t0 : num 0
#> ..$ tP : num 1
#> ..$ h : num 0.0101
#> ..$ P : int 100
#> ..$ values: num [1:100, 1:100] 0.0616 0.0624 0.0616 0.0617 0.0628 ...
#> ..- attr(*, "class")= chr "Cov"
#> $ comp1_comp3:List of 5
#> ..$ t0 : num 0
#> ..$ tP : num 1
#> ..$ h : num 0.0101
#> ..$ P : int 100
#> ..$ values: num [1:100, 1:100] 0.0638 0.0638 0.0625 0.0626 0.0631 ...
#> ..- attr(*, "class")= chr "Cov"
#> $ comp2_comp2:List of 5
#> ..$ t0 : num 0
#> ..$ tP : num 1
#> ..$ h : num 0.0101
#> ..$ P : int 100
#> ..$ values: num [1:100, 1:100] 0.162 0.162 0.161 0.16 0.162 ...
#> ..- attr(*, "class")= chr "Cov"
#> $ comp2_comp3:List of 5
#> ..$ t0 : num 0
#> ..$ tP : num 1
#> ..$ h : num 0.0101
#> ..$ P : int 100
#> ..$ values: num [1:100, 1:100] 0.1027 0.1011 0.0993 0.1 0.0979 ...
#> ..- attr(*, "class")= chr "Cov"
#> $ comp3_comp3:List of 5
#> ..$ t0 : num 0
#> ..$ tP : num 1
#> ..$ h : num 0.0101
#> ..$ P : int 100
#> ..$ values: num [1:100, 1:100] 0.231 0.229 0.226 0.223 0.221 ...
#> ..- attr(*, "class")= chr "Cov"
# computing cross-covariance between mfData and fData objects
CC = cov_fun( mfD3, fD1 )
str( CC )
#> List of 3
#> $ comp1:List of 5
#> ..$ t0 : num 0
#> ..$ tP : num 1
#> ..$ h : num 0.0101
#> ..$ P : int 100
#> ..$ values: num [1:100, 1:100] -0.01055 -0.01085 -0.01003 -0.00969 -0.01105 ...
#> ..- attr(*, "class")= chr "Cov"
#> $ comp2:List of 5
#> ..$ t0 : num 0
#> ..$ tP : num 1
#> ..$ h : num 0.0101
#> ..$ P : int 100
#> ..$ values: num [1:100, 1:100] -0.01175 -0.01409 -0.01091 -0.00956 -0.01225 ...
#> ..- attr(*, "class")= chr "Cov"
#> $ comp3:List of 5
#> ..$ t0 : num 0
#> ..$ tP : num 1
#> ..$ h : num 0.0101
#> ..$ P : int 100
#> ..$ values: num [1:100, 1:100] -0.0299 -0.0389 -0.0407 -0.0374 -0.038 ...
#> ..- attr(*, "class")= chr "Cov"