@@ -79,7 +79,7 @@ from quantecon.distributions import BetaBinomial
7979
8080import matplotlib.pyplot as plt
8181
82- np.random.seed (123)
82+ rng = np.random.default_rng (123)
8383```
8484
8585## Review of McCall Model
@@ -514,23 +514,23 @@ class Qlearning_McCall:
514514 self.quit_allowed = quit_allowed
515515
516516
517- def draw_offer_index(self):
517+ def draw_offer_index(self, rng ):
518518 """
519519 Draw a state index from the wage distribution.
520520 """
521521
522522 q = self.q
523- return np.searchsorted(np.cumsum(q), np.random .random(), side="right")
523+ return np.searchsorted(np.cumsum(q), rng .random(), side="right")
524524
525- def temp_diff(self, qtable, state, accept):
525+ def temp_diff(self, qtable, state, accept, rng ):
526526 """
527527 Compute the TD associated with state and action.
528528 """
529529
530530 c, β, w = self.c, self.β, self.w
531531
532532 if accept==0:
533- state_next = self.draw_offer_index()
533+ state_next = self.draw_offer_index(rng )
534534 TD = c + β*np.max(qtable[state_next, :]) - qtable[state, accept]
535535 else:
536536 state_next = state
@@ -541,31 +541,31 @@ class Qlearning_McCall:
541541
542542 return TD, state_next
543543
544- def run_one_epoch(self, qtable, max_times=20000):
544+ def run_one_epoch(self, qtable, rng, max_times=20000):
545545 """
546546 Run an "epoch".
547547 """
548548
549549 c, β, w = self.c, self.β, self.w
550550 eps, δ, lr, T = self.eps, self.δ, self.lr, self.T
551551
552- s0 = self.draw_offer_index()
552+ s0 = self.draw_offer_index(rng )
553553 s = s0
554554 accept_count = 0
555555
556556 for t in range(max_times):
557557
558558 # choose action
559559 accept = np.argmax(qtable[s, :])
560- if np.random .random()<=eps:
560+ if rng .random()<=eps:
561561 accept = 1 - accept
562562
563563 if accept == 1:
564564 accept_count += 1
565565 else:
566566 accept_count = 0
567567
568- TD, s_next = self.temp_diff(qtable, s, accept)
568+ TD, s_next = self.temp_diff(qtable, s, accept, rng )
569569
570570 # update qtable
571571 qtable_new = qtable.copy()
@@ -582,15 +582,15 @@ class Qlearning_McCall:
582582 return qtable_new
583583
584584@jit
585- def run_epochs(N, qlmc, qtable):
585+ def run_epochs(N, qlmc, qtable, rng ):
586586 """
587587 Run epochs N times with qtable from the last iteration each time.
588588 """
589589
590590 for n in range(N):
591591 if n%(N/10)==0:
592592 print(f"Progress: EPOCHs = {n}")
593- new_qtable = qlmc.run_one_epoch(qtable)
593+ new_qtable = qlmc.run_one_epoch(qtable, rng )
594594 qtable = new_qtable
595595
596596 return qtable
@@ -608,7 +608,7 @@ qlmc = Qlearning_McCall()
608608
609609# run
610610qtable0 = np.zeros((len(w_default), 2))
611- qtable = run_epochs(20000, qlmc, qtable0)
611+ qtable = run_epochs(20000, qlmc, qtable0, rng )
612612```
613613
614614```{code-cell} ipython3
@@ -685,7 +685,7 @@ def plot_epochs(epochs_to_plot, quit_allowed=1):
685685 ax.plot(w_new, valfunc_qlr, '-o', label=f'QL:epochs={n}, mean error={error}')
686686
687687
688- new_qtable = qlmc_new.run_one_epoch(qtable)
688+ new_qtable = qlmc_new.run_one_epoch(qtable, rng )
689689 qtable = new_qtable
690690
691691 ax.set_xlabel('wages')
0 commit comments