friedrich 0.6.0

Gaussian Process Regression.
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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
//! # Kernels
//!
//! A kernel is a function that maps from two row vectors to a scalar which is used to express the similarity between the vectors.
//!
//! To learn more about the properties of the provided kernels, we recommend the [Usual_covariance_functions](https://en.wikipedia.org/wiki/Gaussian_process#Usual_covariance_functions) Wikipedia page and the [kernel-functions-for-machine-learning-applications](http://crsouza.com/2010/03/17/kernel-functions-for-machine-learning-applications/#kernel_functions) article.
//!
//! User-defined kernels should implement the Kernel trait.
//! To learn more about the implementation of kernels adapted to a particular problem, we recommend the chapter two (*Expressing Structure with Kernels*) and three (*Automatic Model Construction*) of the very good [Automatic Model Construction with Gaussian Processes](http://www.cs.toronto.edu/~duvenaud/thesis.pdf).
//!
//! This implementation is inspired by [rusty-machines'](https://github.com/AtheMathmo/rusty-machine/blob/master/src/learning/toolkit/kernel.rs).

use crate::algebra::{SMatrix, SRowVector, SVector};
use nalgebra::{storage::Storage, Dyn, U1};
use std::ops::{Add, Mul};

//---------------------------------------------------------------------------------------
// TRAIT

/// The Kernel trait.
///
/// If you want to provide a user-defined kernel, you should implement this trait.
pub trait Kernel: Default
{
    /// Numbers of parameters (such as bandwidth and amplitude) of the kernel.
    ///
    /// This should return a constant value for the kernel.
    fn nb_parameters(&self) -> usize;

    /// Can the kernel be rescaled (see the `rescale` function) ? This value is
    /// `false` by default.
    ///
    /// This should return constant value for the kernel.
    fn is_scalable(&self) -> bool
    {
        false // TODO check whether more existing kernel can be made is_scalable
    }

    /// Multiplies the amplitude of the kernel by the `scale` parameter such that a kernel `a*K(x,y)` becomes `scale*a*K(x,y)`.
    ///
    /// When possible, do implement this function as it unlock a faster parameter fitting algorithm.
    ///
    /// *WARNING:* the code will panic if you set `is_scalable` to `true` without providing a user defined implementation of this function.
    fn rescale(&mut self, _scale: f64)
    {
        // TODO Get rid of test and add ScalableKernel trait once specialization lands on stable.
        if self.is_scalable()
        {
            unimplemented!("Please implement the `rescale` function if you set `is_scalable` to true.")
        }
        else
        {
            panic!("You tried to rescale a Kernel that is not Scalable!")
        }
    }
    /// Takes two equal length slices (row vector) and returns a scalar.
    ///
    /// NOTE: Due to the optimization algorithm, this function might get illegal parameters (ie: negative parameters),
    /// it is the duty of the function implementer to deal with them properly (ie : using an absolute value).
    fn kernel<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                    x1: &SRowVector<S1>,
                                                                    x2: &SRowVector<S2>)
                                                                    -> f64;

    /// Takes two equal length slices (row vector) and returns a vector containing the value of the gradient for each parameter in an arbitrary order.
    ///
    /// NOTE: Due to the optimization algorithm, this function might get illegal parameters (ie: negative parameters),
    /// it is the duty of the function implementer to deal with them properly (ie: using the absolute value of the parameter and multiplying its gradient by its original sign).
    fn gradient<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                      x1: &SRowVector<S1>,
                                                                      x2: &SRowVector<S2>)
                                                                      -> Vec<f64>;

    /// Returns a vector containing all the parameters of the kernel in the same order as the outputs of the `gradient` function.
    fn get_parameters(&self) -> Vec<f64>;

    /// Sets all the parameters of the kernel by reading them from a slice where they are in the same order as the outputs of the `gradient` function.
    fn set_parameters(&mut self, parameters: &[f64]);

    /// Optional, function that fits the kernel parameters on the training data using fast heuristics.
    /// This is used as a starting point for gradient descent.
    fn heuristic_fit<SM: Storage<f64, Dyn, Dyn>, SV: Storage<f64, Dyn, U1>>(&mut self,
                                                                            _training_inputs: &SMatrix<SM>,
                                                                            _training_outputs: &SVector<SV>)
    {
    }
}

