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

Use keyword syntax for ess, ess_rhat, rhat, and mcse #72

Merged
merged 53 commits into from
Feb 26, 2023
Merged

Conversation

sethaxen
Copy link
Member

This PR implements the new keyword-based API proposed in #22 (comment).

src/ess.jl Show resolved Hide resolved
@sethaxen sethaxen marked this pull request as ready for review February 25, 2023 15:04
@sethaxen sethaxen requested a review from devmotion February 25, 2023 15:04
Project.toml Outdated Show resolved Hide resolved
src/MCMCDiagnosticTools.jl Outdated Show resolved Hide resolved
src/ess_rhat.jl Outdated Show resolved Hide resolved
src/ess_rhat.jl Outdated Show resolved Hide resolved
src/ess_rhat.jl Outdated Show resolved Hide resolved
src/ess_rhat.jl Outdated
Comment on lines 269 to 277
if estimator !== nothing && type !== nothing
throw(ArgumentError("only one of `estimator` and `type` can be specified"))
elseif estimator !== nothing
return _ess(estimator, samples; kwargs...)
elseif type !== nothing
return _ess(_val(type), samples; kwargs...)
else
return _ess(Val(:basic), samples; kwargs...)
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we simplify the logic by just saying, if estimator !== nothing it is used and otherwise the type determines what ESS estimate is returned?

Suggested change
if estimator !== nothing && type !== nothing
throw(ArgumentError("only one of `estimator` and `type` can be specified"))
elseif estimator !== nothing
return _ess(estimator, samples; kwargs...)
elseif type !== nothing
return _ess(_val(type), samples; kwargs...)
else
return _ess(Val(:basic), samples; kwargs...)
end
if estimator !== nothing
return _ess(estimator, samples; kwargs...)
else
return _ess(_val(type), samples; kwargs...)
end

(Again, noting that I'm not a big fan of supporting both Val and Symbol)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do think it's important to throw an error if the user tries to specify both type and estimator. A bulk-median-ESS is meaningless.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm I was thinking that it would be sufficient to document that estimator would have higher precedence than type but I can see your point.

That makes me wonder though: Do we actually need two keyword arguments? It seems one would be sufficient if one must not specify both anyway. We could just interpret values of type Symbol (or Val) as the type and everything else as estimator?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I liked the explicitness of calling it an estimator, especially for consistency with mcse. If we use type for ess as well, then we should also use type for mcse.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe that would be a good approach? An alternative could be to call it method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer not to use method, as to me method is more about how an estimate is made than about what the estimate is (e.g. method currently used for autocovariance estimate method). I merged estimator and type as a new keyword kind. kind is about as semantically overloaded as type but avoids being potentially misunderstood as the type of the return object (also doesn't get colored strangely in syntax highlighters).

src/ess_rhat.jl Outdated Show resolved Hide resolved
src/ess_rhat.jl Outdated Show resolved Hide resolved
src/ess_rhat.jl Outdated Show resolved Hide resolved
src/utils.jl Outdated Show resolved Hide resolved
src/MCMCDiagnosticTools.jl Outdated Show resolved Hide resolved
src/MCMCDiagnosticTools.jl Outdated Show resolved Hide resolved
Copy link
Member

@devmotion devmotion left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks quite good, I only have a few minor comments left.

::Val{:basic}, chains::AbstractArray{<:Union{Missing,Real},3}; split_chains::Int=2
)
# compute size of matrices (each chain may be split!)
niter = size(chains, 1) ÷ split_chains
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit annoying that the code has (?) to be duplicated. Hopefully at least the tests make it unlikely that they diverge in a noticeable way.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think we may need a refactor of the code that computes the various variances when we tackle #23, and hopefully then we can eliminate at least part of the redundancy. But yes, the tests should prevent divergences.

Comment on lines +274 to +276
x = _expectand_proxy(estimator, samples)
if x === nothing
throw(ArgumentError("the estimator $f is not yet supported by `ess_rhat`"))
throw(ArgumentError("the estimator $estimator is not yet supported by `ess`"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes me wonder, would it be more natural to add a default fallback for _expectand_proxy that does not return nothing but throws the descriptive error message directly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the error includes the function name, it would seem odd to me to put that error in _expectand_proxy, and the traceback would then point to this internal function not relevant to the user. Since this function is meant only for internal use, I don't think it's a problem for it to have this fallback.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The second argument is slightly weak though, I think, since also currently the traceback points to an internal function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, but _ess is more similar to ess than _expectand_proxy is.

src/mcse.jl Outdated Show resolved Hide resolved
src/ess_rhat.jl Show resolved Hide resolved
src/ess_rhat.jl Show resolved Hide resolved
test/ess_rhat.jl Outdated Show resolved Hide resolved
@sethaxen sethaxen merged commit 6088765 into main Feb 26, 2023
@sethaxen sethaxen deleted the essrhat2 branch February 26, 2023 22:46
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

Successfully merging this pull request may close these issues.

3 participants