Chains

The methods listed below are defined in src/chains.jl.

MCMCChains.ChainsMethod
Chains(c::Chains, section::Union{Symbol,String})
Chains(c::Chains, sections)

Return a new chain with only a specific section or multiple sections pulled out.

Examples

julia> chn = Chains(rand(100, 2, 1), [:a, :b], Dict(:internals => [:a]));

julia> names(chn)
2-element Vector{Symbol}:
 :a
 :b

julia> chn2 = Chains(chn, :internals);

julia> names(chn2)
1-element Vector{Symbol}:
 :a
source
Base.getMethod
Base.get(c::Chains, v::Symbol; flatten=false)
Base.get(c::Chains, vs::Vector{Symbol}; flatten=false)

Return a NamedTuple with v as the key, and matching paramter names as the values.

Passing flatten=true will return a NamedTuple with keys ungrouped.

Example

julia> chn = Chains([1:2 3:4]);

julia> get(chn, :param_1)
(param_1 = [1; 2],)

julia> get(chn, [:param_2])
(param_2 = [3; 4],)

julia> get(chn, :param_1; flatten=true)
(param_1 = 1,)
source
Base.getMethod
get(c::Chains; section::Union{Vector{Symbol}, Symbol; flatten=false}

Return all parameters in a given section(s) as a NamedTuple.

Passing flatten=true will return a NamedTuple with keys ungrouped.

Example

julia> chn = Chains([1:2 3:4], [:a, :b], Dict(:internals => [:a]));

julia> get(chn; section=:parameters)
(b = [3; 4],)

julia> get(chn; section=[:internals])
(a = [1; 2],)
source
Base.namesMethod
names(chains::Chains, sections)

Return the parameter names of the sections in the chains.

source
Base.namesMethod
names(chains::Chains, section::Symbol)

Return the parameter names of a section in the chains.

source
Base.namesMethod
names(chains::Chains)

Return the parameter names in the chains.

source
Base.rangeMethod
range(chains::Chains)

Return the range of iteration indices of the chains.

source
Base.sortMethod
sort(c::Chains[; lt=NaturalSort.natural])

Return a new column-sorted version of c.

By default the columns are sorted in natural sort order.

source
MCMCChains.get_paramsMethod
get_params(c::Chains; flatten=false)

Return all parameters packaged as a NamedTuple. Variables with a bracket in their name (as in "P[1]") will be grouped into the returned value as P.

Passing flatten=true will return a NamedTuple with keys ungrouped.

Example

julia> chn = Chains(1:5);

julia> x = get_params(chn);

julia> x.param_1
2-dimensional AxisArray{Int64,2,...} with axes:
    :iter, 1:1:5
    :chain, 1:1
And data, a 5×1 Matrix{Int64}:
 1
 2
 3
 4
 5
source
MCMCChains.groupMethod
group(chains::Chains, name::Union{String,Symbol})

Return a subset of the chain chain with all parameters in the group Symbol(name).

source
MCMCChains.headerMethod
header(c::Chains; section=missing)

Return a string containing summary information for a Chains object. If the section keyword is used, this function prints only the relevant section header.

Example

# Printing the whole header.
header(chn)

# Print only one section's header.
header(chn, section = :parameter)
source
MCMCChains.namesingroupMethod
namesingroup(chains::Chains, sym::Union{String,Symbol})

Return the names of all parameters in a chain that belong to the group sym.

This is based on the MCMCChains convention that parameters with names of the form :sym[index] belong to one group of parameters called :sym.

If the chain contains a parameter of name :sym it will be returned as well.

Example

julia> chn = Chains(rand(100, 2, 2), ["A[1]", "A[2]"]);

julia> namesingroup(chn, :A)
2-element Vector{Symbol}:
 Symbol("A[1]")
 Symbol("A[2]")
source
MCMCChains.replacenamesMethod
replacenames(chains::Chains, dict::AbstractDict)

Replace parameter names by creating a new Chains object that shares the same underlying data.

Examples

julia> chn = Chains(rand(100, 2, 2), ["one", "two"]);

julia> chn2 = replacenames(chn, "one" => "A");

julia> names(chn2)
2-element Vector{Symbol}:
 :A
 :two

julia> chn3 = replacenames(chn2, Dict("A" => "one", "two" => "B"));

julia> names(chn3) 
2-element Vector{Symbol}:
 :one
 :B
source
MCMCChains.resetrangeMethod
resetrange(chains::Chains)

Generate a new chain from chains with iterations indexed by 1:n, where n is the number of samples per chain.

The new chain and chains share the same data in memory.

source
MCMCChains.set_sectionMethod
set_section(chains::Chains, namemap)

Create a new Chains object from chains with the provided namemap mapping of parameter names.

Both chains share the same underlying data. Any parameters in the chain that are unassigned will be placed into the :parameters section.

source
MCMCChains.setinfoMethod
setinfo(c::Chains, n::NamedTuple)

Return a new Chains object with a NamedTuple type n placed in the info field.

Example

new_chn = setinfo(chn, NamedTuple{(:a, :b)}((1, 2)))
source
MCMCChains.setrangeMethod
setrange(chains::Chains, range)

Generate a new chain from chains with iterations indexed by range.

The new chain and chains share the same data in memory.

source