|
107 | 107 | "\n",
|
108 | 108 | "plt.ylim(4.35, 4.65)\n",
|
109 | 109 | "plt.title(\"Convergence of the average of \\n random variables to its \\\n",
|
110 |
| - "expected value\" )\n", |
| 110 | + "expected value\")\n", |
111 | 111 | "plt.ylabel(\"average of $n$ samples\")\n",
|
112 | 112 | "plt.xlabel(\"# of samples, $n$\")\n",
|
113 | 113 | "plt.legend();"
|
|
156 | 156 | "figsize(12.5, 4)\n",
|
157 | 157 | "\n",
|
158 | 158 | "N_Y = 250 # use this many to approximate D(N)\n",
|
159 |
| - "# use this many samples in the approx. to the variance.\n", |
160 |
| - "N_array = np.arange(1000, 50000, 2500)\n", |
| 159 | + "N_array = np.arange(1000, 50000, 2500) # use this many samples in the approx. to the variance.\n", |
161 | 160 | "D_N_results = np.zeros(len(N_array))\n",
|
162 | 161 | "\n",
|
163 | 162 | "lambda_ = 4.5\n",
|
|
244 | 243 | "input": [
|
245 | 244 | "import pymc as pm\n",
|
246 | 245 | "N = 10000\n",
|
247 |
| - "print np.mean([pm.rexponential(0.5) > 10 for i in range(N)]);" |
| 246 | + "print np.mean([pm.rexponential(0.5) > 10 for i in range(N)])" |
248 | 247 | ],
|
249 | 248 | "language": "python",
|
250 | 249 | "metadata": {},
|
|
366 | 365 | "print population[np.argsort(average_across_county)[:10]]\n",
|
367 | 366 | "print\n",
|
368 | 367 | "print \"Population sizes of 10 'tallest' counties: \"\n",
|
369 |
| - "print population[np.argsort(-average_across_county)[:10]];" |
| 368 | + "print population[np.argsort(-average_across_county)[:10]]" |
370 | 369 | ],
|
371 | 370 | "language": "python",
|
372 | 371 | "metadata": {},
|
|
498 | 497 | "# adding a number to the end of the %run call with get the ith top photo.\n",
|
499 | 498 | "%run top_pic_comments.py 2\n",
|
500 | 499 | "\n",
|
501 |
| - "Image(top_post_url);" |
| 500 | + "Image(top_post_url)" |
502 | 501 | ],
|
503 | 502 | "language": "python",
|
504 | 503 | "metadata": {},
|
|
537 | 536 | "for i in comments:\n",
|
538 | 537 | " print '\"' + contents[i] + '\"'\n",
|
539 | 538 | " print\"upvotes/downvotes: \", votes[i, :]\n",
|
540 |
| - " print;" |
| 539 | + " print" |
541 | 540 | ],
|
542 | 541 | "language": "python",
|
543 | 542 | "metadata": {},
|
|
581 | 580 | "\n",
|
582 | 581 | "def posterior_upvote_ratio(upvotes, downvotes, samples=20000):\n",
|
583 | 582 | " \"\"\"\n",
|
584 |
| - " This function accepts the number of upvotes and downvotes a particular comment recieved,\n", |
| 583 | + " This function accepts the number of upvotes and downvotes a particular comment received, \n", |
585 | 584 | " and the number of posterior samples to return to the user. Assumes a uniform prior.\n",
|
586 | 585 | " \"\"\"\n",
|
587 | 586 | " N = upvotes + downvotes\n",
|
588 | 587 | " upvote_ratio = pm.Uniform(\"upvote_ratio\", 0, 1)\n",
|
589 |
| - " observations = pm.Binomial(\n", |
590 |
| - " \"obs\",\n", |
591 |
| - " N,\n", |
592 |
| - " upvote_ratio,\n", |
593 |
| - " value=upvotes,\n", |
594 |
| - " observed=True)\n", |
| 588 | + " observations = pm.Binomial(\"obs\", N, upvote_ratio, value=upvotes, observed=True)\n", |
595 | 589 | " # do the fitting; first do a MAP as it is cheap and useful.\n",
|
596 | 590 | " map_ = pm.MAP([upvote_ratio, observations]).fit()\n",
|
597 | 591 | " mcmc = pm.MCMC([upvote_ratio, observations])\n",
|
598 | 592 | " mcmc.sample(samples, samples / 4)\n",
|
599 |
| - " return mcmc.trace(\"upvote_ratio\")[:];" |
| 593 | + " return mcmc.trace(\"upvote_ratio\")[:]" |
600 | 594 | ],
|
601 | 595 | "language": "python",
|
602 | 596 | "metadata": {},
|
|
687 | 681 | " plt.hist(posteriors[i], bins=20, normed=True, alpha=.2,\n",
|
688 | 682 | " histtype=\"stepfilled\", color=colours[i], lw=3, )\n",
|
689 | 683 | " v = np.sort(posteriors[i])[int(0.05 * N)]\n",
|
690 |
| - " #plt.vlines(v, 0, 15 , color = \"k\", alpha = 1, linewidths=3 )\n", |
| 684 | + " # plt.vlines( v, 0, 15 , color = \"k\", alpha = 1, linewidths=3 )\n", |
691 | 685 | " plt.vlines(v, 0, 10, color=colours[i], linestyles=\"--\", linewidths=3)\n",
|
692 | 686 | " lower_limits.append(v)\n",
|
693 | 687 | " plt.legend(loc=\"upper left\")\n",
|
694 | 688 | "\n",
|
695 | 689 | "plt.legend(loc=\"upper left\")\n",
|
696 |
| - "plt.title(\"Posterior distributions of upvote ratios on different comments\")\n", |
| 690 | + "plt.title(\"Posterior distributions of upvote ratios on different comments\");\n", |
697 | 691 | "order = np.argsort(-np.array(lower_limits))\n",
|
698 |
| - "print order, lower_limits;" |
| 692 | + "print order, lower_limits" |
699 | 693 | ],
|
700 | 694 | "language": "python",
|
701 | 695 | "metadata": {},
|
|
767 | 761 | "for i in order[:40]:\n",
|
768 | 762 | " ordered_contents.append(contents[i])\n",
|
769 | 763 | " print votes[i, 0], votes[i, 1], contents[i]\n",
|
770 |
| - " print \"-------------\";" |
| 764 | + " print \"-------------\"" |
771 | 765 | ],
|
772 | 766 | "language": "python",
|
773 | 767 | "metadata": {},
|
|
905 | 899 | " xerr=std_err[r_order], xuplims=True, capsize=0, fmt=\"o\",\n",
|
906 | 900 | " color=\"#7A68A6\")\n",
|
907 | 901 | "plt.xlim(0.3, 1)\n",
|
908 |
| - "plt.yticks(np.arange(len(r_order) - 1, -1, -1),\n", |
909 |
| - " map(lambda x: x[:30].replace(\"\\n\", \"\"), ordered_contents));" |
| 902 | + "plt.yticks(np.arange(len(r_order) - 1, -1, -1), map(lambda x: x[:30].replace(\"\\n\", \"\"), ordered_contents));" |
910 | 903 | ],
|
911 | 904 | "language": "python",
|
912 | 905 | "metadata": {},
|
|
1020 | 1013 | "exp = stats.expon(scale=4)\n",
|
1021 | 1014 | "N = 1e5\n",
|
1022 | 1015 | "X = exp.rvs(N)\n",
|
1023 |
| - "# ...;" |
| 1016 | + "# ..." |
1024 | 1017 | ],
|
1025 | 1018 | "language": "python",
|
1026 | 1019 | "metadata": {},
|
|
1101 | 1094 | "def css_styling():\n",
|
1102 | 1095 | " styles = open(\"../styles/custom.css\", \"r\").read()\n",
|
1103 | 1096 | " return HTML(styles)\n",
|
1104 |
| - "css_styling();" |
| 1097 | + "css_styling()" |
1105 | 1098 | ],
|
1106 | 1099 | "language": "python",
|
1107 | 1100 | "metadata": {},
|
|
0 commit comments