File size: 10,555 Bytes
c711202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"""
Visualize Cholesky factor sparsity patterns for 2D GP-FVM.

Creates a publication-quality figure showing the precision Cholesky factor
for different orderings, demonstrating how entry magnitudes decay and
why sparse approximation is effective.

Usage:
    julia --project=. experiments/sparsity_pattern_2d.jl
    julia --project=. experiments/sparsity_pattern_2d.jl --nxy 12
"""

using FunctionalGPs, GaussianMarkovRandomFields
using LinearAlgebra, SparseArrays
using CairoMakie
using TuePlots
using GPFiniteVolume
using ArgParse

# ------------------------------------------------------------------------------
# CLI
# ------------------------------------------------------------------------------

function parse_commandline()
    s = ArgParseSettings(description = "Visualize 2D Cholesky sparsity patterns")

    @add_arg_table! s begin
        "--nxy"
            help = "Grid size (N × N)"
            arg_type = Int
            default = 10
        "--lengthscale"
            help = "Kernel lengthscale"
            arg_type = Float64
            default = 0.1
        "--output", "-o"
            help = "Output path (default: figures/cholesky_sparsity_2d.pdf)"
            arg_type = String
            default = ""
    end

    return parse_args(s)
end

# ------------------------------------------------------------------------------
# Setup
# ------------------------------------------------------------------------------

"""
    setup_2d_problem(; N_xy, kernel_lengthscale)

Create 2D measurement setup with evaluations, derivatives, and integrals.
Returns covariance matrix K and metadata.
"""
function setup_2d_problem(; N_xy=10, kernel_lengthscale=0.1)
    # Build 2D product kernel
    k_base = HalfIntegerMaternKernel(1, [kernel_lengthscale])  # Matérn-3/2
    k_prod = k_base ⊗ k_base

    # Create 2D grid
    Xs_base = range(0.0, 1.0, length=N_xy)
    Xs = FactorizedGrid(Xs_base, Xs_base)

    # Evaluation functional
    L_eval = EvaluationFunctional(Xs)
    n_eval = prod(output_shape(L_eval))

    # Derivative functional: ∂/∂x + ∂/∂y
    d_sum = PartialDerivative((1, 0)) + PartialDerivative((0, 1))
    L_deriv = L_eval ∘ d_sum
    n_deriv = prod(output_shape(L_deriv))

    # Integral functional over 2D cells
    base_intervals = intervals_from_endpoints(collect(Xs_base))
    domains_2d = base_intervals ⊗ base_intervals
    L_integ = VectorizedLebesgueIntegral(domains_2d)
    n_int = prod(output_shape(L_integ))

    # Stack functionals
    L_stack = StackedLinearFunctional(L_eval, L_deriv, L_integ)

    # State layout
    state_layout = layout((f = n_eval, f_dx = n_deriv, f_int = n_int))

    # Compute full covariance matrix
    K = Matrix(L_stack(L_stack(k_prod)))

    # Build coordinate matrices
    N_total = N_xy^2
    X_eval = zeros(2, N_total)
    idx = 1
    for iy in 1:N_xy, ix in 1:N_xy
        X_eval[1, idx] = Xs_base[ix]
        X_eval[2, idx] = Xs_base[iy]
        idx += 1
    end
    X_deriv = copy(X_eval)

    n_cells_1d = length(base_intervals)
    X_int = zeros(2, n_cells_1d^2)
    idx = 1
    for iy in 1:n_cells_1d, ix in 1:n_cells_1d
        X_int[1, idx] = midpoint(base_intervals[ix])
        X_int[2, idx] = midpoint(base_intervals[iy])
        idx += 1
    end

    return (
        K = K,
        state_layout = state_layout,
        X_eval = X_eval,
        X_deriv = X_deriv,
        X_int = X_int,
        n_eval = n_eval,
        n_deriv = n_deriv,
        n_int = n_int,
    )
end

