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.088749555 0.146312121 0.378516708   .  0.9590824  0.4682915
##       [2,] 0.819861617 0.230725160 0.680584554   .  0.4320797  0.8079822
##       [3,] 0.686916681 0.783431055 0.969641572   .  0.4648853  0.9012648
##       [4,] 0.006669501 0.037234796 0.006407519   .  0.3834564  0.6972207
##       [5,] 0.818390028 0.367555792 0.131527964   .  0.3306267  0.6765129
##        ...           .           .           .   .          .          .
##  [999996,]  0.30900278  0.08901336  0.94251137   . 0.63387108 0.06897043
##  [999997,]  0.78834067  0.97454977  0.72554844   . 0.37322153 0.33711110
##  [999998,]  0.22263979  0.89681512  0.49848062   . 0.25528731 0.72014648
##  [999999,]  0.82139860  0.62759085  0.70314140   . 0.77755119 0.45608118
## [1000000,]  0.09019603  0.91118117  0.73309726   . 0.76718642 0.54881996

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,] -0.00167534  0.87046782  2.52435151   .  1.48039453  1.48930232
##   [2,] -1.38154513 -0.30684665 -0.59475128   . -0.70695788 -0.41252484
##   [3,]  0.68313606  0.14873788 -0.22648216   .  1.38282197  0.27311371
##   [4,]  0.69840842  1.12524567 -0.80523657   . -0.10651158 -0.24141501
##   [5,]  0.22717179  0.25317672 -0.02009002   .  0.04121443  0.01503194
##    ...           .           .           .   .           .           .
##  [96,] -1.04381703  0.20864848 -0.42539246   .   0.4404512   2.0990996
##  [97,] -0.30662382 -0.70769920 -0.75480321   .   0.9959924   0.7358603
##  [98,] -0.44204403 -1.13808488 -1.21990501   .   1.4304720  -0.6579189
##  [99,] -0.75589976 -1.11087714 -0.90640471   .   0.3883911  -0.2613427
## [100,]  0.39749879 -0.34131594  0.03182672   .   0.1898890   1.1243805
RandomPoisArray(c(100, 50), lambda=5)
## <100 x 50> RandomPoisMatrix object of type "double":
##         [,1]  [,2]  [,3] ... [,49] [,50]
##   [1,]     6     5     1   .     7     6
##   [2,]     8     4     3   .     5     8
##   [3,]     6     4     7   .     7     3
##   [4,]     2     4     5   .     1     5
##   [5,]     5     2     4   .     8     1
##    ...     .     .     .   .     .     .
##  [96,]     8     5     1   .     3     2
##  [97,]     3     7     7   .     4     3
##  [98,]     4     5     4   .     6     5
##  [99,]     8     7     5   .     5     7
## [100,]     6     3     2   .     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.27461648 0.52188450 0.75108070   . 0.4548807 0.7051546
##   [2,] 1.10659961 0.52063733 0.16648515   . 0.5019398 0.4437383
##   [3,] 0.18843464 0.62676451 1.07552935   . 0.9200826 0.2269299
##   [4,] 0.43165348 0.08961792 0.54077202   . 0.4545926 0.8234225
##   [5,] 0.34284343 0.39576893 0.21974446   . 0.3185815 0.7473326
##    ...          .          .          .   .         .         .
##  [96,]  0.6101449  0.3050482  0.1880654   . 0.4964250 0.2436101
##  [97,]  0.5053198  0.5594408  0.1757313   . 0.7156677 0.3337240
##  [98,]  0.5174274  0.1628403  0.9962549   . 0.2097312 0.4757676
##  [99,]  0.3962722  0.9031558  0.7329305   . 0.2391279 0.3615020
## [100,]  0.3248019  0.6424519  0.1235866   . 0.1319319 0.2850831
RandomWeibullArray(c(100, 50), shape=5)
## <100 x 50> RandomWeibullMatrix object of type "double":
##             [,1]      [,2]      [,3] ...     [,49]     [,50]
##   [1,] 1.1372171 0.7532302 0.4797919   . 0.7953768 0.8672548
##   [2,] 1.2337067 0.4068913 0.6599836   . 1.0464132 1.0073082
##   [3,] 1.0564860 1.0127513 0.7763422   . 0.8985116 0.9835002
##   [4,] 0.8383407 0.8739463 0.9444520   . 1.1118857 0.9322232
##   [5,] 1.0464115 0.9190162 0.9458395   . 0.7354442 0.7380417
##    ...         .         .         .   .         .         .
##  [96,] 1.0664724 0.8953067 0.9249280   . 0.9353810 1.0958948
##  [97,] 0.8609039 0.7201273 0.7254585   . 1.1801992 0.8801569
##  [98,] 0.4989000 0.4856727 0.9490905   . 0.8491212 0.7990233
##  [99,] 0.8680392 0.7423924 0.8782773   . 0.9277874 0.8926898
## [100,] 0.6394888 1.1776325 0.7576658   . 0.6374074 0.8155833

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,]  0.3562239  0.3350796  1.2120348   .  0.8258507 -0.7259101
##   [2,]  1.5850195  2.6589192  1.7686540   .  2.2429419  0.3510131
##   [3,]  2.5559902 -0.7006556 -0.1311034   .  1.6949841  2.7101427
##   [4,]  0.9637579  0.6963579  0.9745186   .  1.0816916  0.8489233
##   [5,]  1.3716280  0.3688413  1.3188694   .  1.1337667  0.4184686
##    ...          .          .          .   .          .          .
##  [96,]  1.2787768 -0.0745743 -1.1518107   .  0.6233468  0.4053350
##  [97,]  2.2771021  2.0795850  1.8139124   .  2.3740335  0.9863034
##  [98,]  0.5068449  0.2119329  0.4402911   . -0.7709003 -1.1305573
##  [99,]  0.5112565  1.8248420  0.6599342   .  1.7139338  0.3244719
## [100,] -0.7803401  1.7937184  1.7260532   .  2.2228004  2.0505213

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.5905583 1.6018603 1.5666295   .  0.3850914 -0.1872447
##   [2,] 1.5037429 2.7862329 2.9325015   .  0.3265939  1.5258873
##   [3,] 2.8215183 3.3499540 3.6055303   .  2.6690568  3.5395700
##   [4,] 4.2541171 2.2192317 2.7722935   .  5.0062384  3.2511736
##   [5,] 7.2707726 5.4104155 5.6524855   .  4.0571086  4.3540972
##    ...         .         .         .   .          .          .
##  [96,]  96.52438  95.94424  95.41443   .   97.37200   97.64322
##  [97,]  97.08980  96.08476  96.26023   .   96.88094   95.96963
##  [98,]  99.18540  97.17587  97.66147   .   98.28583   98.25750
##  [99,] 100.53611  98.67861  99.77295   .   96.61773   99.45922
## [100,] 100.61912  98.98571 100.79277   .   98.82663   98.49956

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,]     0     2     0   .     0     1
##   [2,]     3     2     2   .     3     2
##   [3,]     1     0     0   .     2     0
##   [4,]     2     1     0   .     4     3
##   [5,]     0     0     0   .     6     0
##    ...     .     .     .   .     .     .
##  [96,]     0     0     0   .     1     0
##  [97,]     0     2     3   .     4     1
##  [98,]     2     0     2   .     0     2
##  [99,]     2     1     3   .     0     1
## [100,]     0     4     3   .     1     1

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,]         86          0          0   .          1          0
##     [2,]         39          0         14   .          0          2
##     [3,]         92          0         40   .         27          0
##     [4,]          0          0          0   .          1          0
##     [5,]          1         35          8   .          0          5
##      ...          .          .          .   .          .          .
## [19996,]         39         40         12   .         17         32
## [19997,]          0         23         17   .         84          0
## [19998,]          0          0          0   .          0          0
## [19999,]         11         81          0   .         15          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.40981316  0.34707320  0.27827909   .  0.8780658  0.1477425
##    [2,]  0.99421054  0.52779757  0.48573954   .  0.2986602  0.9607987
##    [3,]  0.40867898  0.54731794  0.87196210   .  0.9158948  0.2542384
##    [4,]  0.35830985  0.92038584  0.01933099   .  0.4154390  0.3160844
##    [5,]  0.19637391  0.29740368  0.73458491   .  0.6565679  0.5785468
##     ...           .           .           .   .          .          .
##  [996,] 0.657648608 0.970054148 0.120209566   . 0.85008956 0.81334623
##  [997,] 0.709601086 0.104885442 0.299765311   . 0.93421803 0.26803259
##  [998,] 0.099484018 0.701613049 0.480007401   . 0.58764643 0.06207592
##  [999,] 0.556850949 0.008759209 0.789821773   . 0.38473580 0.65858304
## [1000,] 0.480194961 0.558906938 0.159005155   . 0.48669954 0.83471310
# 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.682077353 0.979525193 0.573242063   .  0.27881640  0.86600313
##    [2,] 0.447863429 0.622299031 0.180889954   .  0.08990158  0.02576768
##    [3,] 0.329634100 0.801760698 0.004235837   .  0.97651056  0.02987674
##    [4,] 0.074984054 0.245481492 0.717025056   .  0.46606069  0.93008676
##    [5,] 0.945279573 0.628740335 0.043825620   .  0.41298395  0.68962270
##     ...           .           .           .   .           .           .
##  [996,]  0.03978868  0.60220814  0.07155656   . 0.524212056 0.477666251
##  [997,]  0.47265352  0.11205378  0.98142932   . 0.887974072 0.003762269
##  [998,]  0.26642318  0.98785813  0.13627945   . 0.325730401 0.888961171
##  [999,]  0.86575896  0.10496876  0.35305925   . 0.284999264 0.276240815
## [1000,]  0.75065061  0.19725626  0.90849992   . 0.885207879 0.391422166

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 Under development (unstable) (2023-10-22 r85388)
## Platform: x86_64-pc-linux-gnu
## Running under: Ubuntu 22.04.3 LTS
## 
## Matrix products: default
## BLAS:   /home/biocbuild/bbs-3.19-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_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [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.11.0 DelayedArray_0.29.0      
##  [3] SparseArray_1.3.0         S4Arrays_1.3.0           
##  [5] abind_1.4-5               IRanges_2.37.0           
##  [7] S4Vectors_0.41.0          MatrixGenerics_1.15.0    
##  [9] matrixStats_1.0.0         BiocGenerics_0.49.0      
## [11] Matrix_1.6-1.1            BiocStyle_2.31.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.4.0          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.4.0     
## [19] Rcpp_1.0.11         XVector_0.43.0      lattice_0.22-5     
## [22] digest_0.6.33       R6_2.5.1            bslib_0.5.1        
## [25] tools_4.4.0         zlibbioc_1.49.0     cachem_1.0.8