-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BAT.jl API definition and initial tests
- Loading branch information
Showing
24 changed files
with
1,370 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
# BATBase.jl | ||
|
||
**Note: Integration of BATBase.jl with BAT.jl is not complete yet.** | ||
|
||
BATBase.jl contains the core interface definition of [BAT.jl](https://github.com/bat/BAT.jl). | ||
|
||
BATBase.jl will serve as a lightweight and low-dependency package that packages | ||
implementing BAT-compatible densities and algorithms can depend on instead | ||
of the full BAT.jl. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# This file is a part of BATBase.jl, licensed under the MIT License (MIT). | ||
|
||
include("bat_default.jl") | ||
include("initval_algorithm.jl") | ||
include("transform_algorithm.jl") | ||
include("sampling_algorithm.jl") | ||
include("autocor_len.jl") | ||
include("eff_sample_size.jl") | ||
include("mode_estimator.jl") | ||
include("median_estimator.jl") | ||
include("differentiation_algorithm.jl") | ||
include("integration_algorithm.jl") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# This file is a part of BATBase.jl, licensed under the MIT License (MIT). | ||
|
||
|
||
""" | ||
abstract type AutocorLenAlgorithm | ||
Abstract type for integrated autocorrelation length estimation algorithms. | ||
""" | ||
abstract type AutocorLenAlgorithm end | ||
export AutocorLenAlgorithm | ||
|
||
|
||
|
||
""" | ||
bat_integrated_autocorr_len( | ||
v::Union{AbstractVector{<:Real},AbstractVectorOfSimilarVectors{<:Real}}, | ||
algorithm::AutocorLenAlgorithm = GeyerAutocorLen() | ||
) | ||
Estimate the integrated autocorrelation length of variate series `v`, | ||
separately for each degree of freedom. | ||
Returns a NamedTuple of the shape | ||
```julia | ||
(result = integrated_autocorr_len, ...) | ||
``` | ||
Result properties not listed here are algorithm-specific and are not part | ||
of the stable public API. | ||
!!! note | ||
Do not add add methods to `bat_integrated_autocorr_len`, add methods to | ||
`bat_integrated_autocorr_len_impl` instead. | ||
""" | ||
function bat_integrated_autocorr_len end | ||
export bat_integrated_autocorr_len | ||
|
||
function bat_integrated_autocorr_len_impl end | ||
|
||
|
||
function bat_integrated_autocorr_len( | ||
v::Union{AbstractVector{<:Real},AbstractVectorOfSimilarVectors{<:Real}}, | ||
algorithm = bat_default_withdebug(bat_integrated_autocorr_len, Val(:algorithm), v) | ||
) | ||
r = bat_integrated_autocorr_len_impl(v, algorithm) | ||
result_with_args(r, (algorithm = algorithm,)) | ||
end | ||
|
||
|
||
function argchoice_msg(::typeof(bat_integrated_autocorr_len), ::Val{:algorithm}, x::AutocorLenAlgorithm) | ||
"Using integrated autocorrelation length estimator $x" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# This file is a part of BATBase.jl, licensed under the MIT License (MIT). | ||
|
||
""" | ||
bat_default(f::Base.Callable, argname::Symbol, objective...) | ||
bat_default(f::Base.Callable, argname::Val, objective...) | ||
Get the default value for argument `argname` of function `f` to use | ||
for `objective`(s). | ||
`objective`(s) are mandatory arguments of function `f` that semantically | ||
constitute it's main objective(s), and that that a good default choice of | ||
optional arguments (e.g. choice of algorithm(s), etc.) may depend on. | ||
Which arguments are considered to be objectives is function-specific. | ||
For example: | ||
```julia | ||
bat_default(bat_sample, :algorithm, density::PosteriorDensity) == MetropolisHastings() | ||
bat_default(bat_sample, Val(:algorithm), samples::DensitySampleVector) == OrderedResampling() | ||
``` | ||
""" | ||
function bat_default end | ||
export bat_default | ||
|
||
@inline bat_default(f::Base.Callable, argname::Symbol, objective...) = bat_default(f, Val{argname}(), objective...) | ||
|
||
|
||
|
||
""" | ||
argchoice_msg(f::Base.Callable, argname::Val, x) | ||
Generates an information message regarding the choice of value `x` for | ||
argument `argname` of function `f`. | ||
The value `x` will often be the result of [`bat_default`](@ref). | ||
""" | ||
function argchoice_msg end | ||
|
||
|
||
function bat_default_withinfo(f::Base.Callable, argname::Val, objective...) | ||
default = bat_default(f::Base.Callable, argname::Val, objective...) | ||
@info argchoice_msg(f, argname::Val, default) | ||
default | ||
end | ||
|
||
function bat_default_withdebug(f::Base.Callable, argname::Val, objective...) | ||
default = bat_default(f::Base.Callable, argname::Val, objective...) | ||
@debug argchoice_msg(f, argname::Val, default) | ||
default | ||
end | ||
|
||
|
||
|
||
result_with_args(r::NamedTuple) = merge(r, (optargs = NamedTuple(), kwargs = NamedTuple())) | ||
|
||
result_with_args(r::NamedTuple, optargs::NamedTuple) = merge(r, (optargs = optargs, kwargs = NamedTuple())) | ||
|
||
result_with_args(r::NamedTuple, optargs::NamedTuple, kwargs::NamedTuple) = merge(r, (optargs = optargs, kwargs = kwargs)) | ||
|
||
result_with_args(r::NamedTuple, optargs::NamedTuple, kwargs::Base.Iterators.Pairs) = result_with_args(r, optargs, values(kwargs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# This file is a part of BATBase.jl, licensed under the MIT License (MIT). | ||
|
||
|
||
""" | ||
abstract type DifferentiationAlgorithm | ||
*Experimental feature, not part of stable public API.* | ||
Abstract type for integrated autocorrelation length estimation algorithms. | ||
""" | ||
abstract type DifferentiationAlgorithm end | ||
export DifferentiationAlgorithm | ||
|
||
|
||
|
||
""" | ||
bat_valgrad(f::Function, [algorithm::DifferentiationAlgorithm]) | ||
*Experimental feature, not part of stable public API.* | ||
Generated a function that calculates both value and gradient of `f` at given | ||
points. | ||
The function `f` must support `ValueShapes.varshape(f)` and | ||
`ValueShapes.unshaped(f)`. | ||
Returns a NamedTuple of the shape | ||
```julia | ||
(result = valgrad_f, ...) | ||
``` | ||
with | ||
```julia | ||
f_at_x, grad_of_f_at_x = valgrad_f(x) | ||
grad_of_f_at_x = zero(x) | ||
f_at_x = valgrad_f(!, grad_of_f_at_x, x) | ||
``` | ||
Result properties not listed here are algorithm-specific and are not part | ||
of the stable public API. | ||
!!! note | ||
Do not add add methods to `bat_valgrad`, add methods to | ||
`bat_valgrad_impl` instead. | ||
""" | ||
function bat_valgrad end | ||
export bat_valgrad | ||
|
||
function bat_valgrad_impl end | ||
|
||
|
||
function bat_valgrad( | ||
f::Function, | ||
algorithm = bat_default_withdebug(bat_valgrad, Val(:algorithm), f) | ||
) | ||
r = bat_valgrad_impl(f, algorithm) | ||
result_with_args(r, (algorithm = algorithm,)) | ||
end | ||
|
||
|
||
function argchoice_msg(::typeof(bat_valgrad), ::Val{:algorithm}, x::DifferentiationAlgorithm) | ||
"Using automiatic differentiation algorithm $x" | ||
end | ||
|
||
|
||
|
||
# ToDo, move to BAT.jl: | ||
#= | ||
gradient_shape(vs::AbstractValueShape) = replace_const_shapes(ValueShapes.const_zero_shape, vs) | ||
abstract type GradientFunction end | ||
=# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# This file is a part of BATBase.jl, licensed under the MIT License (MIT). | ||
|
||
|
||
""" | ||
abstract type EffSampleSizeAlgorithm | ||
Abstract type for integrated autocorrelation length estimation algorithms. | ||
""" | ||
abstract type EffSampleSizeAlgorithm end | ||
export EffSampleSizeAlgorithm | ||
|
||
|
||
|
||
""" | ||
bat_eff_sample_size( | ||
v::Union{AbstractVector{<:Real},AbstractVectorOfSimilarVectors{<:Real}}, | ||
[algorithm::EffSampleSizeAlgorithm] | ||
) | ||
bat_eff_sample_size( | ||
smpls::DensitySampleVector, | ||
[algorithm::EffSampleSizeAlgorithm] | ||
) | ||
Estimate effective sample size estimation for variate series `v`, resp. | ||
density samples `smpls`, separately for each degree of freedom. | ||
Returns a NamedTuple of the shape | ||
```julia | ||
(result = eff_sample_size, ...) | ||
``` | ||
Result properties not listed here are algorithm-specific and are not part | ||
of the stable public API. | ||
!!! note | ||
Do not add add methods to `bat_eff_sample_size`, add methods to | ||
`bat_eff_sample_size_impl` instead. | ||
""" | ||
function bat_eff_sample_size end | ||
export bat_eff_sample_size | ||
|
||
function bat_eff_sample_size_impl end | ||
|
||
|
||
function bat_eff_sample_size( | ||
v::Union{AbstractVector{<:Real},AbstractVectorOfSimilarVectors{<:Real}}, | ||
algorithm = bat_default_withdebug(bat_eff_sample_size, Val(:algorithm), v) | ||
) | ||
r = bat_eff_sample_size_impl(v, algorithm) | ||
result_with_args(r, (algorithm = algorithm,)) | ||
end | ||
|
||
|
||
function bat_eff_sample_size( | ||
smpls::AbstractDensitySampleVector, | ||
algorithm = bat_default_withdebug(bat_eff_sample_size, Val(:algorithm), smpls); | ||
kwargs... | ||
) | ||
r = bat_eff_sample_size_impl(smpls, algorithm; kwargs...) | ||
result_with_args(r, (algorithm = algorithm,)) | ||
end | ||
|
||
|
||
function argchoice_msg(::typeof(bat_eff_sample_size), ::Val{:algorithm}, x::EffSampleSizeAlgorithm) | ||
"Using integrated autocorrelation length estimator $x" | ||
end |
Oops, something went wrong.
f9902c9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JuliaRegistrator register
f9902c9
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Registration pull request created: JuliaRegistries/General/35066
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: