|
12 | 12 | DensityInterface
|
13 | 13 | ```
|
14 | 14 |
|
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). |
16 | 16 |
|
17 | 17 | The following methods must be provided to make a type (e.g. `SomeDensity`) compatible with the interface:
|
18 | 18 |
|
19 | 19 | ```jldoctest a
|
20 | 20 | import DensityInterface
|
21 | 21 |
|
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) |
24 | 24 |
|
25 |
| -DensityInterface.logdensityof(SomeDensity(), x) isa Real |
| 25 | +object = SomeDensity() |
| 26 | +DensityInterface.logdensityof(object, x) isa Real |
26 | 27 |
|
27 | 28 | # output
|
28 | 29 |
|
29 | 30 | true
|
30 | 31 | ```
|
31 | 32 |
|
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]. |
33 | 34 |
|
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.: |
35 | 38 |
|
36 | 39 | ```jldoctest a
|
37 | 40 | using DensityInterface
|
38 | 41 |
|
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) |
42 | 45 |
|
43 | 46 | # output
|
44 | 47 |
|
45 | 48 | true
|
46 | 49 | ```
|
47 | 50 |
|
48 | 51 | ```julia
|
49 |
| -SomeOptimizerPackage.maximize(logdensityof(d), x_init) |
| 52 | +SomeOptimizerPackage.maximize(logdensityof(object), x_init) |
50 | 53 | ```
|
51 | 54 |
|
52 | 55 | Reversely, a given log-density function `log_f` can be converted to a DensityInterface-compatible density object using [`logfuncdensity`](@ref):
|
53 | 56 |
|
54 | 57 | ```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) |
58 | 60 |
|
59 | 61 | # output
|
60 | 62 |
|
|
64 | 66 |
|
65 | 67 | [^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).
|
66 | 68 |
|
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`. |
0 commit comments