//---------------------------------------------------------------------------------------
// FIT

/// Provides a rough estimate for the bandwidth.
///
/// Use the mean distance between points as a baseline for the bandwidth.
fn fit_bandwidth_mean<S: Storage<f64, Dyn, Dyn>>(training_inputs: &SMatrix<S>) -> f64
{
    // Builds the sum of all distances between different samples.
    let mut sum_distances = 0.;
    for (sample_index, sample) in training_inputs.row_iter().enumerate()
    {
        for sample2 in training_inputs.row_iter().skip(sample_index + 1)
        {
            let distance = (sample - sample2).norm();
            sum_distances += distance;
        }
    }

    // Counts the number of distances that have been computed.
    let nb_samples = training_inputs.nrows();
    let nb_distances = ((nb_samples * nb_samples - nb_samples) / 2) as f64;

    // Mean distance.
    sum_distances / nb_distances
}

/// Outputs the variance of the outputs as a best guess of the amplitude.
fn fit_amplitude_var<S: Storage<f64, Dyn, U1>>(training_outputs: &SVector<S>) -> f64
{
    training_outputs.variance()
}

//---------------------------------------------------------------------------------------
// KERNEL COMBINAISON

/// The sum of two kernels.
///
/// This struct should not be directly instantiated but instead is created when we add two kernels together.
///
/// Note that it will be more efficient to implement the final kernel manually yourself.
/// However this provides an easy mechanism to test different combinations.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "friedrich_serde", derive(serde::Deserialize, serde::Serialize))]
pub struct KernelSum<T, U>
    where T: Kernel,
          U: Kernel
{
    k1: T,
    k2: U
}

/// Computes the sum of the two associated kernels.
impl<T, U> Kernel for KernelSum<T, U>
    where T: Kernel,
          U: Kernel
{
    fn nb_parameters(&self) -> usize
    {
        self.k1.nb_parameters() + self.k2.nb_parameters()
    }

    fn is_scalable(&self) -> bool
    {
        self.k1.is_scalable() && self.k2.is_scalable()
    }

    fn kernel<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                    x1: &SRowVector<S1>,
                                                                    x2: &SRowVector<S2>)
                                                                    -> f64
    {
        self.k1.kernel(x1, x2) + self.k2.kernel(x1, x2)
    }

    fn gradient<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                      x1: &SRowVector<S1>,
                                                                      x2: &SRowVector<S2>)
                                                                      -> Vec<f64>
    {
        let mut g1 = self.k1.gradient(x1, x2);
        let mut g2 = self.k2.gradient(x1, x2);
        g1.append(&mut g2);
        g1
    }

    fn rescale(&mut self, scale: f64)
    {
        self.k1.rescale(scale);
        self.k2.rescale(scale);
    }

    fn get_parameters(&self) -> Vec<f64>
    {
        let mut p1 = self.k1.get_parameters();
        let mut p2 = self.k2.get_parameters();
        p1.append(&mut p2);
        p1
    }

    fn set_parameters(&mut self, parameters: &[f64])
    {
        self.k1.set_parameters(&parameters[..self.k1.nb_parameters()]);
        self.k2.set_parameters(&parameters[self.k1.nb_parameters()..]);
    }

    fn heuristic_fit<SM: Storage<f64, Dyn, Dyn>, SV: Storage<f64, Dyn, U1>>(&mut self,
                                                                            training_inputs: &SMatrix<SM>,
                                                                            training_outputs: &SVector<SV>)
    {
        self.k1.heuristic_fit(training_inputs, training_outputs);
        self.k2.heuristic_fit(training_inputs, training_outputs);
    }
}

impl<T: Kernel, U: Kernel> Default for KernelSum<T, U>
{
    fn default() -> Self
    {
        let k1 = T::default();
        let k2 = U::default();
        KernelSum { k1, k2 }
    }
}

/// The point-wise product of two kernels.
///
/// This struct should not be directly instantiated but instead is created when we multiply two kernels together.
///
/// Note that it will be more efficient to implement the final kernel manually yourself.
/// However this provides an easy mechanism to test different combinations.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "friedrich_serde", derive(serde::Deserialize, serde::Serialize))]
pub struct KernelProd<T, U>
    where T: Kernel,
          U: Kernel
{
    k1: T,
    k2: U
}

