@@ -6,11 +6,7 @@ using NLPModelsIpopt
66
77n = 400
88number_of_samples = 100
9-
10- params = [5.0, 0.2, 2.25, 0.25]
11- ptset = VecchiaMLE.generate_safe_xyGrid(n)
12- MatCov = generate_MatCov(params, ptset)
13- samples = generate_samples(MatCov, number_of_samples)
9+ samples = ...
1410
1511P = ones(n, n)
1612P = tril(P)
@@ -29,11 +25,7 @@ using NLPModelsIpopt
2925
3026n = 400
3127number_of_samples = 100
32-
33- params = [5.0, 0.2, 2.25, 0.25]
34- ptset = VecchiaMLE.generate_safe_xyGrid(n)
35- MatCov = generate_MatCov(params, ptset)
36- samples = generate_samples(MatCov, number_of_samples)
28+ samples = ...
3729
3830P = ones(n, n)
3931P = triu(P)
@@ -43,3 +35,50 @@ nlp_U = VecchiaModel(I, J, samples; format=:coo, uplo=:U)
4335output = ipopt(nlp_U)
4436U = recover_factor(nlp_U, output.solution)
4537```
38+
39+ ``` @example Vecchia
40+ using VecchiaMLE
41+ using Vecchia
42+ using StaticArrays
43+ using LinearAlgebra
44+
45+ # Generate some fake data with an exponential covariance function.
46+ # Note that each _column_ of sim is an iid replicate, in keeping with formatting
47+ # standards of the many R packages for GPs. In this demonstration, n is small
48+ # and the number of replicates is large to demonstrate asymptotic correctness.
49+ pts = rand(SVector{2,Float64}, 100)
50+ z = randn(length(pts), 1000)
51+ K = Symmetric([exp(-norm(x-y)) for x in pts, y in pts])
52+ sim = cholesky(K).L * z
53+
54+ # Create a VecchiaModel using the options specified by Vecchia.jl, in
55+ # particular choosing an ordering (in this case RandomOrdering()) and a
56+ # conditioning set design (in this case KNNConditioning(10)). This also returns
57+ # the permutation for you to use with subsequent data operations. See the docs
58+ # and myriad extensions of Vecchia.jl for more information on ordering and
59+ # conditioning set design options.
60+ (perm, nlp) = VecchiaModel(pts, sim, RandomOrdering(), KNNConditioning(10);
61+ lvar_diag=fill(inv(sqrt(1.0)), length(pts)),
62+ uvar_diag=fill(inv(sqrt(1e-2)), length(pts)),
63+ lambda=1e-3)
64+
65+ # Now bring in some optimizers and fit the nonparametric model. This gives a U
66+ # such that Σ^{-1} ≈ U*U', where Σ is the covariance matrix for each column of sim.
67+ using MadNLP
68+ result = madnlp(nlp; tol=1e-10)
69+ U = UpperTriangular(recover_factor(nlp, result.solution))
70+
71+ # KL divergence from the true covariance:
72+ K_perm = K[perm, perm]
73+ kl = (tr(U'*K_perm*U) - length(pts)) + (-2*logdet(U) - logdet(K_perm))
74+
75+ # compare that with what you get from a parametric Vecchia model using the
76+ # correct kernel and the same permutation.
77+ para_vecchia = VecchiaApproximation(pts[perm], (x,y,p)->exp(-norm(x-y)), sim[perm,:];
78+ ordering=NoPermutation())
79+ para_U = rchol(para_vecchia, Float64[]).U
80+ para_kl = (tr(para_U'*K_perm*para_U) - length(pts)) + (-2*logdet(para_U) - logdet(K_perm))
81+
82+ println("KL divergence with true parametric kernel: ", para_kl)
83+ println("KL divergence with nonparametric estimator: ", kl)
84+ ```
0 commit comments