Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Fill the area containing the 94% credible interval of the difference between uniform
variables empirical CDF and the real uniform CDF. A LOO-PIT ECDF clearly outside of these
theoretical boundaries indicates that the observations and the posterior predictive
samples do not follow the same distribution.
.. plot::
:context: close-figs
>>> az.plot_loo_pit(idata=idata, y="obs", ecdf=True)
"""
if ecdf and use_hpd:
raise ValueError("use_hpd is incompatible with ecdf plot")
(figsize, _, _, xt_labelsize, linewidth, _) = _scale_fig_size(figsize, textsize, 1, 1)
loo_pit = _loo_pit(idata=idata, y=y, y_hat=y_hat, log_weights=log_weights)
loo_pit = loo_pit.flatten() if isinstance(loo_pit, np.ndarray) else loo_pit.values.flatten()
if plot_kwargs is None:
plot_kwargs = {}
plot_kwargs["color"] = to_hex(color)
plot_kwargs.setdefault("linewidth", linewidth * 1.4)
if isinstance(y, str):
label = ("{} LOO-PIT ECDF" if ecdf else "{} LOO-PIT").format(y)
elif isinstance(y, DataArray):
label = ("{} LOO-PIT ECDF" if ecdf else "{} LOO-PIT").format(y.name)
elif isinstance(y_hat, str):
label = ("{} LOO-PIT ECDF" if ecdf else "{} LOO-PIT").format(y_hat)
elif isinstance(y_hat, DataArray):
label = ("{} LOO-PIT ECDF" if ecdf else "{} LOO-PIT").format(y_hat.name)
length_plotters = len(obs_plotters)
pp_plotters = [
tup
for _, tup in zip(
range(length_plotters),
xarray_var_iter(
posterior_predictive.isel(coords),
var_names=pp_var_names,
skip_dims=set(flatten_pp),
combined=True,
),
)
]
rows, cols = default_grid(length_plotters)
(figsize, ax_labelsize, _, xt_labelsize, linewidth, markersize) = _scale_fig_size(
figsize, textsize, rows, cols
)
ppcplot_kwargs = dict(
ax=ax,
length_plotters=length_plotters,
rows=rows,
cols=cols,
figsize=figsize,
animated=animated,
obs_plotters=obs_plotters,
pp_plotters=pp_plotters,
posterior_predictive=posterior_predictive,
pp_sample_ix=pp_sample_ix,
kind=kind,
alpha=alpha,
.. plot::
:context: close-figs
>>> lines = (('theta_t',{'school': "Choate"}, [-1]),)
>>> az.plot_trace(data, var_names=('theta_t', 'theta'), coords=coords, lines=lines)
"""
# Set plot default backend kwargs
if backend_kwargs is None:
backend_kwargs = {}
backend_kwargs = {**backend_kwarg_defaults(), **backend_kwargs}
textsize = plot_kwargs.pop("textsize", 10)
figsize, _, titlesize, xt_labelsize, linewidth, _ = _scale_fig_size(
figsize, textsize, rows=len(plotters), cols=2
)
trace_kwargs.setdefault("linewidth", linewidth)
plot_kwargs.setdefault("linewidth", linewidth)
_, axes = plt.subplots(len(plotters), 2, squeeze=False, figsize=figsize, **backend_kwargs)
for idx, (var_name, selection, value) in enumerate(plotters):
value = np.atleast_2d(value)
if len(value.shape) == 2:
_plot_chains_mpl(
axes,
idx,
value,
-------
axes : matplotlib axes or bokeh figures
"""
data = convert_to_dataset(data, group="posterior")
var_names = _var_names(var_names, data)
plotters = filter_plotters_list(
list(xarray_var_iter(data, var_names=var_names, combined=True)), "plot_violin"
)
if kwargs_shade is None:
kwargs_shade = {}
rows, cols = default_grid(len(plotters))
(figsize, ax_labelsize, _, xt_labelsize, linewidth, _) = _scale_fig_size(
figsize, textsize, rows, cols
)
ax_labelsize *= 2
violinplot_kwargs = dict(
ax=ax,
plotters=plotters,
figsize=figsize,
rows=rows,
cols=cols,
sharey=sharey,
kwargs_shade=kwargs_shade,
shade=shade,
bw=bw,
credible_interval=credible_interval,
linewidth=linewidth,
.. plot::
:context: close-figs
>>> az.plot_energy(data, kind='hist')
"""
energy = convert_to_dataset(data, group="sample_stats").energy.values
if fill_kwargs is None:
fill_kwargs = {}
if plot_kwargs is None:
plot_kwargs = {}
figsize, _, _, xt_labelsize, linewidth, _ = _scale_fig_size(figsize, textsize, 1, 1)
_colors = [
prop for _, prop in zip(range(10), cycle(rcParams["axes.prop_cycle"].by_key()["color"]))
]
if (fill_color[0].startswith("C") and len(fill_color[0]) == 2) and (
fill_color[1].startswith("C") and len(fill_color[1]) == 2
):
fill_color = tuple([_colors[int(color[1:]) % 10] for color in fill_color])
elif fill_color[0].startswith("C") and len(fill_color[0]) == 2:
fill_color = tuple([_colors[int(fill_color[0][1:]) % 10]] + list(fill_color[1:]))
elif fill_color[1].startswith("C") and len(fill_color[1]) == 2:
fill_color = tuple(list(fill_color[1:]) + [_colors[int(fill_color[0][1:]) % 10]])
series = zip(
fill_alpha,
fill_color,
ridgeplot_kind,
textsize,
ess,
r_hat,
backend_kwargs,
show,
):
"""Bokeh forest plot."""
plot_handler = PlotHandler(
datasets, var_names=var_names, model_names=model_names, combined=combined, colors=colors
)
if figsize is None:
figsize = (min(12, sum(width_ratios) * 2), plot_handler.fig_height())
(figsize, _, _, _, auto_linewidth, auto_markersize) = _scale_fig_size(figsize, textsize, 1.1, 1)
if linewidth is None:
linewidth = auto_linewidth
if markersize is None:
markersize = auto_markersize
if backend_kwargs is None:
backend_kwargs = {}
backend_kwargs = {
**backend_kwarg_defaults(
("tools", "plot.bokeh.tools"),
("output_backend", "plot.bokeh.output_backend"),
("dpi", "plot.bokeh.figure.dpi"),
),
var_names=var_names,
relative=relative,
method="tail",
)
for draw_div in draw_divisions
],
dim="ess_dim",
)
plotters = filter_plotters_list(
list(xarray_var_iter(ess_dataset, var_names=var_names, skip_dims={"ess_dim"})), "plot_ess"
)
length_plotters = len(plotters)
rows, cols = default_grid(length_plotters)
(figsize, ax_labelsize, titlesize, xt_labelsize, _linewidth, _markersize) = _scale_fig_size(
figsize, textsize, rows, cols
)
_linestyle = kwargs.pop("ls", "-" if kind == "evolution" else "none")
kwargs.setdefault("linestyle", _linestyle)
kwargs.setdefault("linewidth", kwargs.pop("lw", _linewidth))
kwargs.setdefault("markersize", kwargs.pop("ms", _markersize))
kwargs.setdefault("marker", "o")
kwargs.setdefault("zorder", 3)
if extra_kwargs is None:
extra_kwargs = {}
if kind == "evolution":
extra_kwargs = {
**extra_kwargs,
**{key: item for key, item in kwargs.items() if key not in extra_kwargs},
}
kwargs.setdefault("label", "bulk")
data = convert_to_dataset(data, group="posterior")
if coords is None:
coords = {}
var_names = _var_names(var_names, data)
plotters = list(xarray_var_iter(get_coords(data, coords), var_names=var_names, combined=True))
if len(plotters) != 2:
raise Exception(
"Number of variables to be plotted must 2 (you supplied {})".format(len(plotters))
)
figsize, ax_labelsize, _, xt_labelsize, linewidth, _ = _scale_fig_size(figsize, textsize)
if joint_kwargs is None:
joint_kwargs = {}
if marginal_kwargs is None:
marginal_kwargs = {}
marginal_kwargs.setdefault("plot_kwargs", {})
marginal_kwargs["plot_kwargs"]["linewidth"] = linewidth
plot_joint_kwargs = dict(
ax=ax,
figsize=figsize,
plotters=plotters,
ax_labelsize=ax_labelsize,
xt_labelsize=xt_labelsize,
kind=kind,
"{max_plots} plots".format(max_plots=max_plots, len_plotters=length_plotters),
SyntaxWarning,
)
all_labels = all_labels[:max_plots]
to_plot = [
[
(var_name, selection, values)
for var_name, selection, values in plotters
if make_label(var_name, selection) in all_labels
]
for plotters in to_plot
]
length_plotters = max_plots
rows, cols = default_grid(length_plotters, max_cols=3)
(figsize, _, titlesize, xt_labelsize, linewidth, markersize) = _scale_fig_size(
figsize, textsize, rows, cols
)
plot_density_kwargs = dict(
ax=ax,
all_labels=all_labels,
to_plot=to_plot,
colors=colors,
bw=bw,
figsize=figsize,
length_plotters=length_plotters,
rows=rows,
cols=cols,
titlesize=titlesize,
xt_labelsize=xt_labelsize,
linewidth=linewidth,
else:
max_plots = (
numvars ** 2 if rcParams["plot.max_subplots"] is None else rcParams["plot.max_subplots"]
)
vars_to_plot = np.sum(np.arange(numvars).cumsum() < max_plots)
if vars_to_plot < numvars:
warnings.warn(
"rcParams['plot.max_subplots'] ({max_plots}) is smaller than the number "
"of resulting ELPD pairwise plots with these variables, generating only a "
"{side}x{side} grid".format(max_plots=max_plots, side=vars_to_plot),
SyntaxWarning,
)
numvars = vars_to_plot
(figsize, _, _, _, _, markersize) = _scale_fig_size(
figsize, textsize, numvars - 2, numvars - 2
)
plot_kwargs.setdefault("s", markersize)
if ax is None:
ax = []
for row in range(numvars - 1):
ax_row = []
for col in range(numvars - 1):
if row == 0 and col == 0:
ax_first = bkp.figure(
width=int(figsize[0] / (numvars - 1) * dpi),
height=int(figsize[1] / (numvars - 1) * dpi),
**backend_kwargs
)
ax_row.append(ax_first)