/// Computes the product of the two associated kernels.
impl<T, U> Kernel for KernelProd<T, U>
    where T: Kernel,
          U: Kernel
{
    fn nb_parameters(&self) -> usize
    {
        self.k1.nb_parameters() + self.k2.nb_parameters()
    }

    fn is_scalable(&self) -> bool
    {
        self.k1.is_scalable() || self.k2.is_scalable()
    }

    fn kernel<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                    x1: &SRowVector<S1>,
                                                                    x2: &SRowVector<S2>)
                                                                    -> f64
    {
        self.k1.kernel(x1, x2) * self.k2.kernel(x1, x2)
    }

    fn gradient<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                      x1: &SRowVector<S1>,
                                                                      x2: &SRowVector<S2>)
                                                                      -> Vec<f64>
    {
        let k1 = self.k1.kernel(x1, x2);
        let k2 = self.k2.kernel(x1, x2);
        let g1 = self.k1.gradient(x1, x2);
        let g2 = self.k2.gradient(x1, x2);
        g1.iter().map(|g1| g1 * k2).chain(g2.iter().map(|g2| g2 * k1)).collect()
    }

    fn rescale(&mut self, scale: f64)
    {
        if self.k1.is_scalable()
        {
            self.k1.rescale(scale);
        }
        else
        {
            self.k2.rescale(scale);
        }
    }

    fn get_parameters(&self) -> Vec<f64>
    {
        let mut p1 = self.k1.get_parameters();
        let mut p2 = self.k2.get_parameters();
        p1.append(&mut p2);
        p1
    }

    fn set_parameters(&mut self, parameters: &[f64])
    {
        self.k1.set_parameters(&parameters[..self.k1.nb_parameters()]);
        self.k2.set_parameters(&parameters[self.k1.nb_parameters()..]);
    }

    fn heuristic_fit<SM: Storage<f64, Dyn, Dyn>, SV: Storage<f64, Dyn, U1>>(&mut self,
                                                                            training_inputs: &SMatrix<SM>,
                                                                            training_outputs: &SVector<SV>)
    {
        self.k1.heuristic_fit(training_inputs, training_outputs);
        self.k2.heuristic_fit(training_inputs, training_outputs);
    }
}

impl<T: Kernel, U: Kernel> Default for KernelProd<T, U>
{
    fn default() -> Self
    {
        let k1 = T::default();
        let k2 = U::default();
        KernelProd { k1, k2 }
    }
}

/// A wrapper tuple struct used for kernel arithmetic
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "friedrich_serde", derive(serde::Deserialize, serde::Serialize))]
pub struct KernelArith<K: Kernel>(pub K);

impl<T: Kernel, U: Kernel> Add<KernelArith<T>> for KernelArith<U>
{
    type Output = KernelSum<U, T>;

    fn add(self, ker: KernelArith<T>) -> KernelSum<U, T>
    {
        KernelSum { k1: self.0, k2: ker.0 }
    }
}

impl<T: Kernel, U: Kernel> Mul<KernelArith<T>> for KernelArith<U>
{
    type Output = KernelProd<U, T>;

    fn mul(self, ker: KernelArith<T>) -> KernelProd<U, T>
    {
        KernelProd { k1: self.0, k2: ker.0 }
    }
}

//---------------------------------------------------------------------------------------
// CLASSICAL KERNELS

/// The Linear Kernel.
///
/// k(x,y) = x^Ty + c
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "friedrich_serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Linear
{
    /// Constant term added to inner product.
    pub c: f64
}

impl Linear
{
    /// Constructs a new Linear Kernel.
    pub fn new(c: f64) -> Linear
    {
        Linear { c }
    }
}

/// Constructs the default Linear Kernel.
///
/// The defaults are:
/// - c = 0
impl Default for Linear
{
    fn default() -> Linear
    {
        Linear { c: 0f64 }
    }
}

impl Kernel for Linear
{
    fn nb_parameters(&self) -> usize
    {
        1
    }

    fn kernel<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                    x1: &SRowVector<S1>,
                                                                    x2: &SRowVector<S2>)
                                                                    -> f64
    {
        x1.dot(x2) + self.c
    }

    fn gradient<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                      _x1: &SRowVector<S1>,
                                                                      _x2: &SRowVector<S2>)
                                                                      -> Vec<f64>
    {
        let grad_c = 1.;
        vec![grad_c]
    }

    fn get_parameters(&self) -> Vec<f64>
    {
        vec![self.c]
    }

    fn set_parameters(&mut self, parameters: &[f64])
    {
        self.c = parameters[0];
    }
}

//-----------------------------------------------

/// The Polynomial Kernel.
///
/// k(x,y) = (αx^Ty + c)^d
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "friedrich_serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Polynomial
{
    /// Scaling of the inner product.
    pub alpha: f64,
    /// Constant added to inner product.
    pub c: f64,
    /// The power to raise the sum to.
    pub d: f64
}

impl Polynomial
{
    /// Constructs a new Polynomial Kernel.
    pub fn new(alpha: f64, c: f64, d: f64) -> Polynomial
    {
        Polynomial { alpha, c, d }
    }
}

/// Construct a new polynomial kernel.
///
/// The defaults are:
/// - alpha = 1
/// - c = 0
/// - d = 1
impl Default for Polynomial
{
    fn default() -> Polynomial
    {
        Polynomial { alpha: 1f64, c: 0f64, d: 1f64 }
    }
}

impl Kernel for Polynomial
{
    fn nb_parameters(&self) -> usize
    {
        3
    }

    fn kernel<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                    x1: &SRowVector<S1>,
                                                                    x2: &SRowVector<S2>)
                                                                    -> f64
    {
        (self.alpha * x1.dot(x2) + self.c).powf(self.d)
    }

    fn gradient<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                      x1: &SRowVector<S1>,
                                                                      x2: &SRowVector<S2>)
                                                                      -> Vec<f64>
    {
        let x = x1.dot(x2);
        let inner_term = self.alpha * x + self.c;

        let grad_c = self.d * inner_term.powf(self.d - 1.);
        let grad_alpha = x * grad_c;
        let grad_d = inner_term.ln() * inner_term.powf(self.d);

        vec![grad_alpha, grad_c, grad_d]
    }

    fn get_parameters(&self) -> Vec<f64>
    {
        vec![self.alpha, self.c, self.d]
    }

    fn set_parameters(&mut self, parameters: &[f64])
    {
        self.alpha = parameters[0];
        self.c = parameters[1];
        self.d = parameters[2];
    }
}

//-----------------------------------------------

/// Gaussian kernel.
///
/// Equivalent to a squared exponential kernel.
///
/// k(x,y) = A exp(-||x-y||² / 2l²)
///
/// Where A is the amplitude and l the length scale.
pub type Gaussian = SquaredExp;

/// Squared exponential kernel
///
/// Equivalent to a gaussian kernel.
///
/// k(x,y) = A exp(-||x-y||² / 2l²)
///
/// Where A is the amplitude and l the length scale.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "friedrich_serde", derive(serde::Deserialize, serde::Serialize))]
pub struct SquaredExp
{
    /// The length scale of the kernel.
    pub ls: f64,
    /// The amplitude of the kernel.
    pub ampl: f64
}

impl SquaredExp
{
    /// Construct a new squared exponential kernel (gaussian).
    pub fn new(ls: f64, ampl: f64) -> SquaredExp
    {
        SquaredExp { ls, ampl }
    }
}

/// Constructs the default Squared Exp kernel.
///
/// The defaults are:
/// - ls = 1
/// - ampl = 1
impl Default for SquaredExp
{
    fn default() -> SquaredExp
    {
        SquaredExp { ls: 1f64, ampl: 1f64 }
    }
}

impl Kernel for SquaredExp
{
    fn nb_parameters(&self) -> usize
    {
        2
    }

    fn is_scalable(&self) -> bool
    {
        true
    }

    /// The squared exponential kernel function.
    fn kernel<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                    x1: &SRowVector<S1>,
                                                                    x2: &SRowVector<S2>)
                                                                    -> f64
    {
        // Sanitize parameters.
        let ampl = self.ampl.abs();
        // Computes kernel.
        let distance_squared = (x1 - x2).norm_squared();
        let x = -distance_squared / (2f64 * self.ls * self.ls);
        ampl * x.exp()
    }

    fn gradient<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                      x1: &SRowVector<S1>,
                                                                      x2: &SRowVector<S2>)
                                                                      -> Vec<f64>
    {
        // Sanitize parameters.
        let ampl = self.ampl.abs();
        // Compute gradients.
        let distance_squared = (x1 - x2).norm_squared();
        let exponential = (-distance_squared / (2f64 * self.ls * self.ls)).exp();
        let grad_ls = (distance_squared * ampl * exponential) / self.ls.powi(3);
        let grad_ampl = self.ampl.signum() * exponential;
        vec![grad_ls, grad_ampl]
    }

    fn rescale(&mut self, scale: f64)
    {
        self.ampl *= scale;
    }

    fn get_parameters(&self) -> Vec<f64>
    {
        vec![self.ls, self.ampl]
    }

    fn set_parameters(&mut self, parameters: &[f64])
    {
        self.ls = parameters[0];
        self.ampl = parameters[1];
    }

    fn heuristic_fit<SM: Storage<f64, Dyn, Dyn>, SV: Storage<f64, Dyn, U1>>(&mut self,
                                                                            training_inputs: &SMatrix<SM>,
                                                                            training_outputs: &SVector<SV>)
    {
        self.ls = fit_bandwidth_mean(training_inputs);
        self.ampl = fit_amplitude_var(training_outputs);
    }
}

//-----------------------------------------------

/// The Exponential Kernel.
///
/// k(x,y) = A exp(-||x-y|| / 2l²)
///
/// Where A is the amplitude and l is the length scale.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "friedrich_serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Exponential
{
    /// The length scale of the kernel.
    pub ls: f64,
    /// The amplitude of the kernel.
    pub ampl: f64
}

impl Exponential
{
    /// Construct a new squared exponential kernel.
    pub fn new(ls: f64, ampl: f64) -> Exponential
    {
        Exponential { ls, ampl }
    }
}

/// Constructs the default Exponential kernel.
///
/// The defaults are:
/// - ls = 1
/// - amplitude = 1
impl Default for Exponential
{
    fn default() -> Exponential
    {
        Exponential { ls: 1f64, ampl: 1f64 }
    }
}

impl Kernel for Exponential
{
    fn nb_parameters(&self) -> usize
    {
        2
    }

    fn is_scalable(&self) -> bool
    {
        true
    }

    /// The squared exponential kernel function.
    fn kernel<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                    x1: &SRowVector<S1>,
                                                                    x2: &SRowVector<S2>)
                                                                    -> f64
    {
        // sanitize parameters
        let ampl = self.ampl.abs();
        // compute kernel
        let distance = (x1 - x2).norm();
        let x = -distance / (2f64 * self.ls * self.ls);
        ampl * x.exp()
    }

    fn gradient<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                      x1: &SRowVector<S1>,
                                                                      x2: &SRowVector<S2>)
                                                                      -> Vec<f64>
    {
        // Sanitize parameters.
        let ampl = self.ampl.abs();
        // Compute gradients.
        let distance = (x1 - x2).norm();
        let exponential = (-distance / (2f64 * self.ls * self.ls)).exp();
        let grad_ls = (distance * ampl * exponential) / self.ls.powi(3);
        let grad_ampl = self.ampl.signum() * exponential;
        vec![grad_ls, grad_ampl]
    }

    fn rescale(&mut self, scale: f64)
    {
        self.ampl *= scale;
    }

    fn get_parameters(&self) -> Vec<f64>
    {
        vec![self.ls, self.ampl]
    }

    fn set_parameters(&mut self, parameters: &[f64])
    {
        self.ls = parameters[0];
        self.ampl = parameters[1];
    }

    fn heuristic_fit<SM: Storage<f64, Dyn, Dyn>, SV: Storage<f64, Dyn, U1>>(&mut self,
                                                                            training_inputs: &SMatrix<SM>,
                                                                            training_outputs: &SVector<SV>)
    {
        self.ls = fit_bandwidth_mean(training_inputs);
        self.ampl = fit_amplitude_var(training_outputs);
    }
}

