|
79 | 79 | "plt.ylim(0, 4)\n", |
80 | 80 | "leg = plt.legend(loc=\"upper left\")\n", |
81 | 81 | "leg.get_frame().set_alpha(0.4)\n", |
82 | | - "plt.title(\"Comparing objective vs. subjective priors for an unknown probability\")\n" |
| 82 | + "plt.title(\"Comparing objective vs. subjective priors for an unknown probability\");" |
83 | 83 | ], |
84 | 84 | "language": "python", |
85 | 85 | "metadata": {}, |
|
200 | 200 | " plt.fill_between(x, 0, y, alpha=0.2, color=lines[0].get_color())\n", |
201 | 201 | " plt.autoscale(tight=True)\n", |
202 | 202 | "\n", |
203 | | - "plt.legend(title=r\"$\\alpha, \\beta$ - parameters\")\n" |
| 203 | + "plt.legend(title=r\"$\\alpha, \\beta$ - parameters\");" |
204 | 204 | ], |
205 | 205 | "language": "python", |
206 | 206 | "metadata": {}, |
|
240 | 240 | " cmap=plt.cm.hot)\n", |
241 | 241 | " ax.axis(\"off\")\n", |
242 | 242 | "\n", |
243 | | - "plt.suptitle(\"Random matrices from a Wishart Distribution\")\n" |
| 243 | + "plt.suptitle(\"Random matrices from a Wishart Distribution\");" |
244 | 244 | ], |
245 | 245 | "language": "python", |
246 | 246 | "metadata": {}, |
|
292 | 292 | " plt.fill_between(x, 0, y, alpha=0.2, color=lines[0].get_color())\n", |
293 | 293 | " plt.autoscale(tight=True)\n", |
294 | 294 | "plt.ylim(0)\n", |
295 | | - "plt.legend(loc='upper left', title=\"(a,b)-parameters\")\n" |
| 295 | + "plt.legend(loc='upper left', title=\"(a,b)-parameters\");" |
296 | 296 | ], |
297 | 297 | "language": "python", |
298 | 298 | "metadata": {}, |
|
457 | 457 | "\n", |
458 | 458 | " self.bb_score = np.r_[self.bb_score, bb_score]\n", |
459 | 459 | " self.choices = np.r_[self.choices, choices]\n", |
460 | | - " return\n" |
| 460 | + " return;" |
461 | 461 | ], |
462 | 462 | "language": "python", |
463 | 463 | "metadata": {}, |
|
498 | 498 | " plt.title(\"Posteriors After %d pull\" % bayesian_strategy.N +\n", |
499 | 499 | " \"s\" * (bayesian_strategy.N > 1))\n", |
500 | 500 | " plt.autoscale(tight=True)\n", |
501 | | - " return\n" |
| 501 | + " return;" |
502 | 502 | ], |
503 | 503 | "language": "python", |
504 | 504 | "metadata": {}, |
|
521 | 521 | " plot_priors(bayesian_strat, hidden_prob)\n", |
522 | 522 | " # plt.legend()\n", |
523 | 523 | " plt.autoscale(tight=True)\n", |
524 | | - "plt.tight_layout()\n" |
| 524 | + "plt.tight_layout();" |
525 | 525 | ], |
526 | 526 | "language": "python", |
527 | 527 | "metadata": {}, |
|
555 | 555 | "from IPython.core.display import HTML\n", |
556 | 556 | "\n", |
557 | 557 | "# try executing the below command twice if the first time doesn't work\n", |
558 | | - "HTML(filename=\"BanditsD3.html\")\n" |
| 558 | + "HTML(filename=\"BanditsD3.html\");" |
559 | 559 | ], |
560 | 560 | "language": "python", |
561 | 561 | "metadata": {}, |
|
704 | 704 | "bandits = Bandits(hidden_prob)\n", |
705 | 705 | "\n", |
706 | 706 | "# define regret\n", |
| 707 | + "\n", |
| 708 | + "\n", |
707 | 709 | "def regret(probabilities, choices):\n", |
708 | 710 | " w_opt = probabilities.max()\n", |
709 | 711 | " return (w_opt - probabilities[choices.astype(int)]).cumsum()\n", |
|
716 | 718 | " random_choice]\n", |
717 | 719 | "algos = []\n", |
718 | 720 | "for strat in strategies:\n", |
719 | | - " algos.append(GeneralBanditStrat(bandits, strat))\n" |
| 721 | + " algos.append(GeneralBanditStrat(bandits, strat));" |
720 | 722 | ], |
721 | 723 | "language": "python", |
722 | 724 | "metadata": {}, |
|
739 | 741 | "plt.title(\"Total Regret of Bayesian Bandits Strategy vs. Random guessing\")\n", |
740 | 742 | "plt.xlabel(\"Number of pulls\")\n", |
741 | 743 | "plt.ylabel(\"Regret after $n$ pulls\")\n", |
742 | | - "plt.legend(loc=\"upper left\")\n" |
| 744 | + "plt.legend(loc=\"upper left\");" |
743 | 745 | ], |
744 | 746 | "language": "python", |
745 | 747 | "metadata": {}, |
|
793 | 795 | "plt.title(\"Expected Total Regret of Multi-armed Bandit strategies\")\n", |
794 | 796 | "plt.xlabel(\"Number of pulls\")\n", |
795 | 797 | "plt.ylabel(\"Exepected Total Regret \\n after $n$ pulls\")\n", |
796 | | - "plt.legend(loc=\"upper left\")\n" |
| 798 | + "plt.legend(loc=\"upper left\");" |
797 | 799 | ], |
798 | 800 | "language": "python", |
799 | 801 | "metadata": {}, |
|
847 | 849 | " loc=\"upper left\")\n", |
848 | 850 | "plt.ylabel(\"Exepected Total Regret \\n after $\\log{n}$ pulls\")\n", |
849 | 851 | "plt.title(\"log-scale of above\")\n", |
850 | | - "plt.ylabel(\"Exepected Total Regret \\n after $\\log{n}$ pulls\")\n" |
| 852 | + "plt.ylabel(\"Exepected Total Regret \\n after $\\log{n}$ pulls\");" |
851 | 853 | ], |
852 | 854 | "language": "python", |
853 | 855 | "metadata": {}, |
|
920 | 922 | " bayesian_strat.sample_bandits(i)\n", |
921 | 923 | " plot_priors(bayesian_strat, hidden_prob, lw=2, alpha=0.0, plt_vlines=False)\n", |
922 | 924 | " # plt.legend()\n", |
923 | | - " plt.xlim(0, 0.5)\n" |
| 925 | + " plt.xlim(0, 0.5);" |
924 | 926 | ], |
925 | 927 | "language": "python", |
926 | 928 | "metadata": {}, |
|
1038 | 1040 | " plt.title(name + \" prior\")\n", |
1039 | 1041 | " plt.vlines(0, 0, y.max(), \"k\", \"--\", linewidth=0.5)\n", |
1040 | 1042 | " plt.xlim(-0.15, 0.15)\n", |
1041 | | - "plt.tight_layout()\n" |
| 1043 | + "plt.tight_layout();" |
1042 | 1044 | ], |
1043 | 1045 | "language": "python", |
1044 | 1046 | "metadata": {}, |
|
1075 | 1077 | "prior_std = np.array([x[1] for x in expert_prior_params.values()])\n", |
1076 | 1078 | "\n", |
1077 | 1079 | "inv_cov_matrix = pm.Wishart(\"inv_cov_matrix\", n_observations, np.diag(prior_std ** 2))\n", |
1078 | | - "mu = pm.Normal(\"returns\", prior_mu, 1, size=4)\n" |
| 1080 | + "mu = pm.Normal(\"returns\", prior_mu, 1, size=4);" |
1079 | 1081 | ], |
1080 | 1082 | "language": "python", |
1081 | 1083 | "metadata": {}, |
|
1116 | 1118 | " _previous_day = np.roll(stock_closes[stock], -1)\n", |
1117 | 1119 | " stock_returns[stock] = ((stock_closes[stock] - _previous_day) / _previous_day)[:n_observations]\n", |
1118 | 1120 | "\n", |
1119 | | - "dates = map(lambda x: datetime.datetime.strptime(x, \"%Y-%m-%d\"), x[1:n_observations + 1, 0])\n" |
| 1121 | + "dates = map(lambda x: datetime.datetime.strptime(x, \"%Y-%m-%d\"), x[1:n_observations + 1, 0]);" |
1120 | 1122 | ], |
1121 | 1123 | "language": "python", |
1122 | 1124 | "metadata": {}, |
|
1131 | 1133 | "\n", |
1132 | 1134 | "for _stock, _returns in stock_returns.iteritems():\n", |
1133 | 1135 | " p = plt.plot((1 + _returns)[::-1].cumprod() - 1, '-o', label=\"%s\" % _stock,\n", |
1134 | | - " markersize=4, markeredgecolor=\"none\")\n", |
| 1136 | + " markersize=4, markeredgecolor=\"none\")\n", |
1135 | 1137 | "\n", |
1136 | 1138 | "plt.xticks(np.arange(100)[::-8],\n", |
1137 | 1139 | " map(lambda x: datetime.datetime.strftime(x, \"%Y-%m-%d\"), dates[::8]),\n", |
1138 | 1140 | " rotation=60)\n", |
1139 | 1141 | "\n", |
1140 | 1142 | "plt.legend(loc=\"upper left\")\n", |
1141 | 1143 | "plt.title(\"Return space\")\n", |
1142 | | - "plt.ylabel(\"Return of $1 on first date, x100%\")\n" |
| 1144 | + "plt.ylabel(\"Return of $1 on first date, x100%\");" |
1143 | 1145 | ], |
1144 | 1146 | "language": "python", |
1145 | 1147 | "metadata": {}, |
|
1172 | 1174 | " plt.xlim(-0.15, 0.15)\n", |
1173 | 1175 | "\n", |
1174 | 1176 | "plt.tight_layout()\n", |
1175 | | - "plt.suptitle(\"Histogram of daily returns\", size=14)\n" |
| 1177 | + "plt.suptitle(\"Histogram of daily returns\", size=14);" |
1176 | 1178 | ], |
1177 | 1179 | "language": "python", |
1178 | 1180 | "metadata": {}, |
|
1204 | 1206 | "model = pm.Model([obs, mu, inv_cov_matrix])\n", |
1205 | 1207 | "mcmc = pm.MCMC()\n", |
1206 | 1208 | "\n", |
1207 | | - "mcmc.sample(150000, 100000, 3)\n" |
| 1209 | + "mcmc.sample(150000, 100000, 3);" |
1208 | 1210 | ], |
1209 | 1211 | "language": "python", |
1210 | 1212 | "metadata": {}, |
|
1244 | 1246 | "plt.vlines(mu_samples.mean(axis=0), 0, 500, linestyle=\"--\", linewidth=.5)\n", |
1245 | 1247 | "\n", |
1246 | 1248 | "plt.title(\"Posterior distribution of $\\mu$, daily stock returns\")\n", |
1247 | | - "plt.legend()\n" |
| 1249 | + "plt.legend();" |
1248 | 1250 | ], |
1249 | 1251 | "language": "python", |
1250 | 1252 | "metadata": {}, |
|
1286 | 1288 | " plt.xlim(-0.15, 0.15)\n", |
1287 | 1289 | "\n", |
1288 | 1290 | "plt.suptitle(\"Posterior distribution of daily stock returns\")\n", |
1289 | | - "plt.tight_layout()\n" |
| 1291 | + "plt.tight_layout();" |
1290 | 1292 | ], |
1291 | 1293 | "language": "python", |
1292 | 1294 | "metadata": {}, |
|
1343 | 1345 | "plt.xticks(np.arange(4) + 0.5, stock_returns.keys())\n", |
1344 | 1346 | "plt.title(\"(mean posterior) variances of daily stock returns\")\n", |
1345 | 1347 | "\n", |
1346 | | - "plt.tight_layout()\n" |
| 1348 | + "plt.tight_layout();" |
1347 | 1349 | ], |
1348 | 1350 | "language": "python", |
1349 | 1351 | "metadata": {}, |
|
1435 | 1437 | "cell_type": "code", |
1436 | 1438 | "collapsed": false, |
1437 | 1439 | "input": [ |
1438 | | - "figsize( 12.5, 5 )\n", |
| 1440 | + "figsize(12.5, 5)\n", |
1439 | 1441 | "\n", |
1440 | | - "x = np.linspace( 0.000 ,1, 150 )\n", |
1441 | | - "y = np.linspace( 1.0, 1.0, 150 )\n", |
1442 | | - "lines = plt.plot( x, y, color=\"#A60628\", lw = 3 )\n", |
1443 | | - "plt.fill_between( x, 0, y, alpha = 0.2, color = lines[0].get_color() )\n", |
| 1442 | + "x = np.linspace(0.000, 1, 150)\n", |
| 1443 | + "y = np.linspace(1.0, 1.0, 150)\n", |
| 1444 | + "lines = plt.plot(x, y, color=\"#A60628\", lw=3)\n", |
| 1445 | + "plt.fill_between(x, 0, y, alpha=0.2, color=lines[0].get_color())\n", |
1444 | 1446 | "plt.autoscale(tight=True)\n", |
1445 | | - "plt.ylim( 0, 2);" |
| 1447 | + "plt.ylim(0, 2);" |
1446 | 1448 | ], |
1447 | 1449 | "language": "python", |
1448 | 1450 | "metadata": {}, |
|
1469 | 1471 | "cell_type": "code", |
1470 | 1472 | "collapsed": false, |
1471 | 1473 | "input": [ |
1472 | | - "figsize( 12.5, 5 )\n", |
| 1474 | + "figsize(12.5, 5)\n", |
1473 | 1475 | "\n", |
1474 | | - "psi = np.linspace( -10 ,10, 150 )\n", |
1475 | | - "y = np.exp(psi) / (1 + np.exp(psi))**2\n", |
1476 | | - "lines = plt.plot( psi, y, color=\"#A60628\", lw = 3 )\n", |
1477 | | - "plt.fill_between( psi, 0, y, alpha = 0.2, color = lines[0].get_color() )\n", |
| 1476 | + "psi = np.linspace(-10, 10, 150)\n", |
| 1477 | + "y = np.exp(psi) / (1 + np.exp(psi)) ** 2\n", |
| 1478 | + "lines = plt.plot(psi, y, color=\"#A60628\", lw=3)\n", |
| 1479 | + "plt.fill_between(psi, 0, y, alpha=0.2, color=lines[0].get_color())\n", |
1478 | 1480 | "plt.autoscale(tight=True)\n", |
1479 | | - "plt.ylim( 0, 1);" |
| 1481 | + "plt.ylim(0, 1);" |
1480 | 1482 | ], |
1481 | 1483 | "language": "python", |
1482 | 1484 | "metadata": {}, |
|
1560 | 1562 | " plt.fill_between(x, 0, y2, color=\"#A60628\", alpha=0.15)\n", |
1561 | 1563 | " plt.legend(title=\"N=%d\" % N)\n", |
1562 | 1564 | " plt.vlines(p, 0.0, 7.5, linestyles=\"--\", linewidth=1)\n", |
1563 | | - " #plt.ylim( 0, 10)#\n" |
| 1565 | + " #plt.ylim( 0, 10)#;" |
1564 | 1566 | ], |
1565 | 1567 | "language": "python", |
1566 | 1568 | "metadata": {}, |
|
1712 | 1714 | "def css_styling():\n", |
1713 | 1715 | " styles = open(\"../styles/custom.css\", \"r\").read()\n", |
1714 | 1716 | " return HTML(styles)\n", |
1715 | | - "css_styling()\n" |
| 1717 | + "css_styling();" |
1716 | 1718 | ], |
1717 | 1719 | "language": "python", |
1718 | 1720 | "metadata": {}, |
|
0 commit comments