64
64
"ax = fig.add_subplot(122, projection='3d')\n",
65
65
"ax.plot_surface(X, Y, M, cmap=cm.jet, vmax=1, vmin=-.15)\n",
66
66
"ax.view_init(azim=390)\n",
67
- "plt.title(\"Uniform prior landscape; alternate view\")"
67
+ "plt.title(\"Uniform prior landscape; alternate view\")\n "
68
68
],
69
69
"language": "python",
70
70
"metadata": {},
122
122
"ax = fig.add_subplot(122, projection='3d')\n",
123
123
"ax.plot_surface(X, Y, M, cmap=cm.jet)\n",
124
124
"ax.view_init(azim=390)\n",
125
- "plt.title(\"$Exp(3), Exp(10)$ prior landscape; \\nalternate view\")"
125
+ "plt.title(\"$Exp(3), Exp(10)$ prior landscape; \\nalternate view\")\n "
126
126
],
127
127
"language": "python",
128
128
"metadata": {},
158
158
"cell_type": "code",
159
159
"collapsed": false,
160
160
"input": [
161
- "### create the observed data\n",
161
+ "# create the observed data\n",
162
162
"\n",
163
- "#sample size of data we observe, trying varying this (keep it less than 100 ;)\n",
163
+ "# sample size of data we observe, trying varying this (keep it less than 100 ;)\n",
164
164
"N = 1\n",
165
165
"\n",
166
- "#the true parameters, but of course we do not see these values...\n",
166
+ "# the true parameters, but of course we do not see these values...\n",
167
167
"lambda_1_true = 1\n",
168
168
"lambda_2_true = 3\n",
169
169
"\n",
170
170
"#...we see the data generated, dependent on the above two values.\n",
171
171
"data = np.concatenate([\n",
172
172
" stats.poisson.rvs(lambda_1_true, size=(N, 1)),\n",
173
173
" stats.poisson.rvs(lambda_2_true, size=(N, 1))\n",
174
- " ], axis=1)\n",
174
+ "], axis=1)\n",
175
175
"print \"observed (2-dimensional,sample size = %d):\" % N, data\n",
176
176
"\n",
177
- "#plotting details.\n",
177
+ "# plotting details.\n",
178
178
"x = y = np.linspace(.01, 5, 100)\n",
179
179
"likelihood_x = np.array([stats.poisson.pmf(data[:, 0], _x)\n",
180
180
" for _x in x]).prod(axis=1)\n",
181
181
"likelihood_y = np.array([stats.poisson.pmf(data[:, 1], _y)\n",
182
182
" for _y in y]).prod(axis=1)\n",
183
- "L = np.dot(likelihood_x[:, None], likelihood_y[None, :])"
183
+ "L = np.dot(likelihood_x[:, None], likelihood_y[None, :])\n "
184
184
],
185
185
"language": "python",
186
186
"metadata": {},
200
200
"collapsed": false,
201
201
"input": [
202
202
"figsize(12.5, 12)\n",
203
- "#matplotlib heavy lifting below, beware!\n",
203
+ "# matplotlib heavy lifting below, beware!\n",
204
204
"subplot(221)\n",
205
205
"uni_x = stats.uniform.pdf(x, loc=0, scale=5)\n",
206
206
"uni_y = stats.uniform.pdf(x, loc=0, scale=5)\n",
244
244
"plt.title(\"Landscape warped by %d data observation;\\n Exponential priors on \\\n",
245
245
"$p_1, p_2$.\" % N)\n",
246
246
"plt.xlim(0, 5)\n",
247
- "plt.ylim(0, 5)"
247
+ "plt.ylim(0, 5)\n "
248
248
],
249
249
"language": "python",
250
250
"metadata": {},
334
334
"collapsed": false,
335
335
"input": [
336
336
"figsize(12.5, 4)\n",
337
- "data = np.loadtxt(\"data/mixture_data.csv\", delimiter=\",\")\n",
337
+ "data = np.loadtxt(\"data/mixture_data.csv\", delimiter=\",\")\n",
338
338
"\n",
339
- "hist(data, bins=20, color=\"k\", histtype=\"stepfilled\", alpha=0.8)\n",
339
+ "hist(data, bins=20, color=\"k\", histtype=\"stepfilled\", alpha=0.8)\n",
340
340
"plt.title(\"Histogram of the dataset\")\n",
341
341
"plt.ylim([0, None])\n",
342
- "print data[:10], \"...\""
342
+ "print data[:10], \"...\"\n "
343
343
],
344
344
"language": "python",
345
345
"metadata": {},
387
387
"\n",
388
388
"p = pm.Uniform(\"p\", 0, 1)\n",
389
389
"\n",
390
- "assignment = pm.Categorical(\"assignment\", [p, 1- p], size=data.shape[0])\n",
390
+ "assignment = pm.Categorical(\"assignment\", [p, 1 - p], size=data.shape[0])\n",
391
391
"print \"prior assignment, with p = %.2f:\" % p.value\n",
392
- "print assignment.value[:10], \"...\""
392
+ "print assignment.value[:10], \"...\"\n "
393
393
],
394
394
"language": "python",
395
395
"metadata": {},
426
426
"cell_type": "code",
427
427
"collapsed": false,
428
428
"input": [
429
- "taus = 1.0/ pm.Uniform(\"stds\", 0, 100, size=2) ** 2\n",
429
+ "taus = 1.0 / pm.Uniform(\"stds\", 0, 100, size=2) ** 2\n",
430
430
"centers = pm.Normal(\"centers\", [120, 190], [0.01, 0.01], size=2)\n",
431
431
"\n",
432
432
"\"\"\"\n",
447
447
"\n",
448
448
"print \"Random assignments: \", assignment.value[:4], \"...\"\n",
449
449
"print \"Assigned center: \", center_i.value[:4], \"...\"\n",
450
- "print \"Assigned precision: \", tau_i.value[:4], \"...\""
450
+ "print \"Assigned precision: \", tau_i.value[:4], \"...\"\n "
451
451
],
452
452
"language": "python",
453
453
"metadata": {},
468
468
"cell_type": "code",
469
469
"collapsed": false,
470
470
"input": [
471
- "#and to combine it with the observations:\n",
471
+ "# and to combine it with the observations:\n",
472
472
"observations = pm.Normal(\"obs\", center_i, tau_i, value=data, observed=True)\n",
473
473
"\n",
474
- "#below we create a model class\n",
475
- "model = pm.Model([p, assignment, taus, centers])"
474
+ "# below we create a model class\n",
475
+ "model = pm.Model([p, assignment, taus, centers])\n "
476
476
],
477
477
"language": "python",
478
478
"metadata": {},
495
495
"collapsed": false,
496
496
"input": [
497
497
"mcmc = pm.MCMC(model)\n",
498
- "mcmc.sample(50000)"
498
+ "mcmc.sample(50000)\n "
499
499
],
500
500
"language": "python",
501
501
"metadata": {},
527
527
"lw = 1\n",
528
528
"center_trace = mcmc.trace(\"centers\")[:]\n",
529
529
"\n",
530
- "#for pretty colors later in the book.\n",
530
+ "# for pretty colors later in the book.\n",
531
531
"colors = [\"#348ABD\", \"#A60628\"] if center_trace[-1, 0] > center_trace[-1, 1] \\\n",
532
- " else [\"#A60628\", \"#348ABD\"]\n",
532
+ " else [\"#A60628\", \"#348ABD\"]\n",
533
533
"\n",
534
534
"plot(center_trace[:, 0], label=\"trace of center 0\", c=colors[0], lw=lw)\n",
535
535
"plot(center_trace[:, 1], label=\"trace of center 1\", c=colors[1], lw=lw)\n",
551
551
" color=\"#467821\", lw=lw)\n",
552
552
"plt.xlabel(\"Steps\")\n",
553
553
"plt.ylim(0, 1)\n",
554
- "plt.legend()"
554
+ "plt.legend()\n "
555
555
],
556
556
"language": "python",
557
557
"metadata": {},
583
583
"cell_type": "code",
584
584
"collapsed": false,
585
585
"input": [
586
- "mcmc.sample(100000)"
586
+ "mcmc.sample(100000)\n "
587
587
],
588
588
"language": "python",
589
589
"metadata": {},
621
621
" lw=lw, alpha=0.4, c=colors[0])\n",
622
622
"\n",
623
623
"x = np.arange(50000, 150000)\n",
624
- "plot(x, center_trace[:, 0], label=\"new trace of center 0\", lw=lw, c=\"#348ABD\")\n",
625
- "plot(x, center_trace[:, 1], label=\"new trace of center 1\", lw=lw, c=\"#A60628\")\n",
624
+ "plot(x, center_trace[:, 0], label=\"new trace of center 0\", lw=lw, c=\"#348ABD\")\n",
625
+ "plot(x, center_trace[:, 1], label=\"new trace of center 1\", lw=lw, c=\"#A60628\")\n",
626
626
"\n",
627
627
"plt.title(\"Traces of unknown center parameters\")\n",
628
628
"leg = plt.legend(loc=\"upper right\")\n",
629
629
"leg.get_frame().set_alpha(0.8)\n",
630
- "plt.xlabel(\"Steps\")"
630
+ "plt.xlabel(\"Steps\")\n "
631
631
],
632
632
"language": "python",
633
633
"metadata": {},
668
668
" plt.title(\"Posterior of standard deviation of cluster %d\" % i)\n",
669
669
" plt.hist(std_trace[:, i], color=colors[i], bins=30,\n",
670
670
" histtype=\"stepfilled\")\n",
671
- " #plt.autoscale(tight=True)\n",
671
+ " # plt.autoscale(tight=True)\n",
672
672
"\n",
673
- "plt.tight_layout()"
673
+ "plt.tight_layout()\n "
674
674
],
675
675
"language": "python",
676
676
"metadata": {},
703
703
" [\"%.2f\" % s for s in np.sort(data)[::40]])\n",
704
704
"plt.ylabel(\"posterior sample\")\n",
705
705
"plt.xlabel(\"value of $i$th data point\")\n",
706
- "plt.title(\"Posterior labels of data points\")"
706
+ "plt.title(\"Posterior labels of data points\")\n "
707
707
],
708
708
"language": "python",
709
709
"metadata": {},
728
728
"input": [
729
729
"cmap = mpl.colors.LinearSegmentedColormap.from_list(\"BMH\", colors)\n",
730
730
"assign_trace = mcmc.trace(\"assignment\")[:]\n",
731
- "scatter(data, 1- assign_trace.mean(axis=0), cmap=cmap,\n",
731
+ "scatter(data, 1 - assign_trace.mean(axis=0), cmap=cmap,\n",
732
732
" c=assign_trace.mean(axis=0), s=50)\n",
733
733
"plt.ylim(-0.05, 1.05)\n",
734
734
"plt.xlim(35, 300)\n",
735
735
"plt.title(\"Probability of data point belonging to cluster 0\")\n",
736
736
"plt.ylabel(\"probability\")\n",
737
- "plt.xlabel(\"value of data point\")"
737
+ "plt.xlabel(\"value of data point\")\n "
738
738
],
739
739
"language": "python",
740
740
"metadata": {},
778
778
"plt.fill_between(x, y, color=colors[0], alpha=0.3)\n",
779
779
"\n",
780
780
"plt.legend(loc=\"upper left\")\n",
781
- "plt.title(\"Visualizing Clusters using posterior-mean parameters\")"
781
+ "plt.title(\"Visualizing Clusters using posterior-mean parameters\")\n "
782
782
],
783
783
"language": "python",
784
784
"metadata": {},
817
817
"\n",
818
818
"plt.plot(ex_mcmc.trace(\"x\")[:])\n",
819
819
"plt.plot(ex_mcmc.trace(\"y\")[:])\n",
820
- "plt.title(\"Displaying (extreme) case of dependence between unknowns\")"
820
+ "plt.title(\"Displaying (extreme) case of dependence between unknowns\")\n "
821
821
],
822
822
"language": "python",
823
823
"metadata": {},
891
891
"p_trace = mcmc.trace(\"p\")[:]\n",
892
892
"x = 175\n",
893
893
"\n",
894
- "v = p_trace* norm_pdf(x, loc=center_trace[:, 0], scale=std_trace[:, 0]) > \\\n",
895
- " (1- p_trace)* norm_pdf(x, loc=center_trace[:, 1], scale=std_trace[:, 1])\n",
894
+ "v = p_trace * norm_pdf(x, loc=center_trace[:, 0], scale=std_trace[:, 0]) > \\\n",
895
+ " (1 - p_trace) * norm_pdf(x, loc=center_trace[:, 1], scale=std_trace[:, 1])\n",
896
896
"\n",
897
- "print \"Probability of belonging to cluster 1:\", v.mean()"
897
+ "print \"Probability of belonging to cluster 1:\", v.mean()\n "
898
898
],
899
899
"language": "python",
900
900
"metadata": {},
988
988
"plt.plot(y_t, label=\"$y_t$\", lw=3)\n",
989
989
"plt.plot(x_t, label=\"$x_t$\", lw=3)\n",
990
990
"plt.xlabel(\"time, $t$\")\n",
991
- "plt.legend()"
991
+ "plt.legend()\n "
992
992
],
993
993
"language": "python",
994
994
"metadata": {},
1014
1014
"collapsed": false,
1015
1015
"input": [
1016
1016
"def autocorr(x):\n",
1017
- " #from http://tinyurl.com/afz57c4\n",
1017
+ " # from http://tinyurl.com/afz57c4\n",
1018
1018
" result = np.correlate(x, x, mode='full')\n",
1019
1019
" result = result / np.max(result)\n",
1020
1020
" return result[result.size / 2:]\n",
1030
1030
"plt.legend(title=\"Autocorrelation\")\n",
1031
1031
"plt.ylabel(\"measured correlation \\nbetween $y_t$ and $y_{t-k}$.\")\n",
1032
1032
"plt.xlabel(\"k (lag)\")\n",
1033
- "plt.title(\"Autocorrelation plot of $y_t$ and $x_t$ for differing $k$ lags.\")"
1033
+ "plt.title(\"Autocorrelation plot of $y_t$ and $x_t$ for differing $k$ lags.\")\n "
1034
1034
],
1035
1035
"language": "python",
1036
1036
"metadata": {},
1087
1087
"plt.ylabel(\"measured correlation \\nbetween $y_t$ and $y_{t-k}$.\")\n",
1088
1088
"plt.xlabel(\"k (lag)\")\n",
1089
1089
"plt.title(\"Autocorrelation of $y_t$ (no thinning vs. thinning) \\\n",
1090
- "at differing $k$ lags.\")"
1090
+ "at differing $k$ lags.\")\n "
1091
1091
],
1092
1092
"language": "python",
1093
1093
"metadata": {},
1130
1130
"from pymc.Matplot import plot as mcplot\n",
1131
1131
"\n",
1132
1132
"mcmc.sample(25000, 0, 10)\n",
1133
- "mcplot(mcmc.trace(\"centers\", 2), common_scale=False)"
1133
+ "mcplot(mcmc.trace(\"centers\", 2), common_scale=False)\n "
1134
1134
],
1135
1135
"language": "python",
1136
1136
"metadata": {},
1249
1249
"def css_styling():\n",
1250
1250
" styles = open(\"../styles/custom.css\", \"r\").read()\n",
1251
1251
" return HTML(styles)\n",
1252
- "css_styling()"
1252
+ "css_styling()\n "
1253
1253
],
1254
1254
"language": "python",
1255
1255
"metadata": {},
1338
1338
"metadata": {}
1339
1339
}
1340
1340
]
1341
- }
1341
+ }
0 commit comments