1 Introduction

The DelayedRandomArray package implements DelayedArray subclasses containing dynamically sampled random values. Specifically, the actual values are never fully held in memory but are generated when the relevant part of the array is accessed. This allows users to create very large arrays of random values that would not otherwise be possible by filling an ordinary matrix.

To install the package, follow the instructions on DelayedRandomArray landing page. Using the package is then as simple as:

library(DelayedRandomArray)
X <- RandomUnifArray(c(1e6, 1e6))
X
## <1000000 x 1000000> RandomUnifMatrix object of type "double":
##                  [,1]       [,2]       [,3] ...  [,999999] [,1000000]
##       [1,] 0.26379333 0.29222561 0.63984257   . 0.31273825 0.56465540
##       [2,] 0.62382193 0.25647437 0.44456993   . 0.06664912 0.05723420
##       [3,] 0.94583294 0.07172207 0.30236980   . 0.07001733 0.16462900
##       [4,] 0.83734467 0.40643016 0.06427003   . 0.88152434 0.90986213
##       [5,] 0.40368242 0.66762005 0.22670381   . 0.46689954 0.90168587
##        ...          .          .          .   .          .          .
##  [999996,]  0.0417775  0.9536846  0.3116957   .  0.9336127  0.5023319
##  [999997,]  0.2624894  0.8658582  0.6080100   .  0.3532050  0.5190161
##  [999998,]  0.9653936  0.9348063  0.3530679   .  0.5545114  0.7910735
##  [999999,]  0.5776378  0.2023542  0.3899164   .  0.3599636  0.9252263
## [1000000,]  0.7165756  0.7917388  0.4425323   .  0.7409182  0.6737057

The resulting array can be used in any pipeline that is compatible with DelayedArray objects. This object occupies only 64 MB in memory, whereas an ordinary matrix would require 8 PB instead.

2 Available distributions

Almost every distribution in stats is available here. To list a few:

RandomNormArray(c(100, 50))
## <100 x 50> RandomNormMatrix object of type "double":
##               [,1]        [,2]        [,3] ...      [,49]      [,50]
##   [1,]  1.47353922 -0.58378032 -0.21149462   .  0.7287314  0.2156521
##   [2,] -0.53138767  0.71340034 -0.23315905   . -0.8378126  0.1611583
##   [3,]  1.73407049  1.45623680  0.75745612   . -0.6889478 -1.3479071
##   [4,] -0.28019509 -0.22788062  0.76277284   .  0.4275883  0.4063914
##   [5,] -0.13990137  0.07068405  0.56727240   . -0.0167161  0.4578403
##    ...           .           .           .   .          .          .
##  [96,] -0.47514260  1.26061302  1.64520754   . -1.0462828 -1.1075355
##  [97,]  0.32389689  0.65673470  1.15397613   . -0.6170756  0.3129543
##  [98,] -0.56276214 -1.50615228  1.09682486   .  1.6084989  0.7576575
##  [99,]  0.02725097 -0.36949834  0.06634209   .  1.1510367  1.1116256
## [100,]  0.71856267  1.64701003 -1.63950679   . -1.1532210  0.6586700
RandomPoisArray(c(100, 50), lambda=5)
## <100 x 50> RandomPoisMatrix object of type "double":
##         [,1]  [,2]  [,3] ... [,49] [,50]
##   [1,]     5     5     5   .     4     6
##   [2,]     1     6    11   .     2     3
##   [3,]     6     3     2   .     3     5
##   [4,]     3     6     8   .     2     4
##   [5,]     4    10     8   .     7     3
##    ...     .     .     .   .     .     .
##  [96,]     4     9     5   .     5     4
##  [97,]     7     3     9   .     4     9
##  [98,]     5     4     8   .     6     5
##  [99,]     8     4     6   .     4     3
## [100,]     6     9     4   .     4     7
RandomGammaArray(c(100, 50), shape=2, rate=5)
## <100 x 50> RandomGammaMatrix object of type "double":
##              [,1]       [,2]       [,3] ...      [,49]      [,50]
##   [1,] 0.12695576 0.14412624 0.27861928   . 0.44637701 0.29672777
##   [2,] 0.17557847 0.48531861 0.51471465   . 0.29466718 0.09690729
##   [3,] 0.15697618 0.20466327 0.23694951   . 0.14520110 0.55805169
##   [4,] 0.08007816 0.20406054 0.08994813   . 0.53671586 0.72453243
##   [5,] 0.92688089 0.22816229 0.12662315   . 0.84839004 0.32979243
##    ...          .          .          .   .          .          .
##  [96,] 0.18338932 0.16326611 0.41321752   .  0.1693930  0.7066166
##  [97,] 0.32499561 0.02725639 0.60447360   .  0.2929770  0.1423339
##  [98,] 0.13787387 0.87936445 0.29183132   .  0.4603991  0.5743161
##  [99,] 0.16728196 0.24178597 0.18865896   .  0.1830510  0.1554517
## [100,] 0.29514372 0.37397329 0.36971895   .  0.0440218  0.3631392
RandomWeibullArray(c(100, 50), shape=5)
## <100 x 50> RandomWeibullMatrix object of type "double":
##             [,1]      [,2]      [,3] ...     [,49]     [,50]
##   [1,] 0.7704360 0.7831899 0.5156623   . 0.6154432 0.5924296
##   [2,] 1.0563207 0.6989134 0.7785415   . 0.9798323 0.5192600
##   [3,] 0.7947511 1.0300635 1.2521446   . 0.8504665 0.6860547
##   [4,] 0.9109946 0.4666859 0.8359525   . 1.2386188 1.1178206
##   [5,] 0.9122495 0.9048634 1.2651460   . 0.8957470 0.9346490
##    ...         .         .         .   .         .         .
##  [96,] 0.6436715 0.9433429 0.9501543   . 1.2711509 0.6428117
##  [97,] 0.8564764 1.1483935 0.5750862   . 1.0770692 0.6752371
##  [98,] 0.6735952 1.0986668 1.0334677   . 0.8597355 0.8661520
##  [99,] 0.9813829 0.6220124 1.0417367   . 0.9413299 0.7637875
## [100,] 0.8723086 1.2457986 0.9832207   . 1.1469289 0.7225749

Distributional parameters can either be scalars:

RandomNormArray(c(100, 50), mean=1)
## <100 x 50> RandomNormMatrix object of type "double":
##               [,1]        [,2]        [,3] ...       [,49]       [,50]
##   [1,]   2.1991641   1.6261622   1.9892251   .  1.07347782  0.06449775
##   [2,]   1.7251822   0.8192518   0.4628368   . -1.65512197  1.36799530
##   [3,]   1.2485230   1.3300615   1.5978083   .  2.18053048  3.09446889
##   [4,]   0.2562563   1.6114793   0.1308189   .  0.79490994  0.22994007
##   [5,]   0.4574812   0.6342269   1.1946976   .  0.56113356  1.43535700
##    ...           .           .           .   .           .           .
##  [96,] -0.11330858  1.66385794  2.35326842   .  1.62584360  0.22518641
##  [97,] -0.37101450  0.34616463 -0.78119475   .  0.09481825  3.68629582
##  [98,] -0.49751525  2.40535214  1.61267577   .  0.53114770  1.66169941
##  [99,]  0.85040272  1.97440691  0.07422923   .  1.00346549 -0.04531313
## [100,]  1.34745551  1.38790119  0.36700906   .  2.41731230  2.30917957

Or vectors, which are recycled along the length of the array:

