-
Notifications
You must be signed in to change notification settings - Fork 7
Initial proof-of-concept Expectation Propagation #64
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
Open
st--
wants to merge
113
commits into
master
Choose a base branch
from
st/ExpectationPropagation
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 111 commits
Commits
Show all changes
113 commits
Select commit
Hold shift + click to select a range
3b8a075
initial commit of Laplace approximation & EP
st-- c73e9ce
Laplace demo
st-- 93ec795
bugfix
st-- d62b2dc
bugfix demo
st-- 76afc55
WIP: cleanup; pass around dist_y_given_f explicitly
st-- 46009e4
use ForwardDiff elementwise (much faster)
st-- 0c1a329
format
st-- 88410cc
WIP: gradient
st-- 4853b5f
*Very* WIP - but works
st-- 3c91a22
frule bugfix
st-- 513d34d
clean up comments
st-- 33af436
intermediate cleanup (callbacks not tested)
st-- 30b45ac
fix callback support
st-- 09e5e82
Laplace initial tests
st-- 2591555
make argument order consistent
st-- 5d58f5d
more cleanup
st-- 6c4e56d
cleanup
st-- ead526f
cleanup
st-- fe76626
@info -> @debug and ChainRulesTestUtil workaround
st-- f7439d6
chainrule tests
st-- d71ca20
add @info for res_cold/res_warm
st-- ecb29d9
cleanup
st-- b275332
explicit FiniteDifferences gradient test on laplace_lml
st-- 8135c40
Merge branch 'master' of github.com:JuliaGaussianProcesses/Approximat…
st-- 0d8eb02
format
st-- af07036
format
st-- 6f08e73
remove Zygote dependency - part 1
st-- f84ed86
remove Zygote dependency - part 2
st-- e4aab7e
pkg bugfix
st-- d19003f
update example manifests
st-- 2d43113
fix chainrule test by evaluating frule/rrule on newton_inner_loop bas…
st-- bbd24ec
add compat
st-- ac6486d
clean up test
st-- 96957e3
add missing CRC dependency
st-- 5af23fa
remove workaround
st-- 54acbe3
use more of AbstractGPs API
st-- df88117
clean up laplace_steps
st-- 8d20a65
add laplace example
st-- 30ba663
cleanup
st-- 8209442
cleanup2
st-- def61e7
format
st-- 4566c17
remove demo script
st-- 4095df1
bugfix
st-- 91d0b34
update notebook
st-- bdc3618
bugfix 2
st-- 12655f8
bugfiiiix
st-- b1c0f80
also plot mean
st-- 3c97c97
improve plotting
st-- 81ceb93
Apply suggestions from code review
st-- 82d4695
remove `@ref` that does not work
st-- aca90b1
cleanup
st-- 02b528b
make use of closure fields
st-- cbfbc95
Merge branch 'st/laplace_and_ep' of github.com:JuliaGaussianProcesses…
st-- e788f4c
yaf
st-- ca68222
improved type stability
st-- 9144ed3
replace QuadGK with Gauss-Hermite
st-- 771ef3e
Apply suggestions from code review
st-- b52ef55
more type stability cleanup
st-- 4f45ff6
Merge branch 'st/laplace_and_ep' of github.com:JuliaGaussianProcesses…
st-- 4a694d9
fix test
st-- 62840ad
add missing test file
st-- 6f7a5ba
more explanation on the example script
st-- 5445a95
fix test seed
st-- 02f414b
Merge remote-tracking branch 'origin/master' into st/laplace_and_ep
st-- bf3cf02
initial version of full EP inference
st-- a37714f
add posterior
st-- 3ab1f14
add export
st-- d0cc8fd
add EP to example script
st-- 9c4f7c4
Apply suggestions from code review
st-- c690dd0
bugfix
st-- 88de29b
Merge branch 'master' of github.com:JuliaGaussianProcesses/Approximat…
st-- 97c6257
add prediction test for EP
st-- bf98f16
run all tests
st-- 4aa068b
Apply suggestions from code review
st-- 67b69a7
pass n_gh around
st-- 41add63
Merge branch 'st/ExpectationPropagation' of github.com:JuliaGaussianP…
st-- 6564670
whitespace
st-- 40eab41
reorder ep.jl
st-- e068563
bugfix
st-- 1dec761
update Manifests
st-- d3adaf9
add approx_lml for EP (DOES NOT WORK CORRECTLY YET)
st-- f5f61d4
Apply suggestions from code review
st-- 29e5cac
more explicit using statements
st-- 0cfc13a
update Project
st-- 7127c9a
Merge branch 'st/ExpectationPropagation' of github.com:JuliaGaussianP…
st-- 89a1281
Merge branch 'master' of github.com:JuliaGaussianProcesses/Approximat…
st-- 8e30287
WIP
st-- 80a99cc
Apply suggestions from code review
st-- 51323ef
Apply suggestions from code review
st-- b7a38da
update manifests
st-- 514e06a
move testset into test/ep.jl
st-- 1da0cd4
remove approx_lml and tests
st-- 5d88e5a
do not export ExpectationPropagation, add warnings
st-- 5742ca6
fix merge
st-- 83986b1
Merge branch 'master' into st/ExpectationPropagation
st-- 7b34db1
some fixes
st-- e4a53d9
comment
st-- 82c340a
error with message instead of assert
st-- 191ed5a
Apply suggestions from code review
st-- 6de0a2b
add missing import
st-- 1d9b7b1
Merge branch 'st/ExpectationPropagation' of github.com:JuliaGaussianP…
st-- 8778ea0
update comparison notebook
st-- 194ed6b
fix
st-- 75264e0
Update src/ep.jl
st-- 48c7570
Merge branch 'master' into st/ExpectationPropagation
st-- 409f8c3
Update src/ep.jl
st-- 5900e73
add imports
st-- c774892
Merge branch 'master' of github.com:JuliaGaussianProcesses/Approximat…
st-- e71e697
Merge branch 'master' into st/ExpectationPropagation
st-- 2a0ff19
make use of TestUtils
st-- 5e02212
convert to ExpectationPropagationModule
st-- 7234963
Apply suggestions from code review
st-- db9b459
missing import
st-- File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,211 @@ | ||
| module ExpectationPropagationModule | ||
|
|
||
| using ..API | ||
|
|
||
| export ExpectationPropagation | ||
|
|
||
| using LinearAlgebra | ||
| using Random: randperm | ||
|
|
||
| using Distributions | ||
| using FastGaussQuadrature: gausshermite | ||
| using IrrationalConstants: log2π, sqrt2π, sqrt2, invsqrtπ | ||
| using Statistics | ||
| using StatsBase | ||
|
|
||
| using AbstractGPs: LatentFiniteGP | ||
|
|
||
| struct ExpectationPropagation | ||
| maxiter::Int | ||
| epsilon::Float64 | ||
| n_gh::Int | ||
| end | ||
|
|
||
| function ExpectationPropagation(; maxiter=100, epsilon=1e-6, n_gh=150) | ||
| return ExpectationPropagation(maxiter, epsilon, n_gh) | ||
| end | ||
|
|
||
| function AbstractGPs.posterior(ep::ExpectationPropagation, lfx::LatentFiniteGP, ys) | ||
| ep_state = ep_inference(ep, lfx, ys) | ||
| # NOTE: here we simply piggyback on the SparseVariationalApproximation. | ||
| # Should AbstractGPs provide its own "GP conditioned on f(x) ~ q(f)" rather | ||
| # than just "conditioned on observation under some noise" (which is *not* | ||
| # the same thing...)? | ||
| return posterior(SparseVariationalApproximation(Centered(), lfx.fx, ep_state.q)) | ||
| end | ||
|
|
||
| function ep_inference(ep::ExpectationPropagation, lfx::LatentFiniteGP, ys) | ||
| fx = lfx.fx | ||
| mean(fx) == zero(mean(fx)) || | ||
| error("non-zero prior mean currently not supported: discuss on GitHub issue #89") | ||
| length(ys) == length(fx) || error( | ||
| "ExpectationPropagation currently does not support multi-latent likelihoods; please open an issue on GitHub", | ||
| ) | ||
| dist_y_given_f = lfx.lik | ||
| K = cov(fx) | ||
|
|
||
| return ep_inference(dist_y_given_f, ys, K; ep) | ||
| end | ||
|
|
||
| function ep_inference(dist_y_given_f, ys, K; ep=nothing) | ||
| ep_problem = EPProblem(dist_y_given_f, ys, K; ep) | ||
| ep_state = EPState(ep_problem) | ||
| return ep_outer_loop(ep_problem, ep_state) | ||
| end | ||
|
|
||
| function EPProblem(ep::ExpectationPropagation, p::MvNormal, lik_evals::AbstractVector) | ||
| return (; p, lik_evals, ep) | ||
| end | ||
|
|
||
| function EPProblem(dist_y_given_f, ys, K; ep=nothing) | ||
| f_prior = MvNormal(K) | ||
| lik_evals = [f -> pdf(dist_y_given_f(f), y) for y in ys] | ||
| return EPProblem(ep, f_prior, lik_evals) | ||
| end | ||
|
|
||
| function EPState(ep_problem, q::MvNormal, sites::AbstractVector) | ||
| return (; ep_problem, q, sites) | ||
| end | ||
|
|
||
| function EPState(ep_problem) | ||
| N = length(ep_problem.lik_evals) | ||
| # TODO- manually keep track of canonical parameters and initialize precision to 0 | ||
| sites = [ | ||
| (; Z=NaN, log_Ztilde=NaN, q=NormalCanon(0.0, 1e-10), cav=NormalCanon(0.0, 1.0)) for | ||
| _ in 1:N | ||
| ] | ||
| q = ep_problem.p | ||
| return EPState(ep_problem, q, sites) | ||
| end | ||
|
|
||
| function ep_approx_posterior(prior, sites::AbstractVector) | ||
| canon_site_dists = [convert(NormalCanon, t.q) for t in sites] | ||
| potentials = [q.η for q in canon_site_dists] | ||
| precisions = [q.λ for q in canon_site_dists] | ||
| ts_dist = MvNormalCanon(potentials, Diagonal(precisions)) | ||
| return mul_dist(prior, ts_dist) | ||
| end | ||
|
|
||
| function ep_outer_loop(ep_problem, ep_state; maxiter=ep_problem.ep.maxiter) | ||
| for i in 1:maxiter | ||
| @info "Outer loop iteration $i" | ||
| new_state = ep_loop_over_sites(ep_problem, ep_state) | ||
| if ep_converged(ep_state.sites, new_state.sites; epsilon=ep_problem.ep.epsilon) | ||
| @info "converged" | ||
| break | ||
| else | ||
| ep_state = new_state | ||
| end | ||
| end | ||
| return ep_state | ||
| end | ||
|
|
||
| function ep_converged(old_sites, new_sites; epsilon=1e-6) | ||
| # TODO improve convergence check | ||
| diff1 = [(t_old.q.η - t_new.q.η)^2 for (t_old, t_new) in zip(old_sites, new_sites)] | ||
| diff2 = [(t_old.q.λ - t_new.q.λ)^2 for (t_old, t_new) in zip(old_sites, new_sites)] | ||
| return mean(diff1) < epsilon && mean(diff2) < epsilon | ||
| end | ||
|
|
||
| function ep_loop_over_sites(ep_problem, ep_state) | ||
| # TODO: randomize order of updates: make configurable? | ||
| for i in randperm(length(ep_problem.lik_evals)) | ||
| @info " Inner loop iteration $i" | ||
| new_site = ep_single_site_update(ep_problem, ep_state, i) | ||
|
|
||
| # TODO: rank-1 update | ||
| new_sites = deepcopy(ep_state.sites) | ||
| new_sites[i] = new_site | ||
| new_q = meanform(ep_approx_posterior(ep_problem.p, new_sites)) | ||
| ep_state = EPState(ep_problem, new_q, new_sites) | ||
| end | ||
| return ep_state | ||
| end | ||
|
|
||
| function ep_single_site_update(ep_problem, ep_state, i::Int) | ||
| q_fi = ith_marginal(ep_state.q, i) | ||
| alik_i = epsite_dist(ep_state.sites[i]) | ||
| cav_i = div_dist(q_fi, alik_i) | ||
| qhat_i = moment_match(cav_i, ep_problem.lik_evals[i]; n_points=ep_problem.ep.n_gh) | ||
| Zhat = qhat_i.Z | ||
| new_t = div_dist(qhat_i.q, cav_i) | ||
| var_sum = var(cav_i) + var(new_t) | ||
| Ztilde = Zhat * sqrt2π * sqrt(var_sum) * exp((mean(cav_i) - mean(new_t))^2 / (2var_sum)) | ||
| log_Ztilde = | ||
| log(Zhat) + | ||
| log2π / 2 + | ||
| log(var_sum) / 2 + | ||
| (mean(cav_i) - mean(new_t))^2 / (2var_sum) | ||
| return (; Z=Ztilde, log_Ztilde=log_Ztilde, q=new_t, cav=cav_i) # cav_i only required by approx_lml test | ||
| end | ||
|
|
||
| function ith_marginal(d::Union{MvNormal,MvNormalCanon}, i::Int) | ||
| m = mean(d) | ||
| v = var(d) | ||
| return Normal(m[i], sqrt(v[i])) | ||
| end | ||
|
|
||
| function mul_dist(a::NormalCanon, b::NormalCanon) | ||
| # NormalCanon | ||
| # η::T # σ^(-2) * μ | ||
| # λ::T # σ^(-2) | ||
| etaAmulB = a.η + b.η | ||
| lambdaAmulB = a.λ + b.λ | ||
| return NormalCanon(etaAmulB, lambdaAmulB) | ||
| end | ||
|
|
||
| mul_dist(a, b) = mul_dist(convert(NormalCanon, a), convert(NormalCanon, b)) | ||
|
|
||
| function mul_dist(a::MvNormalCanon, b::MvNormalCanon) | ||
| # MvNormalCanon | ||
| # h::V # potential vector, i.e. inv(Σ) * μ | ||
| # J::P # precision matrix, i.e. inv(Σ) | ||
| hAmulB = a.h + b.h | ||
| JAmulB = a.J + b.J | ||
| return MvNormalCanon(hAmulB, JAmulB) | ||
| end | ||
|
|
||
| mul_dist(a::MvNormal, b) = mul_dist(canonform(a), b) | ||
|
|
||
| function div_dist(a::NormalCanon, b::NormalCanon) | ||
| # NormalCanon | ||
| # η::T # σ^(-2) * μ | ||
| # λ::T # σ^(-2) | ||
| etaAdivB = a.η - b.η | ||
| lambdaAdivB = a.λ - b.λ | ||
| return NormalCanon(etaAdivB, lambdaAdivB) | ||
| end | ||
|
|
||
| div_dist(a::Normal, b) = div_dist(convert(NormalCanon, a), b) | ||
| div_dist(a, b::Normal) = div_dist(a, convert(NormalCanon, b)) | ||
|
|
||
| #function EPSite(Z, m, s2) | ||
| # return (; Z, m, s2) | ||
| #end | ||
| # | ||
| #function epsite_dist(site) | ||
| # return Normal(site.m, sqrt(site.s2)) | ||
| #end | ||
|
|
||
| epsite_dist(site) = site.q | ||
|
|
||
| function epsite_pdf(site, f) | ||
| return site.Z * pdf(epsite_dist(site), f) | ||
| end | ||
|
|
||
| function moment_match(cav_i::Union{Normal,NormalCanon}, lik_eval_i; n_points=150) | ||
| # TODO: combine with expected_loglik / move into GPLikelihoods | ||
| xs, ws = gausshermite(n_points) | ||
| fs = (sqrt2 * std(cav_i)) .* xs .+ mean(cav_i) | ||
| lik_ws = lik_eval_i.(fs) .* ws | ||
| fs_lik_ws = fs .* lik_ws | ||
| m0 = invsqrtπ * sum(lik_ws) | ||
| m1 = invsqrtπ * sum(fs_lik_ws) | ||
| m2 = invsqrtπ * dot(fs_lik_ws, fs) | ||
| matched_Z = m0 | ||
| matched_mean = m1 / m0 | ||
| matched_var = m2 / m0 - matched_mean^2 | ||
| return (; Z=matched_Z, q=Normal(matched_mean, sqrt(matched_var))) | ||
| end | ||
|
|
||
| end |
This file contains hidden or 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 hidden or 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,28 @@ | ||
| @testset "Expectation Propagation" begin | ||
| @testset "moment_match" begin | ||
| function moment_match_quadgk(cav_i::UnivariateDistribution, lik_eval_i) | ||
| lower = mean(cav_i) - 20 * std(cav_i) | ||
| upper = mean(cav_i) + 20 * std(cav_i) | ||
| m0, _ = quadgk(f -> pdf(cav_i, f) * lik_eval_i(f), lower, upper) | ||
| m1, _ = quadgk(f -> f * pdf(cav_i, f) * lik_eval_i(f), lower, upper) | ||
| m2, _ = quadgk(f -> f^2 * pdf(cav_i, f) * lik_eval_i(f), lower, upper) | ||
| matched_Z = m0 | ||
| matched_mean = m1 / m0 | ||
| matched_var = m2 / m0 - matched_mean^2 | ||
| return (; Z=matched_Z, q=Normal(matched_mean, sqrt(matched_var))) | ||
| end | ||
|
|
||
| cav_i = Normal(0.8231, 3.213622) # random numbers | ||
| lik_eval_i = f -> pdf(Bernoulli(logistic(f)), true) | ||
| Z_gh, q_gh = ExpectationPropagationModule.moment_match(cav_i, lik_eval_i; n_points=100) | ||
| Z_quad, q_quad = moment_match_quadgk(cav_i, lik_eval_i) | ||
| @test Z_gh ≈ Z_quad | ||
| @test mean(q_gh) ≈ mean(q_quad) | ||
| @test std(q_gh) ≈ std(q_quad) | ||
| end | ||
|
|
||
| @testset "predictions" begin | ||
| approx = ApproximateGPs.ExpectationPropagation(; n_gh=500) | ||
| ApproximateGPs.TestUtils.test_approximation_predictions(approx) | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.