Skip to content

Commit

Permalink
Density measures and Radon-Nikodym (#141)
Browse files Browse the repository at this point in the history
* bugfix and tests

* bump version
  • Loading branch information
cscherrer authored Aug 16, 2021
1 parent 12a6c19 commit 419d2f2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MeasureTheory"
uuid = "eadaa1a4-d27c-401d-8699-e962e1bbc33b"
authors = ["Chad Scherrer <[email protected]> and contributors"]
version = "0.10.2"
version = "0.10.3"

[deps]
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
Expand Down
20 changes: 15 additions & 5 deletions src/density.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,29 @@ basemeasure(μ::DensityMeasure) = μ.base

logdensity::DensityMeasure{F,B,Val{true}}, x) where {F,B} = μ.f(x)

density::DensityMeasure{F,B,Val{false}}, x) where {F,B} = μ.f(x)

logdensity::DensityMeasure{F,B,Val{false}}, x) where {F,B} = log(density(μ,x))

export

"""
∫(f, base::AbstractMeasure; log=false)
∫(f, base::AbstractMeasure)
Define a new measure in terms of a density `f` over some measure `base`. If
`log=true` (`false` is the default), `f` is considered as a log-density.
Define a new measure in terms of a density `f` over some measure `base`.
"""
(f, base::AbstractMeasure; log=false) = DensityMeasure(f, base, Val(log))
(f, base::AbstractMeasure) = DensityMeasure(f, base, Val(false))

::AbstractMeasure, base::AbstractMeasure) = ∫exp(log𝒹(μ, base), base)

::AbstractMeasure, base::AbstractMeasure; log=false) = (𝒹(μ, base), base; log=log)

export ∫exp

"""
∫exp(f, base::AbstractMeasure; log=false)
Define a new measure in terms of a density `f` over some measure `base`.
"""
∫exp(f,μ) = DensityMeasure(f,μ,Val{true}())

# TODO: `density` and `logdensity` functions for `DensityMeasure`
Expand Down
19 changes: 19 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -388,3 +388,22 @@ end
@test repr(Lebesgue(ℝ) ^ (3, 2)) == "Lebesgue(ℝ) ^ (3, 2)"
end
end

@testset "Density measures and Radon-Nikodym" begin
x = randn()
let d = (𝒹(Cauchy(), Normal()), Normal())
@test logdensity(d, x) logdensity(Cauchy(), x)
end

let f = 𝒹((x -> x^2, Normal()), Normal())
@test f(x) x^2
end

let d = ∫exp(log𝒹(Cauchy(), Normal()), Normal())
@test logdensity(d, x) logdensity(Cauchy(), x)
end

let f = log𝒹(∫exp(x -> x^2, Normal()), Normal())
@test f(x) x^2
end
end

2 comments on commit 419d2f2

@cscherrer
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

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/42959

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:

git tag -a v0.10.3 -m "<description of version>" 419d2f2fc3cb27c9b1d969d2e05022f3a4a01f66
git push origin v0.10.3

Please sign in to comment.