Hash-Based Matching Pseudo-Random Number Generation
hash_seed.Rd
Hash-Based Matching Pseudo-Random Number Generation
Arguments
- salt
the matching value for a particular collection of simulations
- ...
distinguishing features to identify the event; see details.
Details
These functions provide convenient invocation for hash-based matching pseudo-random generation (HBM-PRNG).
hash_seed
uses a salt
value along with distinguishing features of an
event.
Typically, salt
distinguishes an overall sample simulation, but it can also
be a temporarily computed value for events that share some-but-not all
features.
hash_salt
computes a partial hash, for when several events need draws,
but share a partially consistent feature set. The result of hash_salt
can for the consistent features can be computed once, then provided to
hash_seed
along with remaining distinct features.
For matched stochastic simulation, we desire a few properties:
the same random events are resolved consistently
possibility of different stochastic samples
reproducibility of pseudo-random simulations
Traditional PRNG seeding provides the latter points. To the extent that the PRNG is traversed the same way across simulations, events will also be resolved consistently. However, once event resolution leads to diverging outcomes (the whole point of doing otherwise-matched simulations with some parameter varying), the overall trajectory of the simulation will be begin to exercise the PRNG differently. When different events occur between the samples, this does not matter - one random deviate is as good as another. However, diverging trajectories can still share some of the same events. These events should be resolved consistently: for example, if a probabilistic threshold is increasing across scenarios, then a particular event testing that should only change from pass to fail. In practice, this means that same events need have the same PRNG draws, which is not possible if the PRNG state has otherwise diverged due to other parts of the simulation.
The HBM PRNG approach encodes events such that when they are the same (as defined by the simulation), they create identical hashes, which are then used to set the PRNG state. This ensure the same subsequent draws for that event.
Examples
salt <- 8675309
evt <- list(type = "infection", from = 1, to = 2, time = 3.1)
evt2 <- list(type = "recovery", from = 1, to = 2, time = 3.1)
salt |> hash_seed(evt$type, evt$from, evt$to, evt$time)
print(runif(10))
#> [1] 0.4282151870 0.0632215929 0.0813261368 0.9317613035 0.8188156663
#> [6] 0.2199115756 0.1473409482 0.5801623734 0.0002066612 0.4696494299
print(runif(10))
#> [1] 0.10914618 0.53921462 0.54795937 0.79914916 0.17098160 0.71468689
#> [7] 0.55110744 0.51945499 0.01689139 0.11398212
salt |> hash_seed(evt$type, evt$from, evt$to, evt$time)
print(runif(10))
#> [1] 0.4282151870 0.0632215929 0.0813261368 0.9317613035 0.8188156663
#> [6] 0.2199115756 0.1473409482 0.5801623734 0.0002066612 0.4696494299
salt <- 42
salt |> hash_seed(evt$type, evt$from, evt$to, evt$time)
print(runif(10))
#> [1] 0.65849122 0.52118664 0.01567331 0.53579509 0.28385360 0.98761759
#> [7] 0.35847539 0.05703028 0.68919263 0.92891680