-
Notifications
You must be signed in to change notification settings - Fork 0
Take 2: Vecchia.jl extension to offload sparsity pattern design. #133
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
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c8c95d7
Take 2: Vecchia.jl extension to offload sparsity pattern design.
cgeoga 7f70158
Big streamlining.
cgeoga 397f136
All tests besides JuMP passing (and GPU, which I can't run).
cgeoga 9158b0b
Test cleanup: a few removed, a few consolidated. Many updated. All
cgeoga 315c1ca
Compat for extensions in testing
cgeoga e9eac94
missing compat for StableRNGs
cgeoga 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| [deps] | ||
| MadNLP = "2621e9c9-9eb4-46b1-8089-e8c72242dfb6" | ||
| StaticArrays = "90137ffa-7385-5640-81b9-e52037218182" | ||
| Vecchia = "8d73829f-f4b0-474a-9580-cecc8e084068" | ||
| VecchiaMLE = "499233a1-c46f-47cc-ab67-5a37788e9870" |
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,43 @@ | ||
|
|
||
| using VecchiaMLE, Vecchia, StaticArrays | ||
|
|
||
| # Generate some fake data with an exponential covariance function. | ||
| # Note that each _column_ of sim is an iid replicate, in keeping with formatting | ||
| # standards of the many R packages for GPs. In this demonstration, n is small | ||
| # and the number of replicates is large to demonstrate asymptotic correctness. | ||
| pts = rand(SVector{2,Float64}, 100) | ||
| z = randn(length(pts), 1000) | ||
| K = Symmetric([exp(-norm(x-y)) for x in pts, y in pts]) | ||
| sim = cholesky(K).L*z | ||
|
|
||
| # Create a VecchiaModel using the options specified by Vecchia.jl, in | ||
| # particular choosing an ordering (in this case RandomOrdering()) and a | ||
| # conditioning set design (in this case KNNConditioning(10)). This also returns | ||
| # the permutation for you to use with subsequent data operations. See the docs | ||
| # and myriad extensions of Vecchia.jl for more information on ordering and | ||
| # conditioning set design options. | ||
| (perm, nlp) = VecchiaModel(pts, sim, RandomOrdering(), KNNConditioning(10); | ||
| lvar_diag=fill(inv(sqrt(1.0)), length(pts)), | ||
| uvar_diag=fill(inv(sqrt(1e-2)), length(pts)), | ||
| lambda=1e-3) | ||
|
|
||
| # Now bring in some optimizers and fit the nonparametric model. This gives a U | ||
| # such that Σ^{-1} ≈ U*U', where Σ is the covariance matrix for each column of sim. | ||
| using MadNLP | ||
| result = madnlp(nlp; tol=1e-10) | ||
| U = UpperTriangular(recover_factor(nlp, result.solution)) | ||
|
|
||
| # KL divergence from the true covariance: | ||
| K_perm = K[perm, perm] | ||
| kl = (tr(U'*K_perm*U) - length(pts)) + (-2*logdet(U) - logdet(K_perm)) | ||
|
|
||
| # compare that with what you get from a parametric Vecchia model using the | ||
| # correct kernel and the same permutation. | ||
| para_vecchia = VecchiaApproximation(pts[perm], (x,y,p)->exp(-norm(x-y)), sim[perm,:]; | ||
| ordering=NoPermutation()) | ||
| para_U = rchol(para_vecchia, Float64[]).U | ||
| para_kl = (tr(para_U'*K_perm*para_U) - length(pts)) + (-2*logdet(para_U) - logdet(K_perm)) | ||
|
|
||
| println("KL divergence with true parametric kernel: ", para_kl) | ||
| println("KL divergence with nonparametric estimator: ", kl) | ||
|
|
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,35 @@ | ||
| module VecchiaMLEVecchiaExt | ||
|
|
||
| using VecchiaMLE, Vecchia, StaticArrays | ||
|
|
||
| function VecchiaMLE.VecchiaModel(pts::Vector{SVector{D,Float64}}, | ||
| data::Matrix{Float64}, | ||
| ordering, conditioning; | ||
| lvar_diag=fill(1e-10, length(pts)), | ||
| uvar_diag=fill(1e10, length(pts)), | ||
| lambda=0.0) where{D} | ||
| va = VecchiaApproximation(pts, nothing, data; ordering=ordering, | ||
| conditioning=conditioning) | ||
| VecchiaModel(va; lvar_diag=lvar_diag, uvar_diag=uvar_diag, lambda=lambda) | ||
| end | ||
|
|
||
| function VecchiaMLE.VecchiaModel(va::VecchiaApproximation; | ||
| lvar_diag=fill(1e-10, length(pts)), | ||
| uvar_diag=fill(1e10, length(pts)), | ||
| lambda=0.0) | ||
| (condsets, perm) = (va.condix, va.perm) | ||
| n = length(condsets) | ||
| nnz = sum(length, condsets) + length(condsets) | ||
| IJ = Vector{Tuple{Int64, Int64}}() | ||
| sizehint!(IJ, nnz) | ||
| for (j, cj) in enumerate(condsets) | ||
| foreach(k->push!(IJ, (k, j)), cj) | ||
| push!(IJ, (j,j)) | ||
| end | ||
| (I, J) = (getindex.(IJ, 1), getindex.(IJ, 2)) | ||
| (perm, VecchiaModel(I, J, permutedims(va.data); | ||
| uplo=:U, lvar_diag=lvar_diag, | ||
| uvar_diag=uvar_diag, lambda=lambda)) | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,33 +1,20 @@ | ||
| module VecchiaMLE | ||
|
|
||
| using BesselK | ||
| using Distances | ||
| using Distributions | ||
| using HNSW | ||
| using KernelAbstractions | ||
| using LinearAlgebra | ||
| using NearestNeighbors | ||
| using NLPModels | ||
| using Random | ||
| using SparseArrays | ||
| using SpecialFunctions | ||
| using Statistics | ||
|
|
||
| const KA = KernelAbstractions | ||
|
|
||
| # Includes | ||
| include("defines.jl") | ||
| include("internals.jl") | ||
| include("utils.jl") | ||
| include("sparsity_pattern.jl") | ||
| include("permutations.jl") | ||
| include("kernels.jl") | ||
| include("models/VecchiaModel.jl") | ||
| include("models/api.jl") | ||
|
|
||
| # Exports | ||
| export VecchiaMLEInput, VecchiaModel | ||
| export sparsity_pattern, recover_factor | ||
| export generate_samples, generate_MatCov, generate_xyGrid, generate_rectGrid | ||
| export VecchiaModel, recover_factor | ||
|
|
||
| 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
Oops, something went wrong.
Oops, something went wrong.
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.