-
-
Notifications
You must be signed in to change notification settings - Fork 380
/
autocorrplot.py
149 lines (119 loc) · 4.02 KB
/
autocorrplot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
"""Autocorrelation plot of data."""
import numpy as np
from ..data import convert_to_dataset
from .plot_utils import (
_scale_fig_size,
default_grid,
xarray_var_iter,
_create_axes_grid,
filter_plotters_list,
get_plotting_method,
)
from ..utils import _var_names
def plot_autocorr(
data,
var_names=None,
max_lag=None,
combined=False,
figsize=None,
textsize=None,
ax=None,
backend=None,
show=True,
):
"""Bar plot of the autocorrelation function for a sequence of data.
Useful in particular for posteriors from MCMC samples which may display correlation.
Parameters
----------
data : obj
Any object that can be converted to an az.InferenceData object
Refer to documentation of az.convert_to_dataset for details
var_names : list of variable names, optional
Variables to be plotted, if None all variable are plotted.
Vector-value stochastics are handled automatically.
max_lag : int, optional
Maximum lag to calculate autocorrelation. Defaults to 100 or num draws, whichever is smaller
combined : bool
Flag for combining multiple chains into a single chain. If False (default), chains will be
plotted separately.
figsize : tuple
Figure size. If None it will be defined automatically.
Note this is not used if ax is supplied.
textsize: float
Text size scaling factor for labels, titles and lines. If None it will be autoscaled based
on figsize.
ax: axes, optional
Matplotlib axes or bokeh figures.
backend: str, optional
Select plotting backend {"matplotlib","bokeh"}. Default "matplotlib".
show: bool, optional
If True, call bokeh.plotting.show.
Returns
-------
axes : matplotlib axes or bokeh figures
Examples
--------
Plot default autocorrelation
.. plot::
:context: close-figs
>>> import arviz as az
>>> data = az.load_arviz_data('centered_eight')
>>> az.plot_autocorr(data)
Plot subset variables by specifying variable name exactly
.. plot::
:context: close-figs
>>> az.plot_autocorr(data, var_names=['mu', 'tau'] )
Combine chains collapsing by variable
.. plot::
:context: close-figs
>>> az.plot_autocorr(data, var_names=['mu', 'tau'], combined=True)
Specify maximum lag (x axis bound)
.. plot::
:context: close-figs
>>> az.plot_autocorr(data, var_names=['mu', 'tau'], max_lag=200, combined=True)
"""
data = convert_to_dataset(data, group="posterior")
var_names = _var_names(var_names, data)
# Default max lag to 100 or max length of chain
if max_lag is None:
max_lag = min(100, data["draw"].shape[0])
plotters = filter_plotters_list(
list(xarray_var_iter(data, var_names, combined)), "plot_autocorr"
)
length_plotters = len(plotters)
rows, cols = default_grid(length_plotters)
figsize, _, titlesize, xt_labelsize, linewidth, _ = _scale_fig_size(
figsize, textsize, rows, cols
)
if ax is None:
_, axes = _create_axes_grid(
length_plotters,
rows,
cols,
figsize=figsize,
squeeze=False,
sharex=True,
sharey=True,
backend=backend,
)
else:
axes = ax
axes = np.atleast_2d(axes) # in case of only 1 plot
autocorr_plot_args = dict(
axes=axes,
plotters=plotters,
max_lag=max_lag,
combined=combined,
linewidth=linewidth,
xt_labelsize=xt_labelsize,
titlesize=titlesize,
)
if backend == "bokeh":
autocorr_plot_args.pop("xt_labelsize")
autocorr_plot_args.pop("titlesize")
autocorr_plot_args["line_width"] = autocorr_plot_args.pop("linewidth")
autocorr_plot_args["show"] = show
# TODO: Add backend kwargs
method = get_plotting_method("plot_autocorr", "autocorrplot", backend)
axes = method(**autocorr_plot_args)
return axes