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> matrix of class RandomUnifMatrix and type "double":
##                   [,1]        [,2]        [,3] ...  [,999999] [,1000000]
##       [1,] 0.343639486 0.441688542 0.860506502   . 0.35359051 0.43480039
##       [2,] 0.786058505 0.003870911 0.608923805   . 0.49550181 0.44800748
##       [3,] 0.083763156 0.283734547 0.462262946   . 0.58422618 0.33584902
##       [4,] 0.070254318 0.537560437 0.133781558   . 0.40583717 0.50611444
##       [5,] 0.066225367 0.430506382 0.759780008   . 0.85291728 0.02988144
##        ...           .           .           .   .          .          .
##  [999996,]  0.52014350  0.20146855  0.27496558   . 0.82846913 0.42983580
##  [999997,]  0.66066026  0.05450986  0.81179433   . 0.09369905 0.35551529
##  [999998,]  0.29845716  0.36392723  0.30505994   . 0.62943668 0.11177970
##  [999999,]  0.88153777  0.08942567  0.12400434   . 0.76730058 0.62662225
## [1000000,]  0.51701192  0.13810760  0.00202917   . 0.45404518 0.33398114

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> matrix of class RandomNormMatrix and type "double":
##               [,1]        [,2]        [,3] ...       [,49]       [,50]
##   [1,]  -0.6011934  -0.5732634  -0.3776995   . -0.06531328 -0.53046833
##   [2,]  -0.2031213  -0.8397323  -1.7185321   . -1.33925974  0.91236988
##   [3,]   0.5905877  -0.5736672   1.1958877   .  0.70870791  1.07072831
##   [4,]  -0.3586262  -1.0403074   0.7092125   . -1.13498870  0.03907916
##   [5,]   0.4096147   0.2160781  -2.0501598   . -0.35863969 -0.01472284
##    ...           .           .           .   .           .           .
##  [96,] -0.33994413  0.30401250  0.49969166   . -0.25103147  1.18573610
##  [97,]  0.42414091  0.16896827 -1.74642439   .  0.38127527 -0.19951169
##  [98,] -1.28831171  1.24661790 -0.07868585   . -0.27801250 -0.93507413
##  [99,] -0.11259853  1.92806474 -0.38688606   .  2.07397547 -0.01448173
## [100,]  1.37592170  0.40540535  0.01244674   .  0.36210234  0.28589014
RandomPoisArray(c(100, 50), lambda=5)
## <100 x 50> matrix of class RandomPoisMatrix and type "double":
##         [,1]  [,2]  [,3] ... [,49] [,50]
##   [1,]     7     6     3   .     6     2
##   [2,]     7     6    11   .     7     6
##   [3,]     6     4     9   .     6     4
##   [4,]     8     7     3   .     3     1
##   [5,]     3     4     8   .     3     2
##    ...     .     .     .   .     .     .
##  [96,]     1     5     5   .     3     6
##  [97,]     6     4     4   .     2     5
##  [98,]     8     7     4   .     0     5
##  [99,]     6     9     6   .     3     5
## [100,]     5     6     6   .     2     8
RandomGammaArray(c(100, 50), shape=2, rate=5)
## <100 x 50> matrix of class RandomGammaMatrix and type "double":
##               [,1]        [,2]        [,3] ...      [,49]      [,50]
##   [1,] 0.006899269 0.234906090 0.241745286   . 0.49782518 0.07577611
##   [2,] 0.344679892 0.696806287 1.056287301   . 0.31318994 0.18588647
##   [3,] 0.098643897 0.236119278 0.552557170   . 0.12933139 0.32307497
##   [4,] 0.383165282 0.331336195 0.430201898   . 0.30807948 0.55313168
##   [5,] 0.149677564 0.114025158 0.485016206   . 0.06408671 0.04863975
##    ...           .           .           .   .          .          .
##  [96,]   0.6245013   0.1682378   0.4525734   .  0.7332528  0.3446594
##  [97,]   0.9045727   0.2125161   0.4749543   .  0.5099448  0.1067387
##  [98,]   0.4839484   0.3373190   0.2172500   .  0.4246909  0.3520892
##  [99,]   0.6237153   0.4712136   0.2615965   .  0.5165173  0.2521653
## [100,]   0.6662330   0.2366885   0.4136943   .  0.9401733  0.2659107
RandomWeibullArray(c(100, 50), shape=5)
## <100 x 50> matrix of class RandomWeibullMatrix and type "double":
##             [,1]      [,2]      [,3] ...     [,49]     [,50]
##   [1,] 0.8914158 1.1823746 1.2109179   . 0.9842744 0.9243360
##   [2,] 0.5782953 0.7656713 0.9739026   . 0.8175007 1.2409482
##   [3,] 0.9957739 0.8485463 0.9241503   . 0.6921695 0.6198248
##   [4,] 0.8635564 0.8661824 0.7410248   . 1.3206495 1.0541520
##   [5,] 0.8337328 0.4554188 0.9239832   . 1.2175298 0.6787119
##    ...         .         .         .   .         .         .
##  [96,] 0.7842048 0.7771269 1.0702554   . 0.9198410 0.9857982
##  [97,] 1.0990874 0.6213034 1.0402819   . 1.4082199 0.7805119
##  [98,] 0.9822062 1.0958025 0.8283657   . 1.0320690 0.9000247
##  [99,] 0.8720288 0.8801317 1.2703948   . 0.7734786 1.0822210
## [100,] 0.9921400 1.1260181 1.1412960   . 0.9124004 0.6221213

