Skip to content

Commit 5808f9c

Browse files
committed
Added a PDMat option
1 parent 1c70c5d commit 5808f9c

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version = "0.2.0"
66
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
77
IRTools = "7869d1d1-7146-5819-86e3-90919afe41df"
88
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
9+
PDMats = "90014a1f-27ba-587c-ab20-58faa44d9150"
910
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
1011
StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c"
1112
Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f"

src/KernelFunctions.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ using Distances, LinearAlgebra
1616
using Zygote: @adjoint
1717
using SpecialFunctions: lgamma, besselk
1818
using StatsFuns: logtwo
19+
using PDMats
1920

2021
const defaultobs = 2
2122

@@ -32,7 +33,7 @@ kernels = ["exponential","matern","polynomial","constant","rationalquad","expone
3233
for k in kernels
3334
include(joinpath("kernels",k*".jl"))
3435
end
35-
include("kernelmatrix.jl")
36+
include("matrix/kernelmatrix.jl")
3637
include("kernels/kernelsum.jl")
3738
include("kernels/kernelproduct.jl")
3839

File renamed without changes.

src/matrix/kernelpdmat.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
"""
2+
Guarantees to return a positive-definite matrix in the form of a `PDMat` matrix with the cholesky decomposition precomputed
3+
"""
4+
function kernelpdmat(
5+
κ::Kernel,
6+
X::AbstractMatrix;
7+
obsdim::Int = defaultobs
8+
)
9+
K = kernelmatrix(κ,X,obsdim=obsdim)
10+
α = eps(eltype(K))
11+
while !isposdef(K+αI) && α < 0.01*maximum(K)
12+
α *= 2.0
13+
end
14+
if α >= 0.01*maximum(K)
15+
@error "Adding noise on the diagonal was not sufficient to build a positive-definite matrix:\n - Check that your kernel parameters are not extreme\n - Check that your data is sufficiently sparse\n - Maybe use a different kernel"
16+
end
17+
return PDMat(K+αI)
18+
end

0 commit comments

Comments
 (0)