API Reference
ArviZ.InferenceData
— TypeInferenceData(::PyObject)
InferenceData(; kwargs...)
Loose wrapper around arviz.InferenceData
, which is a container for inference data storage using xarray.
InferenceData
can be constructed either from an arviz.InferenceData
or from multiple Dataset
s assigned to groups specified as kwargs
.
Instead of directly creating an InferenceData
, use the exported from_xyz
functions or convert_to_inference_data
.
ArviZ.autocorr
— MethodSee documentation for arviz.autocorr
.
ArviZ.autocov
— MethodSee documentation for arviz.autocov
.
ArviZ.bfmi
— MethodSee documentation for arviz.bfmi
.
ArviZ.compare
— MethodSee documentation for arviz.compare
.
ArviZ.concat!
— Methodconcat!(data1::InferenceData, data::InferenceData...; kwargs...) -> InferenceData
In-place version of concat
, where data1
is modified to contain the concatenation of data
and args
. See concat
for a description of kwargs
.
ArviZ.concat
— MethodSee documentation for arviz.concat
.
ArviZ.convert_to_inference_data
— MethodSee documentation for arviz.convert_to_inference_data
.
ArviZ.convert_to_inference_data
— Methodconvert_to_inference_data(obj::AbstractParticles; kwargs...) -> InferenceData
convert_to_inference_data(
obj::AbstractVector{<:AbstractParticles};
kwargs...,
) -> InferenceData
convert_to_inference_data(
obj::AbstractVector{<:AbstractArray{<:AbstractParticles}};
kwargs...,
) -> InferenceData
Convert MonteCarloMeasurements.AbstractParticles
to an InferenceData
.
obj
may have the following types:
::AbstractParticles
: Univariate draws from a single chain.::AbstractVector{<:AbstractParticles}
: Univariate draws from a vector of chains.::AbstractVector{<:AbstractArray{<:AbstractParticles}}
: Multivariate draws from a vector of chains.
ArviZ.convert_to_inference_data
— Methodconvert_to_inference_data(obj::Chains; group = :posterior, kwargs...) -> InferenceData
Convert the chains obj
to an InferenceData
with the specified group
.
Remaining kwargs
are forwarded to from_mcmcchains
.
ArviZ.convert_to_inference_data
— Methodconvert_to_inference_data(obj::NamedTuple; kwargs...) -> InferenceData
convert_to_inference_data(obj::Vector{<:NamedTuple}; kwargs...) -> InferenceData
convert_to_inference_data(obj::Matrix{<:NamedTuple}; kwargs...) -> InferenceData
convert_to_inference_data(obj::Vector{Vector{<:NamedTuple}}; kwargs...) -> InferenceData
Convert obj
to an InferenceData
. See from_namedtuple
for a description of obj
possibilities and kwargs
.
ArviZ.ess
— MethodSee documentation for arviz.ess
.
ArviZ.from_cmdstan
— MethodSee documentation for arviz.from_cmdstan
.
ArviZ.from_cmdstan
— Methodfrom_cmdstan(posterior::Chains; kwargs...) -> InferenceData
Call from_mcmcchains
on output of CmdStan
.
ArviZ.from_dict
— MethodSee documentation for arviz.from_dict
.
ArviZ.from_mcmcchains
— Methodfrom_mcmcchains(posterior::Chains; kwargs...) -> InferenceData
from_mcmcchains(; kwargs...) -> InferenceData
from_mcmcchains(
posterior::Chains,
posterior_predictive::Any,
log_likelihood::String;
kwargs...
) -> InferenceData
Convert data in an MCMCChains.Chains
format into an InferenceData
.
Any keyword argument below without an an explicitly annotated type above is allowed, so long as it can be passed to convert_to_inference_data
.
Arguments
posterior::Chains
: Draws from the posterior
Keywords
posterior_predictive::Any=nothing
: Draws from the posterior predictive distribution or name(s) of predictive variables inposterior
prior::Any=nothing
: Draws from the priorprior_predictive::Any=nothing
: Draws from the prior predictive distribution or name(s) of predictive variables inprior
observed_data::Dict{String,Array}=nothing
: Observed data on which theposterior
is conditional. It should only contain data which is modeled as a random variable. Keys are parameter names and values.constant_data::Dict{String,Array}=nothing
: Model constants, data included in the model which is not modeled as a random variable. Keys are parameter names and values.log_likelihood::String=nothing
: Name of variable inposterior
with log likelihoodslibrary=MCMCChains
: Name of library that generated the chainscoords::Dict{String,Vector}=nothing
: Map from named dimension to named indicesdims::Dict{String,Vector{String}}=nothing
: Map from variable name to names of its dimensions
Returns
InferenceData
: The data with groups corresponding to the provided data
ArviZ.from_namedtuple
— Functionfrom_namedtuple(posterior::NamedTuple; kwargs...) -> InferenceData
from_namedtuple(posterior::Vector{<:NamedTuple}; kwargs...) -> InferenceData
from_namedtuple(posterior::Matrix{<:NamedTuple}; kwargs...) -> InferenceData
from_namedtuple(posterior::Vector{Vector{<:NamedTuple}}; kwargs...) -> InferenceData
Convert a NamedTuple
or container of NamedTuple
s to an InferenceData
.
If containers are passed, they are flattened into a single NamedTuple
with array elements whose first dimensions correspond to the dimensions of the containers.
Arguments
posterior
: The data to be converted. It may be of the following types:::NamedTuple
: The keys are the variable names and the values are arrays with dimensions(nchains, ndraws, sizes...)
.::Vector{<:NamedTuple}
: Each element is aNamedTuple
from a chain withArray
/MonteCarloMeasurements.Particle
values with dimensions(ndraws, sizes...)
.::Matrix{<:NamedTuple}
: Each element is a single draw from a single chain, with array/scalar values with dimensionssizes
. The dimensions of the matrix container are(nchains, ndraws)
::Vector{Vector{<:NamedTuple}}
: The same as the above case.
Keywords
posterior_predictive::Any=nothing
: Draws from the posterior predictive distributionsample_stats::Any=nothing
: Statistics of the posterior sampling processprior::Any=nothing
: Draws from the priorprior_predictive::Any=nothing
: Draws from the prior predictive distributionsample_stats_prior::Any=nothing
: Statistics of the prior sampling processobserved_data::Dict{String,Array}=nothing
: Observed data on which theposterior
is conditional. It should only contain data which is modeled as a random variable. Keys are parameter names and values.constant_data::Dict{String,Array}=nothing
: Model constants, data included in the model which is not modeled as a random variable. Keys are parameter names and values.library=nothing
: Name of library that generated the drawscoords::Dict{String,Vector}=nothing
: Map from named dimension to named indicesdims::Dict{String,Vector{String}}=nothing
: Map from variable name to names of its dimensions
Returns
InferenceData
: The data with groups corresponding to the provided data
Examples
using ArviZ
nchains, ndraws = 2, 10
data1 = (
x = rand(nchains, ndraws),
y = randn(nchains, ndraws, 2),
z = randn(nchains, ndraws, 3, 2),
)
idata1 = from_namedtuple(data1)
data2 = [(x = rand(ndraws), y = randn(ndraws, 2), z = randn(ndraws, 3, 2)) for _ = 1:nchains];
idata2 = from_namedtuple(data2)
data3 = [(x = rand(), y = randn(2), z = randn(3, 2)) for _ = 1:nchains, _ = 1:ndraws];
idata3 = from_namedtuple(data3)
data4 = [[(x = rand(), y = randn(2), z = randn(3, 2)) for _ = 1:ndraws] for _ = 1:nchains];
idata4 = from_namedtuple(data4)
ArviZ.from_netcdf
— MethodSee documentation for arviz.from_netcdf
.
ArviZ.geweke
— MethodSee documentation for arviz.geweke
.
ArviZ.hpd
— MethodSee documentation for arviz.hpd
.
ArviZ.load_arviz_data
— MethodSee documentation for arviz.load_arviz_data
.
ArviZ.loo
— MethodSee documentation for arviz.loo
.
ArviZ.loo_pit
— MethodSee documentation for arviz.loo_pit
.
ArviZ.make_ufunc
— MethodSee documentation for arviz.make_ufunc
.
ArviZ.mcse
— MethodSee documentation for arviz.mcse
.
ArviZ.plot_autocorr
— MethodSee documentation for arviz.plot_autocorr
.
ArviZ.plot_compare
— MethodSee documentation for arviz.plot_compare
.
ArviZ.plot_density
— MethodSee documentation for arviz.plot_density
.
ArviZ.plot_dist
— MethodSee documentation for arviz.plot_dist
.
ArviZ.plot_elpd
— MethodSee documentation for arviz.plot_elpd
.
ArviZ.plot_energy
— MethodSee documentation for arviz.plot_energy
.
ArviZ.plot_ess
— MethodSee documentation for arviz.plot_ess
.
ArviZ.plot_forest
— MethodSee documentation for arviz.plot_forest
.
ArviZ.plot_hpd
— MethodSee documentation for arviz.plot_hpd
.
ArviZ.plot_joint
— MethodSee documentation for arviz.plot_joint
.
ArviZ.plot_kde
— MethodSee documentation for arviz.plot_kde
.
ArviZ.plot_khat
— MethodSee documentation for arviz.plot_khat
.
ArviZ.plot_loo_pit
— MethodSee documentation for arviz.plot_loo_pit
.
ArviZ.plot_mcse
— MethodSee documentation for arviz.plot_mcse
.
ArviZ.plot_pair
— MethodSee documentation for arviz.plot_pair
.
ArviZ.plot_parallel
— MethodSee documentation for arviz.plot_parallel
.
ArviZ.plot_posterior
— MethodSee documentation for arviz.plot_posterior
.
ArviZ.plot_ppc
— MethodSee documentation for arviz.plot_ppc
.
ArviZ.plot_rank
— MethodSee documentation for arviz.plot_rank
.
ArviZ.plot_trace
— MethodSee documentation for arviz.plot_trace
.
ArviZ.plot_violin
— MethodSee documentation for arviz.plot_violin
.
ArviZ.psislw
— MethodSee documentation for arviz.psislw
.
ArviZ.r2_score
— MethodSee documentation for arviz.r2_score
.
ArviZ.rhat
— MethodSee documentation for arviz.rhat
.
ArviZ.to_netcdf
— MethodSee documentation for arviz.to_netcdf
.
ArviZ.waic
— MethodSee documentation for arviz.waic
.
ArviZ.with_interactive_backend
— Methodwith_interactive_backend(f; backend::Symbol = nothing)
Execute the thunk f
in a temporary interactive context with the chosen backend
, or provide no arguments to use a default.
Example
idata = load_arviz_data("centered_eight")
plot_posterior(idata) # inline
with_interactive_backend() do
plot_density(idata) # interactive
end
plot_trace(idata) # inline
ArviZ.with_rc_context
— Methodwith_rc_context(f; rc = nothing, fname = nothing)
Execute the thunk f
within a context controlled by rc params. To see supported params, execute rc_params()
.
This allows one to do:
with_rc_context(fname = "pystan.rc") do
idata = load_arviz_data("radon")
plot_posterior(idata; var_names=["gamma"])
end
The plot would have settings from pystan.rc
.
A dictionary can also be passed to the context manager:
with_rc_context(rc = Dict("plot.max_subplots" => 1), fname = "pystan.rc") do
idata = load_arviz_data("radon")
plot_posterior(idata, var_names=["gamma"])
end
The rc
dictionary takes precedence over the settings loaded from fname
. Passing a dictionary only is also valid.
ArviZ.wrap_xarray_ufunc
— MethodSee documentation for arviz.wrap_xarray_ufunc
.
StatsBase.summarystats
— Methodsummarystats(data::Dataset; kwargs...) -> Union{Pandas.DataFrame,Dataset}
summarystats(
data::InferenceData;
group = :posterior,
kwargs...,
) -> Union{Pandas.DataFrame,Dataset}
Compute summary statistics on data
.
Arguments
data::Union{Dataset,InferenceData}
: The data on which to compute summary statistics. Ifdata
is anInferenceData
, only the dataset corresponding togroup
is used.
Keywords
var_names::Vector{String}=nothing
: Names of variables to include in summaryinclude_circ::Bool=false
: Whether to include circular statisticsfmt::String="wide"
: Return format is eitherPandas.DataFrame
("wide", "long") orDataset
("xarray").round_to::Int=nothing
: Number of decimals used to round results. Usenothing
to return raw numbers.stat_funcs::Union{Dict{String,Function},Vector{Function}}=nothing
: A vector of functions or a dict of functions with function names as keys used to calculate statistics. By default, the mean, standard deviation, simulation standard error, and highest posterior density intervals are included. The functions will be given one argument, the samples for a variable as an array, The functions should operate on an array, returning a single number. For example,Statistics.mean
, orStatistics.var
would both work.extend::Bool=true
: Iftrue
, use the statistics returned bystat_funcs
in addition to, rather than in place of, the default statistics. This is only meaningful whenstat_funcs
is notnothing
.credible_interval::Real=0.94
: Credible interval to plot. This is only meaningful whenstat_funcs
isnothing
.order::String="C"
: Iffmt
is "wide", use either "C" or "F" unpacking order.index_origin::Int=1
: Iffmt
is "wide", select 𝑛-based indexing for multivariate parameters.skipna::Bool=false
: Iftrue
, ignoresNaN
values when computing the summary statistics. It does not affect the behaviour of the functions passed tostat_funcs
.coords::Dict{String,Vector}=Dict()
: Coordinates specification to be used if thefmt
is"xarray"
.dims::Dict{String,Vector}=Dict()
: Dimensions specification for the variables to be used if thefmt
is"xarray"
.
Returns
Union{Pandas.DataFrame,Dataset}
: Return type dicated byfmt
argument. Return value will contain summary statistics for each variable. Default statistics are:mean
sd
hpd_3%
hpd_97%
mcse_mean
mcse_sd
ess_bulk
ess_tail
r_hat
(only computed for traces with 2 or more chains)
Examples
using ArviZ
idata = load_arviz_data("centered_eight")
summarystats(idata; var_names=["mu", "tau"])
Other statistics can be calculated by passing a list of functions or a dictionary with key, function pairs:
using StatsBase, Statistics
function median_sd(x)
med = median(x)
sd = sqrt(mean((x .- med).^2))
return sd
end
func_dict = Dict(
"std" => x -> std(x; corrected = false),
"median_std" => median_sd,
"5%" => x -> percentile(x, 5),
"median" => median,
"95%" => x -> percentile(x, 95),
)
summarystats(idata; var_names = ["mu", "tau"], stat_funcs = func_dict, extend = false)
ArviZ.BokehPlot
— TypeBokehPlot(::PyObject)
Loose wrapper around a Bokeh figure, mostly used for dispatch.
In most cases, use one of the plotting functions with backend=:bokeh
to create a BokehPlot
instead of using a constructor.
ArviZ.Dataset
— TypeDataset(::PyObject)
Dataset(; data_vars = nothing, coords = nothing, attrs = nothing)
Loose wrapper around xarray.Dataset
, mostly used for dispatch.
Keywords
data_vars::Dict{String,Any}
: Dict mapping variable names toVector
: Data vector. Single dimension is named after variable.Tuple{String,Vector}
: Dimension name and data vector.Tuple{NTuple{N,String},Array{T,N}} where {N,T}
: Dimension names and data array.
coords::Dict{String,Any}
: Dict mapping dimension names to index names. Possible arguments has same form asdata_vars
.attrs::Dict{String,Any}
: Global attributes to save on this dataset.
In most cases, use convert_to_dataset
or convert_to_constant_dataset
or to create a Dataset
instead of directly using a constructor.
ArviZ.convert_arguments
— Methodconvert_arguments(f, args...; kwargs...) -> NTuple{2}
Convert arguments to the function f
before calling.
This function is used primarily for pre-processing arguments within macros before sending to arviz.
ArviZ.convert_result
— Methodconvert_result(f, result)
Convert result of the function f
before returning.
This function is used primarily for post-processing outputs of arviz before returning.
ArviZ.convert_to_constant_dataset
— Methodconvert_to_constant_dataset(obj::Dict; kwargs...) -> Dataset
convert_to_constant_dataset(obj::NamedTuple; kwargs...) -> Dataset
Convert obj
into a Dataset
.
Unlike convert_to_dataset
, this is intended for containing constant parameters such as observed data and constant data, and the first two dimensions are not required to be the number of chains and draws.
Keywords
coords::Dict{String,Vector}
: Map from named dimension to index namesdims::Dict{String,Vector{String}}
: Map from variable name to names of its dimensionslibrary::Any
: A library associated with the data to add toattrs
.attrs::Dict{String,Any}
: Global attributes to save on this dataset.
ArviZ.convert_to_dataset
— Methodconvert_to_dataset(obj; group = :posterior, kwargs...) -> Dataset
Convert a supported object to a Dataset
.
In most cases, this function calls convert_to_inference_data
and returns the corresponding group
.
ArviZ.dataset_to_dict
— Methoddataset_to_dict(ds::Dataset) -> Tuple{Dict{String,Array},NamedTuple}
Convert a Dataset
to a dictionary of Array
s. The function also returns keyword arguments to dict_to_dataset
.
ArviZ.dict_to_dataset
— Methoddict_to_dataset(data::Dict{String,Array}; kwargs...) -> Dataset
Convert a dictionary with data and keys as variable names to a Dataset
.
Keywords
attrs::Dict{String,Any}
: Json serializable metadata to attach to the dataset, in addition to defaults.library::String
: Name of library used for performing inference. Will be attached to theattrs
metadata.coords::Dict{String,Array}
: Coordinates for the datasetdims::Dict{String,Vector{String}}
: Dimensions of each variable. The keys are variable names, values are vectors of coordinates.
Examples
using ArviZ
ArviZ.dict_to_dataset(Dict("x" => randn(4, 100), "y" => randn(4, 100)))
ArviZ.from_cmdstanpy
— MethodSee documentation for arviz.from_cmdstanpy
.
ArviZ.from_emcee
— MethodSee documentation for arviz.from_emcee
.
ArviZ.from_numpyro
— MethodSee documentation for arviz.from_numpyro
.
ArviZ.from_pymc3
— MethodSee documentation for arviz.from_pymc3
.
ArviZ.from_pyro
— MethodSee documentation for arviz.from_pyro
.
ArviZ.from_pystan
— MethodSee documentation for arviz.from_pystan
.
ArviZ.from_tfp
— MethodSee documentation for arviz.from_tfp
.
ArviZ.groupnames
— Methodgroupnames(data::InferenceData) -> Vector{Symbol}
Get the names of the groups (datasets) in data
.
ArviZ.groups
— Methodgroups(data::InferenceData) -> Dict{Symbol,Dataset}
Get the groups in data
as a dictionary mapping names to datasets.
ArviZ.rc_params
— Methodrc_params() -> Dict{String,Any}
Get the list of customizable rc
params using with_rc_context
.
ArviZ.replacemissing
— Methodreplacemissing(x)
Replace missing
values with NaN
and do type inference on the result.
ArviZ.reshape_values
— Methodreshape_values(x::AbstractArray) -> AbstractArray
Convert from MCMCChains
variable values with dimensions (ndraw, size..., nchain)
to ArviZ's expected (nchain, ndraw, size...)
.
ArviZ.stack
— Methodstack(x::NamedTuple) -> NamedTuple
stack(x::AbstractArray{NamedTuple}) -> NamedTuple
stack(x::AbstractArray{AbstractArray{<:NamedTuple}}) -> NamedTuple
Given a container of NamedTuple
s, concatenate them, using the container dimensions as the dimensions of the resulting arrays.
Examples
using ArviZ
nchains, ndraws = 4, 100
data = [(x = rand(), y = randn(2), z = randn(2, 3)) for _ in 1:nchains, _ in 1:ndraws];
stacked_data = ArviZ.stack(data);
ArviZ.styles
— Methodstyles() -> Vector{String}
Get all available matplotlib styles for use with use_style
ArviZ.summary
— Methodsummary(
data;
group = :posterior,
coords = nothing,
dims = nothing,
kwargs...,
) -> Union{Pandas.DataFrame,PyObject}
Compute summary statistics on any object that can be passed to convert_to_dataset
.
Keywords
coords::Dict{String,Vector}=nothing
: Map from named dimension to named indices.dims::Dict{String,Vector{String}}=nothing
: Map from variable name to names of its dimensions.kwargs
: Keyword arguments passed tosummarystats
.
ArviZ.use_style
— Methoduse_style(style::String)
use_style(style::Vector{String})
Use matplotlib style settings from a style specification style
.
The style name of "default" is reserved for reverting back to the default style settings.
ArviZ-specific styles are ["arviz-whitegrid", "arviz-darkgrid", "arviz-colors", "arviz-white"]
. To see all available style specifications, use styles()
.
If a Vector
of styles is provided, they are applied from first to last.
Base.write
— Methodwrite(io::IO, plot::BokehPlot)
write(filename::AbstractString, plot::BokehPlot)
Write the HTML representation of the Bokeh plot to the I/O stream or file.
ArviZ.@forwardfun
— Macro@forwardfun f
@forwardfun(f)
Wrap a function arviz.f
in f
, forwarding its docstrings.
Use convert_arguments
and convert_result
to customize what is passed to and returned from f
.
ArviZ.@forwardplotfun
— Macro@forwardplotfun f
@forwardplotfun(f)
Wrap a plotting function arviz.f
in f
, forwarding its docstrings.
This macro also ensures that outputs for the different backends are correctly handled. Use convert_arguments
and convert_result
to customize what is passed to and returned from f
.