RandomNormArray(c(100, 50), mean=1:100)
## <100 x 50> RandomNormMatrix object of type "double":
##              [,1]       [,2]       [,3] ...     [,49]     [,50]
##   [1,] -0.2499603  2.1245209  1.0900076   . 2.3101336 0.3161938
##   [2,]  3.0430464  2.7486272  1.4785630   . 2.8443235 1.5227462
##   [3,]  3.0691044  1.9411396  4.0784779   . 3.9591446 3.2227389
##   [4,]  3.1190301  4.2758497  3.3238294   . 4.1206545 4.7194089
##   [5,]  4.6881131  4.4276570  3.7328424   . 3.2354246 6.1478132
##    ...          .          .          .   .         .         .
##  [96,]   94.19727   96.42392   95.87003   .  96.18081  95.27798
##  [97,]   97.12174   97.34580   97.59181   .  97.98234  96.87389
##  [98,]   97.85531   97.59066   96.54401   .  99.25207  98.40158
##  [99,]   99.21266   96.83881   99.81925   .  99.51494  99.30192
## [100,]  100.25649   98.87097   99.26578   .  99.05367 100.32097

Or other arrays of the same dimensions, which are used to sample the corresponding parts of the random array:

means <- RandomNormArray(c(100, 50))
RandomPoisArray(c(100, 50), lambda=2^means)
## <100 x 50> RandomPoisMatrix object of type "double":
##         [,1]  [,2]  [,3] ... [,49] [,50]
##   [1,]     1     1     0   .     1     0
##   [2,]     2     3     1   .     3     2
##   [3,]     0     4     1   .     1     1
##   [4,]     1     0     1   .     0     2
##   [5,]     1     0     1   .     3     1
##    ...     .     .     .   .     .     .
##  [96,]     0     2     0   .     4     0
##  [97,]     1     4     1   .     3     0
##  [98,]     0     1     0   .     0     4
##  [99,]     0     0     1   .     1     0
## [100,]     2     0     2   .     0     0

For example, a hypothetical simulation of a million-cell single-cell RNA-seq dataset might look like this:

ngenes <- 20000
log.abundances <- runif(ngenes, -2, 5)

nclusters <- 20 # define 20 clusters and their population means.
cluster.means <- matrix(2^rnorm(ngenes*nclusters, log.abundances, sd=2), ncol=nclusters)

ncells <- 1e6
clusters <- sample(nclusters, ncells, replace=TRUE) # randomly allocate cells
cell.means <- DelayedArray(cluster.means)[,clusters]

dispersions <- 0.05 + 10/cell.means # typical mean variance trend.

y <- RandomNbinomArray(c(ngenes, ncells), mu=cell.means, size=1/dispersions)
y
## <20000 x 1000000> RandomNbinomMatrix object of type "double":
##                [,1]       [,2]       [,3] ...  [,999999] [,1000000]
##     [1,]         14         11         60   .         58          0
##     [2,]         44          0          7   .          0          3
##     [3,]          0          0          8   .          2          1
##     [4,]          1          1         17   .         10         14
##     [5,]          0          0          2   .          8          0
##      ...          .          .          .   .          .          .
## [19996,]         13         12          1   .          3         32
## [19997,]         19          0         12   .         67          9
## [19998,]          1          1          5   .         54        138
## [19999,]          0          0          0   .          0          0
## [20000,]          0          0          0   .          0          0

3 Chunking

Each random DelayedArrays is broken into contiguous rectangular chunks of identical size and shape. Each chunk is assigned a seed at construction time that is used to initialize a random number stream (using the PCG32 generator from the dqrng package). When the user accesses any part of the array, we generate the random numbers in the overlapping chunks and return the desired values. This provides efficient random access to any subarray without the need to use any jump-ahead functionality.

The chunking scheme determines the efficiency of accessing our random DelayedArrays. Chunks that are too large require unnecessary number generation when a subarray is requested, while chunks that are too small would increase memory usage and book-keeping overhead. The “best” choice also depends on the downstream access pattern, if such information is known. For example, in a matrix where each column is a chunk, retrieval of a column would be very efficient while retrieval of a single row would be very slow. The default chunk dimensions are set to the square root of the array dimensions (or 100, whichever is larger), providing a reasonable compromise between all of these considerations. This can also be manually specified with the chunkdim= argument.

