Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds mass and normalize for a measure #130

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/MeasureTheory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export basemeasure

export as

export mass, logmass, is_probability_measure, normalize

abstract type AbstractMeasure end

sampletype(μ::AbstractMeasure) = typeof(testvalue(μ))
Expand All @@ -54,6 +56,7 @@ include("absolutecontinuity.jl")
include("parameterized.jl")
include("macros.jl")
include("resettablerng.jl")
include("mass.jl")

include("primitive.jl")
include("primitives/counting.jl")
Expand Down
2 changes: 2 additions & 0 deletions src/combinators/weighted.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ function logdensity(sm::AbstractWeightedMeasure, x)
logdensity(sm.base, x) + sm.logweight
end

logmass(d::AbstractWeightedMeasure) = logmass(d.base) + d.logweight

###############################################################################

struct WeightedMeasure{R,M} <: AbstractWeightedMeasure
Expand Down
4 changes: 4 additions & 0 deletions src/distributions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ end

∫(::typeof(identity), ::Dists.Distribution) = 1.0

# all Distributions distributions are probability measures
logmass(μ::DistributionMeasure) = true
is_probability_measure(μ::DistributionMeasure; kwargs...) = true

logdensity(μ::Dists.Distribution, x) = Dists.logpdf(μ,x)
Dists.logpdf(d::AbstractMeasure, x) = logdensity(d,x)

Expand Down
40 changes: 40 additions & 0 deletions src/mass.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using LinearAlgebra: LinearAlgebra

"""
logmass(μ::AbstractMeasure)

Compute the logarithm of the [`mass`](@ref) of the measure ``μ``.
"""
function logmass end

"""
mass(μ::AbstractMeasure)

Compute the mass of the measure ``μ``.

That is, given a set ``S`` with elements ``x ∈ S``, compute
``μ(S) = ∫_S dμ(x)``.

!!! note
This function should not be directly modified. Instead, developers should
overload [`logmass`](@ref).
"""
mass(μ) = Exp(logmass(μ))

"""
normalize(μ::AbstractMeasure)

Convert the measure ``μ`` to a probability measure, that is, a measure whose
total mass is 1.
"""
LinearAlgebra.normalize(μ::AbstractMeasure) = μ * inv(mass(μ))

"""
is_probability_measure(μ::AbstractMeasure; kwargs...) -> Bool

Return `true` if ``μ`` is a probability measure, that is, a measure whose
total [`mass`](@ref) is 1.

`kwargs` are forwarded to `isapprox`.
"""
is_probability_measure(μ; kwargs...) = isapprox(mass(μ), true; kwargs...)
2 changes: 2 additions & 0 deletions src/primitives/dirac.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ function logdensity(μ::Dirac{M}, ν::Dirac{M}, x) where {M}
return -Inf
end
end

logmass(μ::Dirac) = false