Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoiding numerical issues in show for problematic chains #410

Open
torfjelde opened this issue Mar 12, 2023 · 9 comments
Open

Avoiding numerical issues in show for problematic chains #410

torfjelde opened this issue Mar 12, 2023 · 9 comments

Comments

@torfjelde
Copy link
Member

#401 is dope stuff (thanks @sethaxen ) but the tail ess in many cases seem to cause errors upon trying upon calling show, e.g. I just got the following trying a model

DivideError: integer division error

Stacktrace:
  [1] div
    @ ./int.jl:288 [inlined]
  [2] divgcd(x::Int64, y::Int64)
    @ Base ./rational.jl:45
  [3] //
    @ ./rational.jl:73 [inlined]
  [4] /(x::Rational{Int64}, y::Rational{Int64})
    @ Base ./rational.jl:346
  [5] _ess_rhat(::Val{:basic}, chains::Array{Real, 3}; autocov_method::AutocovMethod, split_chains::Int64, maxlag::Int64)
    @ MCMCDiagnosticTools ~/.julia/packages/MCMCDiagnosticTools/oVEPy/src/ess_rhat.jl:520
  [6] #_ess#36
...

This is for a problematic chain of course (I believe the variance is very high?). I also ran into a bug somewhere earlier, again, in the ESS computation.

Maybe we should provide a way to convert these errors into warnings in MCMCChains.describe?

MCMCChains.jl/src/stats.jl

Lines 286 to 331 in ddac60f

function summarystats(
chains::Chains;
sections = _default_sections(chains),
append_chains::Bool = true,
autocov_method::MCMCDiagnosticTools.AbstractAutocovMethod = AutocovMethod(),
maxlag = 250,
kwargs...
)
# Store everything.
funs = [meancskip, stdcskip]
func_names = [:mean, :std]
# Subset the chain.
_chains = Chains(chains, _clean_sections(chains, sections))
# Calculate MCSE and ESS/R-hat separately.
mcse_df = MCMCDiagnosticTools.mcse(
_chains; sections = nothing, autocov_method = autocov_method, maxlag = maxlag,
)
ess_rhat_rank_df = MCMCDiagnosticTools.ess_rhat(
_chains; sections = nothing, autocov_method = autocov_method, maxlag = maxlag, kind=:rank
)
ess_tail_df = MCMCDiagnosticTools.ess(
_chains; sections = nothing, autocov_method = autocov_method, maxlag = maxlag, kind=:tail
)
nt_additional = (
mcse=mcse_df.nt.mcse,
ess_bulk=ess_rhat_rank_df.nt.ess,
ess_tail=ess_tail_df.nt.ess,
rhat=ess_rhat_rank_df.nt.rhat,
ess_per_sec=ess_rhat_rank_df.nt.ess_per_sec,
)
additional_df = ChainDataFrame("Additional", nt_additional)
# Summarize.
summary_df = summarize(
_chains, funs...;
func_names = func_names,
append_chains = append_chains,
additional_df = additional_df,
name = "Summary Statistics",
sections = nothing
)
return summary_df
end

Maybe just add try-catch statements to all of these and do @warn + NaN in case of failure?

@devmotion
Copy link
Member

At first glance (haven't checked anything), it seems we should just fix the implementation in MCMCDiagnosticTools?

@sethaxen
Copy link
Member

Agreed this should be fixed upstream. @torfjelde can you by any chance share an MWE?

@torfjelde
Copy link
Member Author

I don't have an MWE atm, but the model is the SDE example from https://turinglang.org/v0.24/tutorials/10-bayesian-differential-equations/ that I ran without adaptation 😕

@sethaxen
Copy link
Member

Thanks @torfjelde , that should be enough for me to reproduce the issue when I get to a computer.

@sethaxen
Copy link
Member

@torfjelde I just tried and wasn't able to reproduce this. Could you write your chains to a file and share them with me?

@sethaxen
Copy link
Member

@torfjelde gentle reminder to send the problematic chains when you get the chance

@sethaxen
Copy link
Member

@torfjelde slightly less gentle reminder to send the problematic chains

@torfjelde
Copy link
Member Author

Haha yeah sorry. Basically my computer with the thingy crashed and so I couldn't access it until after I got back from BayesComp, and I've yet to be able to reproduce the example:/

@sethaxen
Copy link
Member

I'm not certain what the original issue is, but if it's the same as #447, then MCMCDiagnosticTools v0.3.10 contains a fix for this: TuringLang/MCMCDiagnosticTools.jl#119

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants