gprx 0.2.0

Gaussian process regression in Rust: exact GPR, sparse GPR (VFE), and SVGP.
Documentation
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
//! Analytic gradient and Hessian of the negative VFE bound.

use super::lit;
use crate::data::pack_points;
use crate::error::GprError;
use crate::kernel::ScalarOps;
use crate::kernel::{CompiledKernel, KernelScalar, ModelKernel, Supply, Triangle};
use crate::linalg::{
    copy_mat, dot, dot_ay, frobenius_dot, gemm, mat_add_mul, mat_sub_mul, mat_vec, quad_form,
    solve_llt, solve_lower, solve_lower_transpose,
};
use crate::precision::ModelPrecision;
use crate::sgpr::{FittedSgpr, InducingLayout};
use crate::sparse::{KernelScratch, SparseSets};
use faer::{Accum, Mat, MatRef};

pub(crate) struct KernelVar<T: KernelScalar> {
    pub(crate) d_kmm: Mat<T>,
    pub(crate) d_kmn: Mat<T>,
    pub(crate) d_kdiag: T,
    pub(crate) d_noise: T,
}

/// The bound's first derivative as weights on the kernel matrices: for any
/// direction, `dF = ⟨w_mm, ∂K_mm⟩ + ⟨w_mn, ∂K_mn⟩ + w_diag · Σᵢ ∂k(xᵢ, xᵢ)`
/// (the noise apart). Formed once in `O(m² n)`, so each kernel parameter or
/// inducing coordinate then costs only its kernel derivatives and an
/// `O(m n)` contraction.
pub(crate) struct VfeAdjoint<T: KernelScalar> {
    /// Symmetric `m × m`.
    pub(crate) w_mm: Mat<T>,
    pub(crate) w_mn: Mat<T>,
    pub(crate) w_diag: T,
}

impl<T: KernelScalar> VfeAdjoint<T> {
    /// `dF` for one direction of the kernel matrices.
    ///
    /// The first derivative contracts in one walk instead of calling this.
    /// The adjoint test still checks one direction against the tangent.
    #[cfg(test)]
    pub(crate) fn contract(&self, d_kmm: MatRef<'_, T>, d_kmn: MatRef<'_, T>, d_kdiag: T) -> T {
        frobenius_dot(self.w_mm.as_ref(), d_kmm)
            + frobenius_dot(self.w_mn.as_ref(), d_kmn)
            + self.w_diag * d_kdiag
    }
}

pub(crate) struct VfeEngine<'a, T: KernelScalar> {
    pub(crate) l: MatRef<'a, T>,
    pub(crate) a: MatRef<'a, T>,
    pub(crate) b_l: MatRef<'a, T>,
    pub(crate) w: &'a [T],
    pub(crate) noise: T,
    pub(crate) y: &'a [T],
    pub(crate) n: usize,
    pub(crate) m: usize,
    quad: T,
    trace: T,
}

impl<'a, T: KernelScalar> VfeEngine<'a, T> {
    fn from_model<O, I: InducingLayout<K::Supply>, P, K: ModelKernel>(
        model: &'a FittedSgpr<O, I, P, K>,
        y: &'a [T],
    ) -> Self
    where
        P: ModelPrecision<Storage = T>,
    {
        let m = model.core.m;
        let n = model.core.n;
        let noise = lit::<T>(model.core.likelihood.noise_variance());
        let a = model.a.as_ref();
        let w = model.w.as_slice();
        let mut y_norm2 = lit::<T>(0.0);
        for v in y {
            y_norm2 += v * v;
        }
        let ay_dot_w = dot_ay(a, y, w);
        Self {
            l: model.k_mm_l.as_ref(),
            a,
            b_l: model.b_l.as_ref(),
            w,
            noise,
            y,
            n,
            m,
            quad: (y_norm2 - ay_dot_w) / noise,
            trace: (model.k_diag_sum - model.a_frobenius2) / (lit::<T>(2.0) * noise),
        }
    }

    /// [`VfeAdjoint`] of the bound.
    ///
    /// With `B = σ² I + A Aᵀ`, `w = B⁻¹ A y`, and `r = y − Aᵀ w`, the bound
    /// moves with `A` as `⟨G, dA⟩` for `G = B⁻¹ A − (w rᵀ + A) / σ²`, and with
    /// `Σ diag K` as `1 / (2σ²)`. `dA = L⁻¹ ∂K_mn − Φ_L A`, where
    /// `Φ = L⁻¹ ∂K_mm L⁻ᵀ` and `Φ_L` is its lower triangle with the diagonal
    /// halved (`dL = L Φ_L`). So `w_mn = L⁻ᵀ G` and
    /// `w_mm = −sym(L⁻ᵀ tril½(G Aᵀ) L⁻¹)`.
    fn adjoint(&self) -> VfeAdjoint<T> {
        let (m, n) = (self.m, self.n);
        let mut g = Mat::zeros(m, n);
        copy_mat(self.a, g.as_mut());
        solve_llt(self.b_l, g.as_mut());
        let mut resid = self.y.to_vec();
        for (j, r) in resid.iter_mut().enumerate() {
            for i in 0..m {
                *r -= self.a[(i, j)] * self.w[i];
            }
        }
        let inv_noise = lit::<T>(1.0) / self.noise;
        for j in 0..n {
            for i in 0..m {
                g[(i, j)] -= (self.w[i] * resid[j] + self.a[(i, j)]) * inv_noise;
            }
        }
        let mut h = Mat::zeros(m, m);
        gemm(
            h.as_mut(),
            Accum::Replace,
            g.as_ref(),
            self.a.transpose(),
            lit::<T>(1.0),
        );
        solve_lower_transpose(self.l, g.as_mut());
        let mut x = tril_half(h.as_ref());
        solve_lower_transpose(self.l, x.as_mut());
        let mut xt = x.transpose().to_owned();
        solve_lower_transpose(self.l, xt.as_mut());
        let mut w_mm = Mat::zeros(m, m);
        let half = lit::<T>(0.5);
        for j in 0..m {
            for i in 0..m {
                w_mm[(i, j)] = -half * (xt[(j, i)] + xt[(i, j)]);
            }
        }
        VfeAdjoint {
            w_mm,
            w_mn: g,
            w_diag: half * inv_noise,
        }
    }

    fn tangent_from(
        &self,
        d_kmm: MatRef<'_, T>,
        mut da: Mat<T>,
        d_kdiag: T,
        d_noise: T,
    ) -> VfeTangent<T> {
        let phi = chol_phi(self.l, d_kmm);
        let phi_l = tril_half(phi.as_ref());
        solve_lower(self.l, da.as_mut());
        mat_sub_mul(&mut da, phi_l.as_ref(), self.a);
        let db = noise_plus_sym_prod(da.as_ref(), self.a, d_noise);
        let u = mat_vec(da.as_ref(), self.y);
        VfeTangent::<T> {
            phi,
            phi_l,
            da,
            db,
            u,
            d_kdiag,
            d_noise,
        }
    }

    fn first_tangent(&self, var: &KernelVar<T>) -> VfeTangent<T> {
        self.tangent_from(
            var.d_kmm.as_ref(),
            var.d_kmn.clone(),
            var.d_kdiag,
            var.d_noise,
        )
    }

    #[cfg(test)]
    fn directional_owned(&self, var: KernelVar<T>) -> T {
        let t = self.tangent_from(var.d_kmm.as_ref(), var.d_kmn, var.d_kdiag, var.d_noise);
        self.directional_from_tangent(&t)
    }

    /// Likelihood `θ` has `∂K = 0` and `∂σn² = σn²`, so the `m×n` products are zero.
    fn directional_noise(&self, d_noise: f64) -> T {
        let d_noise = lit::<T>(d_noise);
        let m = self.m;
        let mut db = Mat::zeros(m, m);
        for i in 0..m {
            db[(i, i)] = d_noise;
        }
        let quad = self.quad;
        let trace = self.trace;
        let d_logdet_b = trace_solve(self.b_l, db.as_ref());
        let d_q = -quad_form(self.w, db.as_ref());
        let d_quad = -d_q / self.noise - quad * d_noise / self.noise;
        let d_trace = -trace * d_noise / self.noise;
        let n_minus_m = lit::<T>(self.n as f64) - lit::<T>(self.m as f64);
        lit::<T>(0.5) * (n_minus_m * d_noise / self.noise + d_logdet_b + d_quad) + d_trace
    }

    #[cfg(test)]
    fn directional_from_tangent(&self, t: &VfeTangent<T>) -> T {
        let quad = self.quad;
        let trace = self.trace;
        let d_logdet_b = trace_solve(self.b_l, t.db.as_ref());
        let d_q = lit::<T>(2.0) * dot(self.w, &t.u) - quad_form(self.w, t.db.as_ref());
        let d_af = lit::<T>(2.0) * frobenius_dot(self.a, t.da.as_ref());
        let d_quad = -d_q / self.noise - quad * t.d_noise / self.noise;
        let d_trace =
            (t.d_kdiag - d_af) / (lit::<T>(2.0) * self.noise) - trace * t.d_noise / self.noise;
        let n_minus_m = lit::<T>(self.n as f64) - lit::<T>(self.m as f64);
        lit::<T>(0.5) * (n_minus_m * t.d_noise / self.noise + d_logdet_b + d_quad) + d_trace
    }

