Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
ax[j, i].tick_params(labelsize=xt_labelsize)
ax[j, i].set_title(
"{} - {}".format(models[i], models[j + 1]), fontsize=titlesize, wrap=True
)
if xlabels:
set_xticklabels(ax[-1, -1], coord_labels)
fig.autofmt_xdate()
fig.tight_layout()
if legend:
ncols = len(handles) // 6 + 1
ax[0, 1].legend(
handles=handles, ncol=ncols, title=color, bbox_to_anchor=(0, 1), loc="upper left"
)
if backend_show(show):
plt.show()
return ax
"""
LOO-PIT Overlay Plot
====================
_thumb: .5, .7
"""
import arviz as az
az.style.use("arviz-darkgrid")
idata = az.load_arviz_data("non_centered_eight")
az.plot_loo_pit(idata=idata, y="obs", color="indigo")
"""
LOO-PIT ECDF Plot
=================
_thumb: .5, .7
"""
import arviz as az
az.style.use("arviz-darkgrid")
idata = az.load_arviz_data("radon")
log_like = idata.sample_stats.log_likelihood.sel(chain=0).values.T
log_weights = az.psislw(-log_like)[0]
az.plot_loo_pit(idata, y="y_like", log_weights=log_weights, ecdf=True, color="maroon")
chain_data = []
for path in posterior_predictive:
parsed_output = _read_output(path)
for sample, *_ in parsed_output:
chain_data.append(sample)
data = _unpack_dataframes(chain_data)
else:
if isinstance(posterior_predictive, str):
posterior_predictive = [posterior_predictive]
posterior_predictive_cols = [
col
for col in columns
if any(item == col.split(".")[0] for item in posterior_predictive)
]
data = _unpack_dataframes([item[posterior_predictive_cols] for item in self.posterior])
return dict_to_dataset(data, coords=self.coords, dims=self.dims)
def prior_predictive_to_xarray(self):
"""Convert prior_predictive samples to xarray."""
prior_predictive = self.prior_predictive
columns = self.prior.column_names
if isinstance(prior_predictive, str):
prior_predictive = [prior_predictive]
valid_cols = [col for col in columns if col.split(".")[0] in set(prior_predictive)]
data = _unpack_frame(self.prior.sample, columns, valid_cols)
return dict_to_dataset(data, library=self.cmdstanpy, coords=self.coords, dims=self.dims)
def posterior_predictive_to_xarray(self):
"""Convert posterior_predictive samples to xarray."""
posterior_predictive = self.posterior_predictive
columns = self.posterior.column_names
if isinstance(posterior_predictive, str):
posterior_predictive = [posterior_predictive]
valid_cols = [col for col in columns if col.split(".")[0] in set(posterior_predictive)]
data = _unpack_frame(self.posterior.sample, columns, valid_cols)
return dict_to_dataset(data, library=self.cmdstanpy, coords=self.coords, dims=self.dims)
"""
LOO-PIT Overlay Plot
====================
_thumb: .5, .7
"""
import arviz as az
az.style.use("arviz-darkgrid")
idata = az.load_arviz_data("non_centered_eight")
az.plot_loo_pit(idata=idata, y="obs", color="indigo")
@requires("prior")
def prior_to_xarray(self):
"""Convert prior samples to xarray."""
# filter prior_predictive
prior_predictive = self.prior_predictive
columns = self.prior[0].columns
if prior_predictive is None or (
isinstance(prior_predictive, str) and prior_predictive.lower().endswith(".csv")
):
prior_predictive = []
elif isinstance(prior_predictive, str):
prior_predictive = [col for col in columns if prior_predictive == col.split(".")[0]]
else:
prior_predictive = [
col
for col in columns
if any(item == col.split(".")[0] for item in prior_predictive)
@requires("posterior_predictive")
def posterior_predictive_to_xarray(self):
"""Convert posterior_predictive samples to xarray."""
posterior_predictive = self.posterior_predictive
columns = self.posterior.column_names
if isinstance(posterior_predictive, str):
posterior_predictive = [posterior_predictive]
valid_cols = [col for col in columns if col.split(".")[0] in set(posterior_predictive)]
data = _unpack_frame(self.posterior.sample, columns, valid_cols)
return dict_to_dataset(data, library=self.cmdstanpy, coords=self.coords, dims=self.dims)
@requires("sample_stats")
def sample_stats_to_xarray(self):
"""Extract sample_stats from fit."""
dtypes = {"divergent__": bool, "n_leapfrog__": np.int64, "treedepth__": np.int64}
# copy dims and coords
dims = deepcopy(self.dims) if self.dims is not None else {}
coords = deepcopy(self.coords) if self.coords is not None else {}
sampler_params = self.sample_stats
log_likelihood = self.log_likelihood
if isinstance(log_likelihood, str):
log_likelihood_cols = [
col for col in self.posterior[0].columns if log_likelihood == col.split(".")[0]
]
log_likelihood_vals = [item[log_likelihood_cols] for item in self.posterior]