|
67 | 67 | "ax = fig.add_subplot(122, projection='3d')\n", |
68 | 68 | "ax.plot_surface(X, Y, M, cmap=plt.cm.jet, vmax=1, vmin=-.15)\n", |
69 | 69 | "ax.view_init(azim=390)\n", |
70 | | - "plt.title(\"Uniform prior landscape; alternate view\")\n" |
| 70 | + "plt.title(\"Uniform prior landscape; alternate view\");" |
71 | 71 | ], |
72 | 72 | "language": "python", |
73 | 73 | "metadata": {}, |
|
113 | 113 | "ax = fig.add_subplot(122, projection='3d')\n", |
114 | 114 | "ax.plot_surface(X, Y, M, cmap=jet)\n", |
115 | 115 | "ax.view_init(azim=390)\n", |
116 | | - "plt.title(\"$Exp(3), Exp(10)$ prior landscape; \\nalternate view\")\n" |
| 116 | + "plt.title(\"$Exp(3), Exp(10)$ prior landscape; \\nalternate view\");" |
117 | 117 | ], |
118 | 118 | "language": "python", |
119 | 119 | "metadata": {}, |
|
168 | 168 | " for _x in x]).prod(axis=1)\n", |
169 | 169 | "likelihood_y = np.array([stats.poisson.pmf(data[:, 1], _y)\n", |
170 | 170 | " for _y in y]).prod(axis=1)\n", |
171 | | - "L = np.dot(likelihood_x[:, None], likelihood_y[None, :])\n" |
| 171 | + "L = np.dot(likelihood_x[:, None], likelihood_y[None, :]);" |
172 | 172 | ], |
173 | 173 | "language": "python", |
174 | 174 | "metadata": {}, |
|
232 | 232 | "plt.title(\"Landscape warped by %d data observation;\\n Exponential priors on \\\n", |
233 | 233 | "$p_1, p_2$.\" % N)\n", |
234 | 234 | "plt.xlim(0, 5)\n", |
235 | | - "plt.ylim(0, 5)\n" |
| 235 | + "plt.ylim(0, 5);" |
236 | 236 | ], |
237 | 237 | "language": "python", |
238 | 238 | "metadata": {}, |
|
324 | 324 | "plt.hist(data, bins=20, color=\"k\", histtype=\"stepfilled\", alpha=0.8)\n", |
325 | 325 | "plt.title(\"Histogram of the dataset\")\n", |
326 | 326 | "plt.ylim([0, None])\n", |
327 | | - "print data[:10], \"...\"\n" |
| 327 | + "print data[:10], \"...\";" |
328 | 328 | ], |
329 | 329 | "language": "python", |
330 | 330 | "metadata": {}, |
|
378 | 378 | "\n", |
379 | 379 | "assignment = pm.Categorical(\"assignment\", [p, 1 - p], size=data.shape[0])\n", |
380 | 380 | "print \"prior assignment, with p = %.2f:\" % p.value\n", |
381 | | - "print assignment.value[:10], \"...\"\n" |
| 381 | + "print assignment.value[:10], \"...\";" |
382 | 382 | ], |
383 | 383 | "language": "python", |
384 | 384 | "metadata": {}, |
|
423 | 423 | "to a set of parameters, located in the (1,2) arrays `taus` and `centers`.\n", |
424 | 424 | "\"\"\"\n", |
425 | 425 | "\n", |
| 426 | + "\n", |
426 | 427 | "@pm.deterministic\n", |
427 | 428 | "def center_i(assignment=assignment, centers=centers):\n", |
428 | 429 | " return centers[assignment]\n", |
429 | 430 | "\n", |
| 431 | + "\n", |
430 | 432 | "@pm.deterministic\n", |
431 | 433 | "def tau_i(assignment=assignment, taus=taus):\n", |
432 | 434 | " return taus[assignment]\n", |
433 | 435 | "\n", |
434 | 436 | "print \"Random assignments: \", assignment.value[:4], \"...\"\n", |
435 | 437 | "print \"Assigned center: \", center_i.value[:4], \"...\"\n", |
436 | | - "print \"Assigned precision: \", tau_i.value[:4], \"...\"\n" |
| 438 | + "print \"Assigned precision: \", tau_i.value[:4], \"...\";" |
437 | 439 | ], |
438 | 440 | "language": "python", |
439 | 441 | "metadata": {}, |
|
458 | 460 | "observations = pm.Normal(\"obs\", center_i, tau_i, value=data, observed=True)\n", |
459 | 461 | "\n", |
460 | 462 | "# below we create a model class\n", |
461 | | - "model = pm.Model([p, assignment, taus, centers])\n" |
| 463 | + "model = pm.Model([p, assignment, taus, centers]);" |
462 | 464 | ], |
463 | 465 | "language": "python", |
464 | 466 | "metadata": {}, |
|
481 | 483 | "collapsed": false, |
482 | 484 | "input": [ |
483 | 485 | "mcmc = pm.MCMC(model)\n", |
484 | | - "mcmc.sample(50000)\n" |
| 486 | + "mcmc.sample(50000);" |
485 | 487 | ], |
486 | 488 | "language": "python", |
487 | 489 | "metadata": {}, |
|
533 | 535 | "plt.subplot(312)\n", |
534 | 536 | "std_trace = mcmc.trace(\"stds\")[:]\n", |
535 | 537 | "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", |
537 | 539 | "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", |
539 | 541 | "plt.legend(loc=\"upper left\")\n", |
540 | 542 | "\n", |
541 | 543 | "plt.subplot(313)\n", |
542 | 544 | "p_trace = mcmc.trace(\"p\")[:]\n", |
543 | 545 | "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", |
545 | 547 | "plt.xlabel(\"Steps\")\n", |
546 | 548 | "plt.ylim(0, 1)\n", |
547 | | - "plt.legend()\n" |
| 549 | + "plt.legend();" |
548 | 550 | ], |
549 | 551 | "language": "python", |
550 | 552 | "metadata": {}, |
|
580 | 582 | "cell_type": "code", |
581 | 583 | "collapsed": false, |
582 | 584 | "input": [ |
583 | | - "mcmc.sample(100000)\n" |
| 585 | + "mcmc.sample(100000);" |
584 | 586 | ], |
585 | 587 | "language": "python", |
586 | 588 | "metadata": {}, |
|
613 | 615 | "\n", |
614 | 616 | "x = np.arange(50000)\n", |
615 | 617 | "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", |
617 | 619 | "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", |
619 | 621 | "\n", |
620 | 622 | "x = np.arange(50000, 150000)\n", |
621 | 623 | "plt.plot(x, center_trace[:, 0], label=\"new trace of center 0\", lw=lw, c=\"#348ABD\")\n", |
|
624 | 626 | "plt.title(\"Traces of unknown center parameters\")\n", |
625 | 627 | "leg = plt.legend(loc=\"upper right\")\n", |
626 | 628 | "leg.get_frame().set_alpha(0.8)\n", |
627 | | - "plt.xlabel(\"Steps\")\n" |
| 629 | + "plt.xlabel(\"Steps\");" |
628 | 630 | ], |
629 | 631 | "language": "python", |
630 | 632 | "metadata": {}, |
|
671 | 673 | " histtype=\"stepfilled\")\n", |
672 | 674 | " # plt.autoscale(tight=True)\n", |
673 | 675 | "\n", |
674 | | - "plt.tight_layout()\n" |
| 676 | + "plt.tight_layout();" |
675 | 677 | ], |
676 | 678 | "language": "python", |
677 | 679 | "metadata": {}, |
|
704 | 706 | "figsize(12.5, 4.5)\n", |
705 | 707 | "plt.cmap = mpl.colors.ListedColormap(colors)\n", |
706 | 708 | "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", |
708 | 710 | "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", |
710 | 712 | "plt.ylabel(\"posterior sample\")\n", |
711 | 713 | "plt.xlabel(\"value of $i$th data point\")\n", |
712 | | - "plt.title(\"Posterior labels of data points\")\n" |
| 714 | + "plt.title(\"Posterior labels of data points\");" |
713 | 715 | ], |
714 | 716 | "language": "python", |
715 | 717 | "metadata": {}, |
|
739 | 741 | "cmap = mpl.colors.LinearSegmentedColormap.from_list(\"BMH\", colors)\n", |
740 | 742 | "assign_trace = mcmc.trace(\"assignment\")[:]\n", |
741 | 743 | "plt.scatter(data, 1 - assign_trace.mean(axis=0), cmap=cmap,\n", |
742 | | - " c=assign_trace.mean(axis=0), s=50)\n", |
| 744 | + " c=assign_trace.mean(axis=0), s=50)\n", |
743 | 745 | "plt.ylim(-0.05, 1.05)\n", |
744 | 746 | "plt.xlim(35, 300)\n", |
745 | 747 | "plt.title(\"Probability of data point belonging to cluster 0\")\n", |
746 | 748 | "plt.ylabel(\"probability\")\n", |
747 | | - "plt.xlabel(\"value of data point\")\n" |
| 749 | + "plt.xlabel(\"value of data point\");" |
748 | 750 | ], |
749 | 751 | "language": "python", |
750 | 752 | "metadata": {}, |
|
788 | 790 | "posterior_p_mean = mcmc.trace(\"p\")[:].mean()\n", |
789 | 791 | "\n", |
790 | 792 | "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", |
792 | 794 | "y = posterior_p_mean * norm.pdf(x, loc=posterior_center_means[0],\n", |
793 | 795 | " scale=posterior_std_means[0])\n", |
794 | 796 | "plt.plot(x, y, label=\"Cluster 0 (using posterior-mean parameters)\", lw=3)\n", |
|
800 | 802 | "plt.fill_between(x, y, color=colors[0], alpha=0.3)\n", |
801 | 803 | "\n", |
802 | 804 | "plt.legend(loc=\"upper left\")\n", |
803 | | - "plt.title(\"Visualizing Clusters using posterior-mean parameters\")\n" |
| 805 | + "plt.title(\"Visualizing Clusters using posterior-mean parameters\");" |
804 | 806 | ], |
805 | 807 | "language": "python", |
806 | 808 | "metadata": {}, |
|
851 | 853 | "\n", |
852 | 854 | "plt.plot(ex_mcmc.trace(\"x\")[:])\n", |
853 | 855 | "plt.plot(ex_mcmc.trace(\"y\")[:])\n", |
854 | | - "plt.title(\"Displaying (extreme) case of dependence between unknowns\")\n" |
| 856 | + "plt.title(\"Displaying (extreme) case of dependence between unknowns\");" |
855 | 857 | ], |
856 | 858 | "language": "python", |
857 | 859 | "metadata": {}, |
|
940 | 942 | "v = p_trace * norm_pdf(x, loc=center_trace[:, 0], scale=std_trace[:, 0]) > \\\n", |
941 | 943 | " (1 - p_trace) * norm_pdf(x, loc=center_trace[:, 1], scale=std_trace[:, 1])\n", |
942 | 944 | "\n", |
943 | | - "print \"Probability of belonging to cluster 1:\", v.mean()\n" |
| 945 | + "print \"Probability of belonging to cluster 1:\", v.mean();" |
944 | 946 | ], |
945 | 947 | "language": "python", |
946 | 948 | "metadata": {}, |
|
1034 | 1036 | "plt.plot(y_t, label=\"$y_t$\", lw=3)\n", |
1035 | 1037 | "plt.plot(x_t, label=\"$x_t$\", lw=3)\n", |
1036 | 1038 | "plt.xlabel(\"time, $t$\")\n", |
1037 | | - "plt.legend()\n" |
| 1039 | + "plt.legend();" |
1038 | 1040 | ], |
1039 | 1041 | "language": "python", |
1040 | 1042 | "metadata": {}, |
|
1080 | 1082 | "plt.legend(title=\"Autocorrelation\")\n", |
1081 | 1083 | "plt.ylabel(\"measured correlation \\nbetween $y_t$ and $y_{t-k}$.\")\n", |
1082 | 1084 | "plt.xlabel(\"k (lag)\")\n", |
1083 | | - "plt.title(\"Autocorrelation plot of $y_t$ and $x_t$ for differing $k$ lags.\")\n" |
| 1085 | + "plt.title(\"Autocorrelation plot of $y_t$ and $x_t$ for differing $k$ lags.\");" |
1084 | 1086 | ], |
1085 | 1087 | "language": "python", |
1086 | 1088 | "metadata": {}, |
|
1140 | 1142 | "plt.ylabel(\"measured correlation \\nbetween $y_t$ and $y_{t-k}$.\")\n", |
1141 | 1143 | "plt.xlabel(\"k (lag)\")\n", |
1142 | 1144 | "plt.title(\"Autocorrelation of $y_t$ (no thinning vs. thinning) \\\n", |
1143 | | - "at differing $k$ lags.\")\n" |
| 1145 | + "at differing $k$ lags.\");" |
1144 | 1146 | ], |
1145 | 1147 | "language": "python", |
1146 | 1148 | "metadata": {}, |
|
1187 | 1189 | "from pymc.Matplot import plot as mcplot\n", |
1188 | 1190 | "\n", |
1189 | 1191 | "mcmc.sample(25000, 0, 10)\n", |
1190 | | - "mcplot(mcmc.trace(\"centers\", 2), common_scale=False)\n" |
| 1192 | + "mcplot(mcmc.trace(\"centers\", 2), common_scale=False);" |
1191 | 1193 | ], |
1192 | 1194 | "language": "python", |
1193 | 1195 | "metadata": {}, |
|
1310 | 1312 | "def css_styling():\n", |
1311 | 1313 | " styles = open(\"../styles/custom.css\", \"r\").read()\n", |
1312 | 1314 | " return HTML(styles)\n", |
1313 | | - "css_styling()\n" |
| 1315 | + "css_styling();" |
1314 | 1316 | ], |
1315 | 1317 | "language": "python", |
1316 | 1318 | "metadata": {}, |
|
0 commit comments