Extension of PosteriorStats

The following methods of PosteriorStats.jl are extended by this package.

PosteriorStats.hdiMethod
hdi(data::InferenceData; kwargs...) -> Dataset
hdi(data::Dataset; kwargs...) -> Dataset

Calculate the highest density interval (HDI) for each parameter in the data.

For more details and a description of the kwargs, see PosteriorStats.hdi.

source
PosteriorStats.looMethod
loo(data::Dataset; [var_name::Symbol,] kwargs...) -> PSISLOOResult{<:NamedTuple,<:Dataset}
loo(data::InferenceData; [var_name::Symbol,] kwargs...) -> PSISLOOResult{<:NamedTuple,<:Dataset}

Compute PSIS-LOO from log-likelihood values in data.

If more than one log-likelihood variable is present, then var_name must be provided.

For more details and a description of the kwargs, see PosteriorStats.loo.

Examples

Calculate PSIS-LOO of a model:

julia> using ArviZExampleData, PosteriorStats

julia> idata = load_example_data("centered_eight");

julia> loo(idata)
PSISLOOResult with estimates
 elpd  elpd_mcse    p  p_mcse
  -31        1.4  0.9    0.33

and PSISResult with 500 draws, 4 chains, and 8 parameters
Pareto shape (k) diagnostic values:
                    Count      Min. ESS
 (-Inf, 0.5]  good  4 (50.0%)  270
  (0.5, 0.7]  okay  4 (50.0%)  307
source
PosteriorStats.loo_pitMethod
loo_pit(idata::InferenceData, log_weights; kwargs...) -> DimArray

Compute LOO-PIT values using existing normalized log LOO importance weights.

Keywords

  • y_name: Name of observed data variable in idata.observed_data. If not provided, then the only observed data variable is used.
  • y_pred_name: Name of posterior predictive variable in idata.posterior_predictive. If not provided, then y_name is used.
  • kwargs: Remaining keywords are forwarded to the base method PosteriorStats.loo_pit.

See PosteriorStats.loo_pit for more details.

Examples

Calculate LOO-PIT values using already computed log weights.

julia> using ArviZExampleData, PosteriorStats

julia> idata = load_example_data("centered_eight");

julia> loo_result = loo(idata; var_name=:obs);