# Row-wise chunks:
RandomUnifArray(c(1000, 500), chunkdim=c(1, 500))
## <1000 x 500> RandomUnifMatrix object of type "double":
##               [,1]       [,2]       [,3] ...     [,499]     [,500]
##    [1,] 0.44075273 0.54748715 0.08166651   . 0.70443730 0.90218985
##    [2,] 0.52211302 0.69261668 0.04083123   . 0.67818346 0.62322710
##    [3,] 0.01423463 0.02492460 0.68268802   . 0.85691766 0.16664383
##    [4,] 0.97241977 0.85742077 0.19255951   . 0.10752350 0.07528164
##    [5,] 0.64143848 0.51046056 0.25555492   . 0.12338117 0.26193262
##     ...          .          .          .   .          .          .
##  [996,]  0.3792450  0.2866269  0.3525310   . 0.60260253 0.99761785
##  [997,]  0.7377363  0.6669897  0.5638813   . 0.98207736 0.02510493
##  [998,]  0.7244371  0.5574278  0.7268788   . 0.73332230 0.28783286
##  [999,]  0.4446221  0.6191326  0.6557881   . 0.31245677 0.97529974
## [1000,]  0.7365729  0.2246130  0.2176517   . 0.05241706 0.29849576
# Column-wise chunks:
RandomUnifArray(c(1000, 500), chunkdim=c(1000, 1))
## <1000 x 500> RandomUnifMatrix object of type "double":
##               [,1]       [,2]       [,3] ...     [,499]     [,500]
##    [1,]  0.5246893  0.5575835  0.1157417   .  0.3148403  0.2352046
##    [2,]  0.5130985  0.1415731  0.1724831   .  0.5080082  0.2863649
##    [3,]  0.4030096  0.1222743  0.6676011   .  0.1086206  0.5465005
##    [4,]  0.1927735  0.2494870  0.1422395   .  0.6986704  0.2220614
##    [5,]  0.7665252  0.2935630  0.5591323   .  0.3559595  0.2988016
##     ...          .          .          .   .          .          .
##  [996,] 0.80865014 0.74066640 0.14950441   . 0.81157026 0.48262237
##  [997,] 0.78247207 0.64517276 0.68147686   . 0.53149473 0.22889009
##  [998,] 0.48362572 0.03203674 0.37975912   . 0.22818368 0.89430266
##  [999,] 0.25163212 0.41469134 0.11154532   . 0.02721764 0.25009425
## [1000,] 0.97344638 0.10635418 0.52000626   . 0.49630184 0.19057009

Unlike other chunk-based DelayedArrays, the actual values of the random DelayedArray are dependent on the chunk parameters. This is because the sampling is done within each chunk and any alteration to the chunk shape or size will rearrange the stream of random numbers within the array. Thus, even when the seed is set, a different chunkdim will yield different results:

