Skip to content

Commit 5b4f569

Browse files
authored
fix @Half macro (#8)
1 parent 83fc0c0 commit 5b4f569

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

src/combinators/half.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ end
1111

1212
unhalf::Half) = μ.parent
1313

14-
basemeasure::Half) = basemeasure(μ.parent)
14+
basemeasure::Half) = WeightedMeasure(logtwo, basemeasure(unhalf(μ)))
1515

1616
function Base.rand(rng::AbstractRNG, T::Type, μ::Half)
1717
return abs(rand(rng, T, unhalf(μ)))
1818
end
19+
20+
logdensity::Half, x) = (x < 0) * (-Inf) + (x > 0) * logdensity(unhalf(μ), x)

src/macros.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ function _half(__module__, ex)
168168
@match ex begin
169169
:($dist) => begin
170170
halfdist = esc(Symbol(:Half, dist))
171-
171+
dist = esc(dist)
172172
quote
173173
$halfdist(args...) = Half($dist(args...))
174174
$halfdist(;kwargs...) = Half($dist(;kwargs...))

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ end
151151
end
152152
end
153153

154+
@testset "Half" begin
155+
Normal() = ∫exp(x -> -0.5x^2, Lebesgue(ℝ))
156+
@half Normal
157+
@test logdensity(HalfNormal(), -0.2) == -Inf
158+
@test logdensity(HalfNormal(), 0.2) == logdensity(Normal(), 0.2)
159+
end
160+
154161
# import MeasureBase.:⋅
155162
# function ⋅(μ::Normal, kernel)
156163
# m = kernel(μ)

0 commit comments

Comments
 (0)