TuringLang / ParetoSmooth.jl

An implementation of PSIS algorithms in Julia.
http://turinglang.org/ParetoSmooth.jl/
MIT License
19 stars 12 forks source link

Artefacts in smoothed weights #66

Closed mschauer closed 2 years ago

mschauer commented 2 years ago

I observed that when smoothing a tail

julia> M = collect(0:0.01:1.0).^10; M2 = copy(M); psis!(M2, tail_length=9); 

julia> plot([M M2])
Screen Shot 2022-02-16 at 12 04 44

the tail is not monotone increasing, as I would expect it to be. Isn't it constructed as sequence of quantiles?

sethaxen commented 2 years ago

PSIS fits a GPD distribution d to the extreme right tail of the log-weights (input values), and then replaces those values by uniformly-spaced quantiles from d. The location parameter of the GPD is set to the largest value not in the tail, so yes, it should be monotonically increasing.

e.g. a comparison with PSIS.jl:

using PSIS, ParetoSmooth

M = collect(0:0.01:1.0).^10
M2 = copy(M)
ParetoSmooth.psis!(M2)
M3 = copy(M)
PSIS.psis!(M3)
plot([M M2 M3]; legend=:topleft, label=["input" "ParetoSmooth" "PSIS"], xlims=(50, Inf), alpha=0.75)

PSIS.jl and ParetoSmooth use the same algorithm, so to me this looks like a bug.

mschauer commented 2 years ago

The thing I find is that signature changed from the help, the following is outdated

help?> psis!
search: psis! pushfirst! psis Psis psis_loo psis_ess PsisLoo PSIS

  psis!(is_ratios::AbstractVector{<:Real}; tail_length::Integer, log_ratios=false) -> Real

and we should use log_ratios=false because the default is now true

Then

using PSIS, ParetoSmooth

M = collect(0:0.01:1.0).^10
M2 = copy(M)
ParetoSmooth.psis!(M2, log_weights=false)
M3 = log.(M)
PSIS.psis!(M3)

plot([M M2 exp.(M3)]; legend=:topleft, linestyle=[:solid :dash :solid], label=["input" "ParetoSmooth" "PSIS"], color=[:blue :green :yellow], xlims=(50, Inf))
Screen Shot 2022-02-16 at 16 04 58

So this is a error in the docs

sethaxen commented 2 years ago

~Ah, yes, the documentation defines log_weights as the opposite of what the code does.~ nevermind

ParadaCarleton commented 2 years ago

Thanks for catching that error in the docs :sweat_smile: