|
11 | 11 | "cell_type": "markdown",
|
12 | 12 | "metadata": {},
|
13 | 13 | "source": [
|
14 |
| - "# Chapter 6\n", |
| 14 | + "#Chapter 6\n", |
15 | 15 | "\n",
|
16 | 16 | "____\n",
|
17 | 17 | "\n",
|
|
27 | 27 | "\n",
|
28 | 28 | "Up until now, we have mostly ignored our choice of priors. This is unfortunate as we can be very expressive with our priors, but we also must be careful about choosing them. This is especially true if we want to be objective, that is, not to express any personal beliefs in the priors. \n",
|
29 | 29 | "\n",
|
30 |
| - "### Subjective vs Objective priors\n", |
| 30 | + "###Subjective vs Objective priors\n", |
31 | 31 | "\n",
|
32 | 32 | "Bayesian priors can be classified into two classes: *objective* priors, which aim to allow the data to influence the posterior the most, and *subjective* priors, which allow the practitioner to express his or her views into the prior. \n",
|
33 | 33 | "\n",
|
|
926 | 926 | "figsize(12.0, 8)\n",
|
927 | 927 | "beta = stats.beta\n",
|
928 | 928 | "hidden_prob = beta.rvs(1, 13, size=35)\n",
|
929 |
| - "print(hidden_prob)\n", |
| 929 | + "print hidden_prob\n", |
930 | 930 | "bandits = Bandits(hidden_prob)\n",
|
931 | 931 | "bayesian_strat = BayesianStrategy(bandits)\n",
|
932 | 932 | "\n",
|
|
1042 | 1042 | " \"AMZN\": (0.03, 0.02),\n",
|
1043 | 1043 | " }\n",
|
1044 | 1044 | "\n",
|
1045 |
| - "for i, (name, params) in enumerate(expert_prior_params.items()):\n", |
1046 |
| - " plt.subplot(2, 2, i + 1)\n", |
| 1045 | + "for i, (name, params) in enumerate(expert_prior_params.iteritems()):\n", |
| 1046 | + " plt.subplot(2, 2, i)\n", |
1047 | 1047 | " y = normal.pdf(x, params[0], scale=params[1])\n",
|
1048 | 1048 | " #plt.plot( x, y, c = colors[i] )\n",
|
1049 | 1049 | " plt.fill_between(x, 0, y, color=colors[i], linewidth=2,\n",
|
|
1103 | 1103 | "\n",
|
1104 | 1104 | "stocks = [\"AAPL\", \"GOOG\", \"TSLA\", \"AMZN\"]\n",
|
1105 | 1105 | "\n",
|
1106 |
| - "enddate = \"2015-04-27\"\n", |
| 1106 | + "enddate = datetime.datetime.now().strftime(\"%Y-%m-%d\") # today's date.\n", |
1107 | 1107 | "startdate = \"2012-09-01\"\n",
|
1108 | 1108 | "\n",
|
1109 | 1109 | "stock_closes = {}\n",
|
|
1120 | 1120 | " _previous_day = np.roll(stock_closes[stock], -1)\n",
|
1121 | 1121 | " stock_returns[stock] = ((stock_closes[stock] - _previous_day) / _previous_day)[:n_observations]\n",
|
1122 | 1122 | "\n",
|
1123 |
| - "dates = list(map(lambda x: datetime.datetime.strptime(x, \"%Y-%m-%d\"), x[1:n_observations + 1, 0]))" |
| 1123 | + "dates = map(lambda x: datetime.datetime.strptime(x, \"%Y-%m-%d\"), x[1:n_observations + 1, 0])" |
1124 | 1124 | ]
|
1125 | 1125 | },
|
1126 | 1126 | {
|
|
1144 | 1144 | "source": [
|
1145 | 1145 | "figsize(12.5, 4)\n",
|
1146 | 1146 | "\n",
|
1147 |
| - "for _stock, _returns in stock_returns.items():\n", |
| 1147 | + "for _stock, _returns in stock_returns.iteritems():\n", |
1148 | 1148 | " p = plt.plot((1 + _returns)[::-1].cumprod() - 1, '-o', label=\"%s\" % _stock,\n",
|
1149 | 1149 | " markersize=4, markeredgecolor=\"none\")\n",
|
1150 | 1150 | "\n",
|
1151 | 1151 | "plt.xticks(np.arange(100)[::-8],\n",
|
1152 |
| - " list(map(lambda x: datetime.datetime.strftime(x, \"%Y-%m-%d\"), dates[::8])),\n", |
| 1152 | + " map(lambda x: datetime.datetime.strftime(x, \"%Y-%m-%d\"), dates[::8]),\n", |
1153 | 1153 | " rotation=60);\n",
|
1154 | 1154 | "\n",
|
1155 | 1155 | "plt.legend(loc=\"upper left\")\n",
|
|
1179 | 1179 | "figsize(11., 5)\n",
|
1180 | 1180 | "returns = np.zeros((n_observations, 4))\n",
|
1181 | 1181 | "\n",
|
1182 |
| - "for i, (_stock, _returns) in enumerate(stock_returns.items()):\n", |
| 1182 | + "for i, (_stock, _returns) in enumerate(stock_returns.iteritems()):\n", |
1183 | 1183 | " returns[:, i] = _returns\n",
|
1184 |
| - " plt.subplot(2, 2, i+1)\n", |
| 1184 | + " plt.subplot(2, 2, i)\n", |
1185 | 1185 | " plt.hist(_returns, bins=20,\n",
|
1186 | 1186 | " normed=True, histtype=\"stepfilled\",\n",
|
1187 | 1187 | " color=colors[i], alpha=0.7)\n",
|
|
1258 | 1258 | "for i in range(4):\n",
|
1259 | 1259 | " plt.hist(mu_samples[:, i], alpha=0.8 - 0.05 * i, bins=30,\n",
|
1260 | 1260 | " histtype=\"stepfilled\", normed=True,\n",
|
1261 |
| - " label=\"%s\" % list(stock_returns.keys())[i])\n", |
| 1261 | + " label=\"%s\" % stock_returns.keys()[i])\n", |
1262 | 1262 | "\n",
|
1263 | 1263 | "plt.vlines(mu_samples.mean(axis=0), 0, 500, linestyle=\"--\", linewidth=.5)\n",
|
1264 | 1264 | "\n",
|
|
1302 | 1302 | " plt.subplot(2, 2, i + 1)\n",
|
1303 | 1303 | " plt.hist(mu_samples[:, i], alpha=0.8 - 0.05 * i, bins=30,\n",
|
1304 | 1304 | " histtype=\"stepfilled\", normed=True, color=colors[i],\n",
|
1305 |
| - " label=\"%s\" % list(stock_returns.keys())[i])\n", |
1306 |
| - " plt.title(\"%s\" % list(stock_returns.keys())[i])\n", |
| 1305 | + " label=\"%s\" % stock_returns.keys()[i])\n", |
| 1306 | + " plt.title(\"%s\" % stock_returns.keys()[i])\n", |
1307 | 1307 | " plt.xlim(-0.15, 0.15)\n",
|
1308 | 1308 | "\n",
|
1309 | 1309 | "plt.suptitle(\"Posterior distribution of daily stock returns\")\n",
|
|
2512 | 2512 | "metadata": {
|
2513 | 2513 | "anaconda-cloud": {},
|
2514 | 2514 | "kernelspec": {
|
2515 |
| - "display_name": "Python [conda env:bayes]", |
| 2515 | + "display_name": "Python 2", |
2516 | 2516 | "language": "python",
|
2517 |
| - "name": "conda-env-bayes-py" |
| 2517 | + "name": "python2" |
2518 | 2518 | },
|
2519 | 2519 | "language_info": {
|
2520 | 2520 | "codemirror_mode": {
|
2521 | 2521 | "name": "ipython",
|
2522 |
| - "version": 3 |
| 2522 | + "version": 2 |
2523 | 2523 | },
|
2524 | 2524 | "file_extension": ".py",
|
2525 | 2525 | "mimetype": "text/x-python",
|
2526 | 2526 | "name": "python",
|
2527 | 2527 | "nbconvert_exporter": "python",
|
2528 |
| - "pygments_lexer": "ipython3", |
2529 |
| - "version": "3.5.2" |
| 2528 | + "pygments_lexer": "ipython2", |
| 2529 | + "version": "2.7.10" |
2530 | 2530 | }
|
2531 | 2531 | },
|
2532 | 2532 | "nbformat": 4,
|
|
0 commit comments