QuantEcon / QuantEcon/lecture-python.myst
Add random seed to wealth dynamics lecture
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 aslecture-python#50.QuantEcon/lecture-pythonwas renamed tolecture-python.rstand 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.mdcontains noseed,default_rngorGeneratoranywhere in the file, whileupdate_cross_sectionis@jit(parallel=True)and draws unseedednp.random.randn()inside aprangeloop. The Gini-coefficient-vs-μ_rfigure 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
prangeeach 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 theprangebody — Numba supportsnp.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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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