//-----------------------------------------------

/// The Matèrn1 kernel which is 1 differentiable and correspond to a classical Matèrn kernel with nu=3/2.
///
/// k(x,y) = A (1 + ||x-y||sqrt(3)/l) exp(-||x-y||sqrt(3)/l)
///
/// Where A is the amplitude and l is the length scale.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "friedrich_serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Matern1
{
    /// The length scale of the kernel.
    pub ls: f64,
    /// The amplitude of the kernel.
    pub ampl: f64
}

impl Matern1
{
    /// Construct a new matèrn1 kernel.
    pub fn new(ls: f64, ampl: f64) -> Matern1
    {
        Matern1 { ls, ampl }
    }
}

/// Constructs the default Matern1 kernel.
///
/// The defaults are:
/// - ls = 1
/// - amplitude = 1
impl Default for Matern1
{
    fn default() -> Matern1
    {
        Matern1 { ls: 1f64, ampl: 1f64 }
    }
}

impl Kernel for Matern1
{
    fn nb_parameters(&self) -> usize
    {
        2
    }

    fn is_scalable(&self) -> bool
    {
        true
    }

    /// The matèrn1 kernel function.
    fn kernel<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                    x1: &SRowVector<S1>,
                                                                    x2: &SRowVector<S2>)
                                                                    -> f64
    {
        // sanitize parameters
        let ampl = self.ampl.abs();
        let l = self.ls.abs();
        // compute kernel
        let distance = (x1 - x2).norm();
        let x = (3f64).sqrt() * distance / l;
        ampl * (1f64 + x) * (-x).exp()
    }

    fn gradient<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                      x1: &SRowVector<S1>,
                                                                      x2: &SRowVector<S2>)
                                                                      -> Vec<f64>
    {
        // Sanitize parameters.
        let ampl = self.ampl.abs();
        let l = self.ls.abs();
        // Compute gradient.
        let distance = (x1 - x2).norm();
        let x = 3f64.sqrt() * distance / l;
        let grad_ls = (3. * ampl * distance.powi(2) * (-x).exp()) / (self.ls.powi(3));
        let grad_ampl = self.ampl.signum() * (1. + x) * (-x).exp();
        vec![grad_ls, grad_ampl]
    }

    fn rescale(&mut self, scale: f64)
    {
        self.ampl *= scale;
    }

    fn get_parameters(&self) -> Vec<f64>
    {
        vec![self.ls, self.ampl]
    }

    fn set_parameters(&mut self, parameters: &[f64])
    {
        self.ls = parameters[0];
        self.ampl = parameters[1];
    }

    fn heuristic_fit<SM: Storage<f64, Dyn, Dyn>, SV: Storage<f64, Dyn, U1>>(&mut self,
                                                                            training_inputs: &SMatrix<SM>,
                                                                            training_outputs: &SVector<SV>)
    {
        self.ls = fit_bandwidth_mean(training_inputs);
        self.ampl = fit_amplitude_var(training_outputs);
    }
}

//-----------------------------------------------

/// The Matèrn2 kernel which is 2 differentiable and correspond to a classical Matèrn kernel with nu=5/2.
///
/// k(x,y) = A (1 + ||x-y||sqrt(5)/l + ||x-y||²5/3l²) exp(-||x-y||sqrt(5)/l)
///
/// Where A is the amplitude and l is the length scale.
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "friedrich_serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Matern2
{
    /// The length scale of the kernel.
    pub ls: f64,
    /// The amplitude of the kernel.
    pub ampl: f64
}

impl Matern2
{
    /// Construct a new matèrn2 kernel.
    pub fn new(ls: f64, ampl: f64) -> Matern2
    {
        Matern2 { ls, ampl }
    }
}

/// Constructs the default Matern2 kernel.
///
/// The defaults are:
/// - ls = 1
/// - amplitude = 1
impl Default for Matern2
{
    fn default() -> Matern2
    {
        Matern2 { ls: 1f64, ampl: 1f64 }
    }
}

impl Kernel for Matern2
{
    fn nb_parameters(&self) -> usize
    {
        2
    }