    fn second_directional(&self, ti: &VfeTangent<T>, tj: &VfeTangent<T>, dd: &KernelVar<T>) -> T {
        let quad = self.quad;
        let trace = self.trace;
        let phi_dd = chol_phi(self.l, dd.d_kmm.as_ref());
        let dphi_j_on_i = dphi_from(ti.phi.as_ref(), tj.phi_l.as_ref(), phi_dd.as_ref());
        let dphi_l = tril_half(dphi_j_on_i.as_ref());
        let mut linv_di_kmn = ti.da.clone();
        mat_add_mul(&mut linv_di_kmn, ti.phi_l.as_ref(), self.a);
        let mut dda = dd.d_kmn.clone();
        solve_lower(self.l, dda.as_mut());
        mat_sub_mul(&mut dda, tj.phi_l.as_ref(), linv_di_kmn.as_ref());
        mat_sub_mul(&mut dda, dphi_l.as_ref(), self.a);
        mat_sub_mul(&mut dda, ti.phi_l.as_ref(), tj.da.as_ref());
        let ddb = second_db(
            ti.da.as_ref(),
            tj.da.as_ref(),
            dda.as_ref(),
            self.a,
            dd.d_noise,
        );
        let ddu = mat_vec(dda.as_ref(), self.y);
        let d_logdet_b_i = trace_solve(self.b_l, ti.db.as_ref());
        let _ = d_logdet_b_i;
        let d2_logdet_b = second_logdet_b(self.b_l, ti.db.as_ref(), tj.db.as_ref(), ddb.as_ref());
        let dwi = dw_from(self.b_l, self.w, ti.db.as_ref(), &ti.u);
        let dwj = dw_from(self.b_l, self.w, tj.db.as_ref(), &tj.u);
        let d_q_i = lit::<T>(2.0) * dot(self.w, &ti.u) - quad_form(self.w, ti.db.as_ref());
        let d_q_j = lit::<T>(2.0) * dot(self.w, &tj.u) - quad_form(self.w, tj.db.as_ref());
        let d2_q = lit::<T>(2.0) * dot(&dwj, &ti.u) + lit::<T>(2.0) * dot(self.w, &ddu)
            - dot(&dwj, &mat_vec(ti.db.as_ref(), self.w))
            - quad_form(self.w, ddb.as_ref())
            - dot(self.w, &mat_vec(ti.db.as_ref(), &dwj));
        let d_af_i = lit::<T>(2.0) * frobenius_dot(self.a, ti.da.as_ref());
        let d_af_j = lit::<T>(2.0) * frobenius_dot(self.a, tj.da.as_ref());
        let d2_af = lit::<T>(2.0) * frobenius_dot(tj.da.as_ref(), ti.da.as_ref())
            + lit::<T>(2.0) * frobenius_dot(self.a, dda.as_ref());
        let d_quad_j = -d_q_j / self.noise - quad * tj.d_noise / self.noise;
        let d2_quad = -d2_q / self.noise + d_q_i * tj.d_noise / (self.noise * self.noise)
            - d_quad_j * ti.d_noise / self.noise
            - quad * dd.d_noise / self.noise
            + quad * ti.d_noise * tj.d_noise / (self.noise * self.noise);
        let d_trace_j =
            (tj.d_kdiag - d_af_j) / (lit::<T>(2.0) * self.noise) - trace * tj.d_noise / self.noise;
        let d2_trace = (dd.d_kdiag - d2_af) / (lit::<T>(2.0) * self.noise)
            - (ti.d_kdiag - d_af_i) * tj.d_noise / (lit::<T>(2.0) * self.noise * self.noise)
            - d_trace_j * ti.d_noise / self.noise
            - trace * dd.d_noise / self.noise
            + trace * ti.d_noise * tj.d_noise / (self.noise * self.noise);
        let n_minus_m = lit::<T>(self.n as f64) - lit::<T>(self.m as f64);
        let d2_log_noise =
            dd.d_noise / self.noise - ti.d_noise * tj.d_noise / (self.noise * self.noise);
        let _ = dwi;
        lit::<T>(0.5) * (n_minus_m * d2_log_noise + d2_logdet_b + d2_quad) + d2_trace
    }
}

pub(crate) struct VfeTangent<T: KernelScalar> {
    pub(crate) phi: Mat<T>,
    pub(crate) phi_l: Mat<T>,
    pub(crate) da: Mat<T>,
    pub(crate) db: Mat<T>,
    pub(crate) u: Vec<T>,
    pub(crate) d_kdiag: T,
    pub(crate) d_noise: T,
}

pub(crate) fn analytic_gradient<
    M: crate::math::KernelMath,
    O,
    I: InducingLayout<K::Supply>,
    P,
    K: ModelKernel,
