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