set.seed(199)
RandomUnifArray(c(10, 5), chunkdim=c(1, 5))
## <10 x 5> RandomUnifMatrix object of type "double":
##               [,1]         [,2]         [,3]         [,4]         [,5]
##  [1,] 0.4432813900 0.7119991907 0.7257159729 0.9557158216 0.2104000037
##  [2,] 0.3134128384 0.0670862596 0.3807734565 0.1526811279 0.1971345709
##  [3,] 0.5525409468 0.7807079460 0.9905007351 0.0898367104 0.8541555854
##  [4,] 0.8914577304 0.3764661429 0.6028462166 0.2576166289 0.0145520803
##  [5,] 0.5042253651 0.2618340398 0.2405915416 0.6800763716 0.5603335327
##  [6,] 0.3953960796 0.1314800435 0.9008538304 0.1165704445 0.9035755624
##  [7,] 0.6270407308 0.0001234491 0.3542078000 0.3546805161 0.9760325255
##  [8,] 0.2882098067 0.9133890264 0.8018615395 0.7615402588 0.6458653482
##  [9,] 0.2233627134 0.2957882064 0.7371763836 0.3001928469 0.8730950402
## [10,] 0.6481108814 0.5491673953 0.6821353873 0.4434014931 0.1461723484
set.seed(199)
RandomUnifArray(c(10, 5), chunkdim=c(10, 1))
## <10 x 5> RandomUnifMatrix object of type "double":
##             [,1]       [,2]       [,3]       [,4]       [,5]
##  [1,] 0.44328139 0.31341284 0.55254095 0.89145773 0.50422537
##  [2,] 0.71199919 0.06708626 0.78070795 0.37646614 0.26183404
##  [3,] 0.72571597 0.38077346 0.99050074 0.60284622 0.24059154
##  [4,] 0.95571582 0.15268113 0.08983671 0.25761663 0.68007637
##  [5,] 0.21040000 0.19713457 0.85415559 0.01455208 0.56033353
##  [6,] 0.63126383 0.10118984 0.76830283 0.17583353 0.62183470
##  [7,] 0.87478047 0.26076972 0.58018989 0.92146566 0.90680388
##  [8,] 0.55180537 0.20464839 0.38008429 0.56606812 0.75101891
##  [9,] 0.40323090 0.20083487 0.28559824 0.67769322 0.11943279
## [10,] 0.13404760 0.94959186 0.61432837 0.67675354 0.64178865

4 Further comments

Like any other random process, the seed should be set to achieve reproducible results. We stress that the R-level seed only needs to be set before construction of the random DelayedArray; it is not necessary to set the seed during its use. This is because the class itself will define further seeds (one per chunk) and store these in the object for use in per-chunk sampling.

set.seed(999)
RandomNormArray(c(10, 5))
## <10 x 5> RandomNormMatrix object of type "double":
##              [,1]        [,2]        [,3]        [,4]        [,5]
##  [1,]  0.07299897 -0.62967402  0.48686043 -1.00406271  0.09839211
##  [2,] -1.87549362  0.11653501  0.98496315 -0.85515988 -1.44932474
##  [3,]  0.62150210 -0.24704984 -0.69766033 -1.60089511  0.94752723
##  [4,] -1.68035676  0.20939894 -3.29663662  0.57270346  0.94413669
##  [5,]  0.22933664  1.12333362 -0.77725398  1.59886411  1.07042538
##  [6,]  0.43082191 -0.22740387  1.25381398  0.32842239  0.20448699
##  [7,] -0.71424097  0.27773134  1.53748664  0.01761569  1.15470498
##  [8,]  0.35908991  0.35618136 -0.49375892 -0.38652760  0.95591577
##  [9,]  0.06472627  0.79333115 -0.73803086 -0.56434493  0.22473943
## [10,] -2.41389122  0.76273286  0.47567035 -0.08524682 -1.17849232
set.seed(999)
RandomNormArray(c(10, 5))
## <10 x 5> RandomNormMatrix object of type "double":
##              [,1]        [,2]        [,3]        [,4]        [,5]
##  [1,]  0.07299897 -0.62967402  0.48686043 -1.00406271  0.09839211
##  [2,] -1.87549362  0.11653501  0.98496315 -0.85515988 -1.44932474
##  [3,]  0.62150210 -0.24704984 -0.69766033 -1.60089511  0.94752723
##  [4,] -1.68035676  0.20939894 -3.29663662  0.57270346  0.94413669
##  [5,]  0.22933664  1.12333362 -0.77725398  1.59886411  1.07042538
##  [6,]  0.43082191 -0.22740387  1.25381398  0.32842239  0.20448699
##  [7,] -0.71424097  0.27773134  1.53748664  0.01761569  1.15470498
##  [8,]  0.35908991  0.35618136 -0.49375892 -0.38652760  0.95591577
##  [9,]  0.06472627  0.79333115 -0.73803086 -0.56434493  0.22473943
## [10,] -2.41389122  0.76273286  0.47567035 -0.08524682 -1.17849232

