Skip to content

Commit a696ef6

Browse files
committed
Additional pushfwd and pullbck tests
1 parent d155042 commit a696ef6

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

test/combinators/transformedmeasure.jl

+100
Original file line numberDiff line numberDiff line change
@@ -79,3 +79,103 @@ end
7979
end
8080
end
8181
end
82+
83+
using IrrationalConstants: loghalf
84+
85+
@testset "Half" begin
86+
μ = Half(StdNormal())
87+
88+
# Test logdensityof for positive values
89+
@test logdensityof(μ, 1.0) logdensityof(StdNormal(), 1.0) - loghalf
90+
91+
# Test logdensityof for negative values (should return -Inf)
92+
@test logdensityof(μ, -1.0) == -Inf
93+
94+
# Test logdensityof for zero
95+
@test logdensityof(μ, 0.0) logdensityof(StdNormal(), 0.0) - loghalf
96+
end
97+
98+
using Test
99+
using MeasureBase
100+
using MeasureBase: gettransform
101+
using InverseFunctions
102+
using StaticArrays
103+
using ChangesOfVariables
104+
105+
@testset "PushforwardMeasure" begin
106+
# Test basic pushforward construction
107+
μ = StdNormal()
108+
f = exp
109+
ν = pushfwd(f, μ)
110+
111+
@test ν isa PushforwardMeasure
112+
113+
# TODO: How do we access the parent?
114+
# @test parent(ν) === μ
115+
@test gettransform(ν) === f
116+
117+
# Test logdensity with AdaptRootMeasure
118+
x = 0.5
119+
y = f(x)
120+
ld = logdensityof(ν, y)
121+
@test !isnan(ld)
122+
@test isfinite(ld)
123+
124+
# Test logdensity with PushfwdRootMeasure
125+
ν_no_corr = pushfwd(f, μ, PushfwdRootMeasure())
126+
ld_no_corr = logdensityof(ν_no_corr, y)
127+
@test !isnan(ld_no_corr)
128+
@test isfinite(ld_no_corr)
129+
130+
# Test non-bijective pushforward
131+
g = x -> x^2
132+
@test_throws ArgumentError logdensityof(pushfwd(g, μ), 1.0)
133+
@test_throws ArgumentError logdensityof(pushfwd(g, μ, PushfwdRootMeasure()), 1.0)
134+
135+
# Test edge cases for _combine_logd_with_ladj
136+
@test MeasureBase._combine_logd_with_ladj(-Inf, Inf) == -Inf # Zero density wins
137+
@test MeasureBase._combine_logd_with_ladj(1.0, -Inf) MeasureBase.near_neg_inf(Float64)
138+
139+
# Test composition of pushforwards
140+
h = x -> exp(x)
141+
ν_comp = pushfwd(h, ν)
142+
@test ν_comp isa PushforwardMeasure
143+
144+
# TODO: How do we access the parent?
145+
# @test parent(ν_comp) === μ
146+
147+
# Test identity pushforward
148+
ν_id = pushfwd(identity, μ)
149+
@test ν_id === μ
150+
151+
# Test rootmeasure behavior
152+
@test rootmeasure(ν) === rootmeasure(μ) # AdaptRootMeasure
153+
@test rootmeasure(ν_no_corr) isa PushforwardMeasure # PushfwdRootMeasure
154+
155+
# Test basemeasure
156+
@test basemeasure(ν) isa PushforwardMeasure
157+
@test basemeasure(ν).style isa PushfwdRootMeasure
158+
159+
# Test massof
160+
# TODO: mass interface is very incomplete
161+
# @test massof(ν) == massof(μ)
162+
163+
# Test rand
164+
@test rand(ν) isa Real
165+
@test insupport(ν, rand(ν))
166+
167+
# Test pullback
168+
pb = pullbck(f, ν)
169+
@test pb isa PushforwardMeasure
170+
@test logdensityof(pb, y) logdensityof(μ, y)
171+
172+
# Test deprecated pullback
173+
@test_deprecated pullback(f, μ)
174+
end
175+
176+
@testset "PushFwdStyle types" begin
177+
@test AdaptRootMeasure() isa PushFwdStyle
178+
@test PushfwdRootMeasure() isa PushFwdStyle
179+
@test MeasureBase.WithVolCorr === AdaptRootMeasure
180+
@test MeasureBase.NoVolCorr === PushfwdRootMeasure
181+
end

0 commit comments

Comments
 (0)