    fn is_scalable(&self) -> bool
    {
        true
    }

    /// The matèrn2 kernel function.
    fn kernel<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                    x1: &SRowVector<S1>,
                                                                    x2: &SRowVector<S2>)
                                                                    -> f64
    {
        // sanitize parameters
        let ampl = self.ampl.abs();
        let l = self.ls.abs();
        // compute kernel
        let distance = (x1 - x2).norm();
        let x = (5f64).sqrt() * distance / l;
        ampl * (1f64 + x + (5f64 * distance * distance) / (3f64 * l * l)) * (-x).exp()
    }

    fn gradient<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                      x1: &SRowVector<S1>,
                                                                      x2: &SRowVector<S2>)
                                                                      -> Vec<f64>
    {
        // Sanitize parameters.
        let ampl = self.ampl.abs();
        let l = self.ls.abs();
        // Compute gradient.
        let distance = (x1 - x2).norm();
        let x = (5f64).sqrt() * distance / self.ls;
        let grad_ls = self.ls.signum()
                      * ampl
                      * ((2. * l / 3. + 1.)
                         + distance * 5f64.sqrt() * ((l.powi(2) / 3. + l + 1.) / l.powi(2)))
                      * (-x).exp();
        let grad_ampl =
            self.ampl.signum() * (1f64 + x + (5f64 * distance * distance) / (3f64 * l * l)) * (-x).exp();
        vec![grad_ls, grad_ampl]
    }

    fn rescale(&mut self, scale: f64)
    {
        self.ampl *= scale;
    }

    fn get_parameters(&self) -> Vec<f64>
    {
        vec![self.ls, self.ampl]
    }

    fn set_parameters(&mut self, parameters: &[f64])
    {
        self.ls = parameters[0];
        self.ampl = parameters[1];
    }

    fn heuristic_fit<SM: Storage<f64, Dyn, Dyn>, SV: Storage<f64, Dyn, U1>>(&mut self,
                                                                            training_inputs: &SMatrix<SM>,
                                                                            training_outputs: &SVector<SV>)
    {
        self.ls = fit_bandwidth_mean(training_inputs);
        self.ampl = fit_amplitude_var(training_outputs);
    }
}

//-----------------------------------------------

/// The Hyperbolic Tangent Kernel.
///
/// ker(x,y) = tanh(αx^Ty + c)
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "friedrich_serde", derive(serde::Deserialize, serde::Serialize))]
pub struct HyperTan
{
    /// The scaling of the inner product.
    pub alpha: f64,
    /// The constant to add to the inner product.
    pub c: f64
}

impl HyperTan
{
    /// Constructs a new Hyperbolic Tangent Kernel.
    pub fn new(alpha: f64, c: f64) -> HyperTan
    {
        HyperTan { alpha, c }
    }
}

/// Constructs a default Hyperbolic Tangent Kernel.
///
/// The defaults are:
/// - alpha = 1
/// - c = 0
impl Default for HyperTan
{
    fn default() -> HyperTan
    {
        HyperTan { alpha: 1f64, c: 0f64 }
    }
}

impl Kernel for HyperTan
{
    fn nb_parameters(&self) -> usize
    {
        2
    }

    fn kernel<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                    x1: &SRowVector<S1>,
                                                                    x2: &SRowVector<S2>)
                                                                    -> f64
    {
        (self.alpha * x1.dot(x2) + self.c).tanh()
    }

    fn gradient<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                      x1: &SRowVector<S1>,
                                                                      x2: &SRowVector<S2>)
                                                                      -> Vec<f64>
    {
        let x = x1.dot(x2);
        let grad_c = 1. / (self.alpha * x + self.c).cosh().powi(2);
        let grad_alpha = x * grad_c;

        vec![grad_alpha, grad_c]
    }

    fn get_parameters(&self) -> Vec<f64>
    {
        vec![self.alpha, self.c]
    }

    fn set_parameters(&mut self, parameters: &[f64])
    {
        self.alpha = parameters[0];
        self.c = parameters[1];
    }
}

//-----------------------------------------------

/// The Multiquadric Kernel.
///
/// k(x,y) = sqrt(||x-y||² + c²)
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "friedrich_serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Multiquadric
{
    /// Constant added to square of difference.
    pub c: f64
}

impl Multiquadric
{
    /// Constructs a new Multiquadric Kernel.
    pub fn new(c: f64) -> Multiquadric
    {
        Multiquadric { c }
    }
}

/// Constructs a default Multiquadric Kernel.
///
/// The defaults are:
/// - c = 0
impl Default for Multiquadric
{
    fn default() -> Multiquadric
    {
        Multiquadric { c: 0f64 }
    }
}

impl Kernel for Multiquadric
{
    fn nb_parameters(&self) -> usize
    {
        2
    }

    fn kernel<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                    x1: &SRowVector<S1>,
                                                                    x2: &SRowVector<S2>)
                                                                    -> f64
    {
        (x1 - x2).norm_squared().hypot(self.c)
    }

    fn gradient<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                      x1: &SRowVector<S1>,
                                                                      x2: &SRowVector<S2>)
                                                                      -> Vec<f64>
    {
        let grad_c = self.c / (x1 - x2).norm().hypot(self.c);
        vec![grad_c]
    }

    fn get_parameters(&self) -> Vec<f64>
    {
        vec![self.c]
    }

    fn set_parameters(&mut self, parameters: &[f64])
    {
        self.c = parameters[1];
    }
}

//-----------------------------------------------

/// The Rational Quadratic Kernel.
///
/// k(x,y) = (1 + ||x-y||² / (2αl²))^-α
#[derive(Clone, Copy, Debug)]
#[cfg_attr(feature = "friedrich_serde", derive(serde::Deserialize, serde::Serialize))]
pub struct RationalQuadratic
{
    /// Controls inverse power and difference scale.
    pub alpha: f64,
    /// Length scale controls scale of difference.
    pub ls: f64
}

impl RationalQuadratic
{
    /// Constructs a new Rational Quadratic Kernel.
    pub fn new(alpha: f64, ls: f64) -> RationalQuadratic
    {
        RationalQuadratic { alpha, ls }
    }
}

/// The default Rational Quadratic Kernel.
///
/// The defaults are:
/// - alpha = 1
/// - ls = 1
impl Default for RationalQuadratic
{
    fn default() -> RationalQuadratic
    {
        RationalQuadratic { alpha: 1f64, ls: 1f64 }
    }
}

impl Kernel for RationalQuadratic
{
    fn nb_parameters(&self) -> usize
    {
        2
    }

    fn kernel<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                    x1: &SRowVector<S1>,
                                                                    x2: &SRowVector<S2>)
                                                                    -> f64
    {
        let distance_squared = (x1 - x2).norm_squared();
        (1f64 + distance_squared / (2f64 * self.alpha * self.ls * self.ls)).powf(-self.alpha)
    }

    fn gradient<S1: Storage<f64, U1, Dyn>, S2: Storage<f64, U1, Dyn>>(&self,
                                                                      x1: &SRowVector<S1>,
                                                                      x2: &SRowVector<S2>)
                                                                      -> Vec<f64>
    {
        // Sanitize parameters.
        let l = self.ls.abs();
        // Compute gradient.
        let distance_squared = (x1 - x2).norm_squared();
        let grad_alpha =
            ((distance_squared + 2. * l.powi(2) * self.alpha) / (l.powi(2) * self.alpha)).powf(-self.alpha)
            * (2f64.powf(self.alpha)
               * (1.
                  - ((distance_squared + 2. * l.powi(2) * self.alpha) / (2. * l.powi(2) * self.alpha)).ln())
               - (l.powi(2) * 2f64.powf(self.alpha + 1.) * self.alpha)
                 / (distance_squared + 2. * l.powi(2) * self.alpha));
        let grad_ls = distance_squared
                      * (distance_squared / (2. * self.alpha * l * l) + 1.).powf(-self.alpha - 1.)
                      / self.ls.powi(3);
        vec![grad_alpha, grad_ls]
    }

    fn get_parameters(&self) -> Vec<f64>
    {
        vec![self.alpha, self.ls]
    }

    fn set_parameters(&mut self, parameters: &[f64])
    {
        self.alpha = parameters[0];
        self.ls = parameters[1];
    }
}