From 538f335c6744cb4701b481ec1fb85ff4216fa910 Mon Sep 17 00:00:00 2001 From: Bagaev Dmitry Date: Mon, 6 Feb 2023 16:03:38 +0100 Subject: [PATCH] update ReactiveMP.jl version, bump version to 2.7.0 --- Project.toml | 4 +- examples/Project.toml | 2 + examples/Universal Mixtures.ipynb | 2770 ++----------------- test/models/mixtures/test_gmm_univariate.jl | 6 +- test/models/mixtures/test_switch.jl | 12 +- 5 files changed, 174 insertions(+), 2620 deletions(-) diff --git a/Project.toml b/Project.toml index 6612b4dd7..04b2dbe74 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "RxInfer" uuid = "86711068-29c9-4ff7-b620-ae75d7495b3d" authors = ["Bagaev Dmitry and contributors"] -version = "2.6.0" +version = "2.7.0" [deps] DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8" @@ -26,7 +26,7 @@ GraphPPL = "3.1.0" MacroTools = "0.5.6" Optim = "1.0.0" ProgressMeter = "1.0.0" -ReactiveMP = "~3.4.0" +ReactiveMP = "~3.5.0" Reexport = "1.2.0" Rocket = "1.6.0" TupleTools = "1.2.0" diff --git a/examples/Project.toml b/examples/Project.toml index 42ff77251..0cbd2d650 100644 --- a/examples/Project.toml +++ b/examples/Project.toml @@ -12,7 +12,9 @@ Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a" Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80" ProgressMeter = "92933f4c-e287-5a05-a399-4b506db050ca" PyPlot = "d330b81b-6aea-500a-939a-2ce795aea3ee" +ReactiveMP = "a194aa59-28ba-4574-a09c-4a745416d6e3" ReverseDiff = "37e2e3b7-166d-5795-8a7a-e32c996b4267" +Rocket = "df971d30-c9d6-4b37-b8ff-e965b2cb3a40" RxInfer = "86711068-29c9-4ff7-b620-ae75d7495b3d" SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b" StableRNGs = "860ef19b-820b-49d6-a774-d7a799459cd3" diff --git a/examples/Universal Mixtures.ipynb b/examples/Universal Mixtures.ipynb index 5acd92d73..abb592921 100644 --- a/examples/Universal Mixtures.ipynb +++ b/examples/Universal Mixtures.ipynb @@ -11,7 +11,15 @@ "cell_type": "code", "execution_count": 1, "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\u001b[36m\u001b[1m[ \u001b[22m\u001b[39m\u001b[36m\u001b[1mInfo: \u001b[22m\u001b[39mPrecompiling RxInfer [86711068-29c9-4ff7-b620-ae75d7495b3d]\n" + ] + } + ], "source": [ "using RxInfer, Distributions, Random, Plots" ] @@ -34,717 +42,53 @@ "\n", "\n", "\n", - " \n", + " \n", " \n", " \n", "\n", - "\n", + "\n", "\n", - " \n", + " \n", " \n", " \n", "\n", - "\n", + "\n", "\n", - " \n", + " \n", " \n", " \n", "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n" + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" ] }, "execution_count": 2, @@ -921,723 +265,55 @@ "\n", "\n", "\n", - " \n", + " \n", " \n", " \n", "\n", - "\n", + "\n", "\n", - " \n", + " \n", " \n", " \n", "\n", - "\n", + "\n", "\n", - " \n", + " \n", " \n", " \n", "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n" + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" ] }, "execution_count": 8, @@ -1670,617 +346,53 @@ "\n", "\n", "\n", - " \n", + " \n", " \n", " \n", "\n", - "\n", + "\n", "\n", - " \n", + " \n", " \n", " \n", "\n", - "\n", + "\n", "\n", - " \n", + " \n", " \n", " \n", "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n" + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" ] }, "execution_count": 9, @@ -2291,7 +403,7 @@ "source": [ "rθ = range(0, 1, length = 1000)\n", "p = plot(title = \"prior belief\")\n", - "plot!(rθ, (x) -> pdf(MixtureModel([Beta(7.0, 2.0), Beta(2.0, 7.0)], Bernoulli(0.7)), x), fillalpha=0.3, fillrange = 0, label=\"P(θ) Mary\", c=1)\n", + "plot!(rθ, (x) -> pdf(MixtureDistribution([Beta(2.0, 7.0), Beta(7.0, 2.0)], [ 0.3, 0.7 ]), x), fillalpha=0.3, fillrange = 0, label=\"P(θ) Mary\", c=1)\n", "plot!(rθ, (x) -> 0.7*pdf(Beta(7.0, 2.0), x), c=3, label=\"\")\n", "plot!(rθ, (x) -> 0.3*pdf(Beta(2.0, 7.0), x), c=3, label=\"\")" ] @@ -2315,14 +427,14 @@ " y = datavar(Int64, nr_throws)\n", "\n", " # specify John's and Jane's prior models over θ\n", - " θ_john ~ Beta(7.0, 2.0)\n", " θ_jane ~ Beta(2.0, 7.0)\n", + " θ_john ~ Beta(7.0, 2.0)\n", "\n", " # specify initial guess as to who is right\n", - " john_is_right ~ Bernoulli(0.7)\n", + " john_is_right ~ Bernoulli(0.7) \n", "\n", " # specify mixture prior Distribution\n", - " θ ~ Switch(john_is_right, (θ_john, θ_jane))\n", + " θ ~ Switch(john_is_right, (θ_jane, θ_john))\n", "\n", " # create likelihood models\n", " for i in 1:nr_throws\n", @@ -2379,611 +491,51 @@ "\n", "\n", "\n", - " \n", + " \n", " \n", " \n", "\n", - "\n", + "\n", "\n", - " \n", + " \n", " \n", " \n", "\n", - "\n", + "\n", "\n", - " \n", + " \n", " \n", " \n", "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n", - "\n" + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n" ] }, "execution_count": 12, @@ -2995,14 +547,14 @@ "rθ = range(0, 1, length = 1000)\n", "p = plot(title = \"posterior belief\")\n", "plot!(rθ, (x) -> pdf(result_mary.posteriors[:θ], x), fillalpha=0.3, fillrange = 0, label=\"P(θ|y) Mary\", c=1)\n", - "plot!(rθ, (x) -> probvec(result_mary.posteriors[:θ].prior)[1]*pdf(result_mary.posteriors[:θ].components[1], x), label=\"\", c=3)\n", - "plot!(rθ, (x) -> probvec(result_mary.posteriors[:θ].prior)[2]*pdf(result_mary.posteriors[:θ].components[2], x), label=\"\", c=3)" + "plot!(rθ, (x) -> result_mary.posteriors[:θ].weights[1] * pdf(result_mary.posteriors[:θ].components[1], x), label=\"\", c=3)\n", + "plot!(rθ, (x) -> result_mary.posteriors[:θ].weights[2] * pdf(result_mary.posteriors[:θ].components[2], x), label=\"\", c=3)" ] } ], "metadata": { "kernelspec": { - "display_name": "Julia 1.8.3", + "display_name": "Julia 1.8.5", "language": "julia", "name": "julia-1.8" }, @@ -3010,7 +562,7 @@ "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", - "version": "1.8.3" + "version": "1.8.5" } }, "nbformat": 4, diff --git a/test/models/mixtures/test_gmm_univariate.jl b/test/models/mixtures/test_gmm_univariate.jl index 496bdcffa..86b9eb21c 100644 --- a/test/models/mixtures/test_gmm_univariate.jl +++ b/test/models/mixtures/test_gmm_univariate.jl @@ -90,7 +90,7 @@ end @test length(mw1) === 10 @test length(mw2) === 10 @test length(fe) === 10 && all(filter(e -> abs(e) > 1e-3, diff(fe)) .< 0) - @test abs(last(fe) - 280.3276104) < 0.01 + @test abs(last(fe) - 139.74362) < 0.01 ms = mean(last(mswitch)) @@ -100,14 +100,14 @@ end rms = sort([μ1, μ2]) foreach(zip(rms, ems)) do (r, e) - @test abs(r - mean(e)) < 0.2 + @test abs(r - mean(e)) < 0.19 end ews = sort([last(mw1), last(mw2)], by = mean) rws = sort([w, w]) foreach(zip(rws, ews)) do (r, e) - @test abs(r - mean(e)) < 0.2 + @test abs(r - mean(e)) < 0.15 end @test_plot "models" "gmm_univariate" begin diff --git a/test/models/mixtures/test_switch.jl b/test/models/mixtures/test_switch.jl index 18ffd0e15..7a296dbdb 100644 --- a/test/models/mixtures/test_switch.jl +++ b/test/models/mixtures/test_switch.jl @@ -86,15 +86,15 @@ end @test getdata(result2.posteriors[:θ]) == getdata(resultswitch.posteriors[:in2]) @test getdata(resultswitch.posteriors[:in1]) == getdata(resultswitch.posteriors[:θ]).components[1] @test getdata(resultswitch.posteriors[:in2]) == getdata(resultswitch.posteriors[:θ]).components[2] - @test getdata(resultswitch.posteriors[:selector]).p ≈ getdata(resultswitch.posteriors[:θ]).prior.p[1] + @test getdata(resultswitch.posteriors[:selector]).p ≈ getdata(resultswitch.posteriors[:θ]).weights # check free energies @test -result1.free_energy[1] ≈ getlogscale(result1.posteriors[:θ]) @test -result2.free_energy[1] ≈ getlogscale(result2.posteriors[:θ]) - @test getlogscale(resultswitch.posteriors[:in1]) ≈ log(0.7) - result1.free_energy[1] - @test getlogscale(resultswitch.posteriors[:in2]) ≈ log(0.3) - result2.free_energy[1] - @test log(0.7 * exp(-result1.free_energy[1]) + 0.3 * exp(-result2.free_energy[1])) ≈ getlogscale(resultswitch.posteriors[:selector]) - @test log(0.7 * exp(-result1.free_energy[1]) + 0.3 * exp(-result2.free_energy[1])) ≈ getlogscale(resultswitch.posteriors[:θ]) + @test getlogscale(resultswitch.posteriors[:in1]) ≈ log(0.3) - result1.free_energy[1] + @test getlogscale(resultswitch.posteriors[:in2]) ≈ log(0.7) - result2.free_energy[1] + @test log(0.3 * exp(-result1.free_energy[1]) + 0.7 * exp(-result2.free_energy[1])) ≈ getlogscale(resultswitch.posteriors[:selector]) + @test log(0.3 * exp(-result1.free_energy[1]) + 0.7 * exp(-result2.free_energy[1])) ≈ getlogscale(resultswitch.posteriors[:θ]) @test getlogscale(resultswitch.posteriors[:θ]) ≈ getlogscale(resultswitch.posteriors[:selector]) ## Create output plots @@ -103,7 +103,7 @@ end θestimated = resultswitch.posteriors[:θ] p = plot(title = "Inference results") - plot!(rθ, (x) -> pdf(MixtureModel([Beta(4.0, 8.0), Beta(8.0, 4.0)], Categorical([0.5, 0.5])), x), fillalpha = 0.3, fillrange = 0, label = "P(θ)", c = 1) + plot!(rθ, (x) -> pdf(MixtureDistribution([Beta(4.0, 8.0), Beta(8.0, 4.0)], [0.5, 0.5]), x), fillalpha = 0.3, fillrange = 0, label = "P(θ)", c = 1) plot!(rθ, (x) -> pdf(getdata(θestimated), x), fillalpha = 0.3, fillrange = 0, label = "P(θ|y)", c = 3) vline!([θ_real], label = "Real θ") return p