lecture-python icon indicating copy to clipboard operation
lecture-python copied to clipboard

Add random seed to wealth dynamics lecture

Open jstac opened this issue 4 years ago • 10 comments

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.

jstac avatar Dec 08 '19 05:12 jstac

I've been in search of a way to set a seed once for the whole lecture and I noticed this.

image

It seems like the seed does not stay across cells.

Harveyt47 avatar Apr 24 '20 05:04 Harveyt47

@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?

mmcky avatar Apr 24 '20 05:04 mmcky

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.

Harveyt47 avatar Apr 24 '20 05:04 Harveyt47

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.

Harveyt47 avatar Apr 24 '20 05:04 Harveyt47

when you rerun the first cell you're just reseting the sequence again right?

mmcky avatar Apr 24 '20 05:04 mmcky

I see. Thanks @mmcky

Harveyt47 avatar Apr 24 '20 06:04 Harveyt47

@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 :-)

jstac avatar Apr 24 '20 06:04 jstac

Hi @jstac that sounds like a great idea. I'll get on it

Harveyt47 avatar Apr 24 '20 06:04 Harveyt47

@Harveyt47, would you mind to let us know the status of this?

jstac avatar Jun 21 '20 02:06 jstac

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.

Harveyt47 avatar Jul 05 '20 11:07 Harveyt47