Skip to content

CLN: notebooks black formatting #4152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 13 additions & 38 deletions docs/source/notebooks/GLM-robust-with-outlier-detection.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -467,9 +467,7 @@
"metadata": {},
"outputs": [],
"source": [
"dfhoggs = (dfhogg[[\"x\", \"y\"]] - dfhogg[[\"x\", \"y\"]].mean(0)) / (\n",
" 2 * dfhogg[[\"x\", \"y\"]].std(0)\n",
")\n",
"dfhoggs = (dfhogg[[\"x\", \"y\"]] - dfhogg[[\"x\", \"y\"]].mean(0)) / (2 * dfhogg[[\"x\", \"y\"]].std(0))\n",
"dfhoggs[\"sigma_x\"] = dfhogg[\"sigma_x\"] / (2 * dfhogg[\"x\"].std())\n",
"dfhoggs[\"sigma_y\"] = dfhogg[\"sigma_y\"] / (2 * dfhogg[\"y\"].std())"
]
Expand Down Expand Up @@ -505,9 +503,7 @@
" marginal_kws={\"bins\": 12, \"kde\": True, \"kde_kws\": {\"cut\": 1}},\n",
" joint_kws={\"edgecolor\": \"w\", \"linewidth\": 1, \"s\": 80},\n",
")\n",
"_ = gd.ax_joint.errorbar(\n",
" \"x\", \"y\", \"sigma_y\", \"sigma_x\", fmt=\"none\", ecolor=\"#4878d0\", data=dfhoggs\n",
")\n",
"_ = gd.ax_joint.errorbar(\"x\", \"y\", \"sigma_y\", \"sigma_x\", fmt=\"none\", ecolor=\"#4878d0\", data=dfhoggs)\n",
"_ = gd.fig.suptitle(\n",
" (\n",
" \"Quick view to confirm action of\\n\"\n",
Expand Down Expand Up @@ -639,9 +635,7 @@
" y_est = b0 + b1 * dfhoggs[\"x\"]\n",
"\n",
" ## Define Normal likelihood\n",
" likelihood = pm.Normal(\n",
" \"likelihood\", mu=y_est, sigma=dfhoggs[\"sigma_y\"], observed=dfhoggs[\"y\"]\n",
" )\n",
" likelihood = pm.Normal(\"likelihood\", mu=y_est, sigma=dfhoggs[\"sigma_y\"], observed=dfhoggs[\"y\"])\n",
"\n",
"pm.model_to_graphviz(mdl_ols)"
]
Expand Down Expand Up @@ -1274,10 +1268,7 @@
"\n",
"gd = sns.JointGrid(x=\"b0_intercept\", y=\"b1_slope\", data=df_trc, height=8)\n",
"_ = gd.fig.suptitle(\n",
" (\n",
" \"Posterior joint distributions\"\n",
" + \"\\n(showing general movement from OLS to StudentT)\"\n",
" ),\n",
" (\"Posterior joint distributions\" + \"\\n(showing general movement from OLS to StudentT)\"),\n",
" y=1.05,\n",
")\n",
"\n",
Expand All @@ -1297,11 +1288,9 @@
" grp[\"b0_intercept\"], grp[\"b1_slope\"], ax=gd.ax_joint, alpha=0.2, label=idx\n",
" )\n",
" _ = sns.kdeplot(grp[\"b0_intercept\"], grp[\"b1_slope\"], ax=gd.ax_joint, **kde_kws)\n",
" _ = sns.distplot(grp[\"b0_intercept\"], bins=x_bin_edges, ax=gd.ax_marg_x, **dist_kws)\n",
" _ = sns.distplot(\n",
" grp[\"b0_intercept\"], bins=x_bin_edges, ax=gd.ax_marg_x, **dist_kws\n",
" )\n",
" _ = sns.distplot(grp[\"b1_slope\"], vertical=True, bins=y_bin_edges,\n",
" ax=gd.ax_marg_y, **dist_kws\n",
" grp[\"b1_slope\"], vertical=True, bins=y_bin_edges, ax=gd.ax_marg_y, **dist_kws\n",
" )\n",
"_ = gd.ax_joint.legend()"
]
Expand Down Expand Up @@ -1419,18 +1408,14 @@
" y_est_out = pm.Normal(\"y_est_out\", mu=0, sigma=10, testval=pm.floatX(0.0)) # (1, )\n",
"\n",
" # very weakly informative prior for additional variance for outliers\n",
" sigma_y_out = pm.HalfNormal(\n",
" \"sigma_y_out\", sigma=10, testval=pm.floatX(1.0)\n",
" ) # (1, )\n",
" sigma_y_out = pm.HalfNormal(\"sigma_y_out\", sigma=10, testval=pm.floatX(1.0)) # (1, )\n",
"\n",
" # create in/outlier distributions to get a logp evaluated on the observed y\n",
" # this is not strictly a pymc3 likelihood, but behaves like one when we\n",
" # evaluate it within a Potential (which is minimised)\n",
" inlier_logp = pm.Normal.dist(mu=y_est_in, sigma=tsv_sigma_y).logp(tsv_y)\n",
"\n",
" outlier_logp = pm.Normal.dist(mu=y_est_out, sigma=tsv_sigma_y + sigma_y_out).logp(\n",
" tsv_y\n",
" )\n",
" outlier_logp = pm.Normal.dist(mu=y_est_out, sigma=tsv_sigma_y + sigma_y_out).logp(tsv_y)\n",
"\n",
" # frac_outliers only needs to span [0, .5]\n",
" # testval for is_outlier initialised in order to create class asymmetry\n",
Expand Down Expand Up @@ -1860,14 +1845,10 @@
"dist_kws = dict(kde_kws=dict(cut=1), axlabel=False)\n",
"\n",
"for idx, grp in df_trc.groupby(\"model\"):\n",
" _ = sns.scatterplot(\n",
" grp[\"b0_intercept\"], grp[\"b1_slope\"], ax=gd.ax_joint, alpha=0.2, label=idx\n",
" )\n",
" _ = sns.scatterplot(grp[\"b0_intercept\"], grp[\"b1_slope\"], ax=gd.ax_joint, alpha=0.2, label=idx)\n",
" _ = sns.kdeplot(grp[\"b0_intercept\"], grp[\"b1_slope\"], ax=gd.ax_joint, **kde_kws)\n",
" _ = sns.distplot(grp[\"b0_intercept\"], **dist_kws, bins=x_bin_edges, ax=gd.ax_marg_x)\n",
" _ = sns.distplot(\n",
" grp[\"b1_slope\"], **dist_kws, vertical=True, bins=y_bin_edges, ax=gd.ax_marg_y\n",
" )\n",
" _ = sns.distplot(grp[\"b1_slope\"], **dist_kws, vertical=True, bins=y_bin_edges, ax=gd.ax_marg_y)\n",
"_ = gd.ax_joint.legend()"
]
},
Expand Down Expand Up @@ -1931,12 +1912,8 @@
}
],
"source": [
"df_outlier_results = pd.DataFrame.from_records(\n",
" trc_hogg[\"is_outlier\"], columns=dfhoggs.index\n",
")\n",
"dfm_outlier_results = pd.melt(\n",
" df_outlier_results, var_name=\"datapoint_id\", value_name=\"is_outlier\"\n",
")\n",
"df_outlier_results = pd.DataFrame.from_records(trc_hogg[\"is_outlier\"], columns=dfhoggs.index)\n",
"dfm_outlier_results = pd.melt(df_outlier_results, var_name=\"datapoint_id\", value_name=\"is_outlier\")\n",
"\n",
"gd = sns.catplot(\n",
" y=\"datapoint_id\",\n",
Expand Down Expand Up @@ -2032,9 +2009,7 @@
}
],
"source": [
"dfhoggs[\"annotate_for_investigation\"] = (\n",
" np.quantile(trc_hogg[\"is_outlier\"], 0.75, axis=0) == 1\n",
")\n",
"dfhoggs[\"annotate_for_investigation\"] = np.quantile(trc_hogg[\"is_outlier\"], 0.75, axis=0) == 1\n",
"dfhoggs[\"annotate_for_investigation\"].value_counts()"
]
},
Expand Down
42 changes: 11 additions & 31 deletions docs/source/notebooks/GP-MaunaLoa.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@
"%config InlineBackend.figure_format = 'retina'\n",
"RANDOM_SEED = 8927\n",
"np.random.seed(RANDOM_SEED)\n",
"az.style.use('arviz-darkgrid')"
"az.style.use(\"arviz-darkgrid\")"
]
},
{
Expand Down Expand Up @@ -687,14 +687,10 @@
" left=pd.to_datetime(\"2003-12-15\"), fill_alpha=0.1, fill_color=\"firebrick\"\n",
")\n",
"p.add_layout(predict_region)\n",
"ppm400 = Span(\n",
" location=400, dimension=\"width\", line_color=\"red\", line_dash=\"dashed\", line_width=2\n",
")\n",
"ppm400 = Span(location=400, dimension=\"width\", line_color=\"red\", line_dash=\"dashed\", line_width=2)\n",
"p.add_layout(ppm400)\n",
"\n",
"p.line(\n",
" data_monthly.index, data_monthly[\"CO2\"], line_width=2, line_color=\"black\", alpha=0.5\n",
")\n",
"p.line(data_monthly.index, data_monthly[\"CO2\"], line_width=2, line_color=\"black\", alpha=0.5)\n",
"p.circle(data_monthly.index, data_monthly[\"CO2\"], line_color=\"black\", alpha=0.1, size=2)\n",
"\n",
"train_label = Label(\n",
Expand Down Expand Up @@ -1074,9 +1070,7 @@
" period = pm.Normal(\"period\", mu=1, sigma=0.05)\n",
" ℓ_psmooth = pm.Gamma(\"ℓ_psmooth \", alpha=4, beta=3)\n",
" cov_seasonal = (\n",
" η_per ** 2\n",
" * pm.gp.cov.Periodic(1, period, ℓ_psmooth)\n",
" * pm.gp.cov.Matern52(1, ℓ_pdecay)\n",
" η_per ** 2 * pm.gp.cov.Periodic(1, period, ℓ_psmooth) * pm.gp.cov.Matern52(1, ℓ_pdecay)\n",
" )\n",
" gp_seasonal = pm.gp.Marginal(cov_func=cov_seasonal)\n",
"\n",
Expand Down Expand Up @@ -1204,25 +1198,19 @@
"mu, var = gp_trend.predict(\n",
" tnew, point=mp, given={\"gp\": gp, \"X\": t, \"y\": y, \"noise\": cov_noise}, diag=True\n",
")\n",
"fit = fit.assign(\n",
" mu_trend=mu * std_co2 + first_co2, sd_trend=np.sqrt(var * std_co2 ** 2)\n",
")\n",
"fit = fit.assign(mu_trend=mu * std_co2 + first_co2, sd_trend=np.sqrt(var * std_co2 ** 2))\n",
"\n",
"print(\"Predicting with gp_medium ...\")\n",
"mu, var = gp_medium.predict(\n",
" tnew, point=mp, given={\"gp\": gp, \"X\": t, \"y\": y, \"noise\": cov_noise}, diag=True\n",
")\n",
"fit = fit.assign(\n",
" mu_medium=mu * std_co2 + first_co2, sd_medium=np.sqrt(var * std_co2 ** 2)\n",
")\n",
"fit = fit.assign(mu_medium=mu * std_co2 + first_co2, sd_medium=np.sqrt(var * std_co2 ** 2))\n",
"\n",
"print(\"Predicting with gp_seasonal ...\")\n",
"mu, var = gp_seasonal.predict(\n",
" tnew, point=mp, given={\"gp\": gp, \"X\": t, \"y\": y, \"noise\": cov_noise}, diag=True\n",
")\n",
"fit = fit.assign(\n",
" mu_seasonal=mu * std_co2 + first_co2, sd_seasonal=np.sqrt(var * std_co2 ** 2)\n",
")\n",
"fit = fit.assign(mu_seasonal=mu * std_co2 + first_co2, sd_seasonal=np.sqrt(var * std_co2 ** 2))\n",
"print(\"Done\")"
]
},
Expand Down Expand Up @@ -1346,9 +1334,7 @@
")\n",
"\n",
"# true value\n",
"p.circle(\n",
" data_early.index, data_early[\"CO2\"], color=\"black\", legend_label=\"Observed data\"\n",
")\n",
"p.circle(data_early.index, data_early[\"CO2\"], color=\"black\", legend_label=\"Observed data\")\n",
"p.legend.location = \"top_left\"\n",
"show(p)"
]
Expand Down Expand Up @@ -1439,9 +1425,7 @@
"source": [
"# plot several years\n",
"\n",
"p = figure(\n",
" title=\"Several years of the seasonal component\", plot_width=550, plot_height=350\n",
")\n",
"p = figure(title=\"Several years of the seasonal component\", plot_width=550, plot_height=350)\n",
"p.yaxis.axis_label = \"Δ CO2 [ppm]\"\n",
"p.xaxis.axis_label = \"Month\"\n",
"\n",
Expand Down Expand Up @@ -1596,9 +1580,7 @@
"band_x = np.append(dates, dates[::-1])\n",
"band_y = np.append(lower, upper[::-1])\n",
"\n",
"p.line(\n",
" dates, mu_pred_sc, line_width=2, line_color=\"firebrick\", legend_label=\"Total fit\"\n",
")\n",
"p.line(dates, mu_pred_sc, line_width=2, line_color=\"firebrick\", legend_label=\"Total fit\")\n",
"p.patch(band_x, band_y, color=\"firebrick\", alpha=0.6, line_color=\"white\")\n",
"\n",
"# some predictions\n",
Expand All @@ -1613,9 +1595,7 @@
"# true value\n",
"# p.line(data_later.index, data_later['CO2'],\n",
"# line_width=2, line_color=\"black\", legend=\"Observed data\")\n",
"p.circle(\n",
" data_later.index, data_later[\"CO2\"], color=\"black\", legend_label=\"Observed data\"\n",
")\n",
"p.circle(data_later.index, data_later[\"CO2\"], color=\"black\", legend_label=\"Observed data\")\n",
"\n",
"ppm400 = Span(\n",
" location=400,\n",
Expand Down
16 changes: 4 additions & 12 deletions docs/source/notebooks/LKJ.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,7 @@
"\n",
"blue, _, red, *_ = sns.color_palette()\n",
"\n",
"e = Ellipse(\n",
" μ_actual, 2 * np.sqrt(5.991 * var[0]), 2 * np.sqrt(5.991 * var[1]), angle=angle\n",
")\n",
"e = Ellipse(μ_actual, 2 * np.sqrt(5.991 * var[0]), 2 * np.sqrt(5.991 * var[1]), angle=angle)\n",
"e.set_alpha(0.5)\n",
"e.set_facecolor(blue)\n",
"e.set_zorder(10)\n",
Expand Down Expand Up @@ -135,9 +133,7 @@
"outputs": [],
"source": [
"with pm.Model() as m:\n",
" packed_L = pm.LKJCholeskyCov(\n",
" \"packed_L\", n=2, eta=2.0, sd_dist=pm.Exponential.dist(1.0)\n",
" )"
" packed_L = pm.LKJCholeskyCov(\"packed_L\", n=2, eta=2.0, sd_dist=pm.Exponential.dist(1.0))"
]
},
{
Expand Down Expand Up @@ -540,9 +536,7 @@
],
"source": [
"with model:\n",
" trace = pm.sample(\n",
" random_seed=RANDOM_SEED, init=\"adapt_diag\", return_inferencedata=True\n",
" )\n",
" trace = pm.sample(random_seed=RANDOM_SEED, init=\"adapt_diag\", return_inferencedata=True)\n",
"az.summary(trace, var_names=[\"~chol\"], round_to=2)"
]
},
Expand Down Expand Up @@ -664,9 +658,7 @@
"\n",
"fig, ax = plt.subplots(figsize=(8, 6))\n",
"\n",
"e = Ellipse(\n",
" μ_actual, 2 * np.sqrt(5.991 * var[0]), 2 * np.sqrt(5.991 * var[1]), angle=angle\n",
")\n",
"e = Ellipse(μ_actual, 2 * np.sqrt(5.991 * var[0]), 2 * np.sqrt(5.991 * var[1]), angle=angle)\n",
"e.set_alpha(0.5)\n",
"e.set_facecolor(blue)\n",
"e.set_zorder(10)\n",
Expand Down
Loading