@@ -55,7 +55,6 @@ translation:
5555
5656在开始本讲座之前,您应该已经阅读过关于 Python 入门的{doc}` 讲座 <getting_started> ` 。
5757
58-
5958## 任务:绘制白噪声过程
6059
6160假设我们想要模拟并绘制白噪声过程 $\epsilon_0, \epsilon_1, \ldots, \epsilon_T$,其中每个抽取值 $\epsilon_t$ 是独立的标准正态分布。
@@ -84,7 +83,8 @@ FONTPATH = "_fonts/SourceHanSerifSC-SemiBold.otf" # i18n
8483mpl.font_manager.fontManager.addfont(FONTPATH) # i18n
8584mpl.rcParams['font.family'] = ['Source Han Serif SC'] # i18n
8685
87- ϵ_values = np.random.randn(100)
86+ rng = np.random.default_rng()
87+ ϵ_values = rng.standard_normal(100)
8888plt.plot(ϵ_values)
8989plt.show()
9090```
@@ -115,7 +115,6 @@ np.sqrt(4)
115115np.log(4)
116116```
117117
118-
119118#### 为什么需要这么多导入语句?
120119
121120Python 程序通常需要多条导入语句。
@@ -124,7 +123,6 @@ Python 程序通常需要多条导入语句。
124123
125124当您想用 Python 做一些有趣的事情时,几乎总是需要导入额外的功能。
126125
127-
128126#### 包
129127
130128``` {index} single: Python; Packages
@@ -155,7 +153,7 @@ print(np.__file__)
155153``` {index} single: Python; Subpackages
156154```
157155
158- 考虑这行代码 ` ϵ_values = np.random.randn(100 )` 。
156+ 考虑这行代码 ` rng = np.random.default_rng( )` 。
159157
160158这里 ` np ` 指的是 NumPy 包,而 ` random ` 是 NumPy 的一个** 子包** 。
161159
@@ -194,7 +192,7 @@ sqrt(4)
194192回到我们绘制白噪声的程序,导入语句之后的剩余三行是
195193
196194``` {code-cell} ipython
197- ϵ_values = np.random.randn (100)
195+ ϵ_values = rng.standard_normal (100)
198196plt.plot(ϵ_values)
199197plt.show()
200198```
@@ -223,7 +221,7 @@ ts_length = 100
223221ϵ_values = [] # 空列表
224222
225223for i in range(ts_length):
226- e = np.random.randn ()
224+ e = rng.standard_normal ()
227225 ϵ_values.append(e)
228226
229227plt.plot(ϵ_values)
@@ -312,7 +310,7 @@ x[1] # x 的第二个元素
312310
313311``` {code-cell} python3
314312for i in range(ts_length):
315- e = np.random.randn ()
313+ e = rng.standard_normal ()
316314 ϵ_values.append(e)
317315```
318316
@@ -345,7 +343,6 @@ Python 解释器执行以下操作:
345343
346344* 对于 ` sequence ` 的每个元素,它将名称 ` variable_name ` "绑定"到该元素,然后执行代码块。
347345
348-
349346### 关于缩进的说明
350347
351348``` {index} single: Python; Indentation
@@ -387,7 +384,7 @@ ts_length = 100
387384ϵ_values = []
388385i = 0
389386while i < ts_length:
390- e = np.random.randn ()
387+ e = rng.standard_normal ()
391388 ϵ_values.append(e)
392389 i = i + 1
393390plt.plot(ϵ_values)
@@ -487,9 +484,10 @@ import matplotlib.pyplot as plt
487484T = 200
488485x = np.empty(T+1)
489486x[0] = 0
487+ rng = np.random.default_rng()
490488
491489for t in range(T):
492- x[t+1] = α * x[t] + np.random.randn ()
490+ x[t+1] = α * x[t] + rng.standard_normal ()
493491
494492plt.plot(x)
495493plt.show()
@@ -527,11 +525,12 @@ plt.show()
527525α_values = [0.0, 0.8, 0.98]
528526T = 200
529527x = np.empty(T+1)
528+ rng = np.random.default_rng()
530529
531530for α in α_values:
532531 x[0] = 0
533532 for t in range(T):
534- x[t+1] = α * x[t] + np.random.randn ()
533+ x[t+1] = α * x[t] + rng.standard_normal ()
535534 plt.plot(x, label=f'$\\alpha = {α}$')
536535
537536plt.legend()
579578T = 200
580579x = np.empty(T+1)
581580x[0] = 0
581+ rng = np.random.default_rng()
582582
583583for t in range(T):
584- x[t+1] = α * np.abs(x[t]) + np.random.randn ()
584+ x[t+1] = α * np.abs(x[t]) + rng.standard_normal ()
585585
586586plt.plot(x)
587587plt.show()
@@ -631,13 +631,14 @@ for x in numbers:
631631T = 200
632632x = np.empty(T+1)
633633x[0] = 0
634+ rng = np.random.default_rng()
634635
635636for t in range(T):
636637 if x[t] < 0:
637638 abs_x = - x[t]
638639 else:
639640 abs_x = x[t]
640- x[t+1] = α * abs_x + np.random.randn ()
641+ x[t+1] = α * abs_x + rng.standard_normal ()
641642
642643plt.plot(x)
643644plt.show()
@@ -650,10 +651,11 @@ plt.show()
650651T = 200
651652x = np.empty(T+1)
652653x[0] = 0
654+ rng = np.random.default_rng()
653655
654656for t in range(T):
655657 abs_x = - x[t] if x[t] < 0 else x[t]
656- x[t+1] = α * abs_x + np.random.randn ()
658+ x[t+1] = α * abs_x + rng.standard_normal ()
657659
658660plt.plot(x)
659661plt.show()
@@ -670,7 +672,7 @@ plt.show()
670672
671673这是一个需要一些思考和规划的较难练习。
672674
673- 任务是使用[ 蒙特卡洛] ( https://en.wikipedia.org/wiki/Monte_Carlo_method ) 方法计算 $\pi$ 的近似值。
675+ 任务是使用 [ 蒙特卡洛] ( https://en.wikipedia.org/wiki/Monte_Carlo_method ) 方法计算 $\pi$ 的近似值。
674676
675677除以下内容外不使用其他导入语句
676678
@@ -710,12 +712,13 @@ import numpy as np
710712
711713``` {code-cell} python3
712714n = 1000000 # 蒙特卡洛模拟的样本量
715+ rng = np.random.default_rng()
713716
714717count = 0
715718for i in range(n):
716719
717720 # 在正方形上随机抽取位置
718- u, v = np.random. uniform(), np.random .uniform()
721+ u, v = rng. uniform(), rng .uniform()
719722
720723 # 检查该点是否落在以 (0.5,0.5) 为圆心的
721724 # 单位圆的边界内
@@ -732,4 +735,4 @@ print(area_estimate * 4) # 除以半径的平方
732735```
733736
734737``` {solution-end}
735- ```
738+ ```
0 commit comments