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

Some convenience functions for WeightedMeasures #43

Merged
merged 7 commits into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 3 additions & 3 deletions src/combinators/smart-constructors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ function productmeasure(f::Returns{FB}, param_maps, pars) where {FB<:FactoredBas
end

function productmeasure(f::Returns{W}, ::typeof(identity), pars) where {W<:WeightedMeasure}
ℓ = f.value.logweight
base = f.value.base
ℓ = _logweight(f.value)
base = basemeasure(f.value)
newbase = productmeasure(Returns(base), identity, pars)
weightedmeasure(length(pars) * ℓ, newbase)
end
Expand Down Expand Up @@ -102,7 +102,7 @@ function weightedmeasure(ℓ::R, b::M) where {R,M}
end

function weightedmeasure(ℓ, b::WeightedMeasure)
weightedmeasure(ℓ + b.logweight, b.base)
weightedmeasure(ℓ + _logweight(b), b.base)
end

###############################################################################
Expand Down
15 changes: 12 additions & 3 deletions src/combinators/weighted.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,29 @@ export WeightedMeasure, AbstractWeightedMeasure

abstract type AbstractWeightedMeasure <: AbstractMeasure end

logweight(μ::AbstractWeightedMeasure) = μ.logweight
basemeasure(μ::AbstractWeightedMeasure) = μ.base
# By default the weight for all measure is 1
_logweight(::AbstractMeasure) = 0

Base.length(μ::AbstractWeightedMeasure) = length(basemeasure(μ))

@inline function logdensity_def(d::AbstractWeightedMeasure, x)
@inline function logdensity_def(d::AbstractWeightedMeasure, _)
d.logweight
end

function Base.rand(rng::AbstractRNG, ::Type{T}, μ::AbstractWeightedMeasure) where {T}
rand(rng, T, basemeasure(μ))
end

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

struct WeightedMeasure{R,M} <: AbstractWeightedMeasure
logweight::R
base::M
end

_logweight(μ::WeightedMeasure) = μ.logweight
basemeasure(μ::AbstractWeightedMeasure) = μ.base

function Base.show(io::IO, μ::WeightedMeasure)
io = IOContext(io, :compact => true)
print(io, exp(μ.logweight), " * ", μ.base)
Expand Down
20 changes: 20 additions & 0 deletions test/combinators/weighted.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Random: MersenneTwister
using Test

using MeasureBase
using MeasureBase: _logweight, weightedmeasure, WeightedMeasure

@testset "weighted" begin
@test iszero(_logweight(Lebesgue(ℝ)))
μ₀ = Dirac(0.0)
w = 2.0
μ = @inferred w * μ₀
@test μ == WeightedMeasure(log(w), μ₀) == weightedmeasure(log(w), μ₀)
@test μ isa WeightedMeasure
@test _logweight(μ) == log(w)
@test _logweight(w * μ) == 2 * log(w)
@test rand(MersenneTwister(123), μ) == rand(MersenneTwister(123), μ₀)
x = rand()
@test logdensity_def(μ, x) == log(w)
@test logdensityof(μ, x) == logdensityof(μ₀, x)
end
theogf marked this conversation as resolved.
Show resolved Hide resolved
2 changes: 2 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,5 @@ end
# @test f(x) ≈ x^2
# end
end

include("combinators/weighted.jl")