Skip to content

Update kernelpdmat to accept vector of vectors #252

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

Merged
merged 8 commits into from
Feb 15, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
21 changes: 12 additions & 9 deletions src/matrix/kernelpdmat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@ using .PDMats: PDMat
export kernelpdmat

"""
Compute a positive-definite matrix in the form of a `PDMat` matrix see [PDMats.jl](https://github.com/JuliaStats/PDMats.jl)
with the cholesky decomposition precomputed.
The algorithm recursively tries to add recursively a diagonal nugget until positive
definiteness is achieved or that the noise is too big.
kernelpdmat(k::Kernel, X::AbstractMatrix; obsdim::Int=2)
kernelpdmat(k::Kernel, X::AbstractVector)

Compute a positive-definite matrix in the form of a `PDMat` matrix see [PDMats.jl](https://github.com/JuliaStats/PDMats.jl)
with the cholesky decomposition precomputed.
The algorithm recursively tries to add recursively a diagonal nugget until positive
definiteness is achieved or until the noise is too big.
"""
function kernelpdmat(κ::Kernel, X::AbstractMatrix; obsdim::Int=defaultobs)
K = kernelmatrix(κ, X; obsdim=obsdim)
kernelpdmat(κ, vec_of_vecs(X; obsdim=obsim))
end

function kernelpdmat(k::Kernel, X::AbstractVector)
K = kernelmatrix(κ, X)
Kmax = maximum(K)
α = eps(eltype(K))
while !isposdef(K + α * I) && α < 0.01 * Kmax
Expand All @@ -24,7 +31,3 @@ function kernelpdmat(κ::Kernel, X::AbstractMatrix; obsdim::Int=defaultobs)
end
return PDMat(K + α * I)
end

function kernelpdmat(κ::Kernel, X::AbstractVector{<:Real}; obsdim=defaultobs)
return kernelpdmat(κ, reshape(X, 1, :); obsdim=2)
end
3 changes: 3 additions & 0 deletions test/matrix/kernelpdmat.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
@testset "kernelpdmat" begin
rng = MersenneTwister(123456)
A = rand(rng, 10, 5)
vecA = (ColVecs(A), RowVecs(A))
a = rand(rng, 10)
k = SqExponentialKernel()
for obsdim in [1, 2]
@test all(
Matrix(kernelpdmat(k, A; obsdim=obsdim)) .≈
Matrix(PDMat(kernelmatrix(k, A; obsdim=obsdim))),
)
@test kernelpdmat(k, vecA[obsdim]) == kernelpdmat(k, A; obsdim=obsdim)
# @test_throws ErrorException kernelpdmat(k,ones(100,100),obsdim=obsdim)
end
end