|
194 | 194 | "\n",
|
195 | 195 | "# For the already prepared, I'm using Binomial's conj. prior.\n",
|
196 | 196 | "for k, N in enumerate(n_trials):\n",
|
197 |
| - " sx = plt.subplot(len(n_trials) / 2, 2, k + 1)\n", |
| 197 | + " sx = plt.subplot(len(n_trials)/2, 2, k+1)\n", |
198 | 198 | " plt.xlabel(\"$p$, probability of heads\") \\\n",
|
199 |
| - " if k in [0, len(n_trials) - 1] else None\n", |
| 199 | + " if k in [0, len(n_trials)-1] else None\n", |
200 | 200 | " plt.setp(sx.get_yticklabels(), visible=False)\n",
|
201 | 201 | " heads = data[:N].sum()\n",
|
202 | 202 | " y = dist.pdf(x, 1 + heads, 1 + N - heads)\n",
|
|
213 | 213 | " y=1.02,\n",
|
214 | 214 | " fontsize=14)\n",
|
215 | 215 | "\n",
|
216 |
| - "plt.tight_layout();" |
| 216 | + "plt.tight_layout()" |
217 | 217 | ],
|
218 | 218 | "language": "python",
|
219 | 219 | "metadata": {},
|
|
286 | 286 | "input": [
|
287 | 287 | "figsize(12.5, 4)\n",
|
288 | 288 | "p = np.linspace(0, 1, 50)\n",
|
289 |
| - "plt.plot(p, 2 * p / (1 + p), color=\"#348ABD\", lw=3)\n", |
290 |
| - "# plt.fill_between(p, 2*p/(1+p), alpha=.5, facecolor=[\"#A60628\"])\n", |
291 |
| - "plt.scatter(0.2, 2 * (0.2) / 1.2, s=140, c=\"#348ABD\")\n", |
| 289 | + "plt.plot(p, 2*p/(1+p), color=\"#348ABD\", lw=3)\n", |
| 290 | + "#plt.fill_between(p, 2*p/(1+p), alpha=.5, facecolor=[\"#A60628\"])\n", |
| 291 | + "plt.scatter(0.2, 2*(0.2)/1.2, s=140, c=\"#348ABD\")\n", |
292 | 292 | "plt.xlim(0, 1)\n",
|
293 | 293 | "plt.ylim(0, 1)\n",
|
294 | 294 | "plt.xlabel(\"Prior, $P(A) = p$\")\n",
|
295 | 295 | "plt.ylabel(\"Posterior, $P(A|X)$, with $P(A) = p$\")\n",
|
296 |
| - "plt.title(\"Are there bugs in my code?\");" |
| 296 | + "plt.title(\"Are there bugs in my code?\")" |
297 | 297 | ],
|
298 | 298 | "language": "python",
|
299 | 299 | "metadata": {},
|
|
336 | 336 | "colours = [\"#348ABD\", \"#A60628\"]\n",
|
337 | 337 | "\n",
|
338 | 338 | "prior = [0.20, 0.80]\n",
|
339 |
| - "posterior = [1. / 3, 2. / 3]\n", |
| 339 | + "posterior = [1./3, 2./3]\n", |
340 | 340 | "plt.bar([0, .7], prior, alpha=0.70, width=0.25,\n",
|
341 | 341 | " color=colours[0], label=\"prior distribution\",\n",
|
342 | 342 | " lw=\"3\", edgecolor=colours[0])\n",
|
343 | 343 | "\n",
|
344 |
| - "plt.bar([0 + 0.25, .7 + 0.25], posterior, alpha=0.7,\n", |
| 344 | + "plt.bar([0+0.25, .7+0.25], posterior, alpha=0.7,\n", |
345 | 345 | " width=0.25, color=colours[1],\n",
|
346 | 346 | " label=\"posterior distribution\",\n",
|
347 | 347 | " lw=\"3\", edgecolor=colours[1])\n",
|
|
438 | 438 | "plt.ylabel(\"probability of $k$\")\n",
|
439 | 439 | "plt.xlabel(\"$k$\")\n",
|
440 | 440 | "plt.title(\"Probability mass function of a Poisson random variable; differing \\\n",
|
441 |
| - "$\\lambda$ values\");" |
| 441 | + "$\\lambda$ values\")" |
442 | 442 | ],
|
443 | 443 | "language": "python",
|
444 | 444 | "metadata": {},
|
|
491 | 491 | "lambda_ = [0.5, 1]\n",
|
492 | 492 | "\n",
|
493 | 493 | "for l, c in zip(lambda_, colours):\n",
|
494 |
| - " plt.plot(a, expo.pdf(a, scale=1. / l), lw=3,\n", |
| 494 | + " plt.plot(a, expo.pdf(a, scale=1./l), lw=3,\n", |
495 | 495 | " color=c, label=\"$\\lambda = %.1f$\" % l)\n",
|
496 |
| - " plt.fill_between(a, expo.pdf(a, scale=1. / l), color=c, alpha=.33)\n", |
| 496 | + " plt.fill_between(a, expo.pdf(a, scale=1./l), color=c, alpha=.33)\n", |
497 | 497 | "\n",
|
498 | 498 | "plt.legend()\n",
|
499 | 499 | "plt.ylabel(\"PDF at $z$\")\n",
|
500 | 500 | "plt.xlabel(\"$z$\")\n",
|
501 |
| - "plt.ylim(0, 1.2)\n", |
| 501 | + "plt.ylim(0,1.2)\n", |
502 | 502 | "plt.title(\"Probability density function of an Exponential random variable;\\\n",
|
503 | 503 | " differing $\\lambda$\");"
|
504 | 504 | ],
|
|
642 | 642 | "input": [
|
643 | 643 | "import pymc as pm\n",
|
644 | 644 | "\n",
|
645 |
| - "alpha = 1.0 / count_data.mean() # Recall count_data is the\n", |
| 645 | + "alpha = 1.0/count_data.mean() # Recall count_data is the\n", |
646 | 646 | " # variable that holds our txt counts\n",
|
647 | 647 | "lambda_1 = pm.Exponential(\"lambda_1\", alpha)\n",
|
648 | 648 | "lambda_2 = pm.Exponential(\"lambda_2\", alpha)\n",
|
649 | 649 | "\n",
|
650 |
| - "tau = pm.DiscreteUniform(\"tau\", lower=0, upper=n_count_data);" |
| 650 | + "tau = pm.DiscreteUniform(\"tau\", lower=0, upper=n_count_data)" |
651 | 651 | ],
|
652 | 652 | "language": "python",
|
653 | 653 | "metadata": {},
|
|
665 | 665 | "cell_type": "code",
|
666 | 666 | "collapsed": false,
|
667 | 667 | "input": [
|
668 |
| - "print \"Random output:\", tau.random(), tau.random(), tau.random();" |
| 668 | + "print \"Random output:\", tau.random(), tau.random(), tau.random()" |
669 | 669 | ],
|
670 | 670 | "language": "python",
|
671 | 671 | "metadata": {},
|
|
689 | 689 | " out = np.zeros(n_count_data)\n",
|
690 | 690 | " out[:tau] = lambda_1 # lambda before tau is lambda1\n",
|
691 | 691 | " out[tau:] = lambda_2 # lambda after (and including) tau is lambda2\n",
|
692 |
| - " return out;" |
| 692 | + " return out" |
693 | 693 | ],
|
694 | 694 | "language": "python",
|
695 | 695 | "metadata": {},
|
|
711 | 711 | "input": [
|
712 | 712 | "observation = pm.Poisson(\"obs\", lambda_, value=count_data, observed=True)\n",
|
713 | 713 | "\n",
|
714 |
| - "model = pm.Model([observation, lambda_1, lambda_2, tau]);" |
| 714 | + "model = pm.Model([observation, lambda_1, lambda_2, tau])" |
715 | 715 | ],
|
716 | 716 | "language": "python",
|
717 | 717 | "metadata": {},
|
|
731 | 731 | "cell_type": "code",
|
732 | 732 | "collapsed": false,
|
733 | 733 | "input": [
|
734 |
| - "# Mysterious code to be explained in Chapter 3.\n", |
| 734 | + "### Mysterious code to be explained in Chapter 3.\n", |
735 | 735 | "mcmc = pm.MCMC(model)\n",
|
736 |
| - "mcmc.sample(40000, 10000, 1);" |
| 736 | + "mcmc.sample(40000, 10000, 1)" |
737 | 737 | ],
|
738 | 738 | "language": "python",
|
739 | 739 | "metadata": {},
|
|
762 | 762 | "input": [
|
763 | 763 | "lambda_1_samples = mcmc.trace('lambda_1')[:]\n",
|
764 | 764 | "lambda_2_samples = mcmc.trace('lambda_2')[:]\n",
|
765 |
| - "tau_samples = mcmc.trace('tau')[:];" |
| 765 | + "tau_samples = mcmc.trace('tau')[:]" |
766 | 766 | ],
|
767 | 767 | "language": "python",
|
768 | 768 | "metadata": {},
|
|
774 | 774 | "collapsed": false,
|
775 | 775 | "input": [
|
776 | 776 | "figsize(12.5, 10)\n",
|
777 |
| - "# histogram of the samples:\n", |
| 777 | + "#histogram of the samples:\n", |
778 | 778 | "\n",
|
779 | 779 | "ax = plt.subplot(311)\n",
|
780 | 780 | "ax.set_autoscaley_on(False)\n",
|
|
804 | 804 | "\n",
|
805 | 805 | "plt.legend(loc=\"upper left\")\n",
|
806 | 806 | "plt.ylim([0, .75])\n",
|
807 |
| - "plt.xlim([35, len(count_data) - 20])\n", |
| 807 | + "plt.xlim([35, len(count_data)-20])\n", |
808 | 808 | "plt.xlabel(r\"$\\tau$ (in days)\")\n",
|
809 | 809 | "plt.ylabel(\"probability\");"
|
810 | 810 | ],
|
|
923 | 923 | "cell_type": "code",
|
924 | 924 | "collapsed": false,
|
925 | 925 | "input": [
|
926 |
| - "# type your code here.;" |
| 926 | + "#type your code here." |
927 | 927 | ],
|
928 | 928 | "language": "python",
|
929 | 929 | "metadata": {},
|
|
941 | 941 | "cell_type": "code",
|
942 | 942 | "collapsed": false,
|
943 | 943 | "input": [
|
944 |
| - "# type your code here.;" |
| 944 | + "#type your code here." |
945 | 945 | ],
|
946 | 946 | "language": "python",
|
947 | 947 | "metadata": {},
|
|
959 | 959 | "cell_type": "code",
|
960 | 960 | "collapsed": false,
|
961 | 961 | "input": [
|
962 |
| - "# type your code here.;" |
| 962 | + "#type your code here." |
963 | 963 | ],
|
964 | 964 | "language": "python",
|
965 | 965 | "metadata": {},
|
|
973 | 973 | "### References\n",
|
974 | 974 | "\n",
|
975 | 975 | "\n",
|
976 |
| - "- [1] Gelman, Andrew. N.p.. Web. 22 Jan 2013. [N is never large enough](http://andrewgelman.com/2005/07/n_is_never_large).\n", |
| 976 | + "- [1] Gelman, Andrew. N.p.. Web. 22 Jan 2013. [N is never large enough](http://andrewgelman.com/2005/07/31/n_is_never_larg/).\n", |
977 | 977 | "- [2] Norvig, Peter. 2009. [The Unreasonable Effectiveness of Data](http://static.googleusercontent.com/media/research.google.com/en//pubs/archive/35179.pdf).\n",
|
978 | 978 | "- [3] Patil, A., D. Huard and C.J. Fonnesbeck. 2010. \n",
|
979 | 979 | "PyMC: Bayesian Stochastic Modelling in Python. Journal of Statistical \n",
|
|
987 | 987 | "collapsed": false,
|
988 | 988 | "input": [
|
989 | 989 | "from IPython.core.display import HTML\n",
|
990 |
| - "\n", |
991 |
| - "\n", |
992 | 990 | "def css_styling():\n",
|
993 | 991 | " styles = open(\"../styles/custom.css\", \"r\").read()\n",
|
994 | 992 | " return HTML(styles)\n",
|
995 |
| - "css_styling();" |
| 993 | + "css_styling()" |
996 | 994 | ],
|
997 | 995 | "language": "python",
|
998 | 996 | "metadata": {},
|
|
1004 | 1002 | " font-family: \"Computer Modern\";\n",
|
1005 | 1003 | " src: url('http://9dbb143991406a7c655e-aa5fcb0a5a4ec34cff238a2d56ca4144.r56.cf5.rackcdn.com/cmunss.otf');\n",
|
1006 | 1004 | " }\n",
|
| 1005 | + " @font-face {\n", |
| 1006 | + " font-family: \"Computer Modern\";\n", |
| 1007 | + " font-weight: bold;\n", |
| 1008 | + " src: url('http://9dbb143991406a7c655e-aa5fcb0a5a4ec34cff238a2d56ca4144.r56.cf5.rackcdn.com/cmunsx.otf');\n", |
| 1009 | + " }\n", |
| 1010 | + " @font-face {\n", |
| 1011 | + " font-family: \"Computer Modern\";\n", |
| 1012 | + " font-style: oblique;\n", |
| 1013 | + " src: url('http://9dbb143991406a7c655e-aa5fcb0a5a4ec34cff238a2d56ca4144.r56.cf5.rackcdn.com/cmunsi.otf');\n", |
| 1014 | + " }\n", |
| 1015 | + " @font-face {\n", |
| 1016 | + " font-family: \"Computer Modern\";\n", |
| 1017 | + " font-weight: bold;\n", |
| 1018 | + " font-style: oblique;\n", |
| 1019 | + " src: url('http://9dbb143991406a7c655e-aa5fcb0a5a4ec34cff238a2d56ca4144.r56.cf5.rackcdn.com/cmunso.otf');\n", |
| 1020 | + " }\n", |
1007 | 1021 | " div.cell{\n",
|
1008 | 1022 | " width:800px;\n",
|
1009 | 1023 | " margin-left:16% !important;\n",
|
|
1043 | 1057 | " .warning{\n",
|
1044 | 1058 | " color: rgb( 240, 20, 20 )\n",
|
1045 | 1059 | " } \n",
|
1046 |
| - "</style>\n" |
| 1060 | + "</style>\n", |
| 1061 | + "<script>\n", |
| 1062 | + " MathJax.Hub.Config({\n", |
| 1063 | + " TeX: {\n", |
| 1064 | + " extensions: [\"AMSmath.js\"]\n", |
| 1065 | + " },\n", |
| 1066 | + " tex2jax: {\n", |
| 1067 | + " inlineMath: [ ['$','$'], [\"\\\\(\",\"\\\\)\"] ],\n", |
| 1068 | + " displayMath: [ ['$$','$$'], [\"\\\\[\",\"\\\\]\"] ]\n", |
| 1069 | + " },\n", |
| 1070 | + " displayAlign: 'center', // Change this to 'center' to center equations.\n", |
| 1071 | + " \"HTML-CSS\": {\n", |
| 1072 | + " styles: {'.MathJax_Display': {\"margin\": 4}}\n", |
| 1073 | + " }\n", |
| 1074 | + " });\n", |
| 1075 | + "</script>\n" |
1047 | 1076 | ],
|
1048 | 1077 | "metadata": {},
|
1049 | 1078 | "output_type": "pyout",
|
1050 | 1079 | "prompt_number": 1,
|
1051 | 1080 | "text": [
|
1052 |
| - "<IPython.core.display.HTML at 0x10f034850>" |
| 1081 | + "<IPython.core.display.HTML at 0x103dff550>" |
1053 | 1082 | ]
|
1054 | 1083 | }
|
1055 | 1084 | ],
|
|
0 commit comments