arviz 0.23.1__py3-none-any.whl → 1.0.0rc0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- arviz/__init__.py +52 -357
- arviz-1.0.0rc0.dist-info/METADATA +182 -0
- arviz-1.0.0rc0.dist-info/RECORD +5 -0
- {arviz-0.23.1.dist-info → arviz-1.0.0rc0.dist-info}/WHEEL +1 -2
- {arviz-0.23.1.dist-info → arviz-1.0.0rc0.dist-info}/licenses/LICENSE +0 -1
- arviz/data/__init__.py +0 -55
- arviz/data/base.py +0 -596
- arviz/data/converters.py +0 -203
- arviz/data/datasets.py +0 -161
- arviz/data/example_data/code/radon/radon.json +0 -326
- arviz/data/example_data/data/centered_eight.nc +0 -0
- arviz/data/example_data/data/non_centered_eight.nc +0 -0
- arviz/data/example_data/data_local.json +0 -12
- arviz/data/example_data/data_remote.json +0 -58
- arviz/data/inference_data.py +0 -2386
- arviz/data/io_beanmachine.py +0 -112
- arviz/data/io_cmdstan.py +0 -1036
- arviz/data/io_cmdstanpy.py +0 -1233
- arviz/data/io_datatree.py +0 -23
- arviz/data/io_dict.py +0 -462
- arviz/data/io_emcee.py +0 -317
- arviz/data/io_json.py +0 -54
- arviz/data/io_netcdf.py +0 -68
- arviz/data/io_numpyro.py +0 -497
- arviz/data/io_pyjags.py +0 -378
- arviz/data/io_pyro.py +0 -333
- arviz/data/io_pystan.py +0 -1095
- arviz/data/io_zarr.py +0 -46
- arviz/data/utils.py +0 -139
- arviz/labels.py +0 -210
- arviz/plots/__init__.py +0 -61
- arviz/plots/autocorrplot.py +0 -171
- arviz/plots/backends/__init__.py +0 -223
- arviz/plots/backends/bokeh/__init__.py +0 -166
- arviz/plots/backends/bokeh/autocorrplot.py +0 -101
- arviz/plots/backends/bokeh/bfplot.py +0 -23
- arviz/plots/backends/bokeh/bpvplot.py +0 -193
- arviz/plots/backends/bokeh/compareplot.py +0 -167
- arviz/plots/backends/bokeh/densityplot.py +0 -239
- arviz/plots/backends/bokeh/distcomparisonplot.py +0 -23
- arviz/plots/backends/bokeh/distplot.py +0 -183
- arviz/plots/backends/bokeh/dotplot.py +0 -113
- arviz/plots/backends/bokeh/ecdfplot.py +0 -73
- arviz/plots/backends/bokeh/elpdplot.py +0 -203
- arviz/plots/backends/bokeh/energyplot.py +0 -155
- arviz/plots/backends/bokeh/essplot.py +0 -176
- arviz/plots/backends/bokeh/forestplot.py +0 -772
- arviz/plots/backends/bokeh/hdiplot.py +0 -54
- arviz/plots/backends/bokeh/kdeplot.py +0 -268
- arviz/plots/backends/bokeh/khatplot.py +0 -163
- arviz/plots/backends/bokeh/lmplot.py +0 -185
- arviz/plots/backends/bokeh/loopitplot.py +0 -211
- arviz/plots/backends/bokeh/mcseplot.py +0 -184
- arviz/plots/backends/bokeh/pairplot.py +0 -328
- arviz/plots/backends/bokeh/parallelplot.py +0 -81
- arviz/plots/backends/bokeh/posteriorplot.py +0 -324
- arviz/plots/backends/bokeh/ppcplot.py +0 -379
- arviz/plots/backends/bokeh/rankplot.py +0 -149
- arviz/plots/backends/bokeh/separationplot.py +0 -107
- arviz/plots/backends/bokeh/traceplot.py +0 -436
- arviz/plots/backends/bokeh/violinplot.py +0 -164
- arviz/plots/backends/matplotlib/__init__.py +0 -124
- arviz/plots/backends/matplotlib/autocorrplot.py +0 -72
- arviz/plots/backends/matplotlib/bfplot.py +0 -78
- arviz/plots/backends/matplotlib/bpvplot.py +0 -177
- arviz/plots/backends/matplotlib/compareplot.py +0 -135
- arviz/plots/backends/matplotlib/densityplot.py +0 -194
- arviz/plots/backends/matplotlib/distcomparisonplot.py +0 -119
- arviz/plots/backends/matplotlib/distplot.py +0 -178
- arviz/plots/backends/matplotlib/dotplot.py +0 -116
- arviz/plots/backends/matplotlib/ecdfplot.py +0 -70
- arviz/plots/backends/matplotlib/elpdplot.py +0 -189
- arviz/plots/backends/matplotlib/energyplot.py +0 -113
- arviz/plots/backends/matplotlib/essplot.py +0 -180
- arviz/plots/backends/matplotlib/forestplot.py +0 -656
- arviz/plots/backends/matplotlib/hdiplot.py +0 -48
- arviz/plots/backends/matplotlib/kdeplot.py +0 -177
- arviz/plots/backends/matplotlib/khatplot.py +0 -241
- arviz/plots/backends/matplotlib/lmplot.py +0 -149
- arviz/plots/backends/matplotlib/loopitplot.py +0 -144
- arviz/plots/backends/matplotlib/mcseplot.py +0 -161
- arviz/plots/backends/matplotlib/pairplot.py +0 -355
- arviz/plots/backends/matplotlib/parallelplot.py +0 -58
- arviz/plots/backends/matplotlib/posteriorplot.py +0 -348
- arviz/plots/backends/matplotlib/ppcplot.py +0 -478
- arviz/plots/backends/matplotlib/rankplot.py +0 -119
- arviz/plots/backends/matplotlib/separationplot.py +0 -97
- arviz/plots/backends/matplotlib/traceplot.py +0 -526
- arviz/plots/backends/matplotlib/tsplot.py +0 -121
- arviz/plots/backends/matplotlib/violinplot.py +0 -148
- arviz/plots/bfplot.py +0 -128
- arviz/plots/bpvplot.py +0 -308
- arviz/plots/compareplot.py +0 -177
- arviz/plots/densityplot.py +0 -284
- arviz/plots/distcomparisonplot.py +0 -197
- arviz/plots/distplot.py +0 -233
- arviz/plots/dotplot.py +0 -233
- arviz/plots/ecdfplot.py +0 -372
- arviz/plots/elpdplot.py +0 -174
- arviz/plots/energyplot.py +0 -147
- arviz/plots/essplot.py +0 -319
- arviz/plots/forestplot.py +0 -304
- arviz/plots/hdiplot.py +0 -211
- arviz/plots/kdeplot.py +0 -357
- arviz/plots/khatplot.py +0 -236
- arviz/plots/lmplot.py +0 -380
- arviz/plots/loopitplot.py +0 -224
- arviz/plots/mcseplot.py +0 -194
- arviz/plots/pairplot.py +0 -281
- arviz/plots/parallelplot.py +0 -204
- arviz/plots/plot_utils.py +0 -599
- arviz/plots/posteriorplot.py +0 -298
- arviz/plots/ppcplot.py +0 -369
- arviz/plots/rankplot.py +0 -232
- arviz/plots/separationplot.py +0 -167
- arviz/plots/styles/arviz-bluish.mplstyle +0 -1
- arviz/plots/styles/arviz-brownish.mplstyle +0 -1
- arviz/plots/styles/arviz-colors.mplstyle +0 -2
- arviz/plots/styles/arviz-cyanish.mplstyle +0 -1
- arviz/plots/styles/arviz-darkgrid.mplstyle +0 -40
- arviz/plots/styles/arviz-doc.mplstyle +0 -88
- arviz/plots/styles/arviz-docgrid.mplstyle +0 -88
- arviz/plots/styles/arviz-grayscale.mplstyle +0 -41
- arviz/plots/styles/arviz-greenish.mplstyle +0 -1
- arviz/plots/styles/arviz-orangish.mplstyle +0 -1
- arviz/plots/styles/arviz-plasmish.mplstyle +0 -1
- arviz/plots/styles/arviz-purplish.mplstyle +0 -1
- arviz/plots/styles/arviz-redish.mplstyle +0 -1
- arviz/plots/styles/arviz-royish.mplstyle +0 -1
- arviz/plots/styles/arviz-viridish.mplstyle +0 -1
- arviz/plots/styles/arviz-white.mplstyle +0 -40
- arviz/plots/styles/arviz-whitegrid.mplstyle +0 -40
- arviz/plots/traceplot.py +0 -273
- arviz/plots/tsplot.py +0 -440
- arviz/plots/violinplot.py +0 -192
- arviz/preview.py +0 -58
- arviz/py.typed +0 -0
- arviz/rcparams.py +0 -606
- arviz/sel_utils.py +0 -223
- arviz/static/css/style.css +0 -340
- arviz/static/html/icons-svg-inline.html +0 -15
- arviz/stats/__init__.py +0 -37
- arviz/stats/density_utils.py +0 -1013
- arviz/stats/diagnostics.py +0 -1013
- arviz/stats/ecdf_utils.py +0 -324
- arviz/stats/stats.py +0 -2422
- arviz/stats/stats_refitting.py +0 -119
- arviz/stats/stats_utils.py +0 -609
- arviz/tests/__init__.py +0 -1
- arviz/tests/base_tests/__init__.py +0 -1
- arviz/tests/base_tests/test_data.py +0 -1679
- arviz/tests/base_tests/test_data_zarr.py +0 -143
- arviz/tests/base_tests/test_diagnostics.py +0 -511
- arviz/tests/base_tests/test_diagnostics_numba.py +0 -87
- arviz/tests/base_tests/test_helpers.py +0 -18
- arviz/tests/base_tests/test_labels.py +0 -69
- arviz/tests/base_tests/test_plot_utils.py +0 -342
- arviz/tests/base_tests/test_plots_bokeh.py +0 -1288
- arviz/tests/base_tests/test_plots_matplotlib.py +0 -2197
- arviz/tests/base_tests/test_rcparams.py +0 -317
- arviz/tests/base_tests/test_stats.py +0 -925
- arviz/tests/base_tests/test_stats_ecdf_utils.py +0 -166
- arviz/tests/base_tests/test_stats_numba.py +0 -45
- arviz/tests/base_tests/test_stats_utils.py +0 -384
- arviz/tests/base_tests/test_utils.py +0 -376
- arviz/tests/base_tests/test_utils_numba.py +0 -87
- arviz/tests/conftest.py +0 -46
- arviz/tests/external_tests/__init__.py +0 -1
- arviz/tests/external_tests/test_data_beanmachine.py +0 -78
- arviz/tests/external_tests/test_data_cmdstan.py +0 -398
- arviz/tests/external_tests/test_data_cmdstanpy.py +0 -496
- arviz/tests/external_tests/test_data_emcee.py +0 -166
- arviz/tests/external_tests/test_data_numpyro.py +0 -434
- arviz/tests/external_tests/test_data_pyjags.py +0 -119
- arviz/tests/external_tests/test_data_pyro.py +0 -260
- arviz/tests/external_tests/test_data_pystan.py +0 -307
- arviz/tests/helpers.py +0 -677
- arviz/utils.py +0 -773
- arviz/wrappers/__init__.py +0 -13
- arviz/wrappers/base.py +0 -236
- arviz/wrappers/wrap_pymc.py +0 -36
- arviz/wrappers/wrap_stan.py +0 -148
- arviz-0.23.1.dist-info/METADATA +0 -263
- arviz-0.23.1.dist-info/RECORD +0 -183
- arviz-0.23.1.dist-info/top_level.txt +0 -1
arviz/plots/hdiplot.py
DELETED
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
"""Plot highest density intervals for regression data."""
|
|
2
|
-
|
|
3
|
-
import warnings
|
|
4
|
-
|
|
5
|
-
import numpy as np
|
|
6
|
-
from scipy.interpolate import griddata
|
|
7
|
-
from scipy.signal import savgol_filter
|
|
8
|
-
from xarray import Dataset
|
|
9
|
-
|
|
10
|
-
from ..rcparams import rcParams
|
|
11
|
-
from ..stats import hdi
|
|
12
|
-
from .plot_utils import get_plotting_function
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
def plot_hdi(
|
|
16
|
-
x,
|
|
17
|
-
y=None,
|
|
18
|
-
hdi_prob=None,
|
|
19
|
-
hdi_data=None,
|
|
20
|
-
color="C1",
|
|
21
|
-
circular=False,
|
|
22
|
-
smooth=True,
|
|
23
|
-
smooth_kwargs=None,
|
|
24
|
-
figsize=None,
|
|
25
|
-
fill_kwargs=None,
|
|
26
|
-
plot_kwargs=None,
|
|
27
|
-
hdi_kwargs=None,
|
|
28
|
-
ax=None,
|
|
29
|
-
backend=None,
|
|
30
|
-
backend_kwargs=None,
|
|
31
|
-
show=None,
|
|
32
|
-
):
|
|
33
|
-
r"""Plot HDI intervals for regression data.
|
|
34
|
-
|
|
35
|
-
Parameters
|
|
36
|
-
----------
|
|
37
|
-
x : array-like
|
|
38
|
-
Values to plot.
|
|
39
|
-
y : array-like, optional
|
|
40
|
-
Values from which to compute the HDI. Assumed shape ``(chain, draw, \*shape)``.
|
|
41
|
-
Only optional if ``hdi_data`` is present.
|
|
42
|
-
hdi_data : array_like, optional
|
|
43
|
-
Precomputed HDI values to use. Assumed shape is ``(*x.shape, 2)``.
|
|
44
|
-
hdi_prob : float, optional
|
|
45
|
-
Probability for the highest density interval. Defaults to ``stats.ci_prob`` rcParam.
|
|
46
|
-
See :ref:`this section <common_ hdi_prob>` for usage examples.
|
|
47
|
-
color : str, default "C1"
|
|
48
|
-
Color used for the limits of the HDI and fill. Should be a valid matplotlib color.
|
|
49
|
-
circular : bool, default False
|
|
50
|
-
Whether to compute the HDI taking into account ``x`` is a circular variable
|
|
51
|
-
(in the range [-np.pi, np.pi]) or not. Defaults to False (i.e non-circular variables).
|
|
52
|
-
smooth : boolean, default True
|
|
53
|
-
If True the result will be smoothed by first computing a linear interpolation of the data
|
|
54
|
-
over a regular grid and then applying the Savitzky-Golay filter to the interpolated data.
|
|
55
|
-
smooth_kwargs : dict, optional
|
|
56
|
-
Additional keywords modifying the Savitzky-Golay filter. See
|
|
57
|
-
:func:`scipy:scipy.signal.savgol_filter` for details.
|
|
58
|
-
figsize : (float, float), optional
|
|
59
|
-
Figure size. If ``None``, it will be defined automatically.
|
|
60
|
-
fill_kwargs : dict, optional
|
|
61
|
-
Keywords passed to :meth:`mpl:matplotlib.axes.Axes.fill_between`
|
|
62
|
-
(use ``fill_kwargs={'alpha': 0}`` to disable fill) or to
|
|
63
|
-
:meth:`bokeh.plotting.Figure.patch`.
|
|
64
|
-
plot_kwargs : dict, optional
|
|
65
|
-
HDI limits keyword arguments, passed to :meth:`mpl:matplotlib.axes.Axes.plot` or
|
|
66
|
-
:meth:`bokeh.plotting.Figure.patch`.
|
|
67
|
-
hdi_kwargs : dict, optional
|
|
68
|
-
Keyword arguments passed to :func:`~arviz.hdi`. Ignored if ``hdi_data`` is present.
|
|
69
|
-
ax : axes, optional
|
|
70
|
-
Matplotlib axes or bokeh figures.
|
|
71
|
-
backend : {"matplotlib", "bokeh"}, default "matplotlib"
|
|
72
|
-
Select plotting backend.
|
|
73
|
-
backend_kwargs : dict, optional
|
|
74
|
-
These are kwargs specific to the backend being used, passed to
|
|
75
|
-
:func:`matplotlib.pyplot.subplots` or :class:`bokeh.plotting.figure`.
|
|
76
|
-
For additional documentation check the plotting method of the backend.
|
|
77
|
-
show : bool, optional
|
|
78
|
-
Call backend show function.
|
|
79
|
-
|
|
80
|
-
Returns
|
|
81
|
-
-------
|
|
82
|
-
axes : matplotlib axes or bokeh figures
|
|
83
|
-
|
|
84
|
-
See Also
|
|
85
|
-
--------
|
|
86
|
-
hdi : Calculate highest density interval (HDI) of array for given probability.
|
|
87
|
-
|
|
88
|
-
Examples
|
|
89
|
-
--------
|
|
90
|
-
Plot HDI interval of simulated random-walk data using `y` argument:
|
|
91
|
-
|
|
92
|
-
.. plot::
|
|
93
|
-
:context: close-figs
|
|
94
|
-
|
|
95
|
-
>>> import numpy as np
|
|
96
|
-
>>> import arviz as az
|
|
97
|
-
>>> # time-steps random walk
|
|
98
|
-
>>> x_data =np.arange(0,100)
|
|
99
|
-
>>> # Mean random walk
|
|
100
|
-
>>> mu = np.zeros(100)
|
|
101
|
-
>>> for i in x_data: mu[i] = mu[i-1] + np.random.normal(0, 1, 1)
|
|
102
|
-
>>> # Simulated pp samples form the random walk time series
|
|
103
|
-
>>> y_data = np.random.normal(2 + mu * 0.5, 0.5, size = (2, 50, 100))
|
|
104
|
-
>>> az.plot_hdi(x_data, y_data)
|
|
105
|
-
|
|
106
|
-
``plot_hdi`` can also be given precalculated values with the argument ``hdi_data``. This example
|
|
107
|
-
shows how to use :func:`~arviz.hdi` to precalculate the values and pass these values to
|
|
108
|
-
``plot_hdi``. Similarly to an example in ``hdi`` we are using the ``input_core_dims``
|
|
109
|
-
argument of :func:`~arviz.wrap_xarray_ufunc` to manually define the dimensions over which
|
|
110
|
-
to calculate the HDI.
|
|
111
|
-
|
|
112
|
-
.. plot::
|
|
113
|
-
:context: close-figs
|
|
114
|
-
|
|
115
|
-
>>> hdi_data = az.hdi(y_data, input_core_dims=[["draw"]])
|
|
116
|
-
>>> ax = az.plot_hdi(x_data, hdi_data=hdi_data[0], color="r", fill_kwargs={"alpha": .2})
|
|
117
|
-
>>> az.plot_hdi(x_data, hdi_data=hdi_data[1], color="k", ax=ax, fill_kwargs={"alpha": .2})
|
|
118
|
-
|
|
119
|
-
``plot_hdi`` can also be used with Inference Data objects. Here we use the posterior predictive
|
|
120
|
-
to plot the HDI interval.
|
|
121
|
-
|
|
122
|
-
.. plot::
|
|
123
|
-
:context: close-figs
|
|
124
|
-
|
|
125
|
-
>>> X = np.random.normal(0,1,100)
|
|
126
|
-
>>> Y = np.random.normal(2 + X * 0.5, 0.5, size=(2,10,100))
|
|
127
|
-
>>> idata = az.from_dict(posterior={"y": Y}, constant_data={"x":X})
|
|
128
|
-
>>> x_data = idata.constant_data.x
|
|
129
|
-
>>> y_data = idata.posterior.y
|
|
130
|
-
>>> az.plot_hdi(x_data, y_data)
|
|
131
|
-
|
|
132
|
-
"""
|
|
133
|
-
if hdi_kwargs is None:
|
|
134
|
-
hdi_kwargs = {}
|
|
135
|
-
|
|
136
|
-
x = np.asarray(x)
|
|
137
|
-
x_shape = x.shape
|
|
138
|
-
|
|
139
|
-
if isinstance(x[0], str):
|
|
140
|
-
raise NotImplementedError(
|
|
141
|
-
"The `arviz.plot_hdi()` function does not support categorical data. "
|
|
142
|
-
"Consider using `arviz.plot_forest()`."
|
|
143
|
-
)
|
|
144
|
-
if y is None and hdi_data is None:
|
|
145
|
-
raise ValueError("One of {y, hdi_data} is required")
|
|
146
|
-
if hdi_data is not None and y is not None:
|
|
147
|
-
warnings.warn("Both y and hdi_data arguments present, ignoring y")
|
|
148
|
-
elif hdi_data is not None:
|
|
149
|
-
hdi_prob = (
|
|
150
|
-
hdi_data.hdi.attrs.get("hdi_prob", np.nan) if hasattr(hdi_data, "hdi") else np.nan
|
|
151
|
-
)
|
|
152
|
-
if isinstance(hdi_data, Dataset):
|
|
153
|
-
data_vars = list(hdi_data.data_vars)
|
|
154
|
-
if len(data_vars) != 1:
|
|
155
|
-
raise ValueError(
|
|
156
|
-
"Found several variables in hdi_data. Only single variable Datasets are "
|
|
157
|
-
"supported."
|
|
158
|
-
)
|
|
159
|
-
hdi_data = hdi_data[data_vars[0]]
|
|
160
|
-
else:
|
|
161
|
-
y = np.asarray(y)
|
|
162
|
-
if hdi_prob is None:
|
|
163
|
-
hdi_prob = rcParams["stats.ci_prob"]
|
|
164
|
-
elif not 1 >= hdi_prob > 0:
|
|
165
|
-
raise ValueError("The value of hdi_prob should be in the interval (0, 1]")
|
|
166
|
-
hdi_data = hdi(y, hdi_prob=hdi_prob, circular=circular, multimodal=False, **hdi_kwargs)
|
|
167
|
-
|
|
168
|
-
hdi_shape = hdi_data.shape
|
|
169
|
-
if hdi_shape[:-1] != x_shape:
|
|
170
|
-
msg = (
|
|
171
|
-
"Dimension mismatch for x: {} and hdi: {}. Check the dimensions of y and"
|
|
172
|
-
"hdi_kwargs to make sure they are compatible"
|
|
173
|
-
)
|
|
174
|
-
raise TypeError(msg.format(x_shape, hdi_shape))
|
|
175
|
-
|
|
176
|
-
if smooth:
|
|
177
|
-
if isinstance(x[0], np.datetime64):
|
|
178
|
-
raise TypeError("Cannot deal with x as type datetime. Recommend setting smooth=False.")
|
|
179
|
-
|
|
180
|
-
if smooth_kwargs is None:
|
|
181
|
-
smooth_kwargs = {}
|
|
182
|
-
smooth_kwargs.setdefault("window_length", 55)
|
|
183
|
-
smooth_kwargs.setdefault("polyorder", 2)
|
|
184
|
-
x_data = np.linspace(x.min(), x.max(), 200)
|
|
185
|
-
x_data[0] = (x_data[0] + x_data[1]) / 2
|
|
186
|
-
hdi_interp = griddata(x, hdi_data, x_data)
|
|
187
|
-
y_data = savgol_filter(hdi_interp, axis=0, **smooth_kwargs)
|
|
188
|
-
else:
|
|
189
|
-
idx = np.argsort(x)
|
|
190
|
-
x_data = x[idx]
|
|
191
|
-
y_data = hdi_data[idx]
|
|
192
|
-
|
|
193
|
-
hdiplot_kwargs = dict(
|
|
194
|
-
ax=ax,
|
|
195
|
-
x_data=x_data,
|
|
196
|
-
y_data=y_data,
|
|
197
|
-
color=color,
|
|
198
|
-
figsize=figsize,
|
|
199
|
-
plot_kwargs=plot_kwargs,
|
|
200
|
-
fill_kwargs=fill_kwargs,
|
|
201
|
-
backend_kwargs=backend_kwargs,
|
|
202
|
-
show=show,
|
|
203
|
-
)
|
|
204
|
-
|
|
205
|
-
if backend is None:
|
|
206
|
-
backend = rcParams["plot.backend"]
|
|
207
|
-
backend = backend.lower()
|
|
208
|
-
|
|
209
|
-
plot = get_plotting_function("plot_hdi", "hdiplot", backend)
|
|
210
|
-
ax = plot(**hdiplot_kwargs)
|
|
211
|
-
return ax
|
arviz/plots/kdeplot.py
DELETED
|
@@ -1,357 +0,0 @@
|
|
|
1
|
-
# pylint: disable=unexpected-keyword-arg
|
|
2
|
-
"""One-dimensional kernel density estimate plots."""
|
|
3
|
-
import warnings
|
|
4
|
-
|
|
5
|
-
import xarray as xr
|
|
6
|
-
|
|
7
|
-
from ..data import InferenceData
|
|
8
|
-
from ..rcparams import rcParams
|
|
9
|
-
from ..stats.density_utils import _fast_kde_2d, kde, _find_hdi_contours
|
|
10
|
-
from .plot_utils import get_plotting_function, _init_kwargs_dict
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def plot_kde(
|
|
14
|
-
values,
|
|
15
|
-
values2=None,
|
|
16
|
-
cumulative=False,
|
|
17
|
-
rug=False,
|
|
18
|
-
label=None,
|
|
19
|
-
bw="default",
|
|
20
|
-
adaptive=False,
|
|
21
|
-
quantiles=None,
|
|
22
|
-
rotated=False,
|
|
23
|
-
contour=True,
|
|
24
|
-
hdi_probs=None,
|
|
25
|
-
fill_last=False,
|
|
26
|
-
figsize=None,
|
|
27
|
-
textsize=None,
|
|
28
|
-
plot_kwargs=None,
|
|
29
|
-
fill_kwargs=None,
|
|
30
|
-
rug_kwargs=None,
|
|
31
|
-
contour_kwargs=None,
|
|
32
|
-
contourf_kwargs=None,
|
|
33
|
-
pcolormesh_kwargs=None,
|
|
34
|
-
is_circular=False,
|
|
35
|
-
ax=None,
|
|
36
|
-
legend=True,
|
|
37
|
-
backend=None,
|
|
38
|
-
backend_kwargs=None,
|
|
39
|
-
show=None,
|
|
40
|
-
return_glyph=False,
|
|
41
|
-
**kwargs
|
|
42
|
-
):
|
|
43
|
-
"""1D or 2D KDE plot taking into account boundary conditions.
|
|
44
|
-
|
|
45
|
-
Parameters
|
|
46
|
-
----------
|
|
47
|
-
values : array-like
|
|
48
|
-
Values to plot
|
|
49
|
-
values2 : array-like, optional
|
|
50
|
-
Values to plot. If present, a 2D KDE will be estimated
|
|
51
|
-
cumulative : bool, dafault False
|
|
52
|
-
If True plot the estimated cumulative distribution function. Ignored for 2D KDE.
|
|
53
|
-
rug : bool, default False
|
|
54
|
-
Add a `rug plot <https://en.wikipedia.org/wiki/Rug_plot>`_ for a specific subset of
|
|
55
|
-
values. Ignored for 2D KDE.
|
|
56
|
-
label : string, optional
|
|
57
|
-
Text to include as part of the legend.
|
|
58
|
-
bw : float or str, optional
|
|
59
|
-
If numeric, indicates the bandwidth and must be positive.
|
|
60
|
-
If str, indicates the method to estimate the bandwidth and must be
|
|
61
|
-
one of "scott", "silverman", "isj" or "experimental" when ``is_circular`` is False
|
|
62
|
-
and "taylor" (for now) when ``is_circular`` is True.
|
|
63
|
-
Defaults to "default" which means "experimental" when variable is not circular
|
|
64
|
-
and "taylor" when it is.
|
|
65
|
-
adaptive : bool, default False
|
|
66
|
-
If True, an adaptative bandwidth is used. Only valid for 1D KDE.
|
|
67
|
-
quantiles : list, optional
|
|
68
|
-
Quantiles in ascending order used to segment the KDE. Use [.25, .5, .75] for quartiles.
|
|
69
|
-
rotated : bool, default False
|
|
70
|
-
Whether to rotate the 1D KDE plot 90 degrees.
|
|
71
|
-
contour : bool, default True
|
|
72
|
-
If True plot the 2D KDE using contours, otherwise plot a smooth 2D KDE.
|
|
73
|
-
hdi_probs : list, optional
|
|
74
|
-
Plots highest density credibility regions for the provided probabilities for a 2D KDE.
|
|
75
|
-
Defaults to [0.5, 0.8, 0.94].
|
|
76
|
-
fill_last : bool, default False
|
|
77
|
-
If True fill the last contour of the 2D KDE plot.
|
|
78
|
-
figsize : (float, float), optional
|
|
79
|
-
Figure size. If ``None`` it will be defined automatically.
|
|
80
|
-
textsize : float, optional
|
|
81
|
-
Text size scaling factor for labels, titles and lines. If ``None`` it will be autoscaled
|
|
82
|
-
based on ``figsize``. Not implemented for bokeh backend.
|
|
83
|
-
plot_kwargs : dict, optional
|
|
84
|
-
Keywords passed to the pdf line of a 1D KDE. See :meth:`mpl:matplotlib.axes.Axes.plot`
|
|
85
|
-
or :meth:`bokeh:bokeh.plotting.Figure.line` for a description of accepted values.
|
|
86
|
-
fill_kwargs : dict, optional
|
|
87
|
-
Keywords passed to the fill under the line (use ``fill_kwargs={'alpha': 0}``
|
|
88
|
-
to disable fill). Ignored for 2D KDE. Passed to
|
|
89
|
-
:meth:`bokeh.plotting.Figure.patch`.
|
|
90
|
-
rug_kwargs : dict, optional
|
|
91
|
-
Keywords passed to the rug plot. Ignored if ``rug=False`` or for 2D KDE
|
|
92
|
-
Use ``space`` keyword (float) to control the position of the rugplot. The larger this number
|
|
93
|
-
the lower the rugplot. Passed to :class:`bokeh:bokeh.models.glyphs.Scatter`.
|
|
94
|
-
contour_kwargs : dict, optional
|
|
95
|
-
Keywords passed to :meth:`mpl:matplotlib.axes.Axes.contour`
|
|
96
|
-
to draw contour lines or :meth:`bokeh.plotting.Figure.patch`.
|
|
97
|
-
Ignored for 1D KDE.
|
|
98
|
-
contourf_kwargs : dict, optional
|
|
99
|
-
Keywords passed to :meth:`mpl:matplotlib.axes.Axes.contourf`
|
|
100
|
-
to draw filled contours. Ignored for 1D KDE.
|
|
101
|
-
pcolormesh_kwargs : dict, optional
|
|
102
|
-
Keywords passed to :meth:`mpl:matplotlib.axes.Axes.pcolormesh` or
|
|
103
|
-
:meth:`bokeh.plotting.Figure.image`.
|
|
104
|
-
Ignored for 1D KDE.
|
|
105
|
-
is_circular : {False, True, "radians", "degrees"}. Default False
|
|
106
|
-
Select input type {"radians", "degrees"} for circular histogram or KDE plot. If True,
|
|
107
|
-
default input type is "radians". When this argument is present, it interprets ``values``
|
|
108
|
-
as a circular variable measured in radians and a circular KDE is used. Inputs in
|
|
109
|
-
"degrees" will undergo an internal conversion to radians.
|
|
110
|
-
ax : axes, optional
|
|
111
|
-
Matplotlib axes or bokeh figures.
|
|
112
|
-
legend : bool, default True
|
|
113
|
-
Add legend to the figure.
|
|
114
|
-
backend : {"matplotlib", "bokeh"}, default "matplotlib"
|
|
115
|
-
Select plotting backend.
|
|
116
|
-
backend_kwargs : dict, optional
|
|
117
|
-
These are kwargs specific to the backend being used, passed to
|
|
118
|
-
:func:`matplotlib.pyplot.subplots` or :class:`bokeh.plotting.figure`.
|
|
119
|
-
For additional documentation check the plotting method of the backend.
|
|
120
|
-
show : bool, optional
|
|
121
|
-
Call backend show function.
|
|
122
|
-
return_glyph : bool, optional
|
|
123
|
-
Internal argument to return glyphs for bokeh.
|
|
124
|
-
|
|
125
|
-
Returns
|
|
126
|
-
-------
|
|
127
|
-
axes : matplotlib.Axes or bokeh.plotting.Figure
|
|
128
|
-
Object containing the kde plot
|
|
129
|
-
glyphs : list, optional
|
|
130
|
-
Bokeh glyphs present in plot. Only provided if ``return_glyph`` is True.
|
|
131
|
-
|
|
132
|
-
See Also
|
|
133
|
-
--------
|
|
134
|
-
kde : One dimensional density estimation.
|
|
135
|
-
plot_dist : Plot distribution as histogram or kernel density estimates.
|
|
136
|
-
|
|
137
|
-
Examples
|
|
138
|
-
--------
|
|
139
|
-
Plot default KDE
|
|
140
|
-
|
|
141
|
-
.. plot::
|
|
142
|
-
:context: close-figs
|
|
143
|
-
|
|
144
|
-
>>> import arviz as az
|
|
145
|
-
>>> non_centered = az.load_arviz_data('non_centered_eight')
|
|
146
|
-
>>> mu_posterior = np.concatenate(non_centered.posterior["mu"].values)
|
|
147
|
-
>>> tau_posterior = np.concatenate(non_centered.posterior["tau"].values)
|
|
148
|
-
>>> az.plot_kde(mu_posterior)
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
Plot KDE with rugplot
|
|
152
|
-
|
|
153
|
-
.. plot::
|
|
154
|
-
:context: close-figs
|
|
155
|
-
|
|
156
|
-
>>> az.plot_kde(mu_posterior, rug=True)
|
|
157
|
-
|
|
158
|
-
Plot KDE with adaptive bandwidth
|
|
159
|
-
|
|
160
|
-
.. plot::
|
|
161
|
-
:context: close-figs
|
|
162
|
-
|
|
163
|
-
>>> az.plot_kde(mu_posterior, adaptive=True)
|
|
164
|
-
|
|
165
|
-
Plot KDE with a different bandwidth estimator
|
|
166
|
-
|
|
167
|
-
.. plot::
|
|
168
|
-
:context: close-figs
|
|
169
|
-
|
|
170
|
-
>>> az.plot_kde(mu_posterior, bw="scott")
|
|
171
|
-
|
|
172
|
-
Plot KDE with a bandwidth specified manually
|
|
173
|
-
|
|
174
|
-
.. plot::
|
|
175
|
-
:context: close-figs
|
|
176
|
-
|
|
177
|
-
>>> az.plot_kde(mu_posterior, bw=0.4)
|
|
178
|
-
|
|
179
|
-
Plot KDE for a circular variable
|
|
180
|
-
|
|
181
|
-
.. plot::
|
|
182
|
-
:context: close-figs
|
|
183
|
-
|
|
184
|
-
>>> rvs = np.random.vonmises(mu=np.pi, kappa=2, size=500)
|
|
185
|
-
>>> az.plot_kde(rvs, is_circular=True)
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
Plot a cumulative distribution
|
|
189
|
-
|
|
190
|
-
.. plot::
|
|
191
|
-
:context: close-figs
|
|
192
|
-
|
|
193
|
-
>>> az.plot_kde(mu_posterior, cumulative=True)
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
Rotate plot 90 degrees
|
|
198
|
-
|
|
199
|
-
.. plot::
|
|
200
|
-
:context: close-figs
|
|
201
|
-
|
|
202
|
-
>>> az.plot_kde(mu_posterior, rotated=True)
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
Plot 2d contour KDE
|
|
206
|
-
|
|
207
|
-
.. plot::
|
|
208
|
-
:context: close-figs
|
|
209
|
-
|
|
210
|
-
>>> az.plot_kde(mu_posterior, values2=tau_posterior)
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
Plot 2d contour KDE, without filling and contour lines using viridis cmap
|
|
214
|
-
|
|
215
|
-
.. plot::
|
|
216
|
-
:context: close-figs
|
|
217
|
-
|
|
218
|
-
>>> az.plot_kde(mu_posterior, values2=tau_posterior,
|
|
219
|
-
... contour_kwargs={"colors":None, "cmap":plt.cm.viridis},
|
|
220
|
-
... contourf_kwargs={"alpha":0});
|
|
221
|
-
|
|
222
|
-
Plot 2d contour KDE, set the number of levels to 3.
|
|
223
|
-
|
|
224
|
-
.. plot::
|
|
225
|
-
:context: close-figs
|
|
226
|
-
|
|
227
|
-
>>> az.plot_kde(
|
|
228
|
-
... mu_posterior, values2=tau_posterior,
|
|
229
|
-
... contour_kwargs={"levels":3}, contourf_kwargs={"levels":3}
|
|
230
|
-
... );
|
|
231
|
-
|
|
232
|
-
Plot 2d contour KDE with 30%, 60% and 90% HDI contours.
|
|
233
|
-
|
|
234
|
-
.. plot::
|
|
235
|
-
:context: close-figs
|
|
236
|
-
|
|
237
|
-
>>> az.plot_kde(mu_posterior, values2=tau_posterior, hdi_probs=[0.3, 0.6, 0.9])
|
|
238
|
-
|
|
239
|
-
Plot 2d smooth KDE
|
|
240
|
-
|
|
241
|
-
.. plot::
|
|
242
|
-
:context: close-figs
|
|
243
|
-
|
|
244
|
-
>>> az.plot_kde(mu_posterior, values2=tau_posterior, contour=False)
|
|
245
|
-
|
|
246
|
-
"""
|
|
247
|
-
if isinstance(values, xr.Dataset):
|
|
248
|
-
raise ValueError(
|
|
249
|
-
"Xarray dataset object detected. Use plot_posterior, plot_density "
|
|
250
|
-
"or plot_pair instead of plot_kde"
|
|
251
|
-
)
|
|
252
|
-
if isinstance(values, InferenceData):
|
|
253
|
-
raise ValueError(
|
|
254
|
-
" Inference Data object detected. Use plot_posterior "
|
|
255
|
-
"or plot_pair instead of plot_kde"
|
|
256
|
-
)
|
|
257
|
-
|
|
258
|
-
if backend is None:
|
|
259
|
-
backend = rcParams["plot.backend"]
|
|
260
|
-
backend = backend.lower()
|
|
261
|
-
|
|
262
|
-
if values2 is None:
|
|
263
|
-
if bw == "default":
|
|
264
|
-
bw = "taylor" if is_circular else "experimental"
|
|
265
|
-
|
|
266
|
-
grid, density = kde(values, is_circular, bw=bw, adaptive=adaptive, cumulative=cumulative)
|
|
267
|
-
lower, upper = grid[0], grid[-1]
|
|
268
|
-
|
|
269
|
-
density_q = density if cumulative else density.cumsum() / density.sum()
|
|
270
|
-
|
|
271
|
-
# This is just a hack placeholder for now
|
|
272
|
-
xmin, xmax, ymin, ymax, gridsize = [None] * 5
|
|
273
|
-
else:
|
|
274
|
-
gridsize = (128, 128) if contour else (256, 256)
|
|
275
|
-
density, xmin, xmax, ymin, ymax = _fast_kde_2d(values, values2, gridsize=gridsize)
|
|
276
|
-
|
|
277
|
-
if hdi_probs is None:
|
|
278
|
-
hdi_probs = [0.5, 0.8, 0.94]
|
|
279
|
-
|
|
280
|
-
if hdi_probs is not None:
|
|
281
|
-
# Check hdi probs are within bounds (0, 1)
|
|
282
|
-
if min(hdi_probs) <= 0 or max(hdi_probs) >= 1:
|
|
283
|
-
raise ValueError("Highest density interval probabilities must be between 0 and 1")
|
|
284
|
-
|
|
285
|
-
# Calculate contour levels and sort for matplotlib
|
|
286
|
-
contour_levels = _find_hdi_contours(density, hdi_probs)
|
|
287
|
-
contour_levels.sort()
|
|
288
|
-
|
|
289
|
-
contour_level_list = [0] + list(contour_levels) + [density.max()]
|
|
290
|
-
|
|
291
|
-
# Add keyword arguments to contour, contourf
|
|
292
|
-
contour_kwargs = _init_kwargs_dict(contour_kwargs)
|
|
293
|
-
if "levels" in contour_kwargs:
|
|
294
|
-
warnings.warn(
|
|
295
|
-
"Both 'levels' in contour_kwargs and 'hdi_probs' have been specified."
|
|
296
|
-
"Using 'hdi_probs' in favor of 'levels'.",
|
|
297
|
-
UserWarning,
|
|
298
|
-
)
|
|
299
|
-
|
|
300
|
-
if backend == "bokeh":
|
|
301
|
-
contour_kwargs["levels"] = contour_level_list
|
|
302
|
-
elif backend == "matplotlib":
|
|
303
|
-
contour_kwargs["levels"] = contour_level_list[1:]
|
|
304
|
-
|
|
305
|
-
contourf_kwargs = _init_kwargs_dict(contourf_kwargs)
|
|
306
|
-
if "levels" in contourf_kwargs:
|
|
307
|
-
warnings.warn(
|
|
308
|
-
"Both 'levels' in contourf_kwargs and 'hdi_probs' have been specified."
|
|
309
|
-
"Using 'hdi_probs' in favor of 'levels'.",
|
|
310
|
-
UserWarning,
|
|
311
|
-
)
|
|
312
|
-
contourf_kwargs["levels"] = contour_level_list
|
|
313
|
-
|
|
314
|
-
lower, upper, density_q = [None] * 3
|
|
315
|
-
|
|
316
|
-
kde_plot_args = dict(
|
|
317
|
-
# Internal API
|
|
318
|
-
density=density,
|
|
319
|
-
lower=lower,
|
|
320
|
-
upper=upper,
|
|
321
|
-
density_q=density_q,
|
|
322
|
-
xmin=xmin,
|
|
323
|
-
xmax=xmax,
|
|
324
|
-
ymin=ymin,
|
|
325
|
-
ymax=ymax,
|
|
326
|
-
gridsize=gridsize,
|
|
327
|
-
# User Facing API that can be simplified
|
|
328
|
-
values=values,
|
|
329
|
-
values2=values2,
|
|
330
|
-
rug=rug,
|
|
331
|
-
label=label,
|
|
332
|
-
quantiles=quantiles,
|
|
333
|
-
rotated=rotated,
|
|
334
|
-
contour=contour,
|
|
335
|
-
fill_last=fill_last,
|
|
336
|
-
figsize=figsize,
|
|
337
|
-
textsize=textsize,
|
|
338
|
-
plot_kwargs=plot_kwargs,
|
|
339
|
-
fill_kwargs=fill_kwargs,
|
|
340
|
-
rug_kwargs=rug_kwargs,
|
|
341
|
-
contour_kwargs=contour_kwargs,
|
|
342
|
-
contourf_kwargs=contourf_kwargs,
|
|
343
|
-
pcolormesh_kwargs=pcolormesh_kwargs,
|
|
344
|
-
is_circular=is_circular,
|
|
345
|
-
ax=ax,
|
|
346
|
-
legend=legend,
|
|
347
|
-
backend_kwargs=backend_kwargs,
|
|
348
|
-
show=show,
|
|
349
|
-
return_glyph=return_glyph,
|
|
350
|
-
**kwargs,
|
|
351
|
-
)
|
|
352
|
-
|
|
353
|
-
# TODO: Add backend kwargs
|
|
354
|
-
plot = get_plotting_function("plot_kde", "kdeplot", backend)
|
|
355
|
-
ax = plot(**kde_plot_args)
|
|
356
|
-
|
|
357
|
-
return ax
|