Skip to content

Commit 7a52759

Browse files
raisadzRaisa Dzhamtyrova
andauthored
CLN: notebooks black formatting (#4152)
Co-authored-by: Raisa Dzhamtyrova <[email protected]>
1 parent 8039f48 commit 7a52759

8 files changed

+187
-262
lines changed

docs/source/notebooks/GLM-robust-with-outlier-detection.ipynb

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,7 @@
467467
"metadata": {},
468468
"outputs": [],
469469
"source": [
470-
"dfhoggs = (dfhogg[[\"x\", \"y\"]] - dfhogg[[\"x\", \"y\"]].mean(0)) / (\n",
471-
" 2 * dfhogg[[\"x\", \"y\"]].std(0)\n",
472-
")\n",
470+
"dfhoggs = (dfhogg[[\"x\", \"y\"]] - dfhogg[[\"x\", \"y\"]].mean(0)) / (2 * dfhogg[[\"x\", \"y\"]].std(0))\n",
473471
"dfhoggs[\"sigma_x\"] = dfhogg[\"sigma_x\"] / (2 * dfhogg[\"x\"].std())\n",
474472
"dfhoggs[\"sigma_y\"] = dfhogg[\"sigma_y\"] / (2 * dfhogg[\"y\"].std())"
475473
]
@@ -505,9 +503,7 @@
505503
" marginal_kws={\"bins\": 12, \"kde\": True, \"kde_kws\": {\"cut\": 1}},\n",
506504
" joint_kws={\"edgecolor\": \"w\", \"linewidth\": 1, \"s\": 80},\n",
507505
")\n",
508-
"_ = gd.ax_joint.errorbar(\n",
509-
" \"x\", \"y\", \"sigma_y\", \"sigma_x\", fmt=\"none\", ecolor=\"#4878d0\", data=dfhoggs\n",
510-
")\n",
506+
"_ = gd.ax_joint.errorbar(\"x\", \"y\", \"sigma_y\", \"sigma_x\", fmt=\"none\", ecolor=\"#4878d0\", data=dfhoggs)\n",
511507
"_ = gd.fig.suptitle(\n",
512508
" (\n",
513509
" \"Quick view to confirm action of\\n\"\n",
@@ -639,9 +635,7 @@
639635
" y_est = b0 + b1 * dfhoggs[\"x\"]\n",
640636
"\n",
641637
" ## Define Normal likelihood\n",
642-
" likelihood = pm.Normal(\n",
643-
" \"likelihood\", mu=y_est, sigma=dfhoggs[\"sigma_y\"], observed=dfhoggs[\"y\"]\n",
644-
" )\n",
638+
" likelihood = pm.Normal(\"likelihood\", mu=y_est, sigma=dfhoggs[\"sigma_y\"], observed=dfhoggs[\"y\"])\n",
645639
"\n",
646640
"pm.model_to_graphviz(mdl_ols)"
647641
]
@@ -1274,10 +1268,7 @@
12741268
"\n",
12751269
"gd = sns.JointGrid(x=\"b0_intercept\", y=\"b1_slope\", data=df_trc, height=8)\n",
12761270
"_ = gd.fig.suptitle(\n",
1277-
" (\n",
1278-
" \"Posterior joint distributions\"\n",
1279-
" + \"\\n(showing general movement from OLS to StudentT)\"\n",
1280-
" ),\n",
1271+
" (\"Posterior joint distributions\" + \"\\n(showing general movement from OLS to StudentT)\"),\n",
12811272
" y=1.05,\n",
12821273
")\n",
12831274
"\n",
@@ -1297,11 +1288,9 @@
12971288
" grp[\"b0_intercept\"], grp[\"b1_slope\"], ax=gd.ax_joint, alpha=0.2, label=idx\n",
12981289
" )\n",
12991290
" _ = sns.kdeplot(grp[\"b0_intercept\"], grp[\"b1_slope\"], ax=gd.ax_joint, **kde_kws)\n",
1291+
" _ = sns.distplot(grp[\"b0_intercept\"], bins=x_bin_edges, ax=gd.ax_marg_x, **dist_kws)\n",
13001292
" _ = sns.distplot(\n",
1301-
" grp[\"b0_intercept\"], bins=x_bin_edges, ax=gd.ax_marg_x, **dist_kws\n",
1302-
" )\n",
1303-
" _ = sns.distplot(grp[\"b1_slope\"], vertical=True, bins=y_bin_edges,\n",
1304-
" ax=gd.ax_marg_y, **dist_kws\n",
1293+
" grp[\"b1_slope\"], vertical=True, bins=y_bin_edges, ax=gd.ax_marg_y, **dist_kws\n",
13051294
" )\n",
13061295
"_ = gd.ax_joint.legend()"
13071296
]
@@ -1419,18 +1408,14 @@
14191408
" y_est_out = pm.Normal(\"y_est_out\", mu=0, sigma=10, testval=pm.floatX(0.0)) # (1, )\n",
14201409
"\n",
14211410
" # very weakly informative prior for additional variance for outliers\n",
1422-
" sigma_y_out = pm.HalfNormal(\n",
1423-
" \"sigma_y_out\", sigma=10, testval=pm.floatX(1.0)\n",
1424-
" ) # (1, )\n",
1411+
" sigma_y_out = pm.HalfNormal(\"sigma_y_out\", sigma=10, testval=pm.floatX(1.0)) # (1, )\n",
14251412
"\n",
14261413
" # create in/outlier distributions to get a logp evaluated on the observed y\n",
14271414
" # this is not strictly a pymc3 likelihood, but behaves like one when we\n",
14281415
" # evaluate it within a Potential (which is minimised)\n",
14291416
" inlier_logp = pm.Normal.dist(mu=y_est_in, sigma=tsv_sigma_y).logp(tsv_y)\n",
14301417
"\n",
1431-
" outlier_logp = pm.Normal.dist(mu=y_est_out, sigma=tsv_sigma_y + sigma_y_out).logp(\n",
1432-
" tsv_y\n",
1433-
" )\n",
1418+
" outlier_logp = pm.Normal.dist(mu=y_est_out, sigma=tsv_sigma_y + sigma_y_out).logp(tsv_y)\n",
14341419
"\n",
14351420
" # frac_outliers only needs to span [0, .5]\n",
14361421
" # testval for is_outlier initialised in order to create class asymmetry\n",
@@ -1860,14 +1845,10 @@
18601845
"dist_kws = dict(kde_kws=dict(cut=1), axlabel=False)\n",
18611846
"\n",
18621847
"for idx, grp in df_trc.groupby(\"model\"):\n",
1863-
" _ = sns.scatterplot(\n",
1864-
" grp[\"b0_intercept\"], grp[\"b1_slope\"], ax=gd.ax_joint, alpha=0.2, label=idx\n",
1865-
" )\n",
1848+
" _ = sns.scatterplot(grp[\"b0_intercept\"], grp[\"b1_slope\"], ax=gd.ax_joint, alpha=0.2, label=idx)\n",
18661849
" _ = sns.kdeplot(grp[\"b0_intercept\"], grp[\"b1_slope\"], ax=gd.ax_joint, **kde_kws)\n",
18671850
" _ = sns.distplot(grp[\"b0_intercept\"], **dist_kws, bins=x_bin_edges, ax=gd.ax_marg_x)\n",
1868-
" _ = sns.distplot(\n",
1869-
" grp[\"b1_slope\"], **dist_kws, vertical=True, bins=y_bin_edges, ax=gd.ax_marg_y\n",
1870-
" )\n",
1851+
" _ = sns.distplot(grp[\"b1_slope\"], **dist_kws, vertical=True, bins=y_bin_edges, ax=gd.ax_marg_y)\n",
18711852
"_ = gd.ax_joint.legend()"
18721853
]
18731854
},
@@ -1931,12 +1912,8 @@
19311912
}
19321913
],
19331914
"source": [
1934-
"df_outlier_results = pd.DataFrame.from_records(\n",
1935-
" trc_hogg[\"is_outlier\"], columns=dfhoggs.index\n",
1936-
")\n",
1937-
"dfm_outlier_results = pd.melt(\n",
1938-
" df_outlier_results, var_name=\"datapoint_id\", value_name=\"is_outlier\"\n",
1939-
")\n",
1915+
"df_outlier_results = pd.DataFrame.from_records(trc_hogg[\"is_outlier\"], columns=dfhoggs.index)\n",
1916+
"dfm_outlier_results = pd.melt(df_outlier_results, var_name=\"datapoint_id\", value_name=\"is_outlier\")\n",
19401917
"\n",
19411918
"gd = sns.catplot(\n",
19421919
" y=\"datapoint_id\",\n",
@@ -2032,9 +2009,7 @@
20322009
}
20332010
],
20342011
"source": [
2035-
"dfhoggs[\"annotate_for_investigation\"] = (\n",
2036-
" np.quantile(trc_hogg[\"is_outlier\"], 0.75, axis=0) == 1\n",
2037-
")\n",
2012+
"dfhoggs[\"annotate_for_investigation\"] = np.quantile(trc_hogg[\"is_outlier\"], 0.75, axis=0) == 1\n",
20382013
"dfhoggs[\"annotate_for_investigation\"].value_counts()"
20392014
]
20402015
},

