Posterior statistics

Display precision

Use digits to choose the decimal places shown in describe's summary and quantile tables:

using MCMCChains
chn = Chains(reshape(1:400, 100, 2, 2) ./ 3, ["mu", "theta[1]"])
describe(chn; digits = 2)
Chains MCMC chain (100×2×2 Array{Float64, 3}):

Iterations        = 1:1:100
Number of chains  = 2
Samples per chain = 100
parameters        = mu, theta[1]

Summary Statistics

  parameters      mean       std      mcse   ess_bulk   ess_tail      rhat   e ⋯
      Symbol   Float64   Float64   Float64    Float64    Float64   Float64     ⋯

          mu     50.17     34.78     23.41       2.43      11.93      3.07     ⋯
    theta[1]     83.50     34.78     23.41       2.43      11.93      3.07     ⋯

                                                                1 column omitted

Quantiles

  parameters      2.5%     25.0%     50.0%     75.0%     97.5%
      Symbol   Float64   Float64   Float64   Float64   Float64

          mu      1.99     16.92     50.17     83.42     98.34
    theta[1]     35.33     50.25     83.50    116.75    131.67

When digits is omitted, describe reads :digits from its IOContext. The same setting applies when displaying a summary table directly:

show(IOContext(stdout, :digits => 2), MIME"text/plain"(), mean(chn))
Mean

  parameters      mean
      Symbol   Float64

          mu     50.17
    theta[1]     83.50

Selecting parameters

Use sections to choose which parameter groups appear in the summary and quantile tables:

grouped = set_section(chn, Dict(:parameters => ["mu"], :vectors => ["theta[1]"]))
describe(grouped; digits = 2)
Chains MCMC chain (100×2×2 Array{Float64, 3}):

Iterations        = 1:1:100
Number of chains  = 2
Samples per chain = 100
parameters        = mu
vectors           = theta[1]

Summary Statistics

  parameters      mean       std      mcse   ess_bulk   ess_tail      rhat   e ⋯
      Symbol   Float64   Float64   Float64    Float64    Float64   Float64     ⋯

          mu     50.17     34.78     23.41       2.43      11.93      3.07     ⋯

                                                                1 column omitted

Quantiles

  parameters      2.5%     25.0%     50.0%     75.0%     97.5%
      Symbol   Float64   Float64   Float64   Float64   Float64

          mu      1.99     16.92     50.17     83.42     98.34

Use sections = :vectors for the vector entries or sections = nothing for all entries.

API

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

StatsBase.autocor — Function
autocor(
    chains;
    append_chains = true,
    demean = true,
    [lags,]
    kwargs...,
)

Compute the autocorrelation of each parameter for the chain.

The default lags are [1, 5, 10, 50], upper-bounded by n - 1 where n is the number of samples used in the estimation.

Setting append_chains=false will return a vector of dataframes containing the autocorrelations for each chain.

source
DataAPI.describe — Function
describe(io, chains[;
         q = [0.025, 0.25, 0.5, 0.75, 0.975],
         etype = :bm,
         digits = nothing,
         kwargs...])

Print chain metadata, summary statistics, and quantiles. Use describe(chains) for REPL output to stdout, or specify io for other streams (e.g., file output). Set digits to a nonnegative integer to choose the decimal places shown in the summary and quantile tables. Boolean values are not accepted.

source
Statistics.var — Function
var(chains; corrected=true, kwargs...)

Return a ChainDataFrame with the variance of each parameter, skipping missing draws. By default, draws are pooled across chains and the divisor is n - 1. Set corrected=false to divide by n. Other keywords are passed to summarize, including sections and append_chains.

source
Statistics.std — Function
std(chains; corrected=true, kwargs...)

Return a ChainDataFrame with the standard deviation of each parameter, skipping missing draws. By default, draws are pooled across chains and the variance uses the divisor n - 1. Set corrected=false to use the divisor n. Other keywords are passed to summarize, including sections and append_chains.

source
Statistics.cov — Function
cov(chains; sections, append_chains=true, corrected=true)

Return the covariance matrix as an AxisArray with parameter names on its row and col axes. Draws are pooled across chains by default. Set append_chains=false to return one matrix per chain. Set corrected=false to divide by n instead of n - 1. Missing draws propagate to the affected rows and columns.

source
StatsBase.summarystats — Function
function summarystats(
    chains;
    sections = _default_sections(chains),
    append_chains= true,
    autocov_method::AbstractAutocovMethod = AutocovMethod(),
    maxlag = 250,
    kwargs...
)

Compute the mean, standard deviation, Monte Carlo standard error, bulk- and tail- effective sample size, and $\widehat{R}$ diagnostic for each parameter in the chain.

Setting append_chains=false will return a vector of dataframes containing the summary statistics for each chain.

When estimating the effective sample size, autocorrelations are computed for at most maxlag lags.

source
Statistics.quantile — Function
quantile(chains[; q = [0.025, 0.25, 0.5, 0.75, 0.975], append_chains = true, kwargs...])

Compute the quantiles for each parameter in the chain.

Setting append_chains=false will return a vector of dataframes containing the quantiles for each chain.

source
MCMCChains.hpd — Function
hpd(chn::Chains; alpha::Real=0.05, kwargs...)

Return the highest posterior density interval representing 1-alpha probability mass.

Note that this will return a single interval and will not return multiple intervals for discontinuous regions.

Examples

julia> val = rand(500, 2, 3);
julia> chn = Chains(val, [:a, :b]);

julia> hpd(chn)
HPD
  parameters     lower     upper 
      Symbol   Float64   Float64 

           a    0.0554    0.9944
           b    0.0114    0.9460
source