@@ -4,27 +4,29 @@ using Tullio
44export relative_eff, psis_ess, sup_ess
55
66"""
7- relative_eff(sample::AbstractArray{<:Real, 3}; [method])
7+ relative_eff(
8+ sample::AbstractArray{Real, 3};
9+ method=MCMCDiagnosticTools.FFTESSMethod()
10+ )
811
912Calculate the relative efficiency of an MCMC chain, i.e. the effective sample size divided
10- by the nominal sample size. If none is provided, use the default method from
11- MCMCDiagnosticTools.
13+ by the nominal sample size.
1214"""
1315function relative_eff (sample:: AbstractArray{<:Real, 3} ; maxlag= size (sample, 2 ), kwargs... )
1416 dims = size (sample)
1517 post_sample_size = dims[2 ] * dims[3 ]
1618 ess_sample = inv .(permutedims (sample, [2 , 1 , 3 ]))
17- ess, = MCMCDiagnosticTools. ess_rhat (ess_sample; maxlag = maxlag, kwargs ... )
19+ ess, = MCMCDiagnosticTools. ess_rhat (ess_sample; method = method, maxlag = dims[ 2 ] )
1820 r_eff = ess / post_sample_size
1921 return r_eff
2022end
2123
2224
2325"""
2426 function psis_ess(
25- weights::AbstractVector{<:Real},
26- r_eff::AbstractVector{<:Real }
27- ) -> AbstractVector{<:Real }
27+ weights::AbstractVector{T <:Real},
28+ r_eff::AbstractVector{T }
29+ ) -> AbstractVector{T }
2830
2931Calculate the (approximate) effective sample size of a PSIS sample, using the correction in
3032Vehtari et al. 2019. This uses the variance-based definition of ESS, and measures the L2
@@ -37,6 +39,14 @@ distance of the proposal and target distributions.
3739
3840See `?relative_eff` to calculate `r_eff`.
3941"""
42+ function psis_ess (
43+ weights:: AbstractVector{T} , r_eff:: AbstractVector{T}
44+ ) where {T <: Union{Real, Missing} }
45+ @tullio sum_of_squares := weights[x]^ 2
46+ return r_eff ./ sum_of_squares
47+ end
48+
49+
4050function psis_ess (
4151 weights:: AbstractMatrix{T} , r_eff:: AbstractVector{T}
4252) where {T <: Union{Real, Missing} }
@@ -45,7 +55,7 @@ function psis_ess(
4555end
4656
4757
48- function psis_ess (weights:: AbstractMatrix{<:Real} )
58+ function psis_ess (weights:: AbstractMatrix{<:Union{ Real, Missing} } )
4959 @warn " PSIS ESS not adjusted based on MCMC ESS. MCSE and ESS estimates " *
5060 " will be overoptimistic if samples are autocorrelated."
5161 return psis_ess (weights, ones (size (weights)))
5464
5565"""
5666 function sup_ess(
57- weights::AbstractVector{<:Real },
58- r_eff::AbstractVector{<:Real }
67+ weights::AbstractVector{T },
68+ r_eff::AbstractVector{T }
5969 ) -> AbstractVector
6070
6171Calculate the supremum-based effective sample size of a PSIS sample, i.e. the inverse of the
@@ -64,8 +74,7 @@ L-∞ norm.
6474
6575# Arguments
6676 - `weights`: A set of importance sampling weights derived from PSIS.
67- - `r_eff`: The relative efficiency of the MCMC chains from which PSIS samples were
68- derived.
77+ - `r_eff`: The relative efficiency of the MCMC chains; see also [`relative_eff`]@ref.
6978"""
7079function sup_ess (
7180 weights:: AbstractMatrix{T} , r_eff:: V
0 commit comments