ScaledMatrix
classResidualMatrix 1.4.0
The ScaledMatrix
provides yet another method of running scale()
on a matrix.
In other words, these three operations are equivalent:
mat <- matrix(rnorm(10000), ncol=10)
smat1 <- scale(mat)
head(smat1)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] -0.6879869 -2.4744562 -0.08085553 0.7393487 1.699333951 -0.7337500
## [2,] 0.4022740 1.5238177 -0.27777835 -1.0588208 0.869307740 -0.4849390
## [3,] 1.2413651 -0.2106369 -0.63199524 -0.2141698 1.672656413 -0.4476750
## [4,] 0.3655585 0.1298194 0.48973999 0.5221128 1.033068754 0.9790542
## [5,] -0.4693535 1.5272390 0.93512006 -0.4449546 0.009737451 0.2248522
## [6,] -0.6107252 0.6488568 1.89085334 -0.5690128 -1.183350083 -0.2954360
## [,7] [,8] [,9] [,10]
## [1,] -0.9541744 0.3694291 -0.521302558 -1.1721416
## [2,] 0.1872745 0.2544220 0.058701038 -0.2826986
## [3,] 0.7294296 -0.6176718 0.002424988 0.9379393
## [4,] 1.4457757 -0.4039322 0.002596846 1.9503449
## [5,] -0.3274869 0.9991039 0.327749560 -1.1296908
## [6,] 1.0976641 -1.9051561 0.988565312 0.8575131
library(DelayedArray)
smat2 <- scale(DelayedArray(mat))
head(smat2)
## <6 x 10> matrix of class DelayedMatrix and type "double":
## [,1] [,2] [,3] ... [,9] [,10]
## [1,] -0.68798686 -2.47445619 -0.08085553 . -0.521302558 -1.172141600
## [2,] 0.40227400 1.52381768 -0.27777835 . 0.058701038 -0.282698630
## [3,] 1.24136511 -0.21063688 -0.63199524 . 0.002424988 0.937939271
## [4,] 0.36555847 0.12981936 0.48973999 . 0.002596846 1.950344885
## [5,] -0.46935354 1.52723902 0.93512006 . 0.327749560 -1.129690811
## [6,] -0.61072524 0.64885683 1.89085334 . 0.988565312 0.857513065
library(ScaledMatrix)
smat3 <- ScaledMatrix(mat, center=TRUE, scale=TRUE)
head(smat3)
## <6 x 10> matrix of class ScaledMatrix and type "double":
## [,1] [,2] [,3] ... [,9] [,10]
## [1,] -0.68798686 -2.47445619 -0.08085553 . -0.521302558 -1.172141600
## [2,] 0.40227400 1.52381768 -0.27777835 . 0.058701038 -0.282698630
## [3,] 1.24136511 -0.21063688 -0.63199524 . 0.002424988 0.937939271
## [4,] 0.36555847 0.12981936 0.48973999 . 0.002596846 1.950344885
## [5,] -0.46935354 1.52723902 0.93512006 . 0.327749560 -1.129690811
## [6,] -0.61072524 0.64885683 1.89085334 . 0.988565312 0.857513065
The biggest difference lies in how they behave in downstream matrix operations.
smat1
is an ordinary matrix, with the scaled and centered values fully realized in memory.
Nothing too unusual here.smat2
is a DelayedMatrix
and undergoes block processing whereby chunks are realized and operated on, one at a time.
This sacrifices speed for greater memory efficiency by avoiding a copy of the entire matrix.
In particular, it preserves the structure of the original mat
, e.g., from a sparse or file-backed representation.smat3
is a ScaledMatrix
that refactors certain operations so that they can be applied to the original mat
without any scaling or centering.
This takes advantage of the original data structure to speed up matrix multiplication and row/column sums,
albeit at the cost of numerical precision.Given an original matrix \(\mathbf{X}\) with \(n\) columns, a vector of column centers \(\mathbf{c}\) and a vector of column scaling values \(\mathbf{s}\), our scaled matrix can be written as:
\[ \mathbf{Y} = (\mathbf{X} - \mathbf{c} \cdot \mathbf{1}_n^T) \mathbf{S} \]
where \(\mathbf{S} = \text{diag}(s_1^{-1}, ..., s_n^{-1})\). If we wanted to right-multiply it with another matrix \(\mathbf{A}\), we would have:
\[ \mathbf{YA} = \mathbf{X}\mathbf{S}\mathbf{A} - \mathbf{c} \cdot \mathbf{1}_n^T \mathbf{S}\mathbf{A} \]
The right-most expression is simply the outer product of \(\mathbf{c}\) with the column sums of \(\mathbf{SA}\). More important is the fact that we can use the matrix multiplication operator for \(\mathbf{X}\) with \(\mathbf{SA}\), as this allows us to use highly efficient algorithms for certain data representations, e.g., sparse matrices.
library(Matrix)
mat <- rsparsematrix(20000, 10000, density=0.01)
smat <- ScaledMatrix(mat, center=TRUE, scale=TRUE)
blob <- matrix(runif(ncol(mat) * 5), ncol=5)
system.time(out <- smat %*% blob)
## user system elapsed
## 0.025 0.000 0.026
# The slower way with block processing.
da <- scale(DelayedArray(mat))
system.time(out2 <- da %*% blob)
## user system elapsed
## 30.305 9.708 42.905
The same logic applies for left-multiplication and cross-products.
This allows us to easily speed up high-level operations involving matrix multiplication by just switching to a ScaledMatrix
,
e.g., in approximate PCA algorithms from the BiocSingular package.
library(BiocSingular)
set.seed(1000)
system.time(pcs <- runSVD(smat, k=10, BSPARAM=IrlbaParam()))
## user system elapsed
## 11.554 0.374 11.928
Row and column sums are special cases of matrix multiplication and can be computed quickly:
system.time(rowSums(smat))
## user system elapsed
## 0.01 0.00 0.01
system.time(rowSums(da))
## user system elapsed
## 19.864 7.946 27.818
Subsetting, transposition and renaming of the dimensions are all supported without loss of the ScaledMatrix
representation:
smat[,1:5]
## <20000 x 5> matrix of class ScaledMatrix and type "double":
## [,1] [,2] [,3] [,4] [,5]
## [1,] 0.0075812114 -0.0047882787 0.0016012997 0.0002451627 -0.0073320631
## [2,] 0.0075812114 -0.0047882787 0.0016012997 0.0002451627 -0.0073320631
## [3,] 0.0075812114 -0.0047882787 0.0016012997 0.0002451627 -0.0073320631
## [4,] 0.0075812114 -0.0047882787 0.0016012997 0.0002451627 -0.0073320631
## [5,] 0.0075812114 -0.0047882787 0.0016012997 0.0002451627 -0.0073320631
## ... . . . . .
## [19996,] 0.0075812114 -0.0047882787 0.0016012997 0.0002451627 -0.0073320631
## [19997,] 0.0075812114 -0.0047882787 0.0016012997 0.0002451627 -0.0073320631
## [19998,] 0.0075812114 -0.0047882787 0.0016012997 0.0002451627 -0.0073320631
## [19999,] 0.0075812114 -0.0047882787 0.0016012997 0.0002451627 -0.0073320631
## [20000,] 0.0075812114 -0.0047882787 0.0016012997 0.0002451627 -0.0073320631
t(smat)
## <10000 x 20000> matrix of class ScaledMatrix and type "double":
## [,1] [,2] [,3] ... [,19999]
## [1,] 0.0075812114 0.0075812114 0.0075812114 . 0.0075812114
## [2,] -0.0047882787 -0.0047882787 -0.0047882787 . -0.0047882787
## [3,] 0.0016012997 0.0016012997 0.0016012997 . 0.0016012997
## [4,] 0.0002451627 0.0002451627 0.0002451627 . 0.0002451627
## [5,] -0.0073320631 -0.0073320631 -0.0073320631 . -0.0073320631
## ... . . . . .
## [9996,] -1.730431e-02 -1.730431e-02 -1.730431e-02 . -1.730431e-02
## [9997,] -8.207621e-03 -8.207621e-03 -8.207621e-03 . -8.207621e-03
## [9998,] -4.056946e-05 -4.056946e-05 -4.056946e-05 . -4.056946e-05
## [9999,] -7.042782e-03 -7.042782e-03 -7.042782e-03 . -7.042782e-03
## [10000,] -5.557036e-03 -5.557036e-03 -5.557036e-03 . -5.557036e-03
## [,20000]
## [1,] 0.0075812114
## [2,] -0.0047882787
## [3,] 0.0016012997
## [4,] 0.0002451627
## [5,] -0.0073320631
## ... .
## [9996,] -1.730431e-02
## [9997,] -8.207621e-03
## [9998,] -4.056946e-05
## [9999,] -7.042782e-03
## [10000,] -5.557036e-03
rownames(smat) <- paste0("GENE_", 1:20000)
smat
## <20000 x 10000> matrix of class ScaledMatrix and type "double":
## [,1] [,2] [,3] ... [,9999] [,10000]
## GENE_1 0.007581211 -0.004788279 0.001601300 . -0.007042782 -0.005557036
## GENE_2 0.007581211 -0.004788279 0.001601300 . -0.007042782 -0.005557036
## GENE_3 0.007581211 -0.004788279 0.001601300 . -0.007042782 -0.005557036
## GENE_4 0.007581211 -0.004788279 0.001601300 . -0.007042782 -0.005557036
## GENE_5 0.007581211 -0.004788279 0.001601300 . -0.007042782 -0.005557036
## ... . . . . . .
## GENE_19996 0.007581211 -0.004788279 0.001601300 . -0.007042782 -0.005557036
## GENE_19997 0.007581211 -0.004788279 0.001601300 . -0.007042782 -0.005557036
## GENE_19998 0.007581211 -0.004788279 0.001601300 . -0.007042782 -0.005557036
## GENE_19999 0.007581211 -0.004788279 0.001601300 . -0.007042782 -0.005557036
## GENE_20000 0.007581211 -0.004788279 0.001601300 . -0.007042782 -0.005557036
Other operations will cause the ScaledMatrix
to collapse to the general DelayedMatrix
representation, after which point block processing will be used.
smat + 1
## <20000 x 10000> matrix of class DelayedMatrix and type "double":
## [,1] [,2] [,3] ... [,9999] [,10000]
## GENE_1 1.0075812 0.9952117 1.0016013 . 0.9929572 0.9944430
## GENE_2 1.0075812 0.9952117 1.0016013 . 0.9929572 0.9944430
## GENE_3 1.0075812 0.9952117 1.0016013 . 0.9929572 0.9944430
## GENE_4 1.0075812 0.9952117 1.0016013 . 0.9929572 0.9944430
## GENE_5 1.0075812 0.9952117 1.0016013 . 0.9929572 0.9944430
## ... . . . . . .
## GENE_19996 1.0075812 0.9952117 1.0016013 . 0.9929572 0.9944430
## GENE_19997 1.0075812 0.9952117 1.0016013 . 0.9929572 0.9944430
## GENE_19998 1.0075812 0.9952117 1.0016013 . 0.9929572 0.9944430
## GENE_19999 1.0075812 0.9952117 1.0016013 . 0.9929572 0.9944430
## GENE_20000 1.0075812 0.9952117 1.0016013 . 0.9929572 0.9944430
For most part, the implementation of the multiplication assumes that the \(\mathbf{A}\) matrix and the matrix product are small compared to \(\mathbf{X}\).
It is also possible to multiply two ScaledMatrix
es together if the underlying matrices have efficient operators for their product.
However, if this is not the case, the ScaledMatrix
offers little benefit for increased overhead.
It is also worth noting that this speed-up is not entirely free.
The expression above involves subtracting two matrix with potentially large values, which runs the risk of catastrophic cancellation.
The example below demonstrates how ScaledMatrix
is more susceptible to loss of precision than a normal DelayedArray
:
set.seed(1000)
mat <- matrix(rnorm(1000000), ncol=100000)
big.mat <- mat + 1e12
# The 'correct' value, unaffected by numerical precision.
ref <- rowMeans(scale(mat))
head(ref)
## [1] -0.0025584703 -0.0008570664 -0.0019225335 -0.0001039903 0.0024761772
## [6] 0.0032943203
# The value from scale'ing a DelayedArray.
library(DelayedArray)
smat2 <- scale(DelayedArray(big.mat))
head(rowMeans(smat2))
## [1] -0.0025583534 -0.0008571123 -0.0019226040 -0.0001039539 0.0024761618
## [6] 0.0032943783
# The value from a ScaledMatrix.
library(ScaledMatrix)
smat3 <- ScaledMatrix(big.mat, center=TRUE, scale=TRUE)
head(rowMeans(smat3))
## [1] -0.00480 0.00848 0.00544 -0.00976 -0.01056 0.01520
In most practical applications, though, this does not seem to be a major concern, especially as most values (e.g., log-normalized expression matrices) lie close to zero anyway.
sessionInfo()
## R version 4.1.1 (2021-08-10)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.3 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.14-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.14-bioc/R/lib/libRlapack.so
##
## locale:
## [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
## [3] LC_TIME=en_GB LC_COLLATE=C
## [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
## [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
## [9] LC_ADDRESS=C LC_TELEPHONE=C
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
##
## attached base packages:
## [1] stats4 stats graphics grDevices utils datasets methods
## [8] base
##
## other attached packages:
## [1] BiocSingular_1.10.0 ScaledMatrix_1.2.0 DelayedArray_0.20.0
## [4] IRanges_2.28.0 S4Vectors_0.32.0 MatrixGenerics_1.6.0
## [7] matrixStats_0.61.0 BiocGenerics_0.40.0 Matrix_1.3-4
## [10] BiocStyle_2.22.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.7 bslib_0.3.1
## [3] compiler_4.1.1 BiocManager_1.30.16
## [5] jquerylib_0.1.4 tools_4.1.1
## [7] DelayedMatrixStats_1.16.0 digest_0.6.28
## [9] jsonlite_1.7.2 evaluate_0.14
## [11] lattice_0.20-45 rlang_0.4.12
## [13] yaml_2.2.1 parallel_4.1.1
## [15] xfun_0.27 fastmap_1.1.0
## [17] stringr_1.4.0 knitr_1.36
## [19] sass_0.4.0 grid_4.1.1
## [21] R6_2.5.1 BiocParallel_1.28.0
## [23] rmarkdown_2.11 bookdown_0.24
## [25] irlba_2.3.3 magrittr_2.0.1
## [27] htmltools_0.5.2 sparseMatrixStats_1.6.0
## [29] rsvd_1.0.5 beachmat_2.10.0
## [31] stringi_1.7.5