>(
    model: &FittedSgpr<O, I, P, K>,
    out: &mut [f64],
    ks: &mut KernelScratch<P::Storage>,
) -> Result<(), GprError>
where
    P: ModelPrecision,
{
    let mut y_cast = P::Storage::empty_rows();
    let y_s = P::Storage::storage_rows(&model.core.y_train, &mut y_cast);
    let engine = VfeEngine::<P::Storage>::from_model(model, y_s);
    let compiled = model.core.kernel.compile_as::<P::Storage>();
    let x64 = pack_points(&model.core.x_train, model.core.n, model.core.d);
    let z64 = pack_points(&model.core.z_train, model.core.m, model.core.d);
    let mut x_cast = P::Storage::empty_cols();
    let mut z_cast = P::Storage::empty_cols();
    let x = P::Storage::storage_cols(x64.as_ref(), &mut x_cast);
    let z = P::Storage::storage_cols(z64.as_ref(), &mut z_cast);
    let sets = SparseSets::<P::Storage, K::Supply>::new(
        x,
        z,
        <K::Supply as crate::kernel::SupplyViews>::try_map_held(
            model.core.supply_held(),
            crate::sparse::SparseSupply::at,
        )?,
    );
    let adjoint = engine.adjoint();
    let n_kernel = model.core.kernel.num_params();
    // One walk per matrix. The Hessian still forms each ∂K in `kernel_theta_var`.
    ks.write_square_contraction::<M, K::Supply>(
        &compiled,
        sets.k_mm(),
        adjoint.w_mm.as_ref(),
        &mut out[..n_kernel],
    )?;
    ks.add_cross_contraction_mn::<M, K::Supply>(
        &compiled,
        sets,
        adjoint.w_mn.as_ref(),
        1.0,
        &mut out[..n_kernel],
    )?;
    ks.add_diag_contraction::<M, K::Supply>(
        &compiled,
        x,
        adjoint.w_diag.to_f64(),
        &mut out[..n_kernel],
    )?;
    out[n_kernel] = engine
        .directional_noise(model.core.likelihood.noise_variance())
        .to_f64();
    if let Some(coords) = I::free_z(&compiled) {
        let (m, n) = (model.core.m, model.core.n);
        let mut g_zz = Mat::zeros(m, m);
        let mut g_xz = Mat::zeros(n, m);
        let two = lit::<P::Storage>(2.0);
        let mut idx = n_kernel + 1;
        for dim in 0..model.core.d {
            // `∂k(zᵢ, z_p)/∂z_p[dim]` for every pair, once per dimension; the
            // coordinate `p` moves row and column `p` of `K_mm` and row `p`
            // of `K_mn`.
            coords.grad_wrt_coord_dim_with::<M>(z, z, g_zz.as_mut(), dim, ks.scratch(m, m))?;
            coords.grad_wrt_coord_dim_with::<M>(x, z, g_xz.as_mut(), dim, ks.scratch(n, m))?;
            for p in 0..m {
                let mut sum = lit::<P::Storage>(0.0);
                for i in 0..m {
                    sum += two * adjoint.w_mm[(i, p)] * g_zz[(i, p)];
                }
                for col in 0..n {
                    sum += adjoint.w_mn[(p, col)] * g_xz[(col, p)];
                }
                out[idx] = sum.to_f64();
                idx += 1;
            }
        }
    }
    Ok(())
}

pub(crate) fn analytic_hessian<
    M: crate::math::KernelMath,
    O,
    I: InducingLayout<K::Supply>,
    P,
    K: ModelKernel,
>(
    model: &FittedSgpr<O, I, P, K>,
    out: &mut [f64],
    ks: &mut KernelScratch<P::Storage>,
) -> Result<(), GprError>
where
    P: ModelPrecision,
{
    let mut y_cast = P::Storage::empty_rows();
    let y_s = P::Storage::storage_rows(&model.core.y_train, &mut y_cast);
    let engine = VfeEngine::<P::Storage>::from_model(model, y_s);
    let vars = collect_first_vars::<M, _, _, _, _>(model, ks)?;
    let tangents: Vec<VfeTangent<P::Storage>> =
        vars.iter().map(|v| engine.first_tangent(v)).collect();
    let p = vars.len();
    for j in 0..p {
        for i in j..p {
            let dd = second_var::<M, _, _, _, _>(model, i, j, ks)?;
            let hij = engine
                .second_directional(&tangents[i], &tangents[j], &dd)
                .to_f64();
            out[i * p + j] = hij;
            out[j * p + i] = hij;
        }
    }
    Ok(())
}

pub(crate) fn collect_first_vars<
    M: crate::math::KernelMath,
    O,
    I: InducingLayout<K::Supply>,
    P,
    K: ModelKernel,
>(
    model: &FittedSgpr<O, I, P, K>,
    ks: &mut KernelScratch<P::Storage>,
) -> Result<Vec<KernelVar<P::Storage>>, GprError>
where
    P: ModelPrecision,
{
    let compiled = model.core.kernel.compile_as::<P::Storage>();
    let x64 = pack_points(&model.core.x_train, model.core.n, model.core.d);
    let z64 = pack_points(&model.core.z_train, model.core.m, model.core.d);
    let mut x_cast = P::Storage::empty_cols();
    let mut z_cast = P::Storage::empty_cols();
    let x = P::Storage::storage_cols(x64.as_ref(), &mut x_cast);
    let z = P::Storage::storage_cols(z64.as_ref(), &mut z_cast);
    let sets = SparseSets::<P::Storage, K::Supply>::new(
        x,
        z,
        <K::Supply as crate::kernel::SupplyViews>::try_map_held(
            model.core.supply_held(),
            crate::sparse::SparseSupply::at,
        )?,
    );
    let n_kernel = model.core.kernel.num_params();
    let n_theta = n_kernel + model.core.likelihood.num_params();
    let mut vars = Vec::with_capacity(n_theta + I::z_params(model.core.m, model.core.d));
    for i in 0..n_kernel {
        vars.push(kernel_theta_var::<M, _, _>(
            &compiled,
            ks,
            sets,
            model.core.n,
            i,
        )?);
    }
    vars.push(likelihood_var(
        model.core.m,
        model.core.n,
        model.core.likelihood.noise_variance(),
    ));
    if let Some(coords) = I::free_z(&compiled) {
        for dim in 0..model.core.d {
            for p in 0..model.core.m {
                vars.push(z_coord_var::<M, _>(coords, ks, x, z, p, dim)?);
            }
        }
    }
    Ok(vars)
}

/// `∂K_mm`, `∂K(Z, X)`, and `Σ ∂k_ii` of one kernel parameter.
///
/// The VFE Hessian forms these matrices because the factor tangent needs
/// `∂K` itself. The first derivative does not: [`analytic_gradient`]
/// contracts the adjoint in one walk per matrix.
pub(crate) fn kernel_theta_var<M: crate::math::KernelMath, T, U: Supply>(
    compiled: &CompiledKernel<T, U>,
    ks: &mut KernelScratch<T>,
    sets: SparseSets<'_, T, U>,
    n: usize,
    param_idx: usize,
) -> Result<KernelVar<T>, GprError>
where
    T: KernelScalar,
{
    let m = sets.z.nrows();
    let mut d_kmm = Mat::zeros(m, m);
    ks.grad::<M, U>(
        compiled,
        sets.k_mm(),
        d_kmm.as_mut(),
        param_idx,
        Triangle::Full,
    )?;
    let mut d_kmn = Mat::zeros(m, n);
    ks.grad_cross_mn_into::<M, U>(compiled, sets, d_kmn.as_mut(), param_idx)?;
    let mut diag = vec![lit::<T>(0.0); n];
    compiled.grad_diag_points::<M>(sets.x, &mut diag, param_idx)?;
    let d_kdiag = diag.iter().fold(lit::<T>(0.0), |acc, v| acc + *v);
    Ok(KernelVar::<T> {
        d_kmm,
        d_kmn,
        d_kdiag,
        d_noise: lit::<T>(0.0),
    })
}

pub(crate) fn likelihood_var<T: KernelScalar>(m: usize, n: usize, noise: f64) -> KernelVar<T> {
    let _ = n;
    KernelVar::<T> {
        d_kmm: Mat::zeros(m, m),
        d_kmn: Mat::zeros(m, n),
        d_kdiag: lit::<T>(0.0),
        d_noise: lit::<T>(noise),
    }
}

pub(crate) fn z_coord_var<M: crate::math::KernelMath, T>(
    compiled: &CompiledKernel<T>,
    ks: &mut KernelScratch<T>,
    x: MatRef<'_, T>,
    z: MatRef<'_, T>,
    point: usize,
    dim: usize,
) -> Result<KernelVar<T>, GprError>
where
    T: KernelScalar,
{
    let m = z.nrows();
    let n = x.nrows();
    let mut g2 = Mat::zeros(m, m);
    compiled.grad_wrt_coord_dim_with::<M>(z, z, g2.as_mut(), dim, ks.scratch(m, m))?;
    let mut g_xz = Mat::zeros(n, m);
    compiled.grad_wrt_coord_dim_with::<M>(x, z, g_xz.as_mut(), dim, ks.scratch(n, m))?;
    let mut d_kmm = Mat::zeros(m, m);
    for i in 0..m {
        d_kmm[(i, point)] += g2[(i, point)];
        d_kmm[(point, i)] += g2[(i, point)];
    }
    let mut d_kmn = Mat::zeros(m, n);
    for col in 0..n {
        d_kmn[(point, col)] = g_xz[(col, point)];
    }
    Ok(KernelVar::<T> {
        d_kmm,
        d_kmn,
        d_kdiag: lit::<T>(0.0),
        d_noise: lit::<T>(0.0),
    })
}

pub(crate) fn second_var<
    M: crate::math::KernelMath,
    O,
    I: InducingLayout<K::Supply>,
    P,
    K: ModelKernel,
>(
    model: &FittedSgpr<O, I, P, K>,
    i: usize,
    j: usize,
    ks: &mut KernelScratch<P::Storage>,
) -> Result<KernelVar<P::Storage>, GprError>
where
    P: ModelPrecision,
{
    let n_kernel = model.core.kernel.num_params();
    let n_theta = n_kernel + model.core.likelihood.num_params();
    let compiled = model.core.kernel.compile_as::<P::Storage>();
    let x64 = pack_points(&model.core.x_train, model.core.n, model.core.d);
    let z64 = pack_points(&model.core.z_train, model.core.m, model.core.d);
    let mut x_cast = P::Storage::empty_cols();
    let mut z_cast = P::Storage::empty_cols();
    let x = P::Storage::storage_cols(x64.as_ref(), &mut x_cast);
    let z = P::Storage::storage_cols(z64.as_ref(), &mut z_cast);
    let sets = SparseSets::<P::Storage, K::Supply>::new(
        x,
        z,
        <K::Supply as crate::kernel::SupplyViews>::try_map_held(
            model.core.supply_held(),
            crate::sparse::SparseSupply::at,
        )?,
    );
    let m = model.core.m;
    let n = model.core.n;
    let z_index = |idx: usize| -> Option<(usize, usize)> {
        if idx < n_theta {
            None
        } else {
            let local = idx - n_theta;
            Some((local % m, local / m))
        }
    };
    if i < n_kernel && j < n_kernel {
        return kernel_theta_second::<M, _, _>(&compiled, ks, sets, n, i, j);
    }
    if i == n_kernel && j == n_kernel {
        return Ok(likelihood_var(m, n, model.core.likelihood.noise_variance()));
    }
    if i < n_theta && j < n_theta {
        return Ok(KernelVar::<P::Storage> {
            d_kmm: Mat::zeros(m, m),
            d_kmn: Mat::zeros(m, n),
            d_kdiag: lit::<P::Storage>(0.0),
            d_noise: lit::<P::Storage>(0.0),
        });
    }
    let Some(coords) = I::free_z(&compiled) else {
        return Err(GprError::IndexOutOfRange {
            reason: "expected a free inducing coordinate".to_owned(),
        });
    };
    if let (Some((pi, ei)), Some((pj, ej))) = (z_index(i), z_index(j)) {
        return z_z_second::<M, _>(coords, ks, x, z, pi, ei, pj, ej);
    }
    let (theta, (p, e)) = if i < n_theta {
        (
            i,
            z_index(j).ok_or_else(|| GprError::IndexOutOfRange {
                reason: "expected a free inducing coordinate".to_owned(),
            })?,
        )
    } else {
        (
            j,
            z_index(i).ok_or_else(|| GprError::IndexOutOfRange {
                reason: "expected a free inducing coordinate".to_owned(),
            })?,
        )
    };
    if theta == n_kernel {
        return Ok(KernelVar::<P::Storage> {
            d_kmm: Mat::zeros(m, m),
            d_kmn: Mat::zeros(m, n),
            d_kdiag: lit::<P::Storage>(0.0),
            d_noise: lit::<P::Storage>(0.0),
        });
    }
    theta_z_second::<M, _>(coords, x, z, theta, p, e)
}

pub(crate) fn kernel_theta_second<M: crate::math::KernelMath, T, U: Supply>(
    compiled: &CompiledKernel<T, U>,
    ks: &mut KernelScratch<T>,
    sets: SparseSets<'_, T, U>,
    n: usize,
    i: usize,
    j: usize,
) -> Result<KernelVar<T>, GprError>
where
    T: KernelScalar,
{
    let m = sets.z.nrows();
    let mut d_kmm = Mat::zeros(m, m);
    ks.hess::<M, U>(
        compiled,
        sets.k_mm(),
        d_kmm.as_mut(),
        (i, j),
        Triangle::Full,
    )?;
    let mut d_kmn = Mat::zeros(m, n);
    ks.hess_cross_mn_into::<M, U>(compiled, sets, d_kmn.as_mut(), (i, j))?;
    let mut diag = vec![lit::<T>(0.0); n];
    compiled.hess_diag_points::<M>(sets.x, &mut diag, i, j)?;
    let d_kdiag = diag.iter().fold(lit::<T>(0.0), |acc, v| acc + *v);
    Ok(KernelVar::<T> {
        d_kmm,
        d_kmn,
        d_kdiag,
        d_noise: lit::<T>(0.0),
    })
}

// The kernel, both views, its scratch, and the two (point, dimension) pairs.
#[allow(clippy::too_many_arguments)]
pub(crate) fn z_z_second<M: crate::math::KernelMath, T>(
    compiled: &CompiledKernel<T>,
    ks: &mut KernelScratch<T>,
    x: MatRef<'_, T>,
    z: MatRef<'_, T>,
    p: usize,
    e: usize,
    q: usize,
    f: usize,
) -> Result<KernelVar<T>, GprError>
where
    T: KernelScalar,
{
    let m = z.nrows();
    let n = x.nrows();
    let mut h22 = Mat::zeros(m, m);
    compiled.hess_wrt_coord_dims::<M>(z, z, h22.as_mut(), e, f, ks.scratch(m, m))?;
    let mut d_kmm = Mat::zeros(m, m);
    let mut h12 = Mat::zeros(m, m);
    compiled.hess_wrt_coord_mixed::<M>(z, z, h12.as_mut(), e, f, ks.scratch(m, m))?;
    let mut h21 = Mat::zeros(m, m);
    compiled.hess_wrt_coord_mixed::<M>(z, z, h21.as_mut(), f, e, ks.scratch(m, m))?;
    if p == q {
        for i in 0..m {
            if i != p {
                d_kmm[(i, p)] += h22[(i, p)];
                d_kmm[(p, i)] += h22[(i, p)];
            }
        }
        // k(z_p, z_p) moves through both arguments: the two second
        // derivatives in one argument and the two mixed ones. They cancel for
        // a stationary kernel and not for `Linear`.
        d_kmm[(p, p)] = h22[(p, p)] + h22[(p, p)] + h12[(p, p)] + h21[(p, p)];
    } else {
        d_kmm[(p, q)] = h12[(p, q)];
        d_kmm[(q, p)] = h21[(q, p)];
    }
    let mut d_kmn = Mat::zeros(m, n);
    if p == q {
        let mut h_xz = Mat::zeros(n, m);
        compiled.hess_wrt_coord_dims::<M>(x, z, h_xz.as_mut(), e, f, ks.scratch(n, m))?;
        for col in 0..n {
            d_kmn[(p, col)] = h_xz[(col, p)];
        }
    }
    Ok(KernelVar::<T> {
        d_kmm,
        d_kmn,
        d_kdiag: lit::<T>(0.0),
        d_noise: lit::<T>(0.0),
    })
}

pub(crate) fn theta_z_second<M: crate::math::KernelMath, T>(
    compiled: &CompiledKernel<T>,
    x: MatRef<'_, T>,
    z: MatRef<'_, T>,
    theta: usize,
    p: usize,
    e: usize,
) -> Result<KernelVar<T>, GprError>
where
    T: KernelScalar,
{
    let m = z.nrows();
    let n = x.nrows();
    let mut g2 = Mat::zeros(m, m);
    compiled.hess_theta_coord_dim::<M>(z, z, g2.as_mut(), theta, e)?;
    let mut d_kmm = Mat::zeros(m, m);
    for i in 0..m {
        d_kmm[(i, p)] += g2[(i, p)];
        d_kmm[(p, i)] += g2[(i, p)];
    }
    let mut g_xz = Mat::zeros(n, m);
    compiled.hess_theta_coord_dim::<M>(x, z, g_xz.as_mut(), theta, e)?;
    let mut d_kmn = Mat::zeros(m, n);
    for col in 0..n {
        d_kmn[(p, col)] = g_xz[(col, p)];
    }
    Ok(KernelVar::<T> {
        d_kmm,
        d_kmn,
        d_kdiag: lit::<T>(0.0),
        d_noise: lit::<T>(0.0),
    })
}

pub(crate) fn chol_phi<T: KernelScalar>(l: MatRef<'_, T>, dk: MatRef<'_, T>) -> Mat<T> {
    let m = l.nrows();
    let mut tmp = Mat::zeros(m, m);
    copy_mat(dk, tmp.as_mut());
    solve_lower(l, tmp.as_mut());
    let mut u = Mat::zeros(m, m);
    for i in 0..m {
        for j in 0..m {
            u[(i, j)] = tmp[(j, i)];
        }
    }
    solve_lower(l, u.as_mut());
    let mut phi = Mat::zeros(m, m);
    for i in 0..m {
        for j in 0..m {
            phi[(i, j)] = u[(j, i)];
        }
    }
    phi
}

pub(crate) fn tril_half<T: KernelScalar>(phi: MatRef<'_, T>) -> Mat<T> {
    let m = phi.nrows();
    let mut out = Mat::zeros(m, m);
    for j in 0..m {
        for i in j..m {
            out[(i, j)] = if i == j {
                lit::<T>(0.5) * phi[(i, j)]
            } else {
                phi[(i, j)]
            };
        }
    }
    out
}

pub(crate) fn dphi_from<T: KernelScalar>(
    phi_i: MatRef<'_, T>,
    phi_l_j: MatRef<'_, T>,
    phi_dd: MatRef<'_, T>,
) -> Mat<T> {
    let m = phi_i.nrows();
    let mut out = Mat::zeros(m, m);
    for i in 0..m {
        for j in 0..m {
            let mut sum = phi_dd[(i, j)];
            for k in 0..m {
                sum -= phi_l_j[(i, k)] * phi_i[(k, j)];
                sum -= phi_i[(i, k)] * phi_l_j[(j, k)];
            }
            out[(i, j)] = sum;
        }
    }
    out
}

pub(crate) fn noise_plus_sym_prod<T: KernelScalar>(
    da: MatRef<'_, T>,
    a: MatRef<'_, T>,
    d_noise: T,
) -> Mat<T> {
    let m = da.nrows();
    let mut db = Mat::zeros(m, m);
    // `da Aᵀ + A daᵀ`. Diagonal terms are `2 Σ_k da_ik a_ik`, matching the scalar sum.
    gemm(
        db.as_mut(),
        Accum::Replace,
        da,
        a.transpose(),
        lit::<T>(1.0),
    );
    gemm(db.as_mut(), Accum::Add, a, da.transpose(), lit::<T>(1.0));
    for j in 0..m {
        db[(j, j)] += d_noise;
    }
    db
}

pub(crate) fn second_db<T: KernelScalar>(
    dai: MatRef<'_, T>,
    daj: MatRef<'_, T>,
    dda: MatRef<'_, T>,
    a: MatRef<'_, T>,
    dd_noise: T,
) -> Mat<T> {
    let m = a.nrows();
    let mut db = Mat::zeros(m, m);
    gemm(
        db.as_mut(),
        Accum::Replace,
        dda,
        a.transpose(),
        lit::<T>(1.0),
    );
    gemm(db.as_mut(), Accum::Add, a, dda.transpose(), lit::<T>(1.0));
    gemm(db.as_mut(), Accum::Add, dai, daj.transpose(), lit::<T>(1.0));
    gemm(db.as_mut(), Accum::Add, daj, dai.transpose(), lit::<T>(1.0));
    for col in 0..m {
        db[(col, col)] += dd_noise;
    }
    db
}

pub(crate) fn trace_solve<T: KernelScalar>(b_l: MatRef<'_, T>, db: MatRef<'_, T>) -> T {
    let mut solved = Mat::zeros(db.nrows(), db.ncols());
    copy_mat(db, solved.as_mut());
    solve_llt(b_l, solved.as_mut());
    let mut tr = lit::<T>(0.0);
    for i in 0..solved.nrows() {
        tr += solved[(i, i)];
    }
    tr
}

pub(crate) fn second_logdet_b<T: KernelScalar>(
    b_l: MatRef<'_, T>,
    dbi: MatRef<'_, T>,
    dbj: MatRef<'_, T>,
    ddb: MatRef<'_, T>,
) -> T {
    let mut si = Mat::zeros(dbi.nrows(), dbi.ncols());
    copy_mat(dbi, si.as_mut());
    solve_llt(b_l, si.as_mut());
    let mut sj = Mat::zeros(dbj.nrows(), dbj.ncols());
    copy_mat(dbj, sj.as_mut());
    solve_llt(b_l, sj.as_mut());
    let mut tr = lit::<T>(0.0);
    for i in 0..si.nrows() {
        for k in 0..si.ncols() {
            tr -= si[(k, i)] * sj[(i, k)];
        }
    }
    tr + trace_solve(b_l, ddb)
}

pub(crate) fn dw_from<T: KernelScalar>(
    b_l: MatRef<'_, T>,
    w: &[T],
    db: MatRef<'_, T>,
    u: &[T],
) -> Vec<T> {
    let m = w.len();
    let mut rhs = Mat::zeros(m, 1);
    let dbw = mat_vec(db, w);
    for i in 0..m {
        rhs[(i, 0)] = u[i] - dbw[i];
    }
    solve_llt(b_l, rhs.as_mut());
    let mut out = vec![lit::<T>(0.0); m];
    for i in 0..m {
        out[i] = rhs[(i, 0)];
    }
    out
}

#[cfg(test)]
mod adjoint_tests {
    use super::{VfeEngine, kernel_theta_var, z_coord_var};
    use crate::data::pack_points;
    use crate::kernel::{KernelSpec, PeriodicKernel, RbfArdKernel};
    use crate::likelihood::GaussianLikelihood;
    use crate::math::Accurate;
    use crate::sparse::{KernelScratch, SparseSets};
    use crate::{FreeInducing, Sgpr};

    /// The adjoint weights give, for every kernel parameter and inducing
    /// coordinate, the same derivative as the per-direction tangent.
    #[test]
    fn adjoint_matches_per_direction_tangent() {
        let x = [0.0, 0.4, 0.9, 1.5, 2.2, 2.8, 0.3, -0.5, 1.1, 0.7, 1.9, 0.2];
        let y = [0.3, -0.1, 0.8, 0.2, -0.6, 0.4];
        let z = [0.1, 1.2, 2.5, 0.6, -0.2, 1.0];
        let kernel = KernelSpec::from(RbfArdKernel::new(&[0.8, 1.6]).expect("ell"))
            * KernelSpec::from(PeriodicKernel::new(1.1, 2.3).expect("periodic"));
        let model = Sgpr::new(kernel, GaussianLikelihood::new(0.2).expect("noise"))
            .with_inducing(FreeInducing)
            .with_optimizer(crate::Fixed)
            .factor(&x, 6, 2, &y, &z, 3)
            .map_err(|(_, e)| e)
            .expect("factor");
        let engine = VfeEngine::<f64>::from_model(&model, &model.core.y_train);
        let adjoint = engine.adjoint();
        let compiled = model.core.kernel.compile();
        let xm = pack_points(&model.core.x_train, 6, 2);
        let zm = pack_points(&model.core.z_train, 3, 2);
        let mut ks = KernelScratch::new();
        for i in 0..model.core.kernel.num_params() {
            let sets =
                SparseSets::<f64, crate::kernel::NoSupply>::new(xm.as_ref(), zm.as_ref(), ());
            let var =
                kernel_theta_var::<Accurate, f64, _>(&compiled, &mut ks, sets, 6, i).expect("var");
            let via_adjoint = adjoint.contract(var.d_kmm.as_ref(), var.d_kmn.as_ref(), var.d_kdiag);
            let direct = engine.directional_owned(var);
            assert!(
                (via_adjoint - direct).abs() <= 1e-10 * direct.abs().max(1.0),
                "θ{i}"
            );
        }
        for dim in 0..2 {
            for p in 0..3 {
                let var = z_coord_var::<Accurate, f64>(
                    &compiled,
                    &mut ks,
                    xm.as_ref(),
                    zm.as_ref(),
                    p,
                    dim,
                )
                .expect("var");
                let via_adjoint =
                    adjoint.contract(var.d_kmm.as_ref(), var.d_kmn.as_ref(), var.d_kdiag);
                let direct = engine.directional_owned(var);
                assert!(
                    (via_adjoint - direct).abs() <= 1e-10 * direct.abs().max(1.0),
                    "z[{p}, {dim}]"
                );
            }
        }
    }
}