Distributional parameters can either be scalars:

RandomNormArray(c(100, 50), mean=1)
## <100 x 50> matrix of class RandomNormMatrix and type "double":
##                [,1]         [,2]         [,3] ...       [,49]       [,50]
##   [1,]   1.52368442   2.60063319   2.70569108   .  0.31277233  0.57769187
##   [2,]  -0.93631469   1.30028738  -0.06348553   .  0.07981494  2.13637321
##   [3,]   0.23908415   1.42845757   1.99587552   .  2.02205998 -1.81439251
##   [4,]   0.36009635   2.77902426   0.12933220   .  0.31847309  1.35683559
##   [5,]   1.04648185   1.51436497   0.66292108   .  1.18351909 -0.50412053
##    ...            .            .            .   .           .           .
##  [96,]  0.891405694  1.102374159  0.755378217   .  -0.1320922   1.9692588
##  [97,]  1.986286103  2.929321810  1.518491429   .   1.2482111  -0.5720886
##  [98,]  0.253480908  0.772429270  1.041767009   .   0.2236283   1.6463496
##  [99,] -0.501357225  2.502618296 -0.007024215   .   2.1293317   0.4395422
## [100,] -0.078641276  2.114969264 -0.735857667   .   2.6548009   2.9203325

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

RandomNormArray(c(100, 50), mean=1:100)
## <100 x 50> matrix of class RandomNormMatrix and type "double":
##              [,1]       [,2]       [,3] ...     [,49]     [,50]
##   [1,]  0.9121911 -0.7278320  1.3207885   . 1.7690530 0.7951444
##   [2,]  1.5145029  1.6986444  2.8645886   . 1.0953847 1.5045678
##   [3,]  3.2572037  3.3808523  2.7380159   . 0.8477638 3.1933172
##   [4,]  3.2726954  5.7405339  5.2301874   . 4.7827226 5.7402561
##   [5,]  5.1068160  4.1497936  6.1490491   . 6.2862074 5.3492352
##    ...          .          .          .   .         .         .
##  [96,]   95.90816   95.89402   98.18725   .  95.18760  95.84486
##  [97,]   97.41878   96.64600   96.47651   .  96.07100  95.85799
##  [98,]   97.83633   98.24940   96.89817   .  99.98465  99.52194
##  [99,]   98.77244  101.68619   99.48375   . 100.49592  99.36224
## [100,]   99.26843   99.22025  100.16641   .  98.94424  99.95261

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> matrix of class RandomPoisMatrix and type "double":
##         [,1]  [,2]  [,3] ... [,49] [,50]
##   [1,]     0     2     1   .     2     5
##   [2,]     0     1     0   .     0     2
##   [3,]     3     1     4   .     1     0
##   [4,]     4     1     3   .     0     1
##   [5,]     1     0     1   .     0     1
##    ...     .     .     .   .     .     .
##  [96,]     0     0     0   .     1     5
##  [97,]     0     2     1   .     2     7
##  [98,]     0     0     1   .     0     3
##  [99,]     1     0     0   .     0     0
## [100,]     0     2     0   .     2     2

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> matrix of class RandomNbinomMatrix and type "double":
##                [,1]       [,2]       [,3] ...  [,999999] [,1000000]
##     [1,]          0          3          0   .          0          0
##     [2,]          0          0          0   .         16          0
##     [3,]          8          7          7   .          7          1
##     [4,]         25          0          1   .          0         10
##     [5,]         17          9         19   .         30         28
##      ...          .          .          .   .          .          .
## [19996,]         32          0         28   .          6         15
## [19997,]          0          7          0   .          0          0
## [19998,]          1          0          0   .          0          1
## [19999,]          0          0          0   .          0          0
## [20000,]          0          0          0   .         14          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> matrix of class RandomUnifMatrix and type "double":
##               [,1]       [,2]       [,3] ...     [,499]     [,500]
##    [1,] 0.82538182 0.55525165 0.20508765   .  0.9822513  0.7756275
##    [2,] 0.75443880 0.98737978 0.27667584   .  0.6428628  0.7925656
##    [3,] 0.75315717 0.05213412 0.57851619   .  0.9122163  0.1481557
##    [4,] 0.78328919 0.03765322 0.66592372   .  0.2223433  0.8771188
##    [5,] 0.64684758 0.09219245 0.77172024   .  0.2890284  0.6538062
##     ...          .          .          .   .          .          .
##  [996,] 0.47997857 0.73928396 0.19609292   . 0.86436470 0.60424003
##  [997,] 0.23830923 0.01205726 0.38285451   . 0.42967867 0.32027265
##  [998,] 0.08480219 0.77452527 0.25249237   . 0.61363324 0.07975318
##  [999,] 0.39707903 0.26346831 0.87477621   . 0.33211263 0.85637735
## [1000,] 0.51665001 0.01884343 0.65535563   . 0.12887278 0.60377038
# Column-wise chunks:
RandomUnifArray(c(1000, 500), chunkdim=c(1000, 1))
## <1000 x 500> matrix of class RandomUnifMatrix and type "double":
##               [,1]       [,2]       [,3] ...      [,499]      [,500]
##    [1,] 0.06685927 0.98697212 0.74641941   .   0.1777374   0.2157995
##    [2,] 0.37917241 0.51072038 0.46034787   .   0.9517207   0.9406009
##    [3,] 0.42800320 0.87441161 0.94995684   .   0.2737562   0.3200407
##    [4,] 0.29329341 0.85883716 0.68452486   .   0.4676254   0.2479140
##    [5,] 0.91681189 0.18006003 0.27011942   .   0.6836801   0.4911534
##     ...          .          .          .   .           .           .
##  [996,] 0.78615267 0.31525526 0.89041853   . 0.253701605 0.333148650
##  [997,] 0.43199027 0.20158723 0.30210784   . 0.006838488 0.083316994
##  [998,] 0.77063618 0.62402169 0.80506868   . 0.428626689 0.519502536
##  [999,] 0.38726051 0.94263637 0.72977910   . 0.161519436 0.540101058
## [1000,] 0.03990283 0.83768343 0.03452436   . 0.516987855 0.771962131

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> matrix of class RandomUnifMatrix and 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> matrix of class RandomUnifMatrix and 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> matrix of class RandomNormMatrix and 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> matrix of class RandomNormMatrix and 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> matrix of class RandomPoisMatrix and 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 matrix of class RandomPoisMatrix and 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.0 RC (2023-04-13 r84269)
## Platform: x86_64-pc-linux-gnu (64-bit)
## Running under: Ubuntu 22.04.2 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.17-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.8.0 DelayedArray_0.26.0      IRanges_2.34.0          
## [4] S4Vectors_0.38.0         MatrixGenerics_1.12.0    matrixStats_0.63.0      
## [7] BiocGenerics_0.46.0      Matrix_1.5-4             BiocStyle_2.28.0        
## 
## loaded via a namespace (and not attached):
##  [1] cli_3.6.1           knitr_1.42          rlang_1.1.0        
##  [4] xfun_0.39           jsonlite_1.8.4      dqrng_0.3.0        
##  [7] htmltools_0.5.5     sass_0.4.5          rmarkdown_2.21     
## [10] grid_4.3.0          evaluate_0.20       jquerylib_0.1.4    
## [13] fastmap_1.1.1       yaml_2.3.7          bookdown_0.33      
## [16] BiocManager_1.30.20 compiler_4.3.0      Rcpp_1.0.10        
## [19] lattice_0.21-8      digest_0.6.31       R6_2.5.1           
## [22] bslib_0.4.2         tools_4.3.0         cachem_1.0.7