QuantEcon / QuantEcon/lecture-python.myst

Add random seed to wealth dynamics lecture

Open
#970 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
TeX
Stars
123
Forks
57
Avg merge
3d 10h
Merged PRs (30d)
11

Description

[!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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lectures/wealth_dynamics.md and inspect update_cross_section, especially the @jit(parallel=True) function and its np.random.randn() call inside prange. Rebuild the lecture and compare the Gini-coefficient-vs-mu_r figure across rebuilds; done means the figure is unchanged while parallel execution is retained.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
documentation
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.