-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor RNGs #392
Merged
Merged
Refactor RNGs #392
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Apologies for the somewhat large PR!
Note: This PR relies on a just-released version of Sciris, so please update (
pip install sciris --upgrade
, orpip install -e .
for Starsim).New RNGs & distributions
ss.SingleRNG()
,ss.MultiRNG()
,ss.ScipyDistribution()
, andss.ScipyHistogram()
with a singless.Dist()
class. Therandom.py
anddistributions.py
submodules have been removed, anddists.py
has been added.ss.Dist
class usesnp.random.default_rng()
rather thanscipy.stats
by default, although ascipy.stats
distribution can be supplied as an alternative. While comparable in most cases, in some instances Numpy is up to 4x faster (including, critically, for Bernoulli distributions).ss.Dist
has various tools for understanding what it's doing, includingdist.show_state(),
dist.plot_hist()`, etc.ss.options.multirng
(this branch is equivalent to it being always on).make_new_cases()
), using an approach similar to the previousmake_new_cases_singlerng()
logic (though we could instead use themake_new_cases_multirng()
logic; it should be similar in terms of performance and outcome, though slightly more complex code).ss.choice()
andss.delta()
.dist = ss.weibull(a=2); dist(5)
will return 5 random variates from a Weibull distribution.Sim
object is parsed and all distributions will be initialized with a unique identifier based on their place in the object (e.g.sim.diseases.sir.pars.dur_inf
), which is used to set their unique seed. (Note: this relies on Sciris 3.1.5, which hasn't been released yet, sosc_nested.py
has been copied here as a temporary fix.)Examples
Other changes
ss.lognorm_u()
, the Numpy/SciPy default, equivalent toss.lognorm_mean()
previously) or the "overlying" lognormal distribution (ss.lognorm_o()
, equivalent toss.lognorm()
previously).ss.dx
,ss.tx
,ss.vx
toss.Dx
,ss.Tx
,ss.Vx
.set_numba_seed()
as a duplicate ofset_seed()
.FAQ
ss.MultiRNG
andss.ScipyDistribution
into a single class?ss.options.rng
, how can we compare results with and without CRN?Dist
instances and replace all the RNGs with the same one (eek!). From a user perspective, keeping an option that we do not recommend using is confusing.multirng='multi'
was before. Going to spend some time figuring out why.test_repeat_slots
, but just showed that the same random numbers are drawn for each slot, whereas we want to show that simulations diverge without using slots but don't diverge when using them.)Closes #243, #316, #348, #312, #334, #340, #313, #322. Opens #395.