Skip to content

Commit 8039f48

Browse files
satrio-hwsatrio
andauthored
CLN: NB black format (#4151)
Co-authored-by: satrio <[email protected]>
1 parent bc44cb1 commit 8039f48

10 files changed

+385
-320
lines changed

docs/source/notebooks/GLM-robust.ipynb

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@
6767
"# y = a + b*x\n",
6868
"true_regression_line = true_intercept + true_slope * x\n",
6969
"# add noise\n",
70-
"y = true_regression_line + np.random.normal(scale=.5, size=size)\n",
70+
"y = true_regression_line + np.random.normal(scale=0.5, size=size)\n",
7171
"\n",
7272
"# Add outliers\n",
73-
"x_out = np.append(x, [.1, .15, .2])\n",
73+
"x_out = np.append(x, [0.1, 0.15, 0.2])\n",
7474
"y_out = np.append(y, [8, 6, 9])\n",
7575
"\n",
7676
"data = dict(x=x_out, y=y_out)"
@@ -103,9 +103,9 @@
103103
],
104104
"source": [
105105
"fig = plt.figure(figsize=(7, 7))\n",
106-
"ax = fig.add_subplot(111, xlabel='x', ylabel='y', title='Generated data and underlying model')\n",
107-
"ax.plot(x_out, y_out, 'x', label='sampled data')\n",
108-
"ax.plot(x, true_regression_line, label='true regression line', lw=2.)\n",
106+
"ax = fig.add_subplot(111, xlabel=\"x\", ylabel=\"y\", title=\"Generated data and underlying model\")\n",
107+
"ax.plot(x_out, y_out, \"x\", label=\"sampled data\")\n",
108+
"ax.plot(x, true_regression_line, label=\"true regression line\", lw=2.0)\n",
109109
"plt.legend(loc=0);"
110110
]
111111
},
@@ -174,7 +174,7 @@
174174
],
175175
"source": [
176176
"with pm.Model() as model:\n",
177-
" pm.glm.GLM.from_formula('y ~ x', data)\n",
177+
" pm.glm.GLM.from_formula(\"y ~ x\", data)\n",
178178
" trace = pm.sample(2000, cores=2)"
179179
]
180180
},
@@ -205,11 +205,9 @@
205205
],
206206
"source": [
207207
"plt.figure(figsize=(7, 5))\n",
208-
"plt.plot(x_out, y_out, 'x', label='data')\n",
209-
"pm.plot_posterior_predictive_glm(trace, samples=100, \n",
210-
" label='posterior predictive regression lines')\n",
211-
"plt.plot(x, true_regression_line, \n",
212-
" label='true regression line', lw=3., c='y')\n",
208+
"plt.plot(x_out, y_out, \"x\", label=\"data\")\n",
209+
"pm.plot_posterior_predictive_glm(trace, samples=100, label=\"posterior predictive regression lines\")\n",
210+
"plt.plot(x, true_regression_line, label=\"true regression line\", lw=3.0, c=\"y\")\n",
213211
"\n",
214212
"plt.legend(loc=0);"
215213
]
@@ -249,10 +247,10 @@
249247
"normal_dist = pm.Normal.dist(mu=0, sigma=1)\n",
250248
"t_dist = pm.StudentT.dist(mu=0, lam=1, nu=1)\n",
251249
"x_eval = np.linspace(-8, 8, 300)\n",
252-
"plt.plot(x_eval, theano.tensor.exp(normal_dist.logp(x_eval)).eval(), label='Normal', lw=2.)\n",
253-
"plt.plot(x_eval, theano.tensor.exp(t_dist.logp(x_eval)).eval(), label='Student T', lw=2.)\n",
254-
"plt.xlabel('x')\n",
255-
"plt.ylabel('Probability density')\n",
250+
"plt.plot(x_eval, theano.tensor.exp(normal_dist.logp(x_eval)).eval(), label=\"Normal\", lw=2.0)\n",
251+
"plt.plot(x_eval, theano.tensor.exp(t_dist.logp(x_eval)).eval(), label=\"Student T\", lw=2.0)\n",
252+
"plt.xlabel(\"x\")\n",
253+
"plt.ylabel(\"Probability density\")\n",
256254
"plt.legend();"
257255
]
258256
},
@@ -332,15 +330,13 @@
332330
"source": [
333331
"with pm.Model() as model_robust:\n",
334332
" family = pm.glm.families.StudentT()\n",
335-
" pm.glm.GLM.from_formula('y ~ x', data, family=family)\n",
333+
" pm.glm.GLM.from_formula(\"y ~ x\", data, family=family)\n",
336334
" trace_robust = pm.sample(2000, cores=2)\n",
337335
"\n",
338336
"plt.figure(figsize=(7, 5))\n",
339-
"plt.plot(x_out, y_out, 'x')\n",
340-
"pm.plot_posterior_predictive_glm(trace_robust,\n",
341-
" label='posterior predictive regression lines')\n",
342-
"plt.plot(x, true_regression_line, \n",
343-
" label='true regression line', lw=3., c='y')\n",
337+
"plt.plot(x_out, y_out, \"x\")\n",
338+
"pm.plot_posterior_predictive_glm(trace_robust, label=\"posterior predictive regression lines\")\n",
339+
"plt.plot(x, true_regression_line, label=\"true regression line\", lw=3.0, c=\"y\")\n",
344340
"plt.legend();"
345341
]
346342
},

