Skip to content

Commit f9bc0e6

Browse files
devmotionclaude
andauthored
docs: clarify reff is computed from inverse of unsmoothed importance ratios (#88)
* docs: clarify `reff` is computed from inverse of unsmoothed importance ratios Fix #76: `reff` was documented as the relative efficiency of the unsmoothed importance ratios, but it is the relative efficiency of their *inverse* (matching Stan's `loo::psis` definition `r_eff = ess(1/r) / S`). Update the `PSISResult`, `psis`/`psis!`, and `ess_is` docstrings accordingly. Also fix the analogue of arviz-devs/PosteriorStats.jl#54 in the `psis` jldoctest: the example computed `reff` directly from `log_ratios`, which gives a different ESS than the correct computation on the inverse ratios. Switch to `ess(softmax(-log_ratios; dims=(1, 2)); ...)` — equivalent to `ess(1 ./ exp.(log_ratios); ...)` but numerically stable. Add `LogExpFunctions` to the docs environment so `softmax` is available, and regenerate the expected output table. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> * Bump version to 0.9.9 --------- Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent ca17ad6 commit f9bc0e6

4 files changed

Lines changed: 16 additions & 12 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "PSIS"
22
uuid = "ce719bf2-d5d0-4fb9-925d-10a81b42ad04"
3-
version = "0.9.8"
3+
version = "0.9.9"
44
authors = ["Seth Axen <seth.axen@gmail.com> and contributors"]
55

66
[deps]

docs/Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
33
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
44
DocumenterCitations = "daee34ce-89f3-4625-b898-19384cb65244"
55
DocumenterInterLinks = "d12716ef-a0f6-4df4-a9f1-a5a34e75c656"
6+
LogExpFunctions = "2ab3a3ac-af41-5b50-aa03-7779005ae688"
67
MCMCDiagnosticTools = "be115224-59cd-429b-ad48-344e309966f0"
78
PSIS = "ce719bf2-d5d0-4fb9-925d-10a81b42ad04"
89
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
@@ -13,5 +14,6 @@ Distributions = "0.25.81"
1314
Documenter = "1"
1415
DocumenterCitations = "1.2.1"
1516
DocumenterInterLinks = "1"
17+
LogExpFunctions = "0.3.3"
1618
MCMCDiagnosticTools = "0.3.2"
1719
Plots = "1.10.1"

src/core.jl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Result of Pareto-smoothed importance sampling (PSIS) using [`psis`](@ref).
2222
- `nparams`: number of parameters in `log_weights`
2323
- `ndraws`: number of draws in `log_weights`
2424
- `nchains`: number of chains in `log_weights`
25-
- `reff`: the ratio of the effective sample size of the unsmoothed importance ratios and
26-
the actual sample size.
25+
- `reff`: the ratio of the effective sample size of the inverse of the unsmoothed
26+
importance ratios and the actual sample size.
2727
- `ess`: estimated effective sample size of estimate of mean using smoothed importance
2828
samples (see [`ess_is`](@ref))
2929
- `tail_length`: length of the upper tail of `log_weights` that was smoothed
@@ -186,10 +186,11 @@ While `psis` computes smoothed log weights out-of-place, `psis!` smooths them in
186186
- `log_ratios`: an array of logarithms of importance ratios, with size
187187
`(draws, [chains, [parameters...]])`, where `chains>1` would be used when chains are
188188
generated using Markov chain Monte Carlo.
189-
- `reff::Union{Real,AbstractArray}`: the ratio(s) of effective sample size of
190-
`log_ratios` and the actual sample size `reff = ess/(draws * chains)`, used to account
191-
for autocorrelation, e.g. due to Markov chain Monte Carlo. If an array, it must have the
192-
size `(parameters...,)` to match `log_ratios`.
189+
- `reff::Union{Real,AbstractArray}`: the ratio(s) of effective sample size of the inverse
190+
of the unsmoothed importance ratios `1 ./ exp.(log_ratios)` and the actual sample size
191+
`reff = ess/(draws * chains)`, used to account for autocorrelation, e.g. due to Markov
192+
chain Monte Carlo. If an array, it must have the size `(parameters...,)` to match
193+
`log_ratios`.
193194
194195
# Keywords
195196
@@ -236,9 +237,9 @@ If the draws were generated using MCMC, we can compute the relative efficiency u
236237
[`MCMCDiagnosticTools.ess`](@extref).
237238
238239
```jldoctest psis
239-
julia> using MCMCDiagnosticTools
240+
julia> using LogExpFunctions, MCMCDiagnosticTools
240241
241-
julia> reff = ess(log_ratios; kind=:basic, split_chains=1, relative=true);
242+
julia> reff = ess(softmax(-log_ratios; dims=(1, 2)); kind=:basic, split_chains=1, relative=true);
242243
243244
julia> result = psis(log_ratios, reff)
244245
┌ Warning: 9 parameters had Pareto shape values 0.7 < k ≤ 1. Resulting importance sampling estimates are likely to be unstable.
@@ -248,8 +249,8 @@ julia> result = psis(log_ratios, reff)
248249
PSISResult with 1000 draws, 1 chains, and 30 parameters
249250
Pareto shape (k) diagnostic values:
250251
Count Min. ESS
251-
(-Inf, 0.5] good 9 (30.0%) 806
252-
(0.5, 0.7] okay 11 (36.7%) 842
252+
(-Inf, 0.5] good 10 (33.3%) 835
253+
(0.5, 0.7] okay 10 (33.3%) 849
253254
(0.7, 1] bad 9 (30.0%) ——
254255
(1, Inf) very bad 1 (3.3%) ——
255256
```

src/ess.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ Given normalized weights ``w_{1:n}``, the ESS is estimated using the L2-norm of
99
\\mathrm{ESS}(w_{1:n}) = \\frac{r_{\\mathrm{eff}}}{\\sum_{i=1}^n w_i^2}
1010
```
1111
12-
where ``r_{\\mathrm{eff}}`` is the relative efficiency of the `log_weights`.
12+
where ``r_{\\mathrm{eff}}`` is the relative efficiency of the inverse of the unsmoothed
13+
importance ratios (see [`psis`](@ref)).
1314
1415
ess_is(result::PSISResult; bad_shape_nan=true)
1516

0 commit comments

Comments
 (0)