Skip to content

Commit b77b57e

Browse files
committed
Checked compatibility with Python 3
1 parent e4e9245 commit b77b57e

File tree

18 files changed

+3161
-3051
lines changed

18 files changed

+3161
-3051
lines changed

Ch1.ipynb

Lines changed: 45 additions & 33 deletions
Large diffs are not rendered by default.

Ch10.ipynb

Lines changed: 56 additions & 52 deletions
Large diffs are not rendered by default.

Ch2.ipynb

Lines changed: 41 additions & 35 deletions
Large diffs are not rendered by default.

Ch3.ipynb

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,20 @@
2121
},
2222
"outputs": [],
2323
"source": [
24-
"\n",
2524
"from __future__ import (absolute_import, division,\n",
2625
" print_function, unicode_literals)\n",
27-
"from builtins import (bytes, dict, int, list, object, range, str,\n",
28-
" ascii, chr, hex, input, next, oct, open,\n",
29-
" pow, round, super, filter, map, zip)\n",
30-
"from future.builtins.disabled import (apply, cmp, coerce, execfile,\n",
31-
" file, long, raw_input, reduce, reload,\n",
32-
" unicode, xrange, StandardError)\n",
33-
"#\n",
3426
"import sys\n",
27+
"if sys.version_info < (3,):\n",
28+
" try:\n",
29+
" from builtins import (bytes, dict, int, list, object, range, str,\n",
30+
" ascii, chr, hex, input, next, oct, open,\n",
31+
" pow, round, super, filter, map, zip)\n",
32+
" from future.builtins.disabled import (apply, cmp, coerce, execfile,\n",
33+
" file, long, raw_input,\n",
34+
" reduce, reload,\n",
35+
" unicode, xrange, StandardError)\n",
36+
" except:\n",
37+
" print(\"need future module\")\n",
3538
"#\n",
3639
"from timeit import default_timer as timer"
3740
]
@@ -1051,21 +1054,21 @@
10511054
],
10521055
"metadata": {
10531056
"kernelspec": {
1054-
"display_name": "Python 2",
1057+
"display_name": "Python 3",
10551058
"language": "python",
1056-
"name": "python2"
1059+
"name": "python3"
10571060
},
10581061
"language_info": {
10591062
"codemirror_mode": {
10601063
"name": "ipython",
1061-
"version": 2
1064+
"version": 3
10621065
},
10631066
"file_extension": ".py",
10641067
"mimetype": "text/x-python",
10651068
"name": "python",
10661069
"nbconvert_exporter": "python",
1067-
"pygments_lexer": "ipython2",
1068-
"version": "2.7.11"
1070+
"pygments_lexer": "ipython3",
1071+
"version": "3.5.1"
10691072
}
10701073
},
10711074
"nbformat": 4,

Ch4.ipynb

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
{
1717
"cell_type": "code",
18-
"execution_count": 4,
18+
"execution_count": 1,
1919
"metadata": {
2020
"collapsed": true
2121
},
@@ -39,9 +39,9 @@
3939
},
4040
{
4141
"cell_type": "code",
42-
"execution_count": 31,
42+
"execution_count": 2,
4343
"metadata": {
44-
"collapsed": true
44+
"collapsed": false
4545
},
4646
"outputs": [],
4747
"source": [
@@ -71,7 +71,7 @@
7171
},
7272
{
7373
"cell_type": "code",
74-
"execution_count": 32,
74+
"execution_count": 3,
7575
"metadata": {
7676
"collapsed": false
7777
},
@@ -90,7 +90,7 @@
9090
},
9191
{
9292
"cell_type": "code",
93-
"execution_count": 33,
93+
"execution_count": 4,
9494
"metadata": {
9595
"collapsed": false
9696
},
@@ -99,7 +99,7 @@
9999
"name": "stdout",
100100
"output_type": "stream",
101101
"text": [
102-
"The sample average is 1.23623083705 and the sample variance 1.30830563846 based on 40 samples for N(1,1)\n"
102+
"The sample average is 0.650211887244 and the sample variance 0.843164549377 based on 40 samples for N(1,1)\n"
103103
]
104104
}
105105
],
@@ -169,12 +169,12 @@
169169
"output_type": "stream",
170170
"text": [
171171
"Identical samples:\n",
172-
" [-0.14653656 -0.47307654 -0.03942304 0.12535728 0.02529963 1.34499499\n",
173-
" -0.05358885 -0.2939091 1.94854913 0.41751495] and\n",
174-
" [-0.14653656 -0.47307654 -0.03942304 0.12535728 0.02529963 1.34499499\n",
175-
" -0.05358885 -0.2939091 1.94854913 0.41751495]\n",
176-
"Covariance of samples [[ 0.9457889 -0.01289219]\n",
177-
" [-0.01289219 0.93458634]]\n"
172+
" [ 0.81489992 0.76143959 0.07694935 -0.61195669 -1.1646947 -2.31956454\n",
173+
" 0.5642974 -1.10766594 0.43282974 -0.20260256] and\n",
174+
" [ 0.81489992 0.76143959 0.07694935 -0.61195669 -1.1646947 -2.31956454\n",
175+
" 0.5642974 -1.10766594 0.43282974 -0.20260256]\n",
176+
"Covariance of samples [[ 0.95500506 -0.00345936]\n",
177+
" [-0.00345936 1.05820756]]\n"
178178
]
179179
}
180180
],
@@ -196,7 +196,7 @@
196196
},
197197
{
198198
"cell_type": "code",
199-
"execution_count": 30,
199+
"execution_count": 12,
200200
"metadata": {
201201
"collapsed": false
202202
},
@@ -205,18 +205,16 @@
205205
"name": "stdout",
206206
"output_type": "stream",
207207
"text": [
208-
"sr= [[-2.5796549 -0.80784927 -0.88291778 1.15429748 1.60292571 -0.94056234]\n",
209-
" [ 2.85426974 0.47565126 0.78506184 2.05482599 0.10387867 0.52951015]\n",
210-
" [-0.63850658 -0.82684445 -0.31751491 -0.11278389 1.38274907 -0.12146601]\n",
211-
" [-0.69092677 0.31169424 0.57306888 -0.50967253 1.14489301 1.12292133]\n",
212-
" [-1.23597204 -0.59336124 0.28783502 2.08356145 1.11139071 0.04475031]] \n",
213-
"sr4= [[-1.23597204 -0.59336124 0.28783502 2.08356145 1.11139071 0.04475031]] ar=\n",
214-
" [array([[-2.5796549 , -0.80784927, -0.88291778, 1.15429748, 1.60292571,\n",
215-
" -0.94056234]]), array([[ 2.85426974, 0.47565126, 0.78506184, 2.05482599, 0.10387867,\n",
216-
" 0.52951015]]), array([[-0.63850658, -0.82684445, -0.31751491, -0.11278389, 1.38274907,\n",
217-
" -0.12146601]]), array([[-0.69092677, 0.31169424, 0.57306888, -0.50967253, 1.14489301,\n",
218-
" 1.12292133]]), array([[-1.23597204, -0.59336124, 0.28783502, 2.08356145, 1.11139071,\n",
219-
" 0.04475031]])]\n"
208+
"sr= [[ 0.90390365 0.65476072 -0.42647782 0.47217152 0.0518215 -0.32324357]\n",
209+
" [-0.24180847 -0.35042203 -1.21377586 -0.32739907 -1.74333538 -0.0759546 ]\n",
210+
" [ 1.27268467 -0.02500331 -0.0904326 -0.82035676 2.3166674 0.37689438]\n",
211+
" [ 0.05270123 -0.1900696 -0.66663564 -0.58521261 -0.03028957 -0.44856859]] \n",
212+
"sr4= [[ 0.05270123 -0.1900696 -0.66663564 -0.58521261 -0.03028957 -0.44856859]] ar=\n",
213+
" [array([[ 0.90390365, 0.65476072, -0.42647782, 0.47217152, 0.0518215 ,\n",
214+
" -0.32324357]]), array([[-0.24180847, -0.35042203, -1.21377586, -0.32739907, -1.74333538,\n",
215+
" -0.0759546 ]]), array([[ 1.27268467, -0.02500331, -0.0904326 , -0.82035676, 2.3166674 ,\n",
216+
" 0.37689438]]), array([[ 0.05270123, -0.1900696 , -0.66663564, -0.58521261, -0.03028957,\n",
217+
" -0.44856859]])]\n"
220218
]
221219
}
222220
],
@@ -225,7 +223,7 @@
225223
" \"\"\"\n",
226224
" A4.4 Page 166\n",
227225
" \"\"\"\n",
228-
" N=5\n",
226+
" N=4\n",
229227
" M=6\n",
230228
" sr=np.zeros((N,M))\n",
231229
" stream={}; init_state={}\n",
@@ -238,7 +236,7 @@
238236
" stream[j].set_state(init_state[j])# reset state of jth rng\n",
239237
" return stream[j].randn(1,M)\n",
240238
" #\n",
241-
" sr4=srandn(4) # reproduces the 4th row \n",
239+
" sr4=srandn(N-1) # reproduces the 4th row \n",
242240
" #\n",
243241
" from ipyparallel import Client\n",
244242
" import os\n",
@@ -255,7 +253,7 @@
255253
},
256254
{
257255
"cell_type": "code",
258-
"execution_count": 15,
256+
"execution_count": 13,
259257
"metadata": {
260258
"collapsed": false
261259
},
@@ -298,7 +296,7 @@
298296
},
299297
{
300298
"cell_type": "code",
301-
"execution_count": 8,
299+
"execution_count": 14,
302300
"metadata": {
303301
"collapsed": false
304302
},
@@ -307,9 +305,9 @@
307305
"name": "stdout",
308306
"output_type": "stream",
309307
"text": [
310-
"Uniform sample from unit ball at origin [ 0.54476269 -0.71305309]\n",
311-
"Uniform sample from unit sphere at origin [-0.64960659 0.73582129 0.19125456]\n",
312-
"Uniform sample of unit ball [-0.66832045 -0.39539924] using 2 attempts (rejection sampling)\n"
308+
"Uniform sample from unit ball at origin [ 0.51655396 0.28705832]\n",
309+
"Uniform sample from unit sphere at origin [ 0.5821194 0.25029924 0.7736196 ]\n",
310+
"Uniform sample of unit ball [-0.10768545 -0.98238243] using 1 attempts (rejection sampling)\n"
313311
]
314312
}
315313
],
@@ -331,7 +329,7 @@
331329
},
332330
{
333331
"cell_type": "code",
334-
"execution_count": 9,
332+
"execution_count": 15,
335333
"metadata": {
336334
"collapsed": false
337335
},
@@ -356,7 +354,7 @@
356354
},
357355
{
358356
"cell_type": "code",
359-
"execution_count": 10,
357+
"execution_count": 16,
360358
"metadata": {
361359
"collapsed": false
362360
},
@@ -365,7 +363,7 @@
365363
"name": "stdout",
366364
"output_type": "stream",
367365
"text": [
368-
"Multivariate Gaussian sample [ 0.52466687 1.12560085]\n"
366+
"Multivariate Gaussian sample [-0.43260261 1.69354539]\n"
369367
]
370368
}
371369
],
@@ -384,7 +382,7 @@
384382
},
385383
{
386384
"cell_type": "code",
387-
"execution_count": 11,
385+
"execution_count": 31,
388386
"metadata": {
389387
"collapsed": false
390388
},
@@ -396,7 +394,7 @@
396394
" A4.9 Page 174\n",
397395
" \"\"\"\n",
398396
" d=baru0.size\n",
399-
" N=T // Dt\n",
397+
" N=int(T // Dt)\n",
400398
" u=np.zeros((M,d)) \n",
401399
" for j in range(M):\n",
402400
" u0=baru0 + epsilon * np.random.uniform(-1,1,baru0.size)\n",
@@ -421,7 +419,7 @@
421419
},
422420
{
423421
"cell_type": "code",
424-
"execution_count": 12,
422+
"execution_count": 32,
425423
"metadata": {
426424
"collapsed": false
427425
},
@@ -430,7 +428,7 @@
430428
"name": "stdout",
431429
"output_type": "stream",
432430
"text": [
433-
"mean 1.48488796481 sd 0.0284883420065\n"
431+
"mean 1.50227354061 sd 0.02939864970796154\n"
434432
]
435433
}
436434
],
@@ -452,15 +450,15 @@
452450
},
453451
{
454452
"cell_type": "code",
455-
"execution_count": 13,
453+
"execution_count": 33,
456454
"metadata": {
457455
"collapsed": true
458456
},
459457
"outputs": [],
460458
"source": [
461459
"def pop_monte_anti(M,T,Dt,baru0,epsilon):\n",
462460
" d=baru0.size\n",
463-
" N=T // Dt\n",
461+
" N=int(T // Dt)\n",
464462
" u=np.zeros((2*M,d)) \n",
465463
" for j in range(M):\n",
466464
" u0=baru0 + epsilon * np.random.uniform(-1,1,baru0.size)\n",
@@ -482,7 +480,7 @@
482480
},
483481
{
484482
"cell_type": "code",
485-
"execution_count": 14,
483+
"execution_count": 44,
486484
"metadata": {
487485
"collapsed": false
488486
},
@@ -491,7 +489,7 @@
491489
"name": "stdout",
492490
"output_type": "stream",
493491
"text": [
494-
"mean 1.49625648822 sd 0.0286472044095\n"
492+
"mean 1.49540169969 sd 0.026396530226\n"
495493
]
496494
}
497495
],
@@ -502,34 +500,25 @@
502500
"bar_x,sig95=pop_monte_anti(M,T,Dt,baru0,epsilon)\n",
503501
"print(\"mean\",bar_x,\"sd\",sig95)"
504502
]
505-
},
506-
{
507-
"cell_type": "code",
508-
"execution_count": null,
509-
"metadata": {
510-
"collapsed": true
511-
},
512-
"outputs": [],
513-
"source": []
514503
}
515504
],
516505
"metadata": {
517506
"kernelspec": {
518-
"display_name": "Python 2",
507+
"display_name": "Python 3",
519508
"language": "python",
520-
"name": "python2"
509+
"name": "python3"
521510
},
522511
"language_info": {
523512
"codemirror_mode": {
524513
"name": "ipython",
525-
"version": 2
514+
"version": 3
526515
},
527516
"file_extension": ".py",
528517
"mimetype": "text/x-python",
529518
"name": "python",
530519
"nbconvert_exporter": "python",
531-
"pygments_lexer": "ipython2",
532-
"version": "2.7.11"
520+
"pygments_lexer": "ipython3",
521+
"version": "3.5.1"
533522
}
534523
},
535524
"nbformat": 4,

0 commit comments

Comments
 (0)