docs/source/notebooks/GLM-rolling-regression.ipynb

Lines changed: 50 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@
115115
"# from pandas_datareader import data\n",
116116
"# prices = data.GoogleDailyReader(symbols=['GLD', 'GFI'], end='2014-8-1').read().loc['Open', :, :]\n",
117117
"\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",
121121
"prices_zscored = (prices - prices.mean()) / prices.std()\n",
122122
"prices.head()"
123123
]
@@ -149,12 +149,12 @@
149149
],
150150
"source": [
151151
"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",
153153
"colors = np.linspace(0.1, 1, len(prices))\n",
154154
"mymap = plt.get_cmap(\"winter\")\n",
155155
"sc = ax.scatter(prices.GFI, prices.GLD, c=colors, cmap=mymap, lw=0)\n",
156156
"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]);"
158158
]
159159
},
160160
{
@@ -220,7 +220,7 @@
220220
],
221221
"source": [
222222
"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",
224224
" trace_reg = pm.sample(2000, tune=1000)"
225225
]
226226
},
@@ -251,15 +251,22 @@
251251
],
252252
"source": [
253253
"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",
256260
"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",
261268
"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",
263270
"ax.legend(loc=0);"
264271
]
265272
},
@@ -291,13 +298,11 @@
291298
"model_randomwalk = pm.Model()\n",
292299
"with model_randomwalk:\n",
293300
" # 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))"
301306
]
302307
},
303308
{
@@ -316,13 +321,10 @@
316321
"with model_randomwalk:\n",
317322
" # Define regression\n",
318323
" regression = alpha + beta * prices_zscored.GFI\n",
319-
" \n",
324+
"\n",
320325
" # 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)"
326328
]
327329
},
328330
{
@@ -394,8 +396,7 @@
394396
],
395397
"source": [
396398
"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)"
399400
]
400401
},
401402
{
@@ -439,9 +440,9 @@
439440
],
440441
"source": [
441442
"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]);"
445446
]
446447
},
447448
{
@@ -471,9 +472,9 @@
471472
],
472473
"source": [
473474
"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]);"
477478
]
478479
},
479480
{
@@ -503,26 +504,27 @@
503504
],
504505
"source": [
505506
"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",
508513
"\n",
509514
"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",
513518
"\n",
514519
"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",
517521
" 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",
523525
"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));"
526528
]
527529
},
528530
{

0 commit comments

Comments
 (0)