Repository navigation
Description
- Copulas version: 0.1
- Python version: 3.6.6
- Operating System: Fedora release 28 (Twenty Eight)
Description
I was expecting the sample methods to allow the user to pass a seed for the random numbers generators.
What I Did
Instead, we have to use, outside the function call, numpy.random.seed(seed_value) and random.setstate(seed_value). This is a bad practise from a software engineering standpoint and it is very error prone because it affects the global state. Also, this can negatively impact experiment reproducibility and the debugging stages.
Recommendations
Currently, in order to get the same sample from the sampling methods, we need to
- invoke
np.random.seed(seed_value) - invoke
random.setstate(random_state_tuple)
outside of the sampling function being invoked (i.e. sample). This results in what is called, in software engineering, a leaky abstraction. In order to solve this issue with seed control, there are (at least) two approaches:
- Create a parameter, in the
samplemethods, namedseedorrandom_state. - Create a parameter in the constructor of classes offering the
samplemethod namedseedorrandom_stateIF the distribution fit method requires some sort of stochastic process.
In scikit-learn and other popular python machine learning tools, what happens is the following
-
When a model depends on some sort of stochastic process during the fit procedure, the model class constructor allows the user to set the
random_statevalue. This value can be one of 3 things: None, an integer or an instance ofnumpy.random.RandomStateinstance. No matter what the value is, it will be checked and processed bysklearn.utils.check_random_state, which will output anumpy.random.RandomStateinstance. Note that thesklearn.utils.check_random_statemethod will be invoked at the beginning of the fit method (check this example: https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/tree/tree.py#L129). If you setrandom_stateas an integer, every fit has to be deterministic in its behavior and output. -
If, besides the fit method, there is another method that depends on stochastic processes (e.g. the
samplemethod insklearn.neighbors.KernelDensity), we are allowed to control the seed through therandom_stateparameter. -
In other more low level APIs like scipy, the seed must be an integer or None.
I also advise against using both the random and numpy.random modules at the same time because it makes the seed management harder.
EDIT: Current fix available at #62
Activity
ManuelAlvarezC commentedon Oct 15, 2018
Hi @echo66
I agree that being able to set the
random_stateto ensure reproducibility of your results is indeed a useful feature to have.From the approaches you propose, I'll definetely pick the second one: having
random_stateas an constructor argument on the classes that use some kind of stochastic process.I would also take your suggestion of using only one of
randomandnumpy.random. Will you be open to do the changes yourself?ManuelAlvarezC commentedon Oct 17, 2018
The required changes should be the following:
randomwithnp.randomrandom_state_calloncopulas.__init__, like this:Change classes
bivariate.base.Bivariateandmultivariate.tree.Tree__new__method in order for them to accept arbitrary positional and keyword arguments*args, **kwargsAdd the argument
random_state=Noneon__init__for both classes, that sets its value to the attribute of the same name.Replace on their subclasses all calls to functions that depend on their np.random with calls to
random_state_call, that is:Repeat the steps 4 & 5 for
univariate.base.Univariate,multivariate.base.Multivariateandmultivariate.vine.VineCopulas.Create unittests for
random_state_calland for thesamplemethods, in which, after a random state is set, the sample values are exactly the same.Check everything is according to our Contributing Guide.
If you plan on updating your PR with this changes, please let us know and we will assign this issue to you.