"""
    create_ordering(setup, order_type::Symbol)

Create global ordering for :eval_first or :int_first.
Returns permutation P and block boundaries.
"""
function create_ordering(setup, order_type::Symbol)
    (; state_layout, X_eval, X_int, n_eval, n_deriv, n_int) = setup

    # Compute maximin orderings
    P_eval, _ = reverse_maximin_ordering(X_eval)
    P_int, _ = reverse_maximin_ordering(X_int)

    # Map to global indices
    P_eval_global = indices(state_layout, :f)[P_eval]
    P_int_global = indices(state_layout, :f_int)[P_int]
    P_deriv_global = indices(state_layout, :f_dx)

    if order_type == :eval_first
        # Order: [derivatives; integrals; evaluations]
        P_global = [P_deriv_global; P_int_global; P_eval_global]
        block_sizes = (n_deriv, n_int, n_eval)
        block_labels = ("Derivatives", "Integrals", "Evaluations")
    elseif order_type == :int_first
        # Order: [derivatives; evaluations; integrals]
        P_global = [P_deriv_global; P_eval_global; P_int_global]
        block_sizes = (n_deriv, n_eval, n_int)
        block_labels = ("Derivatives", "Evaluations", "Integrals")
    else
        error("Unknown order_type: $order_type")
    end

    return P_global, block_sizes, block_labels
end

"""
    compute_precision_cholesky(K, P)

Compute the exact Cholesky factor L of the precision matrix K⁻¹[P,P] = LL'.
"""
function compute_precision_cholesky(K::AbstractMatrix, P::Vector{Int})
    K_perm = K[P, P]
    K_perm_reg = K_perm + 1e-10 * I
    K_inv = inv(Symmetric(K_perm_reg))
    C = cholesky(Symmetric(K_inv))
    return Matrix(C.L)
end

# ------------------------------------------------------------------------------
# Plotting
# ------------------------------------------------------------------------------

