|
115 | 115 | "# from pandas_datareader import data\n",
|
116 | 116 | "# prices = data.GoogleDailyReader(symbols=['GLD', 'GFI'], end='2014-8-1').read().loc['Open', :, :]\n",
|
117 | 117 | "\n",
|
118 |
| - "prices = pd.read_csv(pm.get_data('stock_prices.csv')).dropna()\n", |
119 |
| - "prices['Date'] = pd.DatetimeIndex(prices['Date'])\n", |
120 |
| - "prices = prices.set_index('Date')\n", |
| 118 | + "prices = pd.read_csv(pm.get_data(\"stock_prices.csv\")).dropna()\n", |
| 119 | + "prices[\"Date\"] = pd.DatetimeIndex(prices[\"Date\"])\n", |
| 120 | + "prices = prices.set_index(\"Date\")\n", |
121 | 121 | "prices_zscored = (prices - prices.mean()) / prices.std()\n",
|
122 | 122 | "prices.head()"
|
123 | 123 | ]
|
|
149 | 149 | ],
|
150 | 150 | "source": [
|
151 | 151 | "fig = plt.figure(figsize=(9, 6))\n",
|
152 |
| - "ax = fig.add_subplot(111, xlabel=r'Price GFI in \\$', ylabel=r'Price GLD in \\$')\n", |
| 152 | + "ax = fig.add_subplot(111, xlabel=r\"Price GFI in \\$\", ylabel=r\"Price GLD in \\$\")\n", |
153 | 153 | "colors = np.linspace(0.1, 1, len(prices))\n",
|
154 | 154 | "mymap = plt.get_cmap(\"winter\")\n",
|
155 | 155 | "sc = ax.scatter(prices.GFI, prices.GLD, c=colors, cmap=mymap, lw=0)\n",
|
156 | 156 | "cb = plt.colorbar(sc)\n",
|
157 |
| - "cb.ax.set_yticklabels([str(p.date()) for p in prices[::len(prices)//10].index]);" |
| 157 | + "cb.ax.set_yticklabels([str(p.date()) for p in prices[:: len(prices) // 10].index]);" |
158 | 158 | ]
|
159 | 159 | },
|
160 | 160 | {
|
|
220 | 220 | ],
|
221 | 221 | "source": [
|
222 | 222 | "with pm.Model() as model_reg:\n",
|
223 |
| - " pm.glm.GLM.from_formula('GLD ~ GFI', prices)\n", |
| 223 | + " pm.glm.GLM.from_formula(\"GLD ~ GFI\", prices)\n", |
224 | 224 | " trace_reg = pm.sample(2000, tune=1000)"
|
225 | 225 | ]
|
226 | 226 | },
|
|
251 | 251 | ],
|
252 | 252 | "source": [
|
253 | 253 | "fig = plt.figure(figsize=(9, 6))\n",
|
254 |
| - "ax = fig.add_subplot(111, xlabel=r'Price GFI in \\$', ylabel=r'Price GLD in \\$', \n", |
255 |
| - " title='Posterior predictive regression lines')\n", |
| 254 | + "ax = fig.add_subplot(\n", |
| 255 | + " 111,\n", |
| 256 | + " xlabel=r\"Price GFI in \\$\",\n", |
| 257 | + " ylabel=r\"Price GLD in \\$\",\n", |
| 258 | + " title=\"Posterior predictive regression lines\",\n", |
| 259 | + ")\n", |
256 | 260 | "sc = ax.scatter(prices.GFI, prices.GLD, c=colors, cmap=mymap, lw=0)\n",
|
257 |
| - "pm.plot_posterior_predictive_glm(trace_reg[100:], samples=100, \n", |
258 |
| - " label='posterior predictive regression lines',\n", |
259 |
| - " lm=lambda x, sample: sample['Intercept'] + sample['GFI'] * x,\n", |
260 |
| - " eval=np.linspace(prices.GFI.min(), prices.GFI.max(), 100))\n", |
| 261 | + "pm.plot_posterior_predictive_glm(\n", |
| 262 | + " trace_reg[100:],\n", |
| 263 | + " samples=100,\n", |
| 264 | + " label=\"posterior predictive regression lines\",\n", |
| 265 | + " lm=lambda x, sample: sample[\"Intercept\"] + sample[\"GFI\"] * x,\n", |
| 266 | + " eval=np.linspace(prices.GFI.min(), prices.GFI.max(), 100),\n", |
| 267 | + ")\n", |
261 | 268 | "cb = plt.colorbar(sc)\n",
|
262 |
| - "cb.ax.set_yticklabels([str(p.date()) for p in prices[::len(prices)//10].index]);\n", |
| 269 | + "cb.ax.set_yticklabels([str(p.date()) for p in prices[:: len(prices) // 10].index])\n", |
263 | 270 | "ax.legend(loc=0);"
|
264 | 271 | ]
|
265 | 272 | },
|
|
291 | 298 | "model_randomwalk = pm.Model()\n",
|
292 | 299 | "with model_randomwalk:\n",
|
293 | 300 | " # std of random walk\n",
|
294 |
| - " sigma_alpha = pm.Exponential('sigma_alpha', 50.)\n", |
295 |
| - " sigma_beta = pm.Exponential('sigma_beta', 50.)\n", |
296 |
| - " \n", |
297 |
| - " alpha = pm.GaussianRandomWalk('alpha', sigma=sigma_alpha, \n", |
298 |
| - " shape=len(prices))\n", |
299 |
| - " beta = pm.GaussianRandomWalk('beta', sigma=sigma_beta, \n", |
300 |
| - " shape=len(prices))" |
| 301 | + " sigma_alpha = pm.Exponential(\"sigma_alpha\", 50.0)\n", |
| 302 | + " sigma_beta = pm.Exponential(\"sigma_beta\", 50.0)\n", |
| 303 | + "\n", |
| 304 | + " alpha = pm.GaussianRandomWalk(\"alpha\", sigma=sigma_alpha, shape=len(prices))\n", |
| 305 | + " beta = pm.GaussianRandomWalk(\"beta\", sigma=sigma_beta, shape=len(prices))" |
301 | 306 | ]
|
302 | 307 | },
|
303 | 308 | {
|
|
316 | 321 | "with model_randomwalk:\n",
|
317 | 322 | " # Define regression\n",
|
318 | 323 | " regression = alpha + beta * prices_zscored.GFI\n",
|
319 |
| - " \n", |
| 324 | + "\n", |
320 | 325 | " # Assume prices are Normally distributed, the mean comes from the regression.\n",
|
321 |
| - " sd = pm.HalfNormal('sd', sigma=.1)\n", |
322 |
| - " likelihood = pm.Normal('y', \n", |
323 |
| - " mu=regression, \n", |
324 |
| - " sigma=sd, \n", |
325 |
| - " observed=prices_zscored.GLD)" |
| 326 | + " sd = pm.HalfNormal(\"sd\", sigma=0.1)\n", |
| 327 | + " likelihood = pm.Normal(\"y\", mu=regression, sigma=sd, observed=prices_zscored.GLD)" |
326 | 328 | ]
|
327 | 329 | },
|
328 | 330 | {
|
|
394 | 396 | ],
|
395 | 397 | "source": [
|
396 | 398 | "with model_randomwalk:\n",
|
397 |
| - " trace_rw = pm.sample(tune=2000, cores=4, \n", |
398 |
| - " target_accept=0.9)" |
| 399 | + " trace_rw = pm.sample(tune=2000, cores=4, target_accept=0.9)" |
399 | 400 | ]
|
400 | 401 | },
|
401 | 402 | {
|
|
439 | 440 | ],
|
440 | 441 | "source": [
|
441 | 442 | "fig = plt.figure(figsize=(8, 6))\n",
|
442 |
| - "ax = plt.subplot(111, xlabel='time', ylabel='alpha', title='Change of alpha over time.')\n", |
443 |
| - "ax.plot(trace_rw['alpha'].T, 'r', alpha=.05);\n", |
444 |
| - "ax.set_xticklabels([str(p.date()) for p in prices[::len(prices)//5].index]);" |
| 443 | + "ax = plt.subplot(111, xlabel=\"time\", ylabel=\"alpha\", title=\"Change of alpha over time.\")\n", |
| 444 | + "ax.plot(trace_rw[\"alpha\"].T, \"r\", alpha=0.05)\n", |
| 445 | + "ax.set_xticklabels([str(p.date()) for p in prices[:: len(prices) // 5].index]);" |
445 | 446 | ]
|
446 | 447 | },
|
447 | 448 | {
|
|
471 | 472 | ],
|
472 | 473 | "source": [
|
473 | 474 | "fig = plt.figure(figsize=(8, 6))\n",
|
474 |
| - "ax = fig.add_subplot(111, xlabel='time', ylabel='beta', title='Change of beta over time')\n", |
475 |
| - "ax.plot(trace_rw['beta'].T, 'b', alpha=.05);\n", |
476 |
| - "ax.set_xticklabels([str(p.date()) for p in prices[::len(prices)//5].index]);" |
| 475 | + "ax = fig.add_subplot(111, xlabel=\"time\", ylabel=\"beta\", title=\"Change of beta over time\")\n", |
| 476 | + "ax.plot(trace_rw[\"beta\"].T, \"b\", alpha=0.05)\n", |
| 477 | + "ax.set_xticklabels([str(p.date()) for p in prices[:: len(prices) // 5].index]);" |
477 | 478 | ]
|
478 | 479 | },
|
479 | 480 | {
|
|
503 | 504 | ],
|
504 | 505 | "source": [
|
505 | 506 | "fig = plt.figure(figsize=(8, 6))\n",
|
506 |
| - "ax = fig.add_subplot(111, xlabel=r'Price GFI in \\$', ylabel=r'Price GLD in \\$', \n", |
507 |
| - " title='Posterior predictive regression lines')\n", |
| 507 | + "ax = fig.add_subplot(\n", |
| 508 | + " 111,\n", |
| 509 | + " xlabel=r\"Price GFI in \\$\",\n", |
| 510 | + " ylabel=r\"Price GLD in \\$\",\n", |
| 511 | + " title=\"Posterior predictive regression lines\",\n", |
| 512 | + ")\n", |
508 | 513 | "\n",
|
509 | 514 | "colors = np.linspace(0.1, 1, len(prices))\n",
|
510 |
| - "colors_sc = np.linspace(0.1, 1, len(trace_rw[::10]['alpha'].T))\n", |
511 |
| - "mymap = plt.get_cmap('winter')\n", |
512 |
| - "mymap_sc = plt.get_cmap('winter')\n", |
| 515 | + "colors_sc = np.linspace(0.1, 1, len(trace_rw[::10][\"alpha\"].T))\n", |
| 516 | + "mymap = plt.get_cmap(\"winter\")\n", |
| 517 | + "mymap_sc = plt.get_cmap(\"winter\")\n", |
513 | 518 | "\n",
|
514 | 519 | "xi = np.linspace(prices_zscored.GFI.min(), prices_zscored.GFI.max(), 50)\n",
|
515 |
| - "for i, (alpha, beta) in enumerate(zip(trace_rw[::15]['alpha'].T, \n", |
516 |
| - " trace_rw[::15]['beta'].T)):\n", |
| 520 | + "for i, (alpha, beta) in enumerate(zip(trace_rw[::15][\"alpha\"].T, trace_rw[::15][\"beta\"].T)):\n", |
517 | 521 | " for a, b in zip(alpha[::30], beta[::30]):\n",
|
518 |
| - " ax.plot(xi, a + b*xi, alpha=.01, lw=1, \n", |
519 |
| - " c=mymap_sc(colors_sc[i]))\n", |
520 |
| - " \n", |
521 |
| - "sc = ax.scatter(prices_zscored.GFI, prices_zscored.GLD, \n", |
522 |
| - " label='data', cmap=mymap, c=colors)\n", |
| 522 | + " ax.plot(xi, a + b * xi, alpha=0.01, lw=1, c=mymap_sc(colors_sc[i]))\n", |
| 523 | + "\n", |
| 524 | + "sc = ax.scatter(prices_zscored.GFI, prices_zscored.GLD, label=\"data\", cmap=mymap, c=colors)\n", |
523 | 525 | "cb = plt.colorbar(sc)\n",
|
524 |
| - "cb.ax.set_yticklabels([str(p.date()) for p in prices_zscored[::len(prices)//10].index]);\n", |
525 |
| - "#ax.set(ylim=(100, 190));" |
| 526 | + "cb.ax.set_yticklabels([str(p.date()) for p in prices_zscored[:: len(prices) // 10].index])\n", |
| 527 | + "# ax.set(ylim=(100, 190));" |
526 | 528 | ]
|
527 | 529 | },
|
528 | 530 | {
|
|
0 commit comments