meantensor.RdMean and variance of tensors again tensors.
(set of) dataset(s) of tensors represented by a tensor
a second dataset of connected tensors represented by a tensor
the indices indexing the datasets
here for generic compatibility with the compositions package
the indices indexing the set of datasets
a boolean, if FALSE and missings are in the dataset a error is given. If TRUE pairwise exclusion is used.
the to mark the second instance of indices in var(x,...)
gives a tensor like x without the along dimensions representing the a mean over all tensors in the dataset. It is not necessary to have a by dimension since everything not in along is automatically treated parallel
Gives the covariate tensor representing the covariance of x and y. The data tensor indices of x any y should be different, since otherwise duplicated names exist in the result.
Gives the covariate representation of the variance of x. All data indices (i.e. all indices neither in by nor in along are duplicated. One with and one without the given mark.
Let denote \(a\) the along dimension, \(i_1,\ldots,i_k\) and \(j_1,\ldots,j_l\) the data dimension, and b the by dimension, then the mean is given by: $$M^x_{bi_1,\ldots,i_k}=\frac1{n}\sum_{a}x_{abi_1,\ldots,i_k}$$ the covariance by $$C_{ab i_1,\ldots,i_kj_1,\ldots,j_l}=\frac1{n-1}\sum_{a} (x_{abi_1,\ldots,i_k}-M^x_{bi_1,\ldots,i_k})(y_{abj_1,\ldots,j_l}-M^y_{bj_1,\ldots,j_l})$$ and the variance by $$V_{ab i_1,\ldots,i_ki'_1,\ldots,i'_l}=\frac1{n-1}\sum_{a} (x_{abi_1,\ldots,i_k}-M^x_{bi_1,\ldots,i_k})(x_{abi'_1,\ldots,i'_k}-M^x_{bi'_1,\ldots,i'_l})$$
d1 <- c(a=2,b=2)
d2 <- c("a'"=2,"b'"=2)
# a mean tensor:
m <- to.tensor(1:4,d1)
# a positive definite variance tensor:
V <- delta.tensor(d1)+one.tensor(c(d1,d2))
V
#> , , 1, 1
#>
#> b
#> a [,1] [,2]
#> [1,] 2 1
#> [2,] 1 1
#>
#> , , 2, 1
#>
#> b
#> a [,1] [,2]
#> [1,] 1 1
#> [2,] 2 1
#>
#> , , 1, 2
#>
#> b
#> a [,1] [,2]
#> [1,] 1 2
#> [2,] 1 1
#>
#> , , 2, 2
#>
#> b
#> a [,1] [,2]
#> [1,] 1 1
#> [2,] 1 2
#>
#> attr(,"class")
#> [1] "tensor"
# Simulate Normally distributed tensors with these moments:
X <- (power.tensor(V,c("a","b"),c("a'","b'"),p=1/2) %e%
to.tensor(rnorm(1000*2*2),c(i=1000,d2))) + m
# The mean
mean.tensor(X,along="i")
#> b
#> a [,1] [,2]
#> [1,] 0.9968974 3.018114
#> [2,] 1.9975708 3.990475
#> attr(,"class")
#> [1] "tensor" "matrix"
# Full tensorial covariance:
var.tensor(X,along="i")
#> , , 1, 1
#>
#> b
#> a [,1] [,2]
#> [1,] 2.115895 1.165486
#> [2,] 1.066947 1.112410
#>
#> , , 2, 1
#>
#> b
#> a [,1] [,2]
#> [1,] 1.066947 0.9923136
#> [2,] 1.863172 0.8995142
#>
#> , , 1, 2
#>
#> b
#> a [,1] [,2]
#> [1,] 1.1654859 2.060677
#> [2,] 0.9923136 1.005642
#>
#> , , 2, 2
#>
#> b
#> a [,1] [,2]
#> [1,] 1.1124099 1.005642
#> [2,] 0.8995142 1.952737
#>
#> attr(,"class")
#> [1] "tensor"
# Variance of the slices X[[b=1]] and X[[b=2]] :
var.tensor(X,along="i",by="b")
#> , , 1
#>
#> a'
#> a [,1] [,2]
#> [1,] 2.115895 1.066947
#> [2,] 1.066947 1.863172
#>
#> , , 2
#>
#> a'
#> a [,1] [,2]
#> [1,] 2.060677 1.005642
#> [2,] 1.005642 1.952737
#>
#> attr(,"class")
#> [1] "tensor"
# Covariance of the slices X[[b=1]] and X[[b=2]] :
var.tensor(X[[b=1]],X[[a=~"a'",b=2]],along="i")
#> a'
#> a [,1] [,2]
#> [1,] 1.1654859 1.1124099
#> [2,] 0.9923136 0.8995142
#> attr(,"class")
#> [1] "tensor" "matrix"