JuliaMath / JuliaMath/DensityInterface.jl
use in TransformVariables.jl (seeking advice)
- Dominant language
- Julia
- Stars
- 12
- Forks
- 2
- PR merge metrics
- No merged PRs in 30d
Description
I am refactoring TransformVariables.jl. I have a conceptual question which I think this package solves (I think), so I want to ask for advice.
For those unfamiliar with the package, it encodes uni- and multivariate domain transformations, mostly for use in Bayesian estimation with samplers that work on an unconstrained Euclidean space. When calculating a log likelihood, this requires a correction by the log of the absolute value of the determinant of the Jacobian.
The current API is
```julia
y, lj = transform_and_logjac(transformation, x)
```
mapping `x` to `y`, with `lj` as the log abs det J.
This is cumbersome, as the user has to keep and accumulate `lj`s. Since those statements are usually followed by a `logpdf(some_distribution, y)` somewhere, I thought of returning a
```julia
struct ValueWithLogjac{T,L}
y::T
lj::L
end
```
wrapper, defining
```julia
Base.logpdf(distribution, v::ValueWithLogjac) = logpdf(distribution, v.y) + v.lj
```
but that would require loading Distributions.jl, a heavy dependency. Now https://github.com/JuliaStats/Distributions.jl/issues/1139 would solve that but it seems not to be going anywhere. However, I could just do
```julia
DensityInterface.logdensityof(object, v::ValueWithLogjac) = DensityInterface.logdensityof(distribution, v.y) + v.lj
```
which works automatically for types in Distributions.jl, and is lightweight.
Does this conform with the intended usage of this package? Or should I try factoring out a DistributionsCore.jl again? Thoughts are appreciated.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.