Skip to content

Commit a1b9d37

Browse files
committed
Not adding or removing semicolons
1 parent 5f49156 commit a1b9d37

File tree

1 file changed

+75
-52
lines changed

1 file changed

+75
-52
lines changed

Chapter5_LossFunctions/LossFunctions.ipynb

Lines changed: 75 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -254,13 +254,13 @@
254254
"\n",
255255
"@pm.potential\n",
256256
"def error(true_price=true_price, price_estimate=price_estimate):\n",
257-
" return pm.normal_like(true_price, price_estimate, 1 / (3e3) ** 2)\n",
257+
" return pm.normal_like(true_price, price_estimate, 1 / (3e3) ** 2)\n",
258258
"\n",
259259
"\n",
260260
"mcmc = pm.MCMC([true_price, prize_1, prize_2, price_estimate, error])\n",
261261
"mcmc.sample(50000, 10000)\n",
262262
"\n",
263-
"price_trace = mcmc.trace(\"true_price\")[:];"
263+
"price_trace = mcmc.trace(\"true_price\")[:]"
264264
],
265265
"language": "python",
266266
"metadata": {},
@@ -299,8 +299,9 @@
299299
"plt.title(\"Posterior of the true price estimate\")\n",
300300
"plt.vlines(mu_prior, 0, 1.1 * np.max(_hist[0]), label=\"prior's mean\",\n",
301301
" linestyles=\"--\")\n",
302-
"plt.vlines(price_trace.mean(), 0, 1.1 * np.max(_hist[0]), label=\"posterior's mean\", linestyles=\"-.\")\n",
303-
"plt.legend(loc=\"upper left\");"
302+
"plt.vlines(price_trace.mean(), 0, 1.1 * np.max(_hist[0]),\n",
303+
" label=\"posterior's mean\", linestyles=\"-.\")\n",
304+
"plt.legend(loc=\"upper left\")"
304305
],
305306
"language": "python",
306307
"metadata": {},
@@ -361,23 +362,26 @@
361362
"\n",
362363
"\n",
363364
"def showdown_loss(guess, true_price, risk=80000):\n",
364-
" loss = np.zeros_like(true_price)\n",
365-
" ix = true_price < guess\n",
366-
" loss[~ix] = np.abs(guess - true_price[~ix])\n",
367-
" close_mask = [abs(true_price - guess) <= 250]\n",
368-
" loss[close_mask] = -2 * true_price[close_mask]\n",
369-
" loss[ix] = risk\n",
370-
" return loss\n",
365+
" loss = np.zeros_like(true_price)\n",
366+
" ix = true_price < guess\n",
367+
" loss[~ix] = np.abs(guess - true_price[~ix])\n",
368+
" close_mask = [abs(true_price - guess) <= 250]\n",
369+
" loss[close_mask] = -2 * true_price[close_mask]\n",
370+
" loss[ix] = risk\n",
371+
" return loss\n",
372+
"\n",
371373
"\n",
372374
"guesses = np.linspace(5000, 50000, 70)\n",
373375
"risks = np.linspace(30000, 150000, 6)\n",
374-
"expected_loss = lambda guess, risk: showdown_loss(guess, price_trace, risk).mean()\n",
376+
"expected_loss = lambda guess, risk: \\\n",
377+
" showdown_loss(guess, price_trace, risk).mean()\n",
375378
"\n",
376379
"for _p in risks:\n",
377380
" results = [expected_loss(_g, _p) for _g in guesses]\n",
378381
" plt.plot(guesses, results, label=\"%d\" % _p)\n",
379382
"\n",
380-
"plt.title(\"Expected loss of different guesses, \\nvarious risk-levels of overestimating\")\n",
383+
"plt.title(\"Expected loss of different guesses, \\nvarious risk-levels of \\\n",
384+
"overestimating\")\n",
381385
"plt.legend(loc=\"upper left\", title=\"Risk parameter\")\n",
382386
"plt.xlabel(\"price bid\")\n",
383387
"plt.ylabel(\"expected loss\")\n",
@@ -420,21 +424,24 @@
420424
"\n",
421425
"ax = plt.subplot(111)\n",
422426
"\n",
427+
"\n",
423428
"for _p in risks:\n",
424429
" _color = ax._get_lines.color_cycle.next()\n",
425430
" _min_results = sop.fmin(expected_loss, 15000, args=(_p,), disp=False)\n",
426431
" _results = [expected_loss(_g, _p) for _g in guesses]\n",
427432
" plt.plot(guesses, _results, color=_color)\n",
428-
" plt.scatter(_min_results, 0, s=60, color=_color, label=\"%d\" % _p)\n",
433+
" plt.scatter(_min_results, 0, s=60,\n",
434+
" color=_color, label=\"%d\" % _p)\n",
429435
" plt.vlines(_min_results, 0, 120000, color=_color, linestyles=\"--\")\n",
430436
" print \"minimum at risk %d: %.2f\" % (_p, _min_results)\n",
431437
"\n",
432-
"plt.title(\"Expected loss & Bayes actions of different guesses, \\n various risk-levels of overestimating\")\n",
438+
"plt.title(\"Expected loss & Bayes actions of different guesses, \\n \\\n",
439+
"various risk-levels of overestimating\")\n",
433440
"plt.legend(loc=\"upper left\", scatterpoints=1, title=\"Bayes action at risk:\")\n",
434441
"plt.xlabel(\"price guess\")\n",
435442
"plt.ylabel(\"expected loss\")\n",
436443
"plt.xlim(7000, 30000)\n",
437-
"plt.ylim(-1000, 80000);"
444+
"plt.ylim(-1000, 80000)"
438445
],
439446
"language": "python",
440447
"metadata": {},
@@ -575,15 +582,18 @@
575582
"\n",
576583
"def stock_loss(true_return, yhat, alpha=100.):\n",
577584
" if true_return * yhat < 0:\n",
578-
" # opposite signs, not good\n",
579-
" return alpha * yhat ** 2 - np.sign(true_return) * yhat + abs(true_return)\n",
585+
" # opposite signs, not good\n",
586+
" return alpha * yhat ** 2 - np.sign(true_return) * yhat \\\n",
587+
" + abs(true_return)\n",
580588
" else:\n",
581589
" return abs(true_return - yhat)\n",
582590
"\n",
591+
"\n",
583592
"true_value = .05\n",
584593
"pred = np.linspace(-.04, .12, 75)\n",
585594
"\n",
586-
"plt.plot(pred, [stock_loss(true_value, _p) for _p in pred], label=\"Loss associated with\\n prediction if true value=0.05\", lw=3)\n",
595+
"plt.plot(pred, [stock_loss(true_value, _p) for _p in pred],\n",
596+
" label=\"Loss associated with\\n prediction if true value = 0.05\", lw=3)\n",
587597
"plt.vlines(0, 0, .25, linestyles=\"--\")\n",
588598
"\n",
589599
"plt.xlabel(\"prediction\")\n",
@@ -592,9 +602,10 @@
592602
"plt.ylim(0, 0.25)\n",
593603
"\n",
594604
"true_value = -.02\n",
595-
"plt.plot(pred, [stock_loss(true_value, _p) for _p in pred], alpha=0.6, label=\"Loss associated with\\n prediction if true value=-0.02\", lw=3)\n",
605+
"plt.plot(pred, [stock_loss(true_value, _p) for _p in pred], alpha=0.6,\n",
606+
" label=\"Loss associated with\\n prediction if true value = -0.02\", lw=3)\n",
596607
"plt.legend()\n",
597-
"plt.title(\"Stock returns loss if true value=0.05, -0.02\");"
608+
"plt.title(\"Stock returns loss if true value = 0.05, -0.02\");"
598609
],
599610
"language": "python",
600611
"metadata": {},
@@ -695,7 +706,7 @@
695706
"mcmc = pm.MCMC([obs, beta, alpha, std, prec])\n",
696707
"\n",
697708
"mcmc.sample(100000, 80000)\n",
698-
"mcplot(mcmc);"
709+
"mcplot(mcmc)"
699710
],
700711
"language": "python",
701712
"metadata": {},
@@ -800,7 +811,8 @@
800811
"\n",
801812
"noise = 1. / np.sqrt(tau_samples) * np.random.randn(N)\n",
802813
"\n",
803-
"possible_outcomes = lambda signal: alpha_samples + beta_samples * signal + noise\n",
814+
"possible_outcomes = lambda signal: alpha_samples + beta_samples * signal \\\n",
815+
" + noise\n",
804816
"\n",
805817
"\n",
806818
"opt_predictions = np.zeros(50)\n",
@@ -894,11 +906,12 @@
894906
"from draw_sky2 import draw_sky\n",
895907
"\n",
896908
"n_sky = 3 # choose a file/sky to examine.\n",
897-
"data = np.genfromtxt(\"data/Train_Skies/Train_Skies/Training_Sky%d.csv\" % (n_sky),\n",
898-
" dtype=None,\n",
899-
" skip_header=1,\n",
900-
" delimiter=\",\",\n",
901-
" usecols=[1, 2, 3, 4])\n",
909+
"data = np.genfromtxt(\"data/Train_Skies/Train_Skies/\\\n",
910+
"Training_Sky%d.csv\" % (n_sky),\n",
911+
" dtype=None,\n",
912+
" skip_header=1,\n",
913+
" delimiter=\",\",\n",
914+
" usecols=[1, 2, 3, 4])\n",
902915
"print \"Data on galaxies in sky %d.\" % n_sky\n",
903916
"print \"position_x, position_y, e_1, e_2 \"\n",
904917
"print data[:3]\n",
@@ -952,10 +965,10 @@
952965
"\n",
953966
"and in PyMC, \n",
954967
"\n",
955-
" exp_mass_large = pm.Uniform( \"exp_mass_large\", 40, 180)\n",
968+
" exp_mass_large = pm.Uniform(\"exp_mass_large\", 40, 180)\n",
956969
" @pm.deterministic\n",
957970
" def mass_large(u = exp_mass_large):\n",
958-
" return np.log( u )\n",
971+
" return np.log(u)\n",
959972
"\n",
960973
"(This is what we mean when we say *log*-uniform.) For smaller galaxies, Tim set the mass to be the logarithm of 20. Why did Tim not create a prior for the smaller mass, nor treat it as a unknown? I believe this decision was made to speed up convergence of the algorithm. This is not too restrictive, as by construction the smaller halos have less influence on the galaxies.\n",
961974
"\n",
@@ -1013,7 +1026,8 @@
10131026
"\n",
10141027
"@pm.deterministic\n",
10151028
"def mean(mass=mass_large, h_pos=halo_position, glx_pos=data[:, :2]):\n",
1016-
" return mass / f_distance(glx_pos, h_pos, 240) * tangential_distance(glx_pos, h_pos);"
1029+
" return mass / f_distance(glx_pos, h_pos, 240) *\\\n",
1030+
" tangential_distance(glx_pos, h_pos)"
10171031
],
10181032
"language": "python",
10191033
"metadata": {},
@@ -1029,7 +1043,7 @@
10291043
"mcmc = pm.MCMC([ellpty, mean, halo_position, mass_large])\n",
10301044
"map_ = pm.MAP([ellpty, mean, halo_position, mass_large])\n",
10311045
"map_.fit()\n",
1032-
"mcmc.sample(200000, 140000, 3);"
1046+
"mcmc.sample(200000, 140000, 3)"
10331047
],
10341048
"language": "python",
10351049
"metadata": {},
@@ -1101,7 +1115,7 @@
11011115
" delimiter=\",\",\n",
11021116
" usecols=[1, 2, 3, 4, 5, 6, 7, 8, 9],\n",
11031117
" skip_header=1)\n",
1104-
"print halo_data[n_sky];"
1118+
"print halo_data[n_sky]"
11051119
],
11061120
"language": "python",
11071121
"metadata": {},
@@ -1139,9 +1153,9 @@
11391153
" c=\"k\", s=70)\n",
11401154
"plt.legend(scatterpoints=1, loc=\"lower left\")\n",
11411155
"plt.xlim(0, 4200)\n",
1142-
"plt.ylim(0, 4200)\n",
1156+
"plt.ylim(0, 4200);\n",
11431157
"\n",
1144-
"print \"True halo location:\", halo_data[n_sky][3], halo_data[n_sky][4];"
1158+
"print \"True halo location:\", halo_data[n_sky][3], halo_data[n_sky][4]"
11451159
],
11461160
"language": "python",
11471161
"metadata": {},
@@ -1173,7 +1187,7 @@
11731187
"collapsed": false,
11741188
"input": [
11751189
"mean_posterior = t.mean(axis=0).reshape(1, 2)\n",
1176-
"print mean_posterior;"
1190+
"print mean_posterior"
11771191
],
11781192
"language": "python",
11791193
"metadata": {},
@@ -1204,14 +1218,16 @@
12041218
"sky_prediction = mean_posterior\n",
12051219
"\n",
12061220
"print \"Using the mean:\"\n",
1207-
"main_score(nhalo_all, x_true_all, y_true_all, x_ref_all, y_ref_all, sky_prediction)\n",
1221+
"main_score(nhalo_all, x_true_all, y_true_all,\n",
1222+
" x_ref_all, y_ref_all, sky_prediction)\n",
12081223
"\n",
12091224
"# what's a bad score?\n",
12101225
"print\n",
12111226
"random_guess = np.random.randint(0, 4200, size=(1, 2))\n",
12121227
"print \"Using a random location:\", random_guess\n",
1213-
"main_score(nhalo_all, x_true_all, y_true_all, x_ref_all, y_ref_all, random_guess)\n",
1214-
"print;"
1228+
"main_score(nhalo_all, x_true_all, y_true_all,\n",
1229+
" x_ref_all, y_ref_all, random_guess)\n",
1230+
"print"
12151231
],
12161232
"language": "python",
12171233
"metadata": {},
@@ -1298,7 +1314,8 @@
12981314
"\n",
12991315
" _sum = 0\n",
13001316
" for i in range(n_halos_in_sky):\n",
1301-
" _sum += mass[i] / f_distance(glx_pos, h_pos[i, :], fdist_constants[i]) * tangential_distance(glx_pos, h_pos[i, :])\n",
1317+
" _sum += mass[i] / f_distance(glx_pos, h_pos[i, :], fdist_constants[i]) *\\\n",
1318+
" tangential_distance(glx_pos, h_pos[i, :])\n",
13021319
"\n",
13031320
" return _sum\n",
13041321
"\n",
@@ -1310,7 +1327,7 @@
13101327
"\n",
13111328
" mcmc = pm.MCMC([ellpty, mean, halo_positions, mass_large])\n",
13121329
" mcmc.sample(samples, burn_in, thin)\n",
1313-
" return mcmc.trace(\"halo_positions\")[:];"
1330+
" return mcmc.trace(\"halo_positions\")[:]"
13141331
],
13151332
"language": "python",
13161333
"metadata": {},
@@ -1322,11 +1339,12 @@
13221339
"collapsed": false,
13231340
"input": [
13241341
"n_sky = 215\n",
1325-
"data = np.genfromtxt(\"data/Train_Skies/Train_Skies/Training_Sky%d.csv\" % (n_sky),\n",
1326-
" dtype=None,\n",
1327-
" skip_header=1,\n",
1328-
" delimiter=\",\",\n",
1329-
" usecols=[1, 2, 3, 4]);"
1342+
"data = np.genfromtxt(\"data/Train_Skies/Train_Skies/\\\n",
1343+
"Training_Sky%d.csv\" % (n_sky),\n",
1344+
" dtype=None,\n",
1345+
" skip_header=1,\n",
1346+
" delimiter=\",\",\n",
1347+
" usecols=[1, 2, 3, 4])"
13301348
],
13311349
"language": "python",
13321350
"metadata": {},
@@ -1341,7 +1359,7 @@
13411359
"samples = 10.5e5\n",
13421360
"traces = halo_posteriors(3, data, samples=samples,\n",
13431361
" burn_in=9.5e5,\n",
1344-
" thin=10);"
1362+
" thin=10)"
13451363
],
13461364
"language": "python",
13471365
"metadata": {},
@@ -1368,6 +1386,7 @@
13681386
"cell_type": "code",
13691387
"collapsed": false,
13701388
"input": [
1389+
"\n",
13711390
"fig = draw_sky(data)\n",
13721391
"plt.title(\"Galaxy positions and ellipcities of sky %d.\" % n_sky)\n",
13731392
"plt.xlabel(\"x-position\")\n",
@@ -1384,7 +1403,7 @@
13841403
" label=\"True halo position\",\n",
13851404
" c=\"k\", s=90)\n",
13861405
"\n",
1387-
"# plt.legend(scatterpoints=1)\n",
1406+
"# plt.legend(scatterpoints = 1)\n",
13881407
"plt.xlim(0, 4200)\n",
13891408
"plt.ylim(0, 4200);"
13901409
],
@@ -1424,22 +1443,26 @@
14241443
"mean_posterior = traces.mean(axis=0).reshape(1, 4)\n",
14251444
"print mean_posterior\n",
14261445
"\n",
1446+
"\n",
14271447
"nhalo_all = _halo_data[0].reshape(1, 1)\n",
14281448
"x_true_all = _halo_data[3].reshape(1, 1)\n",
14291449
"y_true_all = _halo_data[4].reshape(1, 1)\n",
14301450
"x_ref_all = _halo_data[1].reshape(1, 1)\n",
14311451
"y_ref_all = _halo_data[2].reshape(1, 1)\n",
14321452
"sky_prediction = mean_posterior\n",
14331453
"\n",
1454+
"\n",
14341455
"print \"Using the mean:\"\n",
1435-
"main_score([1], x_true_all, y_true_all, x_ref_all, y_ref_all, sky_prediction)\n",
1456+
"main_score([1], x_true_all, y_true_all,\n",
1457+
" x_ref_all, y_ref_all, sky_prediction)\n",
14361458
"\n",
14371459
"# what's a bad score?\n",
14381460
"print\n",
14391461
"random_guess = np.random.randint(0, 4200, size=(1, 2))\n",
14401462
"print \"Using a random location:\", random_guess\n",
1441-
"main_score([1], x_true_all, y_true_all, x_ref_all, y_ref_all, random_guess)\n",
1442-
"print;"
1463+
"main_score([1], x_true_all, y_true_all,\n",
1464+
" x_ref_all, y_ref_all, random_guess)\n",
1465+
"print"
14431466
],
14441467
"language": "python",
14451468
"metadata": {},
@@ -1519,7 +1542,7 @@
15191542
"def css_styling():\n",
15201543
" styles = open(\"../styles/custom.css\", \"r\").read()\n",
15211544
" return HTML(styles)\n",
1522-
"css_styling();"
1545+
"css_styling()"
15231546
],
15241547
"language": "python",
15251548
"metadata": {},

0 commit comments

Comments
 (0)