Skip to content

Commit fa1aa60

Browse files
Merge pull request CamDavidsonPilon#180 from eli-b/chap3-style
autopep8 --ignore=E501 fixes. Marginal improvement.
2 parents 854ba3e + 4876681 commit fa1aa60

File tree

1 file changed

+46
-44
lines changed

1 file changed

+46
-44
lines changed

Chapter3_MCMC/IntroMCMC.ipynb

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -146,29 +146,29 @@
146146
"cell_type": "code",
147147
"collapsed": false,
148148
"input": [
149-
"### create the observed data\n",
149+
"# create the observed data\n",
150150
"\n",
151-
"#sample size of data we observe, trying varying this (keep it less than 100 ;)\n",
151+
"# sample size of data we observe, trying varying this (keep it less than 100 ;)\n",
152152
"N = 1\n",
153153
"\n",
154-
"#the true parameters, but of course we do not see these values...\n",
154+
"# the true parameters, but of course we do not see these values...\n",
155155
"lambda_1_true = 1\n",
156156
"lambda_2_true = 3\n",
157157
"\n",
158158
"#...we see the data generated, dependent on the above two values.\n",
159159
"data = np.concatenate([\n",
160160
" stats.poisson.rvs(lambda_1_true, size=(N, 1)),\n",
161161
" stats.poisson.rvs(lambda_2_true, size=(N, 1))\n",
162-
" ], axis=1)\n",
162+
"], axis=1)\n",
163163
"print \"observed (2-dimensional,sample size = %d):\" % N, data\n",
164164
"\n",
165-
"#plotting details.\n",
165+
"# plotting details.\n",
166166
"x = y = np.linspace(.01, 5, 100)\n",
167167
"likelihood_x = np.array([stats.poisson.pmf(data[:, 0], _x)\n",
168168
" for _x in x]).prod(axis=1)\n",
169169
"likelihood_y = np.array([stats.poisson.pmf(data[:, 1], _y)\n",
170170
" for _y in y]).prod(axis=1)\n",
171-
"L = np.dot(likelihood_x[:, None], likelihood_y[None, :])"
171+
"L = np.dot(likelihood_x[:, None], likelihood_y[None, :]);"
172172
],
173173
"language": "python",
174174
"metadata": {},
@@ -188,7 +188,7 @@
188188
"collapsed": false,
189189
"input": [
190190
"figsize(12.5, 12)\n",
191-
"#matplotlib heavy lifting below, beware!\n",
191+
"# matplotlib heavy lifting below, beware!\n",
192192
"plt.subplot(221)\n",
193193
"uni_x = stats.uniform.pdf(x, loc=0, scale=5)\n",
194194
"uni_y = stats.uniform.pdf(x, loc=0, scale=5)\n",
@@ -319,12 +319,12 @@
319319
"collapsed": false,
320320
"input": [
321321
"figsize(12.5, 4)\n",
322-
"data = np.loadtxt(\"data/mixture_data.csv\", delimiter=\",\")\n",
322+
"data = np.loadtxt(\"data/mixture_data.csv\", delimiter=\",\")\n",
323323
"\n",
324-
"plt.hist(data, bins=20, color=\"k\", histtype=\"stepfilled\", alpha=0.8)\n",
324+
"plt.hist(data, bins=20, color=\"k\", histtype=\"stepfilled\", alpha=0.8)\n",
325325
"plt.title(\"Histogram of the dataset\")\n",
326326
"plt.ylim([0, None])\n",
327-
"print data[:10], \"...\""
327+
"print data[:10], \"...\";"
328328
],
329329
"language": "python",
330330
"metadata": {},
@@ -376,9 +376,9 @@
376376
"\n",
377377
"p = pm.Uniform(\"p\", 0, 1)\n",
378378
"\n",
379-
"assignment = pm.Categorical(\"assignment\", [p, 1-p], size=data.shape[0])\n",
379+
"assignment = pm.Categorical(\"assignment\", [p, 1 - p], size=data.shape[0])\n",
380380
"print \"prior assignment, with p = %.2f:\" % p.value\n",
381-
"print assignment.value[:10], \"...\""
381+
"print assignment.value[:10], \"...\";"
382382
],
383383
"language": "python",
384384
"metadata": {},
@@ -415,25 +415,27 @@
415415
"cell_type": "code",
416416
"collapsed": false,
417417
"input": [
418-
"taus = 1.0/pm.Uniform(\"stds\", 0, 100, size=2) ** 2\n",
418+
"taus = 1.0 / pm.Uniform(\"stds\", 0, 100, size=2) ** 2\n",
419419
"centers = pm.Normal(\"centers\", [120, 190], [0.01, 0.01], size=2)\n",
420420
"\n",
421421
"\"\"\"\n",
422422
"The below deterministic functions map an assignment, in this case 0 or 1,\n",
423423
"to a set of parameters, located in the (1,2) arrays `taus` and `centers`.\n",
424424
"\"\"\"\n",
425425
"\n",
426+
"\n",
426427
"@pm.deterministic\n",
427428
"def center_i(assignment=assignment, centers=centers):\n",
428429
" return centers[assignment]\n",
429430
"\n",
431+
"\n",
430432
"@pm.deterministic\n",
431433
"def tau_i(assignment=assignment, taus=taus):\n",
432434
" return taus[assignment]\n",
433435
"\n",
434436
"print \"Random assignments: \", assignment.value[:4], \"...\"\n",
435437
"print \"Assigned center: \", center_i.value[:4], \"...\"\n",
436-
"print \"Assigned precision: \", tau_i.value[:4], \"...\""
438+
"print \"Assigned precision: \", tau_i.value[:4], \"...\";"
437439
],
438440
"language": "python",
439441
"metadata": {},
@@ -454,11 +456,11 @@
454456
"cell_type": "code",
455457
"collapsed": false,
456458
"input": [
457-
"#and to combine it with the observations:\n",
459+
"# and to combine it with the observations:\n",
458460
"observations = pm.Normal(\"obs\", center_i, tau_i, value=data, observed=True)\n",
459461
"\n",
460-
"#below we create a model class\n",
461-
"model = pm.Model([p, assignment, taus, centers])"
462+
"# below we create a model class\n",
463+
"model = pm.Model([p, assignment, taus, centers]);"
462464
],
463465
"language": "python",
464466
"metadata": {},
@@ -481,7 +483,7 @@
481483
"collapsed": false,
482484
"input": [
483485
"mcmc = pm.MCMC(model)\n",
484-
"mcmc.sample(50000)"
486+
"mcmc.sample(50000);"
485487
],
486488
"language": "python",
487489
"metadata": {},
@@ -520,9 +522,9 @@
520522
"lw = 1\n",
521523
"center_trace = mcmc.trace(\"centers\")[:]\n",
522524
"\n",
523-
"#for pretty colors later in the book.\n",
525+
"# for pretty colors later in the book.\n",
524526
"colors = [\"#348ABD\", \"#A60628\"] if center_trace[-1, 0] > center_trace[-1, 1] \\\n",
525-
" else [\"#A60628\", \"#348ABD\"]\n",
527+
" else [\"#A60628\", \"#348ABD\"]\n",
526528
"\n",
527529
"plt.plot(center_trace[:, 0], label=\"trace of center 0\", c=colors[0], lw=lw)\n",
528530
"plt.plot(center_trace[:, 1], label=\"trace of center 1\", c=colors[1], lw=lw)\n",
@@ -533,15 +535,15 @@
533535
"plt.subplot(312)\n",
534536
"std_trace = mcmc.trace(\"stds\")[:]\n",
535537
"plt.plot(std_trace[:, 0], label=\"trace of standard deviation of cluster 0\",\n",
536-
" c=colors[0], lw=lw)\n",
538+
" c=colors[0], lw=lw)\n",
537539
"plt.plot(std_trace[:, 1], label=\"trace of standard deviation of cluster 1\",\n",
538-
" c=colors[1], lw=lw)\n",
540+
" c=colors[1], lw=lw)\n",
539541
"plt.legend(loc=\"upper left\")\n",
540542
"\n",
541543
"plt.subplot(313)\n",
542544
"p_trace = mcmc.trace(\"p\")[:]\n",
543545
"plt.plot(p_trace, label=\"$p$: frequency of assignment to cluster 0\",\n",
544-
" color=\"#467821\", lw=lw)\n",
546+
" color=\"#467821\", lw=lw)\n",
545547
"plt.xlabel(\"Steps\")\n",
546548
"plt.ylim(0, 1)\n",
547549
"plt.legend();"
@@ -580,7 +582,7 @@
580582
"cell_type": "code",
581583
"collapsed": false,
582584
"input": [
583-
"mcmc.sample(100000)"
585+
"mcmc.sample(100000);"
584586
],
585587
"language": "python",
586588
"metadata": {},
@@ -613,13 +615,13 @@
613615
"\n",
614616
"x = np.arange(50000)\n",
615617
"plt.plot(x, prev_center_trace[:, 0], label=\"previous trace of center 0\",\n",
616-
" lw=lw, alpha=0.4, c=colors[1])\n",
618+
" lw=lw, alpha=0.4, c=colors[1])\n",
617619
"plt.plot(x, prev_center_trace[:, 1], label=\"previous trace of center 1\",\n",
618-
" lw=lw, alpha=0.4, c=colors[0])\n",
620+
" lw=lw, alpha=0.4, c=colors[0])\n",
619621
"\n",
620622
"x = np.arange(50000, 150000)\n",
621-
"plt.plot(x, center_trace[:, 0], label=\"new trace of center 0\", lw=lw, c=\"#348ABD\")\n",
622-
"plt.plot(x, center_trace[:, 1], label=\"new trace of center 1\", lw=lw, c=\"#A60628\")\n",
623+
"plt.plot(x, center_trace[:, 0], label=\"new trace of center 0\", lw=lw, c=\"#348ABD\")\n",
624+
"plt.plot(x, center_trace[:, 1], label=\"new trace of center 1\", lw=lw, c=\"#A60628\")\n",
623625
"\n",
624626
"plt.title(\"Traces of unknown center parameters\")\n",
625627
"leg = plt.legend(loc=\"upper right\")\n",
@@ -669,9 +671,9 @@
669671
" plt.title(\"Posterior of standard deviation of cluster %d\" % i)\n",
670672
" plt.hist(std_trace[:, i], color=colors[i], bins=30,\n",
671673
" histtype=\"stepfilled\")\n",
672-
" #plt.autoscale(tight=True)\n",
674+
" # plt.autoscale(tight=True)\n",
673675
"\n",
674-
"plt.tight_layout()"
676+
"plt.tight_layout();"
675677
],
676678
"language": "python",
677679
"metadata": {},
@@ -704,9 +706,9 @@
704706
"figsize(12.5, 4.5)\n",
705707
"plt.cmap = mpl.colors.ListedColormap(colors)\n",
706708
"plt.imshow(mcmc.trace(\"assignment\")[::400, np.argsort(data)],\n",
707-
" cmap=plt.cmap, aspect=.4, alpha=.9)\n",
709+
" cmap=plt.cmap, aspect=.4, alpha=.9)\n",
708710
"plt.xticks(np.arange(0, data.shape[0], 40),\n",
709-
" [\"%.2f\" % s for s in np.sort(data)[::40]])\n",
711+
" [\"%.2f\" % s for s in np.sort(data)[::40]])\n",
710712
"plt.ylabel(\"posterior sample\")\n",
711713
"plt.xlabel(\"value of $i$th data point\")\n",
712714
"plt.title(\"Posterior labels of data points\");"
@@ -738,13 +740,13 @@
738740
"input": [
739741
"cmap = mpl.colors.LinearSegmentedColormap.from_list(\"BMH\", colors)\n",
740742
"assign_trace = mcmc.trace(\"assignment\")[:]\n",
741-
"plt.scatter(data, 1-assign_trace.mean(axis=0), cmap=cmap,\n",
742-
" c=assign_trace.mean(axis=0), s=50)\n",
743+
"plt.scatter(data, 1 - assign_trace.mean(axis=0), cmap=cmap,\n",
744+
" c=assign_trace.mean(axis=0), s=50)\n",
743745
"plt.ylim(-0.05, 1.05)\n",
744746
"plt.xlim(35, 300)\n",
745747
"plt.title(\"Probability of data point belonging to cluster 0\")\n",
746748
"plt.ylabel(\"probability\")\n",
747-
"plt.xlabel(\"value of data point\")"
749+
"plt.xlabel(\"value of data point\");"
748750
],
749751
"language": "python",
750752
"metadata": {},
@@ -788,7 +790,7 @@
788790
"posterior_p_mean = mcmc.trace(\"p\")[:].mean()\n",
789791
"\n",
790792
"plt.hist(data, bins=20, histtype=\"step\", normed=True, color=\"k\",\n",
791-
" lw=2, label=\"histogram of data\")\n",
793+
" lw=2, label=\"histogram of data\")\n",
792794
"y = posterior_p_mean * norm.pdf(x, loc=posterior_center_means[0],\n",
793795
" scale=posterior_std_means[0])\n",
794796
"plt.plot(x, y, label=\"Cluster 0 (using posterior-mean parameters)\", lw=3)\n",
@@ -800,7 +802,7 @@
800802
"plt.fill_between(x, y, color=colors[0], alpha=0.3)\n",
801803
"\n",
802804
"plt.legend(loc=\"upper left\")\n",
803-
"plt.title(\"Visualizing Clusters using posterior-mean parameters\")"
805+
"plt.title(\"Visualizing Clusters using posterior-mean parameters\");"
804806
],
805807
"language": "python",
806808
"metadata": {},
@@ -851,7 +853,7 @@
851853
"\n",
852854
"plt.plot(ex_mcmc.trace(\"x\")[:])\n",
853855
"plt.plot(ex_mcmc.trace(\"y\")[:])\n",
854-
"plt.title(\"Displaying (extreme) case of dependence between unknowns\")"
856+
"plt.title(\"Displaying (extreme) case of dependence between unknowns\");"
855857
],
856858
"language": "python",
857859
"metadata": {},
@@ -937,10 +939,10 @@
937939
"p_trace = mcmc.trace(\"p\")[:]\n",
938940
"x = 175\n",
939941
"\n",
940-
"v = p_trace*norm_pdf(x, loc=center_trace[:, 0], scale=std_trace[:, 0]) > \\\n",
941-
" (1-p_trace)*norm_pdf(x, loc=center_trace[:, 1], scale=std_trace[:, 1])\n",
942+
"v = p_trace * norm_pdf(x, loc=center_trace[:, 0], scale=std_trace[:, 0]) > \\\n",
943+
" (1 - p_trace) * norm_pdf(x, loc=center_trace[:, 1], scale=std_trace[:, 1])\n",
942944
"\n",
943-
"print \"Probability of belonging to cluster 1:\", v.mean()"
945+
"print \"Probability of belonging to cluster 1:\", v.mean();"
944946
],
945947
"language": "python",
946948
"metadata": {},
@@ -1064,7 +1066,7 @@
10641066
"collapsed": false,
10651067
"input": [
10661068
"def autocorr(x):\n",
1067-
" #from http://tinyurl.com/afz57c4\n",
1069+
" # from http://tinyurl.com/afz57c4\n",
10681070
" result = np.correlate(x, x, mode='full')\n",
10691071
" result = result / np.max(result)\n",
10701072
" return result[result.size / 2:]\n",
@@ -1187,7 +1189,7 @@
11871189
"from pymc.Matplot import plot as mcplot\n",
11881190
"\n",
11891191
"mcmc.sample(25000, 0, 10)\n",
1190-
"mcplot(mcmc.trace(\"centers\", 2), common_scale=False)"
1192+
"mcplot(mcmc.trace(\"centers\", 2), common_scale=False);"
11911193
],
11921194
"language": "python",
11931195
"metadata": {},
@@ -1310,7 +1312,7 @@
13101312
"def css_styling():\n",
13111313
" styles = open(\"../styles/custom.css\", \"r\").read()\n",
13121314
" return HTML(styles)\n",
1313-
"css_styling()"
1315+
"css_styling();"
13141316
],
13151317
"language": "python",
13161318
"metadata": {},

0 commit comments

Comments
 (0)