1- using FFTW
2- using LoopVectorization
31using MCMCDiagnosticTools
4-
52using Tullio
63
74export relative_eff, psis_ess, sup_ess
85
96"""
10- relative_eff(
11- sample::AbstractArray{Real, 3};
12- method=MCMCDiagnosticTools.FFTESSMethod()
13- )
7+ relative_eff(sample::AbstractArray{<:Real, 3}; [method])
148
159Calculate the relative efficiency of an MCMC chain, i.e. the effective sample size divided
16- by the nominal sample size.
10+ by the nominal sample size. If none is provided, use the default method from
11+ MCMCDiagnosticTools.
1712"""
18- function relative_eff (
19- sample:: AbstractArray{T, 3} ; method= MCMCDiagnosticTools. FFTESSMethod ()
20- ) where {T <: Union{Real, Missing} }
13+ function relative_eff (sample:: AbstractArray{<:Real, 3} ; maxlag= size (sample, 2 ), kwargs... )
2114 dims = size (sample)
2215 post_sample_size = dims[2 ] * dims[3 ]
2316 ess_sample = inv .(permutedims (sample, [2 , 1 , 3 ]))
24- ess, = MCMCDiagnosticTools. ess_rhat (ess_sample; method = method, maxlag = dims[ 2 ] )
17+ ess, = MCMCDiagnosticTools. ess_rhat (ess_sample; maxlag = maxlag, kwargs ... )
2518 r_eff = ess / post_sample_size
2619 return r_eff
2720end
2821
2922
3023"""
3124 function psis_ess(
32- weights::AbstractVector{T },
33- r_eff::AbstractVector{T }
34- ) -> AbstractVector{T }
25+ weights::AbstractVector{<:Real },
26+ r_eff::AbstractVector{<:Real }
27+ ) -> AbstractVector{<:Real }
3528
3629Calculate the (approximate) effective sample size of a PSIS sample, using the correction in
3730Vehtari et al. 2019. This uses the variance-based definition of ESS, and measures the L2
@@ -44,23 +37,15 @@ distance of the proposal and target distributions.
4437
4538See `?relative_eff` to calculate `r_eff`.
4639"""
47- function psis_ess (
48- weights:: AbstractVector{T} , r_eff:: AbstractVector{T}
49- ) where {T <: Union{Real, Missing} }
50- @tullio sum_of_squares := weights[x]^ 2
51- return @turbo r_eff ./ sum_of_squares
52- end
53-
54-
5540function psis_ess (
5641 weights:: AbstractMatrix{T} , r_eff:: AbstractVector{T}
5742) where {T <: Union{Real, Missing} }
5843 @tullio sum_of_squares[x] := weights[x, y]^ 2
59- return @turbo r_eff ./ sum_of_squares
44+ return r_eff ./ sum_of_squares
6045end
6146
6247
63- function psis_ess (weights:: AbstractMatrix{<:Union{ Real, Missing} } )
48+ function psis_ess (weights:: AbstractMatrix{<:Real} )
6449 @warn " PSIS ESS not adjusted based on MCMC ESS. MCSE and ESS estimates " *
6550 " will be overoptimistic if samples are autocorrelated."
6651 return psis_ess (weights, ones (size (weights)))
6954
7055"""
7156 function sup_ess(
72- weights::AbstractVector{T },
73- r_eff::AbstractVector{T }
57+ weights::AbstractVector{<:Real },
58+ r_eff::AbstractVector{<:Real }
7459 ) -> AbstractVector
7560
7661Calculate the supremum-based effective sample size of a PSIS sample, i.e. the inverse of the
@@ -79,10 +64,11 @@ L-∞ norm.
7964
8065# Arguments
8166 - `weights`: A set of importance sampling weights derived from PSIS.
82- - `r_eff`: The relative efficiency of the MCMC chains from which PSIS samples were derived.
67+ - `r_eff`: The relative efficiency of the MCMC chains from which PSIS samples were
68+ derived.
8369"""
8470function sup_ess (
8571 weights:: AbstractMatrix{T} , r_eff:: V
86- ) where {T<: Union{ Real, Missing} , V<: AbstractVector{T} }
87- return @turbo inv .(dropdims (maximum (weights; dims= 2 ); dims= 2 )) .* r_eff
72+ ) where {T<: Real , V<: AbstractVector{T} }
73+ return inv .(dropdims (maximum (weights; dims= 2 ); dims= 2 )) .* r_eff
8874end
0 commit comments