PoissonRandom.jl: Fast Poisson Random Numbers
PoissonRandom.jl is a component of the SciML ecosystem which allows for fast generation of Poisson random numbers.
Installation
To install PoissonRandom.jl, use the Julia package manager:
using Pkg
Pkg.add("PoissonRandom")
Usage
# Simple Poisson random
pois_rand(λ)
# Using another RNG
using RandomNumbers
rng = Xorshifts.Xoroshiro128Plus()
pois_rand(rng,λ)
Implementation
It mixes two methods. A simple count method and a method from a normal approximation. See this blog post for details.
Benchmark
using RandomNumbers, Distributions, BenchmarkTools, StaticArrays,
RecursiveArrayTools, Plots, PoissonRandom
labels = ["count_rand","ad_rand","pois_rand","Distributions.jl"]
rng = Xorshifts.Xoroshiro128Plus()
function n_count(rng,λ,n)
tmp = 0
for i in 1:n
tmp += PoissonRandom.count_rand(rng,λ)
end
end
function n_pois(rng,λ,n)
tmp = 0
for i in 1:n
tmp += pois_rand(rng,λ)
end
end
function n_ad(rng,λ,n)
tmp = 0
for i in 1:n
tmp += PoissonRandom.ad_rand(rng,λ)
end
end
function n_dist(λ,n)
tmp = 0
for i in 1:n
tmp += rand(Poisson(λ))
end
end
function time_λ(rng,λ,n)
t1 = @elapsed n_count(rng,λ,n)
t2 = @elapsed n_ad(rng,λ,n)
t3 = @elapsed n_pois(rng,λ,n)
t4 = @elapsed n_dist(λ,n)
@SArray [t1,t2,t3,t4]
end
# Compile
time_λ(rng,5,5000000)
# Run with a bunch of λ
times = VectorOfArray([time_λ(rng,n,5000000) for n in 1:20])'
plot(times,labels = labels, lw = 3)
So this package ends up about 30% or so faster than Distributions.jl (the method at the far edge is λ-independent so that goes on forever).
Contributing
- Please refer to the SciML ColPrac: Contributor's Guide on Collaborative Practices for Community Packages for guidance on PRs, issues, and other matters relating to contributing to SciML.
- There are a few community forums:
- the #diffeq-bridged channel in the Julia Slack
- JuliaDiffEq on Gitter
- on the Julia Discourse forums
- see also SciML Community page
Reproducibility
The documentation of this SciML package was built using these direct dependencies,
Status `~/work/PoissonRandom.jl/PoissonRandom.jl/docs/Project.toml`
[e30172f5] Documenter v0.27.24
[e409e4f3] PoissonRandom v0.4.4 `~/work/PoissonRandom.jl/PoissonRandom.jl`
and using this machine and Julia version.
Julia Version 1.8.5
Commit 17cfb8e65ea (2023-01-08 06:45 UTC)
Platform Info:
OS: Linux (x86_64-linux-gnu)
CPU: 2 × Intel(R) Xeon(R) CPU E5-2673 v3 @ 2.40GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-13.0.1 (ORCJIT, haswell)
Threads: 1 on 2 virtual cores
A more complete overview of all dependencies and their versions is also provided.
Status `~/work/PoissonRandom.jl/PoissonRandom.jl/docs/Manifest.toml`
[a4c015fc] ANSIColoredPrinters v0.0.1
[ffbed154] DocStringExtensions v0.9.3
[e30172f5] Documenter v0.27.24
[b5f81e59] IOCapture v0.2.2
[682c06a0] JSON v0.21.3
[69de0a69] Parsers v2.5.8
[e409e4f3] PoissonRandom v0.4.4 `~/work/PoissonRandom.jl/PoissonRandom.jl`
[21216c6a] Preferences v1.3.0
[66db9d55] SnoopPrecompile v1.0.3
[2a0f44e3] Base64
[ade2ca70] Dates
[b77e0a4c] InteractiveUtils
[76f85450] LibGit2
[56ddb016] Logging
[d6f4376e] Markdown
[a63ad114] Mmap
[ca575930] NetworkOptions v1.2.0
[de0858da] Printf
[3fa0cd96] REPL
[9a3f8284] Random
[ea8e919c] SHA v0.7.0
[9e88b42a] Serialization
[6462fe0b] Sockets
[fa267f1f] TOML v1.0.0
[8dfed614] Test
[4ec0a83e] Unicode