Skip to content

Add random seed to wealth dynamics lecture #970

Open

Description

@jstac

Note

Triaged from the archiving of the legacy RST repo. Originally filed by @jstac on 2019-12-08 as lecture-python#50. QuantEcon/lecture-python was renamed to lecture-python.rst and archived on 2026-07-16; this issue was moved here because it was verified to still apply to the live lecture. Archiving context: QuantEcon/workspace-lectures#17.

Verified still present 2026-07-16. lectures/wealth_dynamics.md contains no seed, default_rng or Generator anywhere in the file, while update_cross_section is @jit(parallel=True) and draws unseeded np.random.randn() inside a prange loop. The Gini-coefficient-vs-μ_r figure named in the report still changes on every rebuild. For contrast, 43 other lectures in this repo do seed their randomness, so this one is an outlier against the repo's own norm rather than a deliberate convention.

Root cause, already diagnosed in the original thread by @Harveyt47: under prange each thread gets its own thread-local RNG slot, so seeding the main thread only fixes the first batch while the remaining threads reseed from entropy. A tractable fix is per-iteration seeding inside the prange body — Numba supports np.random.seed() in nopython mode — which makes the cross-section deterministic without giving up parallelism.


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.

Activity

  1. Harveyt47 commented on Apr 24, 2020

    @Harveyt47
    Member

    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.

  2. mmcky commented on Apr 24, 2020

    @mmcky
    Contributor

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

  3. Harveyt47 commented on Apr 24, 2020

    @Harveyt47
    Member

    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.

  4. Harveyt47 commented on Apr 24, 2020

    @Harveyt47
    Member

    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.

  5. mmcky commented on Apr 24, 2020

    @mmcky
    Contributor

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

  6. Harveyt47 commented on Apr 24, 2020

    @Harveyt47
    Member

    I see. Thanks @mmcky

  7. jstac commented on Apr 24, 2020

    @jstac
    ContributorAuthor

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

  8. Harveyt47 commented on Apr 24, 2020

    @Harveyt47
    Member

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

  9. jstac commented on Jun 21, 2020

    @jstac
    ContributorAuthor

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

  10. Harveyt47 commented on Jul 5, 2020

    @Harveyt47
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions