ScaledMatrix
classResidualMatrix 1.8.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.9600655 -0.8907511 0.2329264 -0.6650757 -0.07961061 -1.5115874
## [2,] -0.3885523 -1.4955274 -0.6306516 -1.7619577 0.44233791 -1.4540773
## [3,] 1.2521771 1.1819444 -0.2432960 -0.7948620 0.34246595 -0.3581938
## [4,] 0.8619637 0.1331795 0.3053893 1.0910042 1.28343605 -1.0751796
## [5,] 1.4145500 0.3116180 -0.9453342 0.1618665 1.86993652 -0.2923993
## [6,] 0.7365568 2.1419108 0.4154857 -0.9438854 -0.26062765 0.7730081
## [,7] [,8] [,9] [,10]
## [1,] -0.1039902 -0.18227349 -2.0612549 0.19185802
## [2,] 1.4584763 -0.05466575 0.4093444 -1.33824237
## [3,] 0.9965179 0.49833719 0.1493172 -1.16557493
## [4,] -0.7394816 -0.44728297 -0.7001517 0.40324423
## [5,] 1.4141236 -1.11305873 -1.1326581 -0.54584325
## [6,] 0.5167600 1.19616813 1.1423795 0.03809215
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.9600655 -0.8907511 0.2329264 . -2.06125487 0.19185802
## [2,] -0.3885523 -1.4955274 -0.6306516 . 0.40934437 -1.33824237
## [3,] 1.2521771 1.1819444 -0.2432960 . 0.14931724 -1.16557493
## [4,] 0.8619637 0.1331795 0.3053893 . -0.70015169 0.40324423
## [5,] 1.4145500 0.3116180 -0.9453342 . -1.13265808 -0.54584325
## [6,] 0.7365568 2.1419108 0.4154857 . 1.14237952 0.03809215
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.9600655 -0.8907511 0.2329264 . -2.06125487 0.19185802
## [2,] -0.3885523 -1.4955274 -0.6306516 . 0.40934437 -1.33824237
## [3,] 1.2521771 1.1819444 -0.2432960 . 0.14931724 -1.16557493
## [4,] 0.8619637 0.1331795 0.3053893 . -0.70015169 0.40324423
## [5,] 1.4145500 0.3116180 -0.9453342 . -1.13265808 -0.54584325
## [6,] 0.7365568 2.1419108 0.4154857 . 1.14237952 0.03809215
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.023 0.000 0.023
# The slower way with block processing.
da <- scale(DelayedArray(mat))
system.time(out2 <- da %*% blob)
## user system elapsed
## 28.737 7.519 38.095
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
## 12.208 0.172 12.380
Row and column sums are special cases of matrix multiplication and can be computed quickly:
system.time(rowSums(smat))
## user system elapsed
## 0.010 0.000 0.011
system.time(rowSums(da))
## user system elapsed
## 19.664 6.864 26.528
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.003974920 -0.007102022 0.005706479 -0.011039582 0.001072816
## [2,] 0.003974920 -0.007102022 0.005706479 -0.011039582 0.001072816
## [3,] 0.003974920 -0.007102022 0.005706479 -0.011039582 0.001072816
## [4,] 0.003974920 -0.007102022 0.005706479 -0.011039582 0.001072816
## [5,] 0.003974920 -0.007102022 0.005706479 -0.011039582 0.001072816
## ... . . . . .
## [19996,] 0.003974920 -0.007102022 0.005706479 -0.011039582 0.001072816
## [19997,] 0.003974920 -0.007102022 0.005706479 -0.011039582 0.001072816
## [19998,] 0.003974920 -0.007102022 0.005706479 -0.011039582 0.001072816
## [19999,] 0.003974920 -0.007102022 0.005706479 -1.468346036 0.001072816
## [20000,] 0.003974920 -0.007102022 0.005706479 -0.011039582 0.001072816
t(smat)
## <10000 x 20000> matrix of class ScaledMatrix and type "double":
## [,1] [,2] [,3] ... [,19999]
## [1,] 0.003974920 0.003974920 0.003974920 . 0.003974920
## [2,] -0.007102022 -0.007102022 -0.007102022 . -0.007102022
## [3,] 0.005706479 0.005706479 0.005706479 . 0.005706479
## [4,] -0.011039582 -0.011039582 -0.011039582 . -1.468346036
## [5,] 0.001072816 0.001072816 0.001072816 . 0.001072816
## ... . . . . .
## [9996,] -0.0024630686 -0.0024630686 -0.0024630686 . -0.0024630686
## [9997,] 0.0073029333 0.0073029333 0.0073029333 . 0.0073029333
## [9998,] 0.0003235198 0.0003235198 0.0003235198 . 0.0003235198
## [9999,] -0.0030012310 -0.0030012310 -0.0030012310 . -0.0030012310
## [10000,] -0.0025196508 -0.0025196508 -8.1371583685 . -0.0025196508
## [,20000]
## [1,] 0.003974920
## [2,] -0.007102022
## [3,] 0.005706479
## [4,] -0.011039582
## [5,] 0.001072816
## ... .
## [9996,] -0.0024630686
## [9997,] 0.0073029333
## [9998,] 0.0003235198
## [9999,] -0.0030012310
## [10000,] -0.0025196508
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.003974920 -0.007102022 0.005706479 . -0.003001231 -0.002519651
## GENE_2 0.003974920 -0.007102022 0.005706479 . -0.003001231 -0.002519651
## GENE_3 0.003974920 -0.007102022 0.005706479 . -0.003001231 -8.137158368
## GENE_4 0.003974920 -0.007102022 0.005706479 . -0.003001231 -0.002519651
## GENE_5 0.003974920 -0.007102022 0.005706479 . -0.003001231 -0.002519651
## ... . . . . . .
## GENE_19996 0.003974920 -0.007102022 0.005706479 . -0.003001231 -0.002519651
## GENE_19997 0.003974920 -0.007102022 0.005706479 . -0.003001231 -0.002519651
## GENE_19998 0.003974920 -0.007102022 0.005706479 . -0.003001231 -0.002519651
## GENE_19999 0.003974920 -0.007102022 0.005706479 . -0.003001231 -0.002519651
## GENE_20000 0.003974920 -0.007102022 0.005706479 . -0.003001231 -0.002519651
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.003975 0.992898 1.005706 . 0.9969988 0.9974803
## GENE_2 1.003975 0.992898 1.005706 . 0.9969988 0.9974803
## GENE_3 1.003975 0.992898 1.005706 . 0.9969988 -7.1371584
## GENE_4 1.003975 0.992898 1.005706 . 0.9969988 0.9974803
## GENE_5 1.003975 0.992898 1.005706 . 0.9969988 0.9974803
## ... . . . . . .
## GENE_19996 1.003975 0.992898 1.005706 . 0.9969988 0.9974803
## GENE_19997 1.003975 0.992898 1.005706 . 0.9969988 0.9974803
## GENE_19998 1.003975 0.992898 1.005706 . 0.9969988 0.9974803
## GENE_19999 1.003975 0.992898 1.005706 . 0.9969988 0.9974803
## GENE_20000 1.003975 0.992898 1.005706 . 0.9969988 0.9974803
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.2.1 (2022-06-23)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 20.04.5 LTS
##
## Matrix products: default
## BLAS: /home/biocbuild/bbs-3.16-bioc/R/lib/libRblas.so
## LAPACK: /home/biocbuild/bbs-3.16-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.14.0 ScaledMatrix_1.6.0 DelayedArray_0.24.0
## [4] IRanges_2.32.0 S4Vectors_0.36.0 MatrixGenerics_1.10.0
## [7] matrixStats_0.62.0 BiocGenerics_0.44.0 Matrix_1.5-1
## [10] BiocStyle_2.26.0
##
## loaded via a namespace (and not attached):
## [1] Rcpp_1.0.9 bslib_0.4.0
## [3] compiler_4.2.1 BiocManager_1.30.19
## [5] jquerylib_0.1.4 tools_4.2.1
## [7] DelayedMatrixStats_1.20.0 digest_0.6.30
## [9] jsonlite_1.8.3 evaluate_0.17
## [11] lattice_0.20-45 rlang_1.0.6
## [13] cli_3.4.1 parallel_4.2.1
## [15] yaml_2.3.6 xfun_0.34
## [17] fastmap_1.1.0 stringr_1.4.1
## [19] knitr_1.40 sass_0.4.2
## [21] grid_4.2.1 R6_2.5.1
## [23] BiocParallel_1.32.0 rmarkdown_2.17
## [25] bookdown_0.29 irlba_2.3.5.1
## [27] magrittr_2.0.3 codetools_0.2-18
## [29] htmltools_0.5.3 sparseMatrixStats_1.10.0
## [31] rsvd_1.0.5 beachmat_2.14.0
## [33] stringi_1.7.8 cachem_1.0.6