For certain distributions, it is possible to indicate that the array is sparse. This does not change the result or efficiency of the sampling process, but can still be useful as it allows downstream functions to use more efficient sparse algorithms. Of course, this is only relevant if the distributional parameters are such that sparsity is actually observed.

RandomPoisArray(c(1e6, 1e6), lambda=0.5) # dense by default
## <1000000 x 1000000> RandomPoisMatrix object of type "double":
##                  [,1]       [,2]       [,3] ...  [,999999] [,1000000]
##       [1,]          0          0          1   .          1          0
##       [2,]          0          1          1   .          0          1
##       [3,]          0          1          1   .          0          0
##       [4,]          0          1          0   .          1          0
##       [5,]          0          0          0   .          1          0
##        ...          .          .          .   .          .          .
##  [999996,]          1          0          0   .          1          1
##  [999997,]          0          1          0   .          1          1
##  [999998,]          2          0          1   .          0          0
##  [999999,]          1          0          1   .          0          1
## [1000000,]          0          0          0   .          1          0
RandomPoisArray(c(1e6, 1e6), lambda=0.5, sparse=TRUE) # treat as sparse
## <1000000 x 1000000> sparse RandomPoisMatrix object of type "double":
##                  [,1]       [,2]       [,3] ...  [,999999] [,1000000]
##       [1,]          0          1          1   .          1          0
##       [2,]          0          1          1   .          1          0
##       [3,]          0          1          1   .          0          0
##       [4,]          1          2          0   .          0          0
##       [5,]          0          1          0   .          0          2
##        ...          .          .          .   .          .          .
##  [999996,]          0          0          1   .          0          0
##  [999997,]          0          0          0   .          1          0
##  [999998,]          3          0          0   .          1          1
##  [999999,]          1          0          0   .          1          0
## [1000000,]          0          2          1   .          1          0

Session information

sessionInfo()
## R version 4.3.1 (2023-06-16)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.18-bioc/R/lib/libRblas.so 
## LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.10.0
## 
## 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       
## 
## time zone: America/New_York
## tzcode source: system (glibc)
## 
## attached base packages:
## [1] stats4    stats     graphics  grDevices utils     datasets  methods  
## [8] base     
## 
## other attached packages:
##  [1] DelayedRandomArray_1.10.0 DelayedArray_0.28.0      
##  [3] SparseArray_1.2.0         S4Arrays_1.2.0           
##  [5] abind_1.4-5               IRanges_2.36.0           
##  [7] S4Vectors_0.40.0          MatrixGenerics_1.14.0    
##  [9] matrixStats_1.0.0         BiocGenerics_0.48.0      
## [11] Matrix_1.6-1.1            BiocStyle_2.30.0         
## 
## loaded via a namespace (and not attached):
##  [1] crayon_1.5.2        cli_3.6.1           knitr_1.44         
##  [4] rlang_1.1.1         xfun_0.40           jsonlite_1.8.7     
##  [7] dqrng_0.3.1         htmltools_0.5.6.1   sass_0.4.7         
## [10] rmarkdown_2.25      grid_4.3.1          evaluate_0.22      
## [13] jquerylib_0.1.4     fastmap_1.1.1       yaml_2.3.7         
## [16] bookdown_0.36       BiocManager_1.30.22 compiler_4.3.1     
## [19] Rcpp_1.0.11         XVector_0.42.0      lattice_0.22-5     
## [22] digest_0.6.33       R6_2.5.1            bslib_0.5.1        
## [25] tools_4.3.1         zlibbioc_1.48.0     cachem_1.0.8