🌐 [translation-sync] [inventory_q.md] Update np.random → Generator API - #217
🌐 [translation-sync] [inventory_q.md] Update np.random → Generator API#217mmcky wants to merge 2 commits into
Conversation
✅ Deploy Preview for astonishing-narwhal-a8fc64 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
| Criterion | Score |
|---|---|
| Accuracy | 9/10 |
| Fluency | 9/10 |
| Terminology | 9/10 |
| Formatting | 8/10 |
| Overall | 8.9/10 |
Summary: The translation of the changed sections (Simulating the optimal policy, Implementation, Visualizing learning over time) is accurate, fluent, and terminologically consistent with the glossary. There is one critical LaTeX delimiter error in 'The Q-table and the role of the max' section (technically outside the strictly listed changed sections but adjacent) that would break rendering — however checking the officially modified sections list, this issue falls just outside them, so it's flagged as a minor formatting note for completeness. Overall the translation quality is high with natural academic register and correct technical terminology throughout the three modified sections. Technical concepts such as Q-factor Bellman equation, off-policy learning, optimistic initialization, and epsilon-greedy exploration are translated accurately and consistently with standard RL terminology Code comments and docstrings are fully and naturally translated into Chinese while preserving code functionality Mathematical notation and equations are preserved correctly throughout, with only one minor delimiter slip
⚠️ Markdown Syntax Errors (CRITICAL)
- 🔴 经理在时间 $t+1 实际采取 哪个动作是一个单独的决策。 — missing closing $ delimiter after $t+1, causing broken math rendering
Suggestions:
-
[major · formatting] lectures/inventory_q.md — ### The Q-table and the role of the max: Math delimiter error: '经理在时间 $t+1 实际采取 哪个动作是一个单独的决策' has an unclosed inline math expression (
$t+1 is not closed with a second $ ), which will break rendering. The original English text is 'Which action the manager actually takes at time$t+1$ is a separate decision.' → 经理在时间$t+1$ 实际采取 哪个动作是一个单独的决策。 -
[minor · terminology] lectures/inventory_q.md — ### Implementation, code comment '接下来我们运行
$n$ = 500 万步': The English text states 'run n = 5 million steps' but the code sets n = 5_000_000, which matches '500万'. This is actually correct, just double-checked for consistency - no issue found here upon closer review. -
[minor · fluency] lectures/inventory_q.md — ### The Q-table and the role of the max: The phrase '因为更新目标中的
$\max$ 总是指向 $q^$,无论经理如何选择动作' slightly reorders the English 'Because the max in the update target always points toward q regardless of how the manager selects actions' — acceptable but could be smoother by keeping the reason clause together. → 因为无论经理如何选择动作,更新目标中的$\max$ 始终指向$q^*$ ,
🔍 Diff Quality
| Check | Status |
|---|---|
| Scope Correct | ✅ |
| Position Correct | ✅ |
| Structure Preserved | ✅ |
| Heading-map Correct | ✅ |
| Overall | 10/10 |
Summary: The RNG refactor (seed-based globals to explicit rng objects) was correctly and completely mirrored in the target document at matching positions, with translated docstrings and unchanged heading metadata.
This review was generated automatically by action-translation review mode.
There was a problem hiding this comment.
Pull request overview
This translation-sync PR updates the Chinese inventory_q lecture to match upstream changes that migrate simulation and Q-learning code from legacy np.random.* calls toward the newer RNG/Generator-style API, and refreshes the translation state metadata accordingly.
Changes:
- Updated inventory simulation and Q-learning code cells to use
np.random.default_rng()/rng.*APIs and propagate seeds via RNG objects. - Adjusted plotting helper signature to accept a
seedparameter for reproducible time series plots. - Updated
.translate/state/inventory_q.md.ymlsync metadata (source SHA, model, tool version, timestamps).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| lectures/inventory_q.md | Replaces legacy np.random.* usage with Generator-style RNG usage in simulation and Q-learning code cells. |
| .translate/state/inventory_q.md.yml | Updates translation-sync state metadata to reflect the new upstream source and tool/model versions. |
Suppressed comments (7)
lectures/inventory_q.md:386
plot_ts这里将np.random.default_rng(seed)传入@numba.jit(nopython=True)的sim_inventories,会触发 Numba 对Generator对象的类型推断/编译问题。若sim_inventories改回seed参数(见上条建议),这里也应按新签名传递seed。
def plot_ts(ts_length=200, fontsize=10, seed=0):
X = sim_inventories(ts_length, σ_star, p, np.random.default_rng(seed))
fig, ax = plt.subplots()
lectures/inventory_q.md:710
- 这里同样把
np.random.default_rng(sim_seed)作为实参传给@numba.jit(nopython=True)的sim_inventories。如果sim_inventories使用seed(更兼容 Numba nopython),请改为传seed=sim_seed。
# 最优策略
X_opt = sim_inventories(ts_length, σ_star, p,
np.random.default_rng(sim_seed), X_init)
axes[0].plot(X_opt, alpha=0.7)
lectures/inventory_q.md:720
- 循环内同样将
Generator传入@numba.jit(nopython=True)的sim_inventories。如果按建议改回seed参数,请在此处也改为seed=sim_seed,以保持“所有面板使用相同需求序列”的设定。
for i in range(n_snaps):
σ_snap = snapshots[i]
X = sim_inventories(ts_length, σ_snap, p,
np.random.default_rng(sim_seed), X_init)
axes[i + 1].plot(X, alpha=0.7)
lectures/inventory_q.md:591
- 这里使用
rng.geometric会要求 Numba 支持np.random.Generator方法调用。若按建议改回seed+np.random.*,这里应使用np.random.geometric。
# === 抽取 D_{t+1} 并观察结果 ===
d = rng.geometric(p) - 1
reward = min(x, d) - c * a - κ * (a > 0)
lectures/inventory_q.md:582
- 这里使用
rng.integers会要求 Numba 支持np.random.Generator方法调用。若按建议改回seed+np.random.*,这里应使用np.random.randint。
x = X_init
a = rng.integers(0, K - x + 1)
lectures/inventory_q.md:612
- ε-贪婪分支里调用了
rng.random()/rng.integers()。若按建议改回seed+np.random.*(以保持 Numba nopython 兼容),这里应改为np.random.random()/np.random.randint()。
x = x_next
if rng.random() < ε:
a = rng.integers(0, K - x + 1)
else:
lectures/inventory_q.md:631
q_learning包装函数当前创建rng = np.random.default_rng(seed)并传入@numba.jit(nopython=True)的 kernel。若 kernel 改回接收seed并在内部np.random.seed(seed)(更兼容 Numba nopython),这里应去掉rng并直接传seed。
K = len(x_values) - 1
if snapshot_steps is None:
snapshot_steps = np.array([], dtype=np.int64)
rng = np.random.default_rng(seed)
return q_learning_kernel(K, p, c, κ, β, n_steps, X_init,
ε_init, ε_min, ε_decay, q_init, snapshot_steps, rng)
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @numba.jit(nopython=True) | ||
| def sim_inventories(ts_length, σ, p, X_init=0, seed=0): | ||
| def sim_inventories(ts_length, σ, p, rng, X_init=0): | ||
| """在策略 σ 下模拟库存动态。""" | ||
| np.random.seed(seed) | ||
| X = np.zeros(ts_length, dtype=np.int32) | ||
| X[0] = X_init |
| def q_learning_kernel(K, p, c, κ, β, n_steps, X_init, | ||
| ε_init, ε_min, ε_decay, q_init, snapshot_steps, seed): | ||
| np.random.seed(seed) | ||
| ε_init, ε_min, ε_decay, q_init, snapshot_steps, rng): | ||
| q = np.full((K + 1, K + 1), q_init) |
Automated Translation Sync
This PR contains automated translations from QuantEcon/lecture-python.myst.
Source PR
#1012 - [inventory_q.md] Update np.random → Generator API
Files Updated
lectures/inventory_q.md.translate/state/inventory_q.md.ymlDetails
This PR was created automatically by the translation action.