-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #338 from biaslab/half_normal
Add Half-normal node
- Loading branch information
Showing
12 changed files
with
147 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export HalfNormal | ||
|
||
struct HalfNormal end | ||
|
||
@node HalfNormal Stochastic [out, (v, aliases = [var, σ²])] | ||
|
||
@average_energy HalfNormal (q_out::Any, q_v::Any) = begin | ||
out_mean, out_var = mean_var(q_out) | ||
return (log(π / 2) + mean(log, q_v) + mean(inv, q_v) * (out_mean^2 + out_var)) / 2 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
@rule HalfNormal(:out, Marginalisation) (q_v::PointMass,) = begin | ||
mean_v = mean(q_v) | ||
return Truncated(Normal(zero(eltype(q_v)), sqrt(mean_v)), zero(eltype(q_v)), typemax(float(mean_v))) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
module HalfNormalNodeTest | ||
|
||
using Test | ||
using ReactiveMP | ||
using Random | ||
|
||
import ReactiveMP: make_node | ||
|
||
@testset "HalfNormalNode" begin | ||
@testset "Creation" begin | ||
node = make_node(HalfNormal) | ||
@test functionalform(node) === HalfNormal | ||
@test sdtype(node) === Stochastic() | ||
@test name.(interfaces(node)) === (:out, :v) | ||
@test factorisation(node) === ((1, 2),) | ||
@test localmarginalnames(node) === (:out_v,) | ||
@test metadata(node) === nothing | ||
|
||
node = make_node(HalfNormal, FactorNodeCreationOptions(nothing, 1, nothing)) | ||
@test metadata(node) === 1 | ||
end | ||
|
||
@testset "AverageEnergy" begin | ||
begin | ||
q_out = GammaShapeRate(2.0, 1.0) | ||
q_v = PointMass(2.0) | ||
|
||
marginals = (Marginal(q_out, false, false, nothing), Marginal(q_v, false, false, nothing)) | ||
|
||
@test score(AverageEnergy(), HalfNormal, Val{(:out, :v)}(), marginals, nothing) ≈ 2.072364942925 | ||
end | ||
begin | ||
q_out = GammaShapeScale(2.0, 1.0) | ||
q_v = PointMass(2.0) | ||
|
||
marginals = (Marginal(q_out, false, false, nothing), Marginal(q_v, false, false, nothing)) | ||
|
||
@test score(AverageEnergy(), HalfNormal, Val{(:out, :v)}(), marginals, nothing) ≈ 2.072364942925 | ||
end | ||
|
||
begin | ||
q_out = GammaInverse(3.0, 1.0) | ||
q_v = PointMass(2.0) | ||
|
||
marginals = (Marginal(q_out, false, false, nothing), Marginal(q_v, false, false, nothing)) | ||
|
||
@test score(AverageEnergy(), HalfNormal, Val{(:out, :v)}(), marginals, nothing) ≈ 0.6973649429247 | ||
end | ||
end # testset: AverageEnergy | ||
end # testset | ||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module RulesHalfNormalOutTest | ||
|
||
using Test | ||
using ReactiveMP | ||
using Random | ||
using Distributions | ||
|
||
import ReactiveMP: @test_rules | ||
|
||
@testset "rules:HalfNormal:out" begin | ||
@testset "Variational Message Passing: (q_v::Any)" begin | ||
@test_rules [check_type_promotion = true] HalfNormal(:out, Marginalisation) [ | ||
(input = (q_v = PointMass(1.0),), output = Truncated(Normal(0.0, 1.0), 0.0, Inf)), | ||
(input = (q_v = PointMass(100.0),), output = Truncated(Normal(0.0, 10.0), 0.0, Inf)), | ||
(input = (q_v = PointMass(100),), output = Truncated(Normal(0.0, 10.0), 0.0, Inf)) | ||
] | ||
end | ||
end # testset | ||
end # module |