Skip to content
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

Fix bug #40

Merged
merged 5 commits into from
Jan 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "InducingPoints"
uuid = "b4bd816d-b975-4295-ac05-5f2992945579"
authors = ["Théo Galy-Fajou <theo.galyfajou@gmail.com> and JuliaGaussianProcesses"]
version = "0.3.4"
version = "0.3.5"

[deps]
AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918"
Expand Down
29 changes: 14 additions & 15 deletions src/offline/greedy_var_selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,24 @@ function partial_pivoted_cholesky(k::Kernel, x::AbstractVector, M::Int, tol::Rea
j_max += j - 1

if d_max < tol * C_max
return (V, p, j - 1)
return (V, p, j - 1, d)
end

if j_max ≢ j
switch!(p, j, j_max)
switch!(d, j, j_max)

u .= kernelmatrix(k, x, x[j_max:j_max])
switch_rows!(V, j, j_max)
end
switch!(p, j, j_max)
switch!(d, j, j_max)
switch_rows!(V, j, j_max, 1:M)
u .= kernelmatrix(k, x[p], x[p[j]:p[j]])

V[j, j] = sqrt(d_max)

for i in (j + 1):N
V[i, j] = (u[i] - dot(view(V, i, 1:(j - 1)), view(V, j, 1:(j - 1)))) / V[j, j]
a = u[i]
b = view(V, i, 1:(j - 1))' * view(V, j, 1:(j - 1))
V[i, j] = (a - b) / V[j, j]
d[i] -= V[i, j]^2
end
end
return (V, p, M)
return (V, p, M, d)
end

@inline function switch!(x::Array, i::Int, j::Int)
Expand All @@ -56,10 +55,10 @@ end
return nothing
end

@inline function switch_rows!(x::Array, i::Int, j::Int)
tmp = x[i, :]
x[i, :] .= x[j, :]
x[j, :] .= tmp
@inline function switch_rows!(x::Array, i::Int, j::Int, col_indices)
tmp = x[i, col_indices]
x[i, col_indices] .= x[j, col_indices]
x[j, col_indices] .= tmp
return nothing
end

Expand Down Expand Up @@ -100,6 +99,6 @@ function inducingpoints(

# Perform the partial Cholesky, and return the elements of `x` residing in the first
# M_used elements of the permutation vector returned.
V, p, M_used = partial_pivoted_cholesky(kernel, x, alg.M, alg.tol)
_, p, M_used, _ = partial_pivoted_cholesky(kernel, x, alg.M, alg.tol)
return x[p[1:M_used]]
end
3 changes: 2 additions & 1 deletion test/offline/greedy_var_selection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
tol in [1e-15, 1e-12, 1e-9, 1e-6, 1e-3, 1e0]

x = range(0, 1; length=N)
V, p, M_used = InducingPoints.partial_pivoted_cholesky(SEKernel(), x, M, tol)
V, p, M_used, d = InducingPoints.partial_pivoted_cholesky(SEKernel(), x, M, tol)
V_M = V[:, 1:M_used]
p_M = p[1:M_used]

C = kernelmatrix(SEKernel(), range(0, 1; length=N))
@test C[p_M, p_M][1:M_used, 1:M_used] ≈ (V_M * V_M')[1:M_used, 1:M_used]
@test M_used <= M
@test M_used == M || maximum(diag(C - V_M * V_M')) < maximum(diag(C)) * tol
@test all(d .>= -10 * eps(Float64))
end
end

Expand Down