julia> loo_pit(idata, loo_result.psis_result.log_weights; y_name=:obs)
┌ 8-element DimArray{Float64, 1} loo_pit_obs ┐
├────────────────────────────────────────────┴─────────────────────────── dims ┐
  ↓ school Categorical{String} [Choate, Deerfield, …, St. Paul's, Mt. Hermon] Unordered
└──────────────────────────────────────────────────────────────────────────────┘
 "Choate"            0.942759
 "Deerfield"         0.641057
 "Phillips Andover"  0.32729
 "Phillips Exeter"   0.581451
 "Hotchkiss"         0.288523
 "Lawrenceville"     0.393741
 "St. Paul's"        0.886175
 "Mt. Hermon"        0.638821
source
PosteriorStats.loo_pitMethod
loo_pit(idata::InferenceData; kwargs...) -> DimArray

Compute LOO-PIT from groups in idata using PSIS-LOO.

Keywords

  • y_name: Name of observed data variable in idata.observed_data. If not provided, then the only observed data variable is used.
  • y_pred_name: Name of posterior predictive variable in idata.posterior_predictive. If not provided, then y_name is used.
  • log_likelihood_name: Name of log-likelihood variable in idata.log_likelihood. If not provided, then y_name is used if idata has a log_likelihood group, otherwise the only variable is used.
  • reff::Union{Real,AbstractArray{<:Real}}: The relative effective sample size(s) of the likelihood values. If an array, it must have the same data dimensions as the corresponding log-likelihood variable. If not provided, then this is estimated using ess.
  • kwargs: Remaining keywords are forwarded to PosteriorStats.loo_pit.

See PosteriorStats.loo_pit for more details.

Examples

Calculate LOO-PIT values using as test quantity the observed values themselves.

julia> using ArviZExampleData, PosteriorStats

julia> idata = load_example_data("centered_eight");

julia> loo_pit(idata; y_name=:obs)
┌ 8-element DimArray{Float64, 1} loo_pit_obs ┐
├────────────────────────────────────────────┴─────────────────────────── dims ┐
  ↓ school Categorical{String} [Choate, Deerfield, …, St. Paul's, Mt. Hermon] Unordered
└──────────────────────────────────────────────────────────────────────────────┘
 "Choate"            0.942759
 "Deerfield"         0.641057
 "Phillips Andover"  0.32729
 "Phillips Exeter"   0.581451
 "Hotchkiss"         0.288523
 "Lawrenceville"     0.393741
 "St. Paul's"        0.886175
 "Mt. Hermon"        0.638821
source
PosteriorStats.r2_scoreMethod
r2_score(idata::InferenceData; y_name, y_pred_name) -> (; r2, r2_std)

Compute $R²$ from idata, automatically formatting the predictions to the correct shape.

Keywords

  • y_name: Name of observed data variable in idata.observed_data. If not provided, then the only observed data variable is used.
  • y_pred_name: Name of posterior predictive variable in idata.posterior_predictive. If not provided, then y_name is used.

See PosteriorStats.r2_score for more details.

Examples

julia> using ArviZExampleData, PosteriorStats

julia> idata = load_example_data("regression10d");

julia> r2_score(idata) |> pairs
pairs(::NamedTuple) with 2 entries:
  :r2     => 0.998385
  :r2_std => 0.000100621
source
PosteriorStats.summarizeMethod
summarize(data::InferenceData, group=:posterior, stats_funs...; kwargs...)
summarize(data::Dataset, stats_funs...; kwargs...)

Compute summary statistics for the data using the provided functions.

For verbose variable labels, provide compat_labels=false. For details on stats_funs and kwargs, see PosteriorStats.summarize.

Examples

Compute all default summary statistics for the eight schools model in the centered parameterization:

julia> using ArviZExampleData, PosteriorStats, StatsBase

julia> data = load_example_data("centered_eight");

julia> summarize(data)
SummaryStats
                          mean  std  hdi_3%  hdi_97%  mcse_mean  mcse_std  ess ⋯
 mu                        4.2  3.3  -1.61     10.3        0.21     0.088      ⋯
 theta[Choate]             6.4  5.9  -3.68     17.9        0.25     0.20       ⋯
 theta[Deerfield]          5.0  4.9  -4.98     13.4        0.21     0.15       ⋯
 theta[Phillips Andover]   3.4  5.4  -7.54     12.9        0.23     0.17       ⋯
 theta[Phillips Exeter]    4.8  5.2  -5.11     14.1        0.21     0.21       ⋯
 theta[Hotchkiss]          3.5  4.8  -6.12     12.0        0.25     0.15       ⋯
 theta[Lawrenceville]      3.7  5.2  -6.50     12.7        0.22     0.21       ⋯
 theta[St. Paul's]         6.5  5.2  -2.67     16.9        0.22     0.15       ⋯
 theta[Mt. Hermon]         4.8  5.7  -5.97     15.4        0.24     0.23       ⋯
 tau                       4.3  3.0   0.715     9.41       0.22     0.14       ⋯
                                                               3 columns omitted

Compute the mean, standard deviation, median, and median absolute deviation of the theta parameters:

julia> summarize(data.posterior[(:theta,)], (:mean, :std) => mean_and_std, median, mad)
SummaryStats
                          mean   std  median   mad
 theta[Choate]            6.42  5.85    5.80  4.95
 theta[Deerfield]         4.95  4.91    5.02  4.68
 theta[Phillips Andover]  3.42  5.43    3.74  4.84
 theta[Phillips Exeter]   4.75  5.25    4.69  4.84
 theta[Hotchkiss]         3.45  4.78    3.62  4.55
 theta[Lawrenceville]     3.66  5.23    3.90  4.88
 theta[St. Paul's]        6.51  5.24    6.09  4.57
 theta[Mt. Hermon]        4.82  5.70    4.65  4.89
source
PosteriorStats.waicMethod
waic(data::Dataset; [var_name::Symbol]) -> WAICResult{<:NamedTuple,<:Dataset}
waic(data::InferenceData; [var_name::Symbol]) -> WAICResult{<:NamedTuple,<:Dataset}

Compute WAIC from log-likelihood values in data.

If more than one log-likelihood variable is present, then var_name must be provided.

See PosteriorStats.waic for more details.

Examples

Calculate WAIC of a model:

julia> using ArviZExampleData, PosteriorStats

julia> idata = load_example_data("centered_eight");

julia> waic(idata)
WAICResult with estimates
 elpd  elpd_mcse    p  p_mcse
  -31        1.4  0.9    0.32
source