Skip to content

Commit e71eb2d

Browse files
oschulzphipsgablerdevmotion
authored
Replace hasdensity by DensityKind (#9)
Also changes behavior of `logfuncdensity` to always return a density and adds `funcdensity`, Co-authored-by: Philipp Gabler <[email protected]> Co-authored-by: David Widmann <[email protected]>
1 parent ccade11 commit e71eb2d

File tree

10 files changed

+318
-124
lines changed

10 files changed

+318
-124
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "DensityInterface"
22
uuid = "b429d917-457f-4dbc-8f4c-0cc954292b1d"
3-
version = "0.3.3"
3+
version = "0.4.0"
44

55
[deps]
66
InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
[![Build Status](https://github.com/JuliaMath/DensityInterface.jl/workflows/CI/badge.svg?branch=master)](https://github.com/JuliaMath/DensityInterface.jl/actions?query=workflow%3ACI)
77
[![Codecov](https://codecov.io/gh/JuliaMath/DensityInterface.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/JuliaMath/DensityInterface.jl)
88

9-
This package defines an interface for mathematical/statistical densities in Julia. See the documentation for details.
9+
This package defines an interface for mathematical/statistical densities and objects associated with a density in Julia. See the documentation for details.
1010

1111

1212
## Documentation

docs/make.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ DocMeta.setdocmeta!(
1313
:DocTestSetup,
1414
quote
1515
using DensityInterface
16-
d = logfuncdensity(x -> x^2)
17-
log_f = logdensityof(d)
16+
object = logfuncdensity(x -> -x^2)
17+
log_f = logdensityof(object)
18+
f = densityof(object)
1819
x = 4
1920
end;
2021
recursive=true,

docs/src/api.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,24 @@
33
## Interface
44

55
```@docs
6-
hasdensity
76
logdensityof
87
logdensityof(::Any)
98
logfuncdensity
9+
funcdensity
1010
densityof
1111
densityof(::Any)
1212
```
1313

14-
1514
## Types
1615

1716
```@docs
17+
IsDensity
18+
HasDensity
19+
IsOrHasDensity
20+
NoDensity
21+
DensityKind
1822
DensityInterface.LogFuncDensity
23+
DensityInterface.FuncDensity
1924
```
2025

2126
## Test utility

docs/src/index.md

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,51 @@ end
1212
DensityInterface
1313
```
1414

15-
This package defines an interface for mathematical/statistical densities and objects associated with a density in Julia. The interface comprises the functions [`hasdensity`](@ref), [`logdensityof`](@ref)/[`densityof`](@ref)[^1] and [`logfuncdensity`](@ref).
15+
This package defines an interface for mathematical/statistical densities and objects associated with a density in Julia. The interface comprises the type [`DensityKind`](@ref) and the functions [`logdensityof`](@ref)/[`densityof`](@ref)[^1] and [`logfuncdensity`](@ref)/[`funcdensity`](@ref).
1616

1717
The following methods must be provided to make a type (e.g. `SomeDensity`) compatible with the interface:
1818

1919
```jldoctest a
2020
import DensityInterface
2121
22-
DensityInterface.hasdensity(::SomeDensity) = true
23-
DensityInterface.logdensityof(d::SomeDensity, x) = log_of_d_at(x)
22+
@inline DensityInterface.DensityKind(::SomeDensity) = IsDensity()
23+
DensityInterface.logdensityof(object::SomeDensity, x) = log_of_d_at(x)
2424
25-
DensityInterface.logdensityof(SomeDensity(), x) isa Real
25+
object = SomeDensity()
26+
DensityInterface.logdensityof(object, x) isa Real
2627
2728
# output
2829
2930
true
3031
```
3132

32-
The object `d` may be a density itself or something that can be said to have a density. If `d` is a distribution, the density is its probability density function. In the measure theoretical sense, the density function is the Radon–Nikodym derivative of `d` with respect to an implicit base measure. In statistical inference applications, for example, `d` might be a likelihood, prior or posterior[^2].
33+
`object` may be/represent a density itself (`DensityKind(object) === IsDensity()`) or it may be something that can be said to have a density (`DensityKind(object) === HasDensity()`)[^2].
3334

34-
DensityInterface automatically provides `logdensityof(d)`, equivalent to `x -> logdensityof(d, x)`. This constitutes a convenient way of passing a (log-)density function to algorithms like optimizers, samplers, etc.:
35+
In statistical inference applications, for example, `object` might be a likelihood, prior or posterior.
36+
37+
DensityInterface automatically provides `logdensityof(object)`, equivalent to `x -> logdensityof(object, x)`. This constitutes a convenient way of passing a (log-)density function to algorithms like optimizers, samplers, etc.:
3538

3639
```jldoctest a
3740
using DensityInterface
3841
39-
d = SomeDensity()
40-
log_f = logdensityof(d)
41-
log_f(x) == logdensityof(d, x)
42+
object = SomeDensity()
43+
log_f = logdensityof(object)
44+
log_f(x) == logdensityof(object, x)
4245
4346
# output
4447
4548
true
4649
```
4750

4851
```julia
49-
SomeOptimizerPackage.maximize(logdensityof(d), x_init)
52+
SomeOptimizerPackage.maximize(logdensityof(object), x_init)
5053
```
5154

5255
Reversely, a given log-density function `log_f` can be converted to a DensityInterface-compatible density object using [`logfuncdensity`](@ref):
5356

5457
```julia
55-
d = logfuncdensity(log_f)
56-
hasdensity(d) == true
57-
logdensityof(d, x) == log_f(x)
58+
object = logfuncdensity(log_f)
59+
DensityKind(object) === IsDensity() && logdensityof(object, x) == log_f(x)
5860

5961
# output
6062

@@ -64,5 +66,4 @@ true
6466

6567
[^1]: The function names `logdensityof` and `densityof` were chosen to convey that the target object may either *be* a density or something that can be said to *have* a density. They also have less naming conflict potential than `logdensity` and esp. `density` (the latter already being exported by Plots.jl).
6668

67-
[^2]: The package [`MeasureTheory`](https://github.com/cscherrer/MeasureTheory.jl) provides tools to work with densities and measures that go beyond the density in
68-
respect to an implied base measure.
69+
[^2]: The package [`Distributions`](https://github.com/JuliaStats/Distributions.jl) supports `DensityInterface` for `Distributions.Distribution`.

src/DensityInterface.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"""
44
DensityInterface
55
6-
Trait-based interface for mathematical/statistical densities
6+
Trait-based interface for mathematical/statistical densities and objects
7+
associated with a density.
78
"""
89
module DensityInterface
910

0 commit comments

Comments
 (0)