Skip to content

[python_by_example.md] Update np.random → Generator API#538

Open
Chihiro2000GitHub wants to merge 1 commit intomainfrom
update-python-by-example-rng
Open

[python_by_example.md] Update np.random → Generator API#538
Chihiro2000GitHub wants to merge 1 commit intomainfrom
update-python-by-example-rng

Conversation

@Chihiro2000GitHub
Copy link
Copy Markdown
Collaborator

This PR updates legacy NumPy random API usage in python_by_example.md to the recommended Generator API.

I introduced a local rng = np.random.default_rng() and replaced the relevant legacy random calls using the new Generator API.

In the main lecture flow, I introduced rng only once and then reused it in subsequent cells, in order to keep the structure and presentation as close to the original as possible.

For the exercise solutions, I took a slightly different approach and introduced rng within each solution block. My reasoning was that these solutions seemed more natural as relatively self-contained answer units, especially since the exercise statements explicitly specify the relevant imports. That said, if this is not the preferred approach, it should be easy to revise by removing those local rng definitions.

I also updated the first sentence in Section 3.3.1.3 (“Subpackages”), since it explicitly referred to the legacy line np.random.randn(100) and would otherwise have become inconsistent with the revised code.

I built the lecture locally to check that the changes work as expected.

I’d be very glad to hear any thoughts, especially on the choice to define rng separately within each exercise solution block.

@github-actions
Copy link
Copy Markdown

Copy link
Copy Markdown
Member

@HumphreyYang HumphreyYang left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks, @Chihiro2000GitHub, and congratulations on your first QuantEcon PR!

This looks great to me. Your reasoning about redefining rng in solutions makes sense, and it would be nice to apply this rule consistently across lectures when making similar changes.

Just one minor suggestion: it would be helpful if you could reference the issue you are trying to resolve (or partially resolve, in this case):
QuantEcon/meta#299

Aside from that, this is a great PR!

Please feel free to move on to the rest of the lectures in the series. Also, remember to create a new branch based on main.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants