lecture-python
lecture-python copied to clipboard
Add random seed to wealth dynamics lecture
For example, we want the figure showing Gini coefficient vs mu_r in weath_dynamics.rst
to be unchanging.
This is quite subtle due to use of Numba and parallelization (which tends to cause problems with seeds). I'm not sure it can be done but it needs to be investigated and a rewrite of the code might be required.
I've been in search of a way to set a seed once for the whole lecture and I noticed this.
It seems like the seed does not stay across cells.
@Harveyt47 I think what is happening here is that RandomState is setting a deterministic sequence of values. So randn
is extracting numbers from that sequence right?
That's what I thought but when I run the first cell it always returns 0.471... Is that consistent with what your saying @mmcky. I would have expected it to change.
Also using this type of method has issues with jitted classes and functions. I'm not sure jit can interpret the seed
variable in the above pic.
I've noticed that in the draw function in the qe library we can not set a seed for it.
when you rerun the first cell you're just reseting the sequence again right?
I see. Thanks @mmcky
@Harveyt47 , I recommend you create a little test notebook where you try to understand random seeds and how they interact with numba and prange.
You might need to do some background reading in the numba docs. Once you have the main ideas worked out, it would be great if you could share it with us. We might even end up writing a short lecture on this topic, because not many people understand it, including me :-)
Hi @jstac that sounds like a great idea. I'll get on it
@Harveyt47, would you mind to let us know the status of this?
Hi @jstac from the research I did the main problem is setting a seed across all the threads.
For example in the case of the gini coefficient curve the household wealth distribution comes from the function update_cross_section
. If we were to simply set a seed in that function it would only get stored a TLS slot in the main thread, while the other threads just get random junk based on the entropy value picked up and stuffed into their TLS slot on launch. So the first batch (25000) of households will have the same seed everytime but the other batches will have different seeds and change everytime. This causes the household wealth distribution to change everytime, hence causing the gini coefficient to change.