"""
    create_sparsity_figure(setup; output_path)

Create publication-quality figure comparing sparsity patterns.
"""
function create_sparsity_figure(setup; output_path="figures/cholesky_sparsity_2d.pdf")
    (; K, n_eval, n_deriv, n_int) = setup
    n_total = n_eval + n_deriv + n_int

    println("Matrix size: $n_total × $n_total")
    println("  Evaluations: $n_eval")
    println("  Derivatives: $n_deriv")
    println("  Integrals: $n_int")

    # Compute Cholesky factors for both orderings
    println("\nComputing Cholesky factors...")

    P_int, blocks_int, labels_int = create_ordering(setup, :int_first)
    L_int = compute_precision_cholesky(K, P_int)
    println("  Integrals-first: done")

    P_eval, blocks_eval, labels_eval = create_ordering(setup, :eval_first)
    L_eval = compute_precision_cholesky(K, P_eval)
    println("  Evaluations-first: done")

    # Set up TuePlots theme (appendix full-width, 2 panels)
    theme = Theme(
        TuePlots.SETTINGS[:ICML];
        font=true, fontsize=true, figsize=true,
        single_column=false,
        nrows=1, ncols=2,
        subplot_height_to_width_ratio=1.0,
    )
    set_theme!(theme)

    fig = Figure()

    # Colorbar range
    crange = (-12, 0)

    # Panel (a): Integrals-first
    ax1 = Axis(fig[1, 1],
        xlabel="Column index",
        ylabel="Row index",
        title="Integrals coarsest",
        yreversed=true,
        aspect=DataAspect(),
    )

    L_log_int = log10.(abs.(L_int) .+ 1e-16)
    hm1 = heatmap!(ax1, 1:n_total, 1:n_total, L_log_int',
        colormap=:viridis, colorrange=crange)

    # Add block boundaries
    add_block_boundaries!(ax1, blocks_int, labels_int, n_total)

    # Panel (b): Evaluations-first
    ax2 = Axis(fig[1, 2],
        xlabel="Column index",
        ylabel="Row index",
        title="Evaluations coarsest",
        yreversed=true,
        aspect=DataAspect(),
    )

    L_log_eval = log10.(abs.(L_eval) .+ 1e-16)
    hm2 = heatmap!(ax2, 1:n_total, 1:n_total, L_log_eval',
        colormap=:viridis, colorrange=crange)

    add_block_boundaries!(ax2, blocks_eval, labels_eval, n_total)

    # Panel labels (top right)
    text!(ax1, 0.98, 0.98, text="(a)", align=(:right, :top),
          space=:relative, fontsize=9, font=:bold, color=:white)
    text!(ax2, 0.98, 0.98, text="(b)", align=(:right, :top),
          space=:relative, fontsize=9, font=:bold, color=:white)

    # Shared colorbar
    Colorbar(fig[1, 3], hm1, label=L"\log_{10} |L_{ij}|", width=10)

    # Adjust layout
    colgap!(fig.layout, 1, 8)
    colgap!(fig.layout, 2, 5)

    # Save
    mkpath(dirname(output_path))
    save(output_path, fig, pt_per_unit=1)
    println("\nSaved: $output_path")

    # Also save PNG for quick viewing
    png_path = replace(output_path, ".pdf" => ".png")
    save(png_path, fig, px_per_unit=3)
    println("Saved: $png_path")

    # Reset theme
    set_theme!()

    # Print sparsity statistics
    println("\nSparsity statistics (entries with |L_ij| > threshold):")
    for (name, L) in [("Integrals-first", L_int), ("Evaluations-first", L_eval)]
        max_nnz = n_total * (n_total + 1) ÷ 2
        nnz_1e3 = count(x -> abs(x) > 1e-3, L)
        nnz_1e6 = count(x -> abs(x) > 1e-6, L)
        nnz_1e9 = count(x -> abs(x) > 1e-9, L)
        println("  $name:")
        println("    |L| > 1e-3: $nnz_1e3 ($(round(100*nnz_1e3/max_nnz, digits=1))%)")
        println("    |L| > 1e-6: $nnz_1e6 ($(round(100*nnz_1e6/max_nnz, digits=1))%)")
        println("    |L| > 1e-9: $nnz_1e9 ($(round(100*nnz_1e9/max_nnz, digits=1))%)")
    end

    return fig
end

"""
    add_block_boundaries!(ax, block_sizes, block_labels, n_total)

Add dashed lines and labels for block boundaries.
"""
function add_block_boundaries!(ax, block_sizes, block_labels, n_total)
    cumsum_blocks = cumsum(collect(block_sizes))

    for (i, boundary) in enumerate(cumsum_blocks[1:end-1])
        b = boundary + 0.5
        # Horizontal and vertical lines
        hlines!(ax, [b], color=:white, linewidth=0.8, linestyle=:dash)
        vlines!(ax, [b], color=:white, linewidth=0.8, linestyle=:dash)
    end

    # Add block labels in top-right corner of each block
    block_starts = [0; cumsum_blocks[1:end-1]]
    block_ends = cumsum_blocks
    for (i, (start, finish, label)) in enumerate(zip(block_starts, block_ends, block_labels))
        # Top-right corner: x near end, y near start (since y is reversed)
        x_pos = finish - 2
        y_pos = start + 3
        text!(ax, x_pos, y_pos, text=label, align=(:right, :top),
              fontsize=6, color=:white)
    end
end

# ------------------------------------------------------------------------------
# Main
# ------------------------------------------------------------------------------

function main()
    args = parse_commandline()

    N_xy = args["nxy"]
    lengthscale = args["lengthscale"]

    if isempty(args["output"])
        output_path = joinpath(@__DIR__, "..", "figures", "cholesky_sparsity_2d.pdf")
    else
        output_path = args["output"]
    end

    println("Setting up 2D problem ($(N_xy) × $(N_xy) grid)...")
    setup = setup_2d_problem(; N_xy=N_xy, kernel_lengthscale=lengthscale)

    create_sparsity_figure(setup; output_path=output_path)
end

if abspath(PROGRAM_FILE) == @__FILE__
    main()
end