Skip to content

Commit e57ac47

Browse files
Split code into submodules for the different approximations (#96)
* stab at submodules * move approx_lml to API * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * clean up imports * remove unused imports * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * docfix * missing import * test: remove submodules from api docs * subpages in docs * hide private API docstrings * mention types in api/index.md * qualify * remove extra line * minor bump# * change docs/ compat * update test compat * explicitly `@reexport` to avoid exporting the submodules! * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * add likelihood imports * add import * Apply suggestions from code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * add import * Update src/SparseVariationalApproximationModule.jl Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 783edda commit e57ac47

18 files changed

+146
-52
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ApproximateGPs"
22
uuid = "298c2ebc-0411-48ad-af38-99e88101b606"
33
authors = ["JuliaGaussianProcesses Team"]
4-
version = "0.2.8"
4+
version = "0.3.0"
55

66
[deps]
77
AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918"

docs/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ ApproximateGPs = "298c2ebc-0411-48ad-af38-99e88101b606"
33
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
44

55
[compat]
6-
ApproximateGPs = "0.2"
6+
ApproximateGPs = "0.3"
77
Documenter = "0.27"

docs/make.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ makedocs(;
5858
pages=[
5959
"Home" => "index.md",
6060
"userguide.md",
61-
"API" => "api.md",
61+
"API" => ["api/index.md", "api/sparsevariational.md", "api/laplace.md"],
6262
"Examples" =>
6363
map(filter!(filename -> endswith(filename, ".md"), readdir(EXAMPLES_OUT))) do x
6464
return joinpath("examples", x)

docs/src/api.md

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/src/api/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# ApproximateGPs API
2+
3+
```@autodocs
4+
Modules = [ApproximateGPs, ApproximateGPs.API]
5+
Private = false
6+
```

docs/src/api/laplace.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Laplace Approximation
2+
3+
```@autodocs
4+
Modules = [ApproximateGPs.LaplaceApproximationModule]
5+
Private = false
6+
```

docs/src/api/sparsevariational.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Sparse Variational Approximation
2+
3+
```@autodocs
4+
Modules = [ApproximateGPs.SparseVariationalApproximationModule]
5+
Private = false
6+
```

src/API.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module API
2+
3+
export approx_lml # TODO move to AbstractGPs, see https://github.com/JuliaGaussianProcesses/AbstractGPs.jl/issues/221
4+
5+
"""
6+
approx_lml(approx::<Approximation>, lfx::LatentFiniteGP, ys)
7+
8+
Compute an approximation to the log of the marginal likelihood (also known as
9+
"evidence") under the given `approx` to the posterior. This approximation can be used to optimise the hyperparameters of `lfx`.
10+
11+
This should become part of the AbstractGPs API (see JuliaGaussianProcesses/AbstractGPs.jl#221).
12+
"""
13+
function approx_lml end
14+
15+
end

src/ApproximateGPs.jl

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,25 @@
11
module ApproximateGPs
22

33
using Reexport
4+
45
@reexport using AbstractGPs
56
@reexport using GPLikelihoods
6-
using Distributions
7-
using LinearAlgebra
8-
using Statistics
9-
using StatsBase
10-
using FastGaussQuadrature
11-
using SpecialFunctions
12-
using ChainRulesCore
13-
using FillArrays
14-
using PDMats: chol_lower
15-
using IrrationalConstants: sqrt2, invsqrtπ
16-
17-
using AbstractGPs: AbstractGP, FiniteGP, LatentFiniteGP, ApproxPosteriorGP, At_A, diag_At_A
18-
19-
include("utils.jl")
207

21-
export DefaultQuadrature, Analytic, GaussHermite, MonteCarlo
22-
include("expected_loglik.jl")
8+
include("API.jl")
9+
@reexport using .API: approx_lml
2310

24-
export SparseVariationalApproximation, Centered, NonCentered
25-
include("sparse_variational.jl")
11+
include("utils.jl")
2612

27-
using ForwardDiff
13+
include("SparseVariationalApproximationModule.jl")
14+
@reexport using .SparseVariationalApproximationModule:
15+
SparseVariationalApproximation, Centered, NonCentered
16+
@reexport using .SparseVariationalApproximationModule:
17+
DefaultQuadrature, Analytic, GaussHermite, MonteCarlo
2818

29-
export LaplaceApproximation
30-
export build_laplace_objective, build_laplace_objective!
31-
export approx_lml # TODO move to AbstractGPs, see https://github.com/JuliaGaussianProcesses/AbstractGPs.jl/issues/221
32-
include("laplace.jl")
19+
include("LaplaceApproximationModule.jl")
20+
@reexport using .LaplaceApproximationModule: LaplaceApproximation
21+
@reexport using .LaplaceApproximationModule:
22+
build_laplace_objective, build_laplace_objective!
3323

3424
include("deprecations.jl")
3525

src/laplace.jl renamed to src/LaplaceApproximationModule.jl

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
module LaplaceApproximationModule
2+
3+
using ..API
4+
5+
export LaplaceApproximation
6+
export build_laplace_objective, build_laplace_objective!
7+
8+
using ForwardDiff: ForwardDiff
9+
using Distributions
10+
using LinearAlgebra
11+
using Statistics
12+
using StatsBase
13+
14+
using ChainRulesCore: ignore_derivatives, NoTangent, @thunk
15+
using ChainRulesCore: ChainRulesCore
16+
17+
using AbstractGPs: AbstractGPs
18+
using AbstractGPs: LatentFiniteGP, ApproxPosteriorGP
19+
120
# Implementation follows Rasmussen & Williams, Gaussian Processes for Machine
221
# Learning, the MIT Press, 2006. In the following referred to as 'RW'.
322
# Online text:
@@ -36,7 +55,7 @@ Compute an approximation to the log of the marginal likelihood (also known as
3655
3756
This should become part of the AbstractGPs API (see JuliaGaussianProcesses/AbstractGPs.jl#221).
3857
"""
39-
function approx_lml(la::LaplaceApproximation, lfx::LatentFiniteGP, ys)
58+
function API.approx_lml(la::LaplaceApproximation, lfx::LatentFiniteGP, ys)
4059
return laplace_lml(lfx, ys; la.newton_kwargs...)
4160
end
4261

@@ -309,11 +328,13 @@ function ChainRulesCore.rrule(::typeof(newton_inner_loop), dist_y_given_f, ys, K
309328
function newton_pullback(Δf_opt)
310329
∂self = NoTangent()
311330

312-
∂dist_y_given_f = @not_implemented(
331+
∂dist_y_given_f = ChainRulesCore.@not_implemented(
313332
"gradient of Newton's method w.r.t. likelihood parameters"
314333
)
315334

316-
∂ys = @not_implemented("gradient of Newton's method w.r.t. observations")
335+
∂ys = ChainRulesCore.@not_implemented(
336+
"gradient of Newton's method w.r.t. observations"
337+
)
317338

318339
# ∂K = df/dK Δf
319340
∂K = @thunk(cache.Wsqrt * (cache.B_ch \ (cache.Wsqrt \ Δf_opt)) * cache.d_loglik')
@@ -417,3 +438,5 @@ function Statistics.cov(f::LaplacePosteriorGP, x::AbstractVector, y::AbstractVec
417438
vy = L \ (f.data.Wsqrt * cov(f.prior.f, f.prior.x, y))
418439
return cov(f.prior.f, x, y) - vx' * vy
419440
end
441+
442+
end

0 commit comments

Comments
 (0)