Skip to content

Commit 994262c

Browse files
committed
Merge remote-tracking branch 'origin/main' into jb2
# Conflicts: # .github/workflows/ci.yml
2 parents c474a0b + a2b929f commit 994262c

11 files changed

Lines changed: 46 additions & 41 deletions

.github/workflows/cache.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
steps:
1111
- uses: actions/checkout@v6
1212
- name: Setup Anaconda
13-
uses: conda-incubator/setup-miniconda@v3
13+
uses: conda-incubator/setup-miniconda@v4
1414
with:
1515
auto-update-conda: true
1616
auto-activate-base: true

.github/workflows/execution-linux.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
steps:
1616
- name: Checkout
1717
uses: actions/checkout@v6
18-
- uses: conda-incubator/setup-miniconda@v3
18+
- uses: conda-incubator/setup-miniconda@v4
1919
with:
2020
auto-update-conda: true
2121
python-version: ${{ matrix.python-version }}

.github/workflows/execution-osx.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
steps:
1616
- name: Checkout
1717
uses: actions/checkout@v6
18-
- uses: conda-incubator/setup-miniconda@v3
18+
- uses: conda-incubator/setup-miniconda@v4
1919
with:
2020
auto-update-conda: true
2121
python-version: ${{ matrix.python-version }}

.github/workflows/execution-win.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
steps:
1616
- name: Checkout
1717
uses: actions/checkout@v6
18-
- uses: conda-incubator/setup-miniconda@v3
18+
- uses: conda-incubator/setup-miniconda@v4
1919
with:
2020
auto-update-conda: true
2121
python-version: ${{ matrix.python-version }}

.github/workflows/publish.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
with:
2828
fetch-depth: 0 # Fetch full git history for changelog feature
2929
- name: Setup Anaconda
30-
uses: conda-incubator/setup-miniconda@v3
30+
uses: conda-incubator/setup-miniconda@v4
3131
with:
3232
auto-update-conda: true
3333
auto-activate-base: true
@@ -66,7 +66,7 @@ jobs:
6666
run: pip list
6767
# Download Build Cache from cache.yml
6868
- name: Download "build" folder (cache)
69-
uses: dawidd6/action-download-artifact@v20
69+
uses: dawidd6/action-download-artifact@v21
7070
with:
7171
workflow: cache.yml
7272
branch: main
@@ -119,7 +119,7 @@ jobs:
119119
}
120120
EOF
121121
- name: Upload archives to release
122-
uses: softprops/action-gh-release@v2
122+
uses: softprops/action-gh-release@v3
123123
with:
124124
files: |
125125
lecture-python-programming-html-${{ github.ref_name }}.tar.gz
@@ -132,7 +132,7 @@ jobs:
132132
- name: Setup Pages
133133
uses: actions/configure-pages@v6
134134
- name: Upload Pages artifact
135-
uses: actions/upload-pages-artifact@v4
135+
uses: actions/upload-pages-artifact@v5
136136
with:
137137
path: _build/html/
138138
- name: Deploy to GitHub Pages

.github/workflows/sync-translations-fa.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
with:
2525
fetch-depth: 2
2626

27-
- uses: QuantEcon/action-translation@v0.14.1
27+
- uses: QuantEcon/action-translation@v0.15.0
2828
with:
2929
mode: sync
3030
target-repo: QuantEcon/lecture-python-programming.fa

.github/workflows/sync-translations-zh-cn.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
with:
2525
fetch-depth: 2
2626

27-
- uses: QuantEcon/action-translation@v0.14.1
27+
- uses: QuantEcon/action-translation@v0.15.0
2828
with:
2929
mode: sync
3030
target-repo: QuantEcon/lecture-python-programming.zh-cn

environment.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ dependencies:
77
- pip
88
- pip:
99
- jupyter-book>=1.0.4post1,<2.0
10-
- quantecon-book-theme==0.20.0
10+
- quantecon-book-theme==0.20.3
1111
- sphinx-tojupyter==0.6.0
1212
- sphinxext-rediraffe==0.3.0
1313
- sphinx-exercise==1.2.1