docs/source/notebooks/GP-MaunaLoa.ipynb

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@
400400
"%config InlineBackend.figure_format = 'retina'\n",
401401
"RANDOM_SEED = 8927\n",
402402
"np.random.seed(RANDOM_SEED)\n",
403-
"az.style.use('arviz-darkgrid')"
403+
"az.style.use(\"arviz-darkgrid\")"
404404
]
405405
},
406406
{
@@ -687,14 +687,10 @@
687687
" left=pd.to_datetime(\"2003-12-15\"), fill_alpha=0.1, fill_color=\"firebrick\"\n",
688688
")\n",
689689
"p.add_layout(predict_region)\n",
690-
"ppm400 = Span(\n",
691-
" location=400, dimension=\"width\", line_color=\"red\", line_dash=\"dashed\", line_width=2\n",
692-
")\n",
690+
"ppm400 = Span(location=400, dimension=\"width\", line_color=\"red\", line_dash=\"dashed\", line_width=2)\n",
693691
"p.add_layout(ppm400)\n",
694692
"\n",
695-
"p.line(\n",
696-
" data_monthly.index, data_monthly[\"CO2\"], line_width=2, line_color=\"black\", alpha=0.5\n",
697-
")\n",
693+
"p.line(data_monthly.index, data_monthly[\"CO2\"], line_width=2, line_color=\"black\", alpha=0.5)\n",
698694
"p.circle(data_monthly.index, data_monthly[\"CO2\"], line_color=\"black\", alpha=0.1, size=2)\n",
699695
"\n",
700696
"train_label = Label(\n",
@@ -1074,9 +1070,7 @@
10741070
" period = pm.Normal(\"period\", mu=1, sigma=0.05)\n",
10751071
" ℓ_psmooth = pm.Gamma(\"ℓ_psmooth \", alpha=4, beta=3)\n",
10761072
" cov_seasonal = (\n",
1077-
" η_per ** 2\n",
1078-
" * pm.gp.cov.Periodic(1, period, ℓ_psmooth)\n",
1079-
" * pm.gp.cov.Matern52(1, ℓ_pdecay)\n",
1073+
" η_per ** 2 * pm.gp.cov.Periodic(1, period, ℓ_psmooth) * pm.gp.cov.Matern52(1, ℓ_pdecay)\n",
10801074
" )\n",
10811075
" gp_seasonal = pm.gp.Marginal(cov_func=cov_seasonal)\n",
10821076
"\n",
@@ -1204,25 +1198,19 @@
12041198
"mu, var = gp_trend.predict(\n",
12051199
" tnew, point=mp, given={\"gp\": gp, \"X\": t, \"y\": y, \"noise\": cov_noise}, diag=True\n",
12061200
")\n",
1207-
"fit = fit.assign(\n",
1208-
" mu_trend=mu * std_co2 + first_co2, sd_trend=np.sqrt(var * std_co2 ** 2)\n",
1209-
")\n",
1201+
"fit = fit.assign(mu_trend=mu * std_co2 + first_co2, sd_trend=np.sqrt(var * std_co2 ** 2))\n",
12101202
"\n",
12111203
"print(\"Predicting with gp_medium ...\")\n",
12121204
"mu, var = gp_medium.predict(\n",
12131205
" tnew, point=mp, given={\"gp\": gp, \"X\": t, \"y\": y, \"noise\": cov_noise}, diag=True\n",
12141206
")\n",
1215-
"fit = fit.assign(\n",
1216-
" mu_medium=mu * std_co2 + first_co2, sd_medium=np.sqrt(var * std_co2 ** 2)\n",
1217-
")\n",
1207+
"fit = fit.assign(mu_medium=mu * std_co2 + first_co2, sd_medium=np.sqrt(var * std_co2 ** 2))\n",
12181208
"\n",
12191209
"print(\"Predicting with gp_seasonal ...\")\n",
12201210
"mu, var = gp_seasonal.predict(\n",
12211211
" tnew, point=mp, given={\"gp\": gp, \"X\": t, \"y\": y, \"noise\": cov_noise}, diag=True\n",
12221212
")\n",
1223-
"fit = fit.assign(\n",
1224-
" mu_seasonal=mu * std_co2 + first_co2, sd_seasonal=np.sqrt(var * std_co2 ** 2)\n",
1225-
")\n",
1213+
"fit = fit.assign(mu_seasonal=mu * std_co2 + first_co2, sd_seasonal=np.sqrt(var * std_co2 ** 2))\n",
12261214
"print(\"Done\")"
12271215
]
12281216
},
@@ -1346,9 +1334,7 @@
13461334
")\n",
13471335
"\n",
13481336
"# true value\n",
1349-
"p.circle(\n",
1350-
" data_early.index, data_early[\"CO2\"], color=\"black\", legend_label=\"Observed data\"\n",
1351-
")\n",
1337+
"p.circle(data_early.index, data_early[\"CO2\"], color=\"black\", legend_label=\"Observed data\")\n",
13521338
"p.legend.location = \"top_left\"\n",
13531339
"show(p)"
13541340
]
@@ -1439,9 +1425,7 @@
14391425
"source": [
14401426
"# plot several years\n",
14411427
"\n",
1442-
"p = figure(\n",
1443-
" title=\"Several years of the seasonal component\", plot_width=550, plot_height=350\n",
1444-
")\n",
1428+
"p = figure(title=\"Several years of the seasonal component\", plot_width=550, plot_height=350)\n",
14451429
"p.yaxis.axis_label = \"Δ CO2 [ppm]\"\n",
14461430
"p.xaxis.axis_label = \"Month\"\n",
14471431
"\n",
@@ -1596,9 +1580,7 @@
15961580
"band_x = np.append(dates, dates[::-1])\n",
15971581
"band_y = np.append(lower, upper[::-1])\n",
15981582
"\n",
1599-
"p.line(\n",
1600-
" dates, mu_pred_sc, line_width=2, line_color=\"firebrick\", legend_label=\"Total fit\"\n",
1601-
")\n",
1583+
"p.line(dates, mu_pred_sc, line_width=2, line_color=\"firebrick\", legend_label=\"Total fit\")\n",
16021584
"p.patch(band_x, band_y, color=\"firebrick\", alpha=0.6, line_color=\"white\")\n",
16031585
"\n",
16041586
"# some predictions\n",
@@ -1613,9 +1595,7 @@
16131595
"# true value\n",
16141596
"# p.line(data_later.index, data_later['CO2'],\n",
16151597
"# line_width=2, line_color=\"black\", legend=\"Observed data\")\n",
1616-
"p.circle(\n",
1617-
" data_later.index, data_later[\"CO2\"], color=\"black\", legend_label=\"Observed data\"\n",
1618-
")\n",
1598+
"p.circle(data_later.index, data_later[\"CO2\"], color=\"black\", legend_label=\"Observed data\")\n",
16191599
"\n",
16201600
"ppm400 = Span(\n",
16211601
" location=400,\n",

docs/source/notebooks/LKJ.ipynb

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,7 @@
9999
"\n",
100100
"blue, _, red, *_ = sns.color_palette()\n",
101101
"\n",
102-
"e = Ellipse(\n",
103-
" μ_actual, 2 * np.sqrt(5.991 * var[0]), 2 * np.sqrt(5.991 * var[1]), angle=angle\n",
104-
")\n",
102+
"e = Ellipse(μ_actual, 2 * np.sqrt(5.991 * var[0]), 2 * np.sqrt(5.991 * var[1]), angle=angle)\n",
105103
"e.set_alpha(0.5)\n",
106104
"e.set_facecolor(blue)\n",
107105
"e.set_zorder(10)\n",
@@ -135,9 +133,7 @@
135133
"outputs": [],
136134
"source": [
137135
"with pm.Model() as m:\n",
138-
" packed_L = pm.LKJCholeskyCov(\n",
139-
" \"packed_L\", n=2, eta=2.0, sd_dist=pm.Exponential.dist(1.0)\n",
140-
" )"
136+
" packed_L = pm.LKJCholeskyCov(\"packed_L\", n=2, eta=2.0, sd_dist=pm.Exponential.dist(1.0))"
141137
]
142138
},
143139
{
@@ -540,9 +536,7 @@
540536
],
541537
"source": [
542538
"with model:\n",
543-
" trace = pm.sample(\n",
544-
" random_seed=RANDOM_SEED, init=\"adapt_diag\", return_inferencedata=True\n",
545-
" )\n",
539+
" trace = pm.sample(random_seed=RANDOM_SEED, init=\"adapt_diag\", return_inferencedata=True)\n",
546540
"az.summary(trace, var_names=[\"~chol\"], round_to=2)"
547541
]
548542
},
@@ -664,9 +658,7 @@
664658
"\n",
665659
"fig, ax = plt.subplots(figsize=(8, 6))\n",
666660
"\n",
667-
"e = Ellipse(\n",
668-
" μ_actual, 2 * np.sqrt(5.991 * var[0]), 2 * np.sqrt(5.991 * var[1]), angle=angle\n",
669-
")\n",
661+
"e = Ellipse(μ_actual, 2 * np.sqrt(5.991 * var[0]), 2 * np.sqrt(5.991 * var[1]), angle=angle)\n",
670662
"e.set_alpha(0.5)\n",
671663
"e.set_facecolor(blue)\n",
672664
"e.set_zorder(10)\n",

0 commit comments

Comments
 (0)