lectures/about_py.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,10 +451,10 @@ Here's some example code that generates and plots a random graph, with node colo
451451
```{code-cell} ipython
452452
import networkx as nx
453453
import matplotlib.pyplot as plt
454-
np.random.seed(1234)
454+
rng = np.random.default_rng(1234)
455455
456456
# Generate a random graph
457-
p = dict((i, (np.random.uniform(0, 1), np.random.uniform(0, 1)))
457+
p = dict((i, (rng.uniform(0, 1), rng.uniform(0, 1)))
458458
for i in range(200))
459459
g = nx.random_geometric_graph(200, 0.12, pos=p)
460460
pos = nx.get_node_attributes(g, 'pos')

lectures/numpy.md

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,8 @@ a
839839
Mutability leads to the following behavior (which can be shocking to MATLAB programmers...)
840840

841841
```{code-cell} python3
842-
a = np.random.randn(3)
842+
rng = np.random.default_rng()
843+
a = rng.standard_normal(3)
843844
a
844845
```
845846

@@ -869,7 +870,7 @@ It is of course possible to make `b` an independent copy of `a` when required.
869870
This can be done using `np.copy`
870871

871872
```{code-cell} python3
872-
a = np.random.randn(3)
873+
a = rng.standard_normal(3)
873874
a
874875
```
875876

@@ -945,7 +946,7 @@ def f(x):
945946
The NumPy function `np.where` provides a vectorized alternative:
946947

947948
```{code-cell} python3
948-
x = np.random.randn(4)
949+
x = rng.standard_normal(4)
949950
x
950951
```
951952

@@ -1020,11 +1021,12 @@ z[z > 3]
10201021
NumPy provides some additional functionality related to scientific programming
10211022
through its sub-packages.
10221023

1023-
We've already seen how we can generate random variables using np.random
1024+
We've already seen how we can generate random variables using NumPy's
1025+
[random `Generator`](https://numpy.org/doc/stable/reference/random/generator.html#random-generator).
10241026

10251027
```{code-cell} python3
1026-
z = np.random.randn(10000) # Generate standard normals
1027-
y = np.random.binomial(10, 0.5, size=1000) # 1,000 draws from Bin(10, 0.5)
1028+
z = rng.standard_normal(10000) # Generate standard normals
1029+
y = rng.binomial(10, 0.5, size=1000) # 1,000 draws from Bin(10, 0.5)
10281030
y.mean()
10291031
```
10301032

@@ -1064,7 +1066,7 @@ It takes a few seconds to run.
10641066
n = 20
10651067
m = 1000
10661068
for i in range(n):
1067-
X = np.random.randn(m, m)
1069+
X = rng.standard_normal((m, m))
10681070
λ = np.linalg.eigvals(X)
10691071
```
10701072

@@ -1203,28 +1205,28 @@ Here's our first pass at a solution:
12031205

12041206
```{code-cell} python3
12051207
from numpy import cumsum
1206-
from numpy.random import uniform
12071208
12081209
class DiscreteRV:
12091210
"""
12101211
Generates an array of draws from a discrete random variable with vector of
12111212
probabilities given by q.
12121213
"""
12131214
1214-
def __init__(self, q):
1215+
def __init__(self, q, seed=None):
12151216
"""
12161217
The argument q is a NumPy array, or array like, nonnegative and sums
12171218
to 1
12181219
"""
12191220
self.q = q
12201221
self.Q = cumsum(q)
1222+
self.rng = np.random.default_rng(seed)
12211223
12221224
def draw(self, k=1):
12231225
"""
12241226
Returns k draws from q. For each such draw, the value i is returned
12251227
with probability q[i].
12261228
"""
1227-
return self.Q.searchsorted(uniform(0, 1, size=k))
1229+
return self.Q.searchsorted(self.rng.uniform(0, 1, size=k))
12281230
```
12291231

12301232
The logic is not obvious, but if you take your time and read it slowly,
@@ -1353,7 +1355,8 @@ Here's an example of usage
13531355

13541356
```{code-cell} python3
13551357
fig, ax = plt.subplots()
1356-
X = np.random.randn(1000)
1358+
rng = np.random.default_rng()
1359+
X = rng.standard_normal(1000)
13571360
F = ECDF(X)
13581361
F.plot(ax)
13591362
```
@@ -1374,9 +1377,9 @@ In this exercise, try to use `for` loops to replicate the result of the followin
13741377

13751378
```{code-cell} python3
13761379
1377-
np.random.seed(123)
1378-
x = np.random.randn(4, 4)
1379-
y = np.random.randn(4)
1380+
rng = np.random.default_rng(123)
1381+
x = rng.standard_normal((4, 4))
1382+
y = rng.standard_normal(4)
13801383
A = x / y
13811384
```
13821385

@@ -1404,9 +1407,9 @@ Now we can import the quantecon package.
14041407

14051408
```{code-cell} python3
14061409
1407-
np.random.seed(123)
1408-
x = np.random.randn(1000, 100, 100)
1409-
y = np.random.randn(100)
1410+
rng = np.random.default_rng(123)
1411+
x = rng.standard_normal((1000, 100, 100))
1412+
y = rng.standard_normal(100)
14101413
14111414
with qe.Timer("Broadcasting operation"):
14121415
B = x / y
@@ -1431,9 +1434,9 @@ print(B)
14311434
**Part 1 Solution**
14321435

14331436
```{code-cell} python3
1434-
np.random.seed(123)
1435-
x = np.random.randn(4, 4)
1436-
y = np.random.randn(4)
1437+
rng = np.random.default_rng(123)
1438+
x = rng.standard_normal((4, 4))
1439+
y = rng.standard_normal(4)
14371440
14381441
C = np.empty_like(x)
14391442
n = len(x)
@@ -1461,9 +1464,9 @@ print(np.array_equal(A, C))
14611464

14621465
```{code-cell} python3
14631466
1464-
np.random.seed(123)
1465-
x = np.random.randn(1000, 100, 100)
1466-
y = np.random.randn(100)
1467+
rng = np.random.default_rng(123)
1468+
x = rng.standard_normal((1000, 100, 100))
1469+
y = rng.standard_normal(100)
14671470
14681471
with qe.Timer("For loop operation"):
14691472
D = np.empty_like(x)

0 commit comments

Comments
 (0)