-
Notifications
You must be signed in to change notification settings - Fork 28
/
CKern.h
1229 lines (1156 loc) · 43 KB
/
CKern.h
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
/* This file and CKern.cpp contains classes for representing kernels. These classes do not store the kernel matrix, they merely store the kernels characteristics. There are several types of kernel provided, and it should be easy to add others. CCmpndKern provides a class which forms an additive kernel from individual kernels (i.e. a linear plus an RBF kernel).
16/03/2007 Added code to compute parameter gradients when two X matrices are provided. This enables use of the toolbox with DTC, FITC, PITC approximations. Also changed the transformation of kernel parameters to be by exponential rather than negLogLogit, in line with the default in the MATLAB kern toolbox.
22/10/2005 Added updateX method to class, from William V. Baxter's changes to make computation more efficient. The changes involve precomputing the distance matrix and a portion of the kernel matrix and storing it for later use. UpdateX is called when X has changed so that these stored values are recomputed.
21/10/2005 More changes from William V. Baxter. Most are removal of redundant const modifiers, but also vector<CMatrix*> is now being passed as a reference, rather than by value.
20/10/2005 Incorporated changes from William V. Baxter to allow compilation under MSVC. */
#ifndef CKERN_H
#define CKERN_H
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include "ndlassert.h"
#include "CTransform.h"
#include "CDataModel.h"
#include "CMatrix.h"
#include "CDist.h"
#include "ndlstrutil.h"
#include "ndlutil.h"
using namespace std;
const string KERNVERSION="0.1";
// The base class for all kernels. It implements: CMatinterface, for
// saving and loading from MATLAB; CRegularisable, for placing priors
// over parameters; CTransformable, for allowing parameters to be
// transformed so that they are only optimised in, for example,
// positive half-spaces
class CKern : public CMatInterface, public CStreamInterface, public CTransformable, public CRegularisable {
public:
CKern() : updateXused(false) {}
CKern(const CMatrix& X) : updateXused(false) {}
CKern(unsigned int inDim) : updateXused(false) {}
CKern(const CKern& kern) : updateXused(false) {}
virtual ~CKern() {}
virtual CKern* clone() const=0;
// set initial parameters.
virtual void setInitParam()=0;
// compute an element of the diagonal.
virtual double diagComputeElement(const CMatrix& X, unsigned int index) const=0;
// compute the entire diagonal
virtual void diagCompute(CMatrix& d, const CMatrix& X) const
{
DIMENSIONMATCH(X.rowsMatch(d));
DIMENSIONMATCH(d.getCols()==1);
for(unsigned int i=0; i<X.getRows(); i++)
d.setVal(diagComputeElement(X, i), i);
}
// Compute the diagonal at particular indices.
virtual void diagCompute(CMatrix& d, const CMatrix& X, const vector<unsigned int> indices) const
{
DIMENSIONMATCH(d.getRows()==indices.size());
DIMENSIONMATCH(d.getCols()==1);
for(unsigned int i=0; i<indices.size(); i++)
d.setVal(diagComputeElement(X, indices[i]), i);
}
// Set the parameters of the kernel.
virtual void setParam(double, unsigned int)=0;
// Get gradients of the kernel with respect to input values.
// g[i].val(k,j) = d kern(X_row_i,X2_row_k)/ d x_component_j
virtual void getGradX(vector<CMatrix*>& gX, const CMatrix& X, const CMatrix& X2, bool addG=false) const
{
for(unsigned int i=0; i<X.getRows(); i++)
getGradX(*gX[i], X, i, X2, addG);
}
virtual void getGradX(CMatrix& g, const CMatrix& X, unsigned int pointNo, const CMatrix& X2, bool addG=false) const=0;
// Get gradients of the kernel diagonal with respect to input values.
// WVB: I think this is separate from getGradX just because the diagonal vals
// (i.e. kern(X_row_i,X_row_i) should always be taken from the same matrix X.
// Not to imply that I understand why we need X and X2 in getGradX, really.
virtual void getDiagGradX(CMatrix& g, const CMatrix& X, bool addG=false) const=0;
// return the `signal strength' of the kernel.
virtual double getVariance() const=0;
// set the `signal strength' of the kernel.
virtual void setVariance(double val)=0;
// Return the white noise component of the kernel.
virtual double getWhite() const
{
return 0.0;
}
// Compute an element of the kernel matrix.
virtual double computeElement(const CMatrix& X1, unsigned int index1,
const CMatrix& X2, unsigned int index2) const=0;
// Compute specified rows and columns of the kernel matrix.
virtual void compute(CMatrix& K, const CMatrix& X1, const vector<unsigned int> indices1,
const CMatrix& X2, const vector<unsigned int> indices2) const
{
DIMENSIONMATCH(K.getRows()==indices1.size());
DIMENSIONMATCH(K.getCols()==indices2.size());
for(unsigned int i=0; i<indices1.size(); i++)
{
for(unsigned int j=0; j<indices2.size(); j++)
{
BOUNDCHECK(indices1[i]<X1.getRows());
BOUNDCHECK(indices2[j]<X2.getRows());
K.setVal(computeElement(X1, indices1[i], X2, indices2[j]), i, j);
}
}
}
virtual void compute(CMatrix& K, const CMatrix& X, const vector<unsigned int> indices) const
{
DIMENSIONMATCH(K.getRows()==indices.size());
MATRIXPROPERTIES(K.isSquare());
double k = 0.0;
for(unsigned int i=0; i<indices.size(); i++)
{
for(unsigned int j=0; j<i; j++)
{
BOUNDCHECK(indices[i]<X.getRows());
BOUNDCHECK(indices[j]<X.getRows());
k = computeElement(X, indices[i], X, indices[j]);
K.setVal(k, i, j);
K.setVal(k, j, i);
}
K.setVal(diagComputeElement(X, indices[i]), i, i);
}
}
// Compute the kernel matrix for a data set.
virtual void compute(CMatrix& K, const CMatrix& X) const
{
DIMENSIONMATCH(K.rowsMatch(X));
MATRIXPROPERTIES(K.isSquare());
double k = 0.0;
for(unsigned int i=0; i<K.getRows(); i++)
{
for(unsigned int j=0; j<i; j++)
{
k = computeElement(X, i, X, j);
K.setVal(k, i, j);
K.setVal(k, j, i);
}
K.setVal(diagComputeElement(X, i), i, i);
}
K.setSymmetric(true);
}
// Compute portions of the kernel matrix.
virtual void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2) const
{
DIMENSIONMATCH(K.rowsMatch(X));
DIMENSIONMATCH(K.getCols()==X2.getRows());
for(unsigned int i=0; i<K.getRows(); i++)
{
for(unsigned int j=0; j<K.getCols(); j++)
{
K.setVal(computeElement(X, i, X2, j), i, j);
}
}
}
// Compute portions of the kernel matrix.
virtual void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2, unsigned int row) const
{
DIMENSIONMATCH(K.rowsMatch(X));
DIMENSIONMATCH(K.getCols()==1);
for(unsigned int i=0; i<K.getRows(); i++)
{
K.setVal(computeElement(X, i, X2, row), i, 0);
}
}
// Dummy function to allow CTransformable to be used.
virtual void getGradParams(CMatrix& g) const
{
// This is a dummy function
cerr << "getGradParams should not be used in CKern" << endl;
exit(1);
}
// Compute the gradient of the kernel matrix with respect to parameters given an additional gradient matrix.
virtual void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd, bool regularise=true) const
{
DIMENSIONMATCH(g.getRows()==1);
DIMENSIONMATCH(g.getCols()==nParams);
DIMENSIONMATCH(X.getRows()==cvGrd.getRows());
DIMENSIONMATCH(X2.getRows()==cvGrd.getCols());
for(unsigned int i=0; i<nParams; i++)
g.setVal(getGradParam(i, X, X2, cvGrd), i);
if(regularise)
addPriorGrad(g);
}
virtual void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& cvGrd, bool regularise=true) const
{
DIMENSIONMATCH(g.getRows()==1);
DIMENSIONMATCH(g.getCols()==nParams);
DIMENSIONMATCH(X.rowsMatch(cvGrd));
DIMENSIONMATCH(cvGrd.isSquare());
for(unsigned int i=0; i<nParams; i++)
g.setVal(getGradParam(i, X, cvGrd), i);
if(regularise)
addPriorGrad(g); /// don't forget to add prior gradient at the end.
}
virtual void getDiagGradParams(CMatrix& g, const CMatrix& X, const CMatrix& cvGrad, bool regularise=true) const
{
//TODO Code explicitly for things like RBF where it will always be zero.
CMatrix xi(1, X.getCols());
CMatrix cvGradi(1, 1);
CMatrix gtemp(1, g.getCols());
g.zeros();
for(unsigned int i=0; i<X.getRows(); i++)
{
xi.copyRowRow(0, X, i);
cvGradi.copyRowRow(0, cvGrad, i);
cvGradi.setSymmetric(true);
getGradParams(gtemp, xi, cvGradi, regularise);
g.axpy(gtemp, 1.0);
}
}
// Get gradient of a particular parameter.
virtual double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd) const=0;
virtual double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& cvGrd) const=0;
// Called to indicate the value of X has changed and kernel should do any
// precomputation it needs to do per value of X.
virtual void updateX(const CMatrix& X) {}
virtual bool isUpdateXused() const
{
return updateXused;
}
virtual void setUpdateXused(bool val)
{
updateXused = val;
}
// For compound kernels when a new kernel is added.
virtual unsigned int addKern(const CKern* kern)
{
cerr << "You cannot add a kernel to this kernel." << endl;
return 0;
}
// Get a particular parameter.
virtual double getParam(unsigned int) const=0;
// Test if kernel leads to stationary functions.
bool isStationary() const
{
return stationary;
}
void setStationary(bool val)
{
stationary = val;
}
// Set the parameters from a vector of parameters.
void setParams(const CMatrix& paramVec)
{
for(unsigned int i=0; i<nParams; i++)
setParam(paramVec.getVal(i), i);
}
// Place the parameters in a vector.
void getParams(CMatrix& paramVec) const
{
for(unsigned int i=0; i<nParams; i++)
paramVec.setVal(getParam(i), i);
}
// Return a string representing the kernel type.
inline string getType() const
{
return type;
}
// Set a string representing the kernel type.
inline void setType(const string name)
{
type = name;
}
string getBaseType() const
{
return "kern";
}
// Get the long name of the kernel.
inline string getName() const
{
return kernName;
}
// Set the long name of the kernel.
inline void setName(const string name)
{
kernName = name;
}
// Set the input dimension.
inline void setInputDim(unsigned int dim)
{
inputDim = dim;
setInitParam();
}
// Get the input dimension.
inline unsigned getInputDim() const
{
return inputDim;
}
// How many kernel parameters are there?
inline unsigned int getNumParams() const
{
return nParams;
}
// Assign a name to the kernel parameters.
void setParamName(const string name, unsigned int index)
{
BOUNDCHECK(index<nParams);
if(paramNames.size() == index)
paramNames.push_back(name);
else
{
if(paramNames.size()<index)
paramNames.resize(index+1, "no name");
paramNames[index] = name;
}
}
// Get the name of a kernel parameter.
virtual string getParamName(unsigned int index) const
{
BOUNDCHECK(index<paramNames.size());
return paramNames[index];
}
// Write out the kernel parameters to a stream.
virtual void writeParamsToStream(ostream& out) const;
// Read in the kernel parameters from a stream.
virtual void readParamsFromStream(istream& in);
// Get the gradients of the parameters associated with the priors.
void getGradPrior(CMatrix& g) const;
// Get the log probabilities associated with the priors.
void getPriorLogProb(CMatrix& L) const;
// Display the kernel on an ostream.
virtual ostream& display(ostream& os) const;
#ifdef _NDLMATLAB
// Create a kernel from an mxArray* object
CKern(mxArray* kern){}
// returns an mxArray of the kern for use with matlab.
virtual mxArray* toMxArray() const;
virtual void fromMxArray(const mxArray* matlabArray);
// Adds parameters to the mxArray.
virtual void addParamToMxArray(mxArray* matlabArray) const;
// Gets the parameters from the mxArray.
virtual void extractParamFromMxArray(const mxArray* matlabArray);
#endif /* _NDLMATLAB*/
// Get the gradient of the transformed parameters.
void getGradTransParams(CMatrix& g, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd, bool regularise=true) const;
void getGradTransParams(CMatrix& g, const CMatrix& X, const CMatrix& cvGrd, bool regularise=true) const;
void getDiagGradTransParams(CMatrix& g, const CMatrix& X, const CMatrix& cvGrd, bool regularise=true) const;
// specify tests for equality between kernels.
bool equals(const CKern& kern, double tol=ndlutil::MATCHTOL) const;
protected:
unsigned int nParams;
string kernName;
string type;
vector<string> paramNames;
private:
bool updateXused;
unsigned int inputDim;
bool stationary;
};
// CArdKern is the base class for any kernel that uses multiple input parameters.
class CArdKern : public CKern {
public:
CArdKern() : CKern() {}
CArdKern(const CMatrix& X) : CKern(X) {}
CArdKern(unsigned int inDim) : CKern(inDim) {}
CArdKern(const CKern& kern) : CKern(kern) {}
#ifdef _NDLMATLAB
virtual void addParamToMxArray(mxArray* matlabArray) const;
// Gets the parameters from the mxArray.
virtual void extractParamFromMxArray(const mxArray* matlabArray);
// returns sum(sum(cvGrd.*dK/dparam))
#endif
protected:
CMatrix scales;
};
// Component kernel (such as cmpnd or tensor)
class CComponentKern : public CKern
{
public:
CComponentKern() : CKern() {}
CComponentKern(unsigned int inDim) : CKern(inDim) {}
CComponentKern(const CMatrix& X) : CKern(X) {}
CComponentKern(const CComponentKern& kern) : CKern(kern), components(kern.components) {}
virtual unsigned int addKern(const CKern* kern)
{
components.push_back(kern->clone());
unsigned int oldNParams = nParams;
nParams+=kern->getNumParams();
for(size_t i=0; i<kern->getNumTransforms(); i++)
addTransform(kern->getTransform(i), kern->getTransformIndex(i)+oldNParams);
setStationary(isStationary() && kern->isStationary());
return components.size()-1;
}
virtual void setParam(double val, unsigned int paramNo)
{
unsigned int start = 0;
unsigned int end = 0;
for(size_t i=0; i<components.size(); i++)
{
end = start+components[i]->getNumParams()-1;
if(paramNo <= end)
{
components[i]->setParam(val, paramNo-start);
return;
}
start = end + 1;
}
}
// Parameters are kernel parameters
virtual double getParam(unsigned int paramNo) const
{
unsigned int start = 0;
unsigned int end = 0;
for(size_t i=0; i<components.size(); i++)
{
end = start+components[i]->getNumParams()-1;
if(paramNo <= end)
return components[i]->getParam(paramNo-start);
start = end + 1;
}
return -1;
}
virtual string getParamName(unsigned int paramNo) const
{
unsigned int start = 0;
unsigned int end = 0;
for(size_t i=0; i<components.size(); i++)
{
end = start+components[i]->getNumParams()-1;
if(paramNo <= end)
return components[i]->getType() + components[i]->getParamName(paramNo-start);
start = end + 1;
}
return "";
}
virtual void updateX(const CMatrix& X)
{
for(size_t i=0; i<components.size(); i++)
components[i]->updateX(X);
}
virtual void addPrior(CDist* prior, unsigned int index)
{
throw ndlexceptions::Error("Error cannot add priors to component kernels directly, please add to the components.");
}
virtual double priorLogProb() const
{
double L = 0.0;
for(unsigned int i=0; i<components.size(); i++)
{
L+=components[i]->priorLogProb();
}
return L;
}
virtual void readParamsFromStream(istream& in);
virtual void writeParamsToStream(ostream& out) const;
virtual unsigned int getNumKerns() const
{
return components.size();
}
#ifdef _NDLMATLAB
// sets the parameters in the mxArray.
virtual void addParamToMxArray(mxArray* matlabArray) const;
// Gets the parameters from the mxArray.
virtual void extractParamFromMxArray(const mxArray* matlabArray);
#endif
protected:
// this is a heterogeneous container.
vector<CKern*> components;
};
// Compound Kernel --- This kernel combines other kernels additively together.
class CCmpndKern: public CComponentKern {
public:
CCmpndKern();
CCmpndKern(unsigned int inDim);
CCmpndKern(const CMatrix& X);
~CCmpndKern();
CCmpndKern(const CCmpndKern&);
CCmpndKern* clone() const
{
return new CCmpndKern(*this);
}
//CCmpndKern(vector<CKern*> kernels);
double getVariance() const;
void setVariance(double val)
{
double totalVariance = getVariance();
double factor = val/totalVariance;
for(size_t i=0; i<components.size(); i++)
{
double newVariance = components[i]->getVariance()*factor;
components[i]->setVariance(newVariance);
}
}
double getWhite() const;
void setInitParam();
double diagComputeElement(const CMatrix& X, unsigned int index1) const;
void diagCompute(CMatrix& d, const CMatrix& X) const;
void getGradX(CMatrix& g, const CMatrix& X, unsigned int pointNo, const CMatrix& X2, bool addG=false) const;
void getDiagGradX(CMatrix& g, const CMatrix& X, bool addG=false) const;
double computeElement(const CMatrix& X1, unsigned int index1,
const CMatrix& X2, unsigned int index2) const;
/* void compute(CMatrix& K, const CMatrix& X) const; */
/* void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2) const; */
/* void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2, unsigned int row) const; */
// void compute(CMatrix& K, const CMatrix& X, const vector<unsigned int> indices) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd, bool regularise=true) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& cvGrd, bool regularise=true) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& cvGrd) const;
private:
void _init();
};
// Tensor Kernel --- This kernel combines other multiplicitavely together.
class CTensorKern: public CComponentKern {
public:
CTensorKern();
CTensorKern(unsigned int inDim);
CTensorKern(const CMatrix& X);
~CTensorKern();
CTensorKern(const CTensorKern&);
CTensorKern* clone() const
{
return new CTensorKern(*this);
}
CTensorKern(const CTensorKern&, unsigned int i);
//CTensorKern(vector<CKern*> kernels);
double getVariance() const;
void setVariance(double val)
{
double totalVariance = getVariance();
double factor = val/totalVariance;
for(size_t i=0; i<components.size(); i++)
{
double newVariance = components[i]->getVariance()*factor;
components[i]->setVariance(newVariance);
}
}
double getWhite() const;
void setInitParam();
double diagComputeElement(const CMatrix& X, unsigned int index1) const;
void diagCompute(CMatrix& d, const CMatrix& X) const;
void getGradX(CMatrix& g, const CMatrix& X, unsigned int pointNo, const CMatrix& X2, bool addG=false) const;
void getDiagGradX(CMatrix& g, const CMatrix& X, bool addG=false) const;
double computeElement(const CMatrix& X1, unsigned int index1, const CMatrix& X2, unsigned int index2) const;
/* void compute(CMatrix& K, const CMatrix& X) const; */
/* void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2) const; */
/* void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2, unsigned int row) const; */
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd, bool regularise=true) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& cvGrd, bool regularise=true) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& cvGrd) const;
unsigned int addKern(const CKern* kern);
private:
void _init();
};
// White Noise Kernel.
class CWhiteKern: public CKern {
public:
CWhiteKern();
CWhiteKern(unsigned int inDim);
CWhiteKern(const CMatrix& X);
~CWhiteKern();
CWhiteKern(const CWhiteKern&);
CWhiteKern* clone() const
{
return new CWhiteKern(*this);
}
double getVariance() const;
void setVariance(double val)
{
variance = val;
}
void setInitParam();
double diagComputeElement(const CMatrix& X, unsigned int index) const;
void diagCompute(CMatrix& d, const CMatrix& X) const;
void setParam(double val, unsigned int paramNum);
double getParam(unsigned int paramNum) const;
void getGradX(CMatrix& g, const CMatrix& X, unsigned int pointNo, const CMatrix& X2, bool addG=false) const;
void getDiagGradX(CMatrix& g, const CMatrix& X, bool addG=false) const;
double getWhite() const;
double computeElement(const CMatrix& X1, unsigned int index1,
const CMatrix& X2, unsigned int index2) const;
void compute(CMatrix& K, const CMatrix& X) const;
void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2) const;
void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2, unsigned int row) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& cvGrd) const;
private:
void _init();
double variance;
};
// Whitefixed Noise Kernel.
class CWhitefixedKern: public CKern {
public:
CWhitefixedKern();
CWhitefixedKern(unsigned int inDim);
CWhitefixedKern(const CMatrix& X);
~CWhitefixedKern();
CWhitefixedKern(const CWhitefixedKern&);
CWhitefixedKern* clone() const
{
return new CWhitefixedKern(*this);
}
double getVariance() const;
void setVariance(double val)
{
variance = val;
}
void setInitParam();
double diagComputeElement(const CMatrix& X, unsigned int index) const;
void diagCompute(CMatrix& d, const CMatrix& X) const;
void setParam(double val, unsigned int paramNum);
double getParam(unsigned int paramNum) const;
void getGradX(CMatrix& g, const CMatrix& X, unsigned int pointNo, const CMatrix& X2, bool addG=false) const;
void getDiagGradX(CMatrix& g, const CMatrix& X, bool addG=false) const;
double getWhite() const;
double computeElement(const CMatrix& X1, unsigned int index1,
const CMatrix& X2, unsigned int index2) const;
void compute(CMatrix& K, const CMatrix& X) const;
void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2) const;
void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2, unsigned int row) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& cvGrd) const;
void writeParamsToStream(ostream& out) const;
void readParamsFromStream(istream& in);
ostream& display(ostream& os) const;
private:
void _init();
double variance;
};
// Bias Kernel.
class CBiasKern: public CKern {
public:
CBiasKern();
CBiasKern(unsigned int inDim);
CBiasKern(const CMatrix& X);
~CBiasKern();
CBiasKern(const CBiasKern&);
CBiasKern* clone() const
{
return new CBiasKern(*this);
}
double getVariance() const;
void setVariance(double val)
{
variance = val;
}
void setInitParam();
double diagComputeElement(const CMatrix& X, unsigned int index) const;
void diagCompute(CMatrix& d, const CMatrix& X) const;
void setParam(double val, unsigned int paramNum);
double getParam(unsigned int paramNum) const;
void getGradX(CMatrix& g, const CMatrix& X, unsigned int pointNo, const CMatrix& X2, bool addG=false) const;
void getDiagGradX(CMatrix& g, const CMatrix& X, bool addG=false) const;
double getWhite() const;
double computeElement(const CMatrix& X1, unsigned int index1,
const CMatrix& X2, unsigned int index2) const;
void compute(CMatrix& K, const CMatrix& X) const;
void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2) const;
void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2, unsigned int row) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& cvGrd) const;
private:
void _init();
double variance;
};
// RBF Kernel, also known as the Gaussian or squared exponential kernel.
class CRbfKern: public CKern {
public:
CRbfKern();
CRbfKern(unsigned int inDim);
CRbfKern(const CMatrix& X);
~CRbfKern();
CRbfKern(const CRbfKern&);
CRbfKern* clone() const
{
return new CRbfKern(*this);
}
double getVariance() const;
void setVariance(double val)
{
variance = val;
}
void setInverseWidth(double val)
{
inverseWidth = val;
}
double getInverseWidth() const
{
return inverseWidth;
}
void setLengthScale(double val)
{
inverseWidth = 1/(val*val);
}
double getLengthScale() const
{
return 1/sqrt(inverseWidth);
}
void setInitParam();
double diagComputeElement(const CMatrix& X, unsigned int index) const;
void diagCompute(CMatrix& d, const CMatrix& X) const;
void setParam(double val, unsigned int paramNum);
double getParam(unsigned int paramNum) const;
void getGradX(CMatrix& g, const CMatrix& X, unsigned int pointNo, const CMatrix& X2, bool addG=false) const;
void getDiagGradX(CMatrix& g, const CMatrix& X, bool addG=false) const;
double getWhite() const;
double computeElement(const CMatrix& X1, unsigned int index1,
const CMatrix& X2, unsigned int index2) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& cvGrd, bool regularise=true) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd, bool regularise=true) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& cvGrd) const;
void updateX(const CMatrix& X);
private:
void _init();
double variance;
double inverseWidth;
mutable CMatrix Xdists;
};
// Exponential Kernel, written by Karel Lebeda with no guarantees whatsoever...
class CExpKern: public CKern {
public:
CExpKern();
CExpKern(unsigned int inDim);
CExpKern(const CMatrix& X);
~CExpKern();
CExpKern(const CExpKern&);
CExpKern* clone() const
{
return new CExpKern(*this);
}
double getVariance() const;
void setVariance(double val)
{
variance = val;
}
void setInverseWidth(double val)
{
inverseWidth = val;
}
double getInverseWidth() const
{
return inverseWidth;
}
void setLengthScale(double val)
{
inverseWidth = 1/(val*val);
}
double getLengthScale() const
{
return 1/sqrt(inverseWidth);
}
void setInitParam();
double diagComputeElement(const CMatrix& X, unsigned int index) const;
void diagCompute(CMatrix& d, const CMatrix& X) const;
void setParam(double val, unsigned int paramNum);
double getParam(unsigned int paramNum) const;
void getGradX(CMatrix& g, const CMatrix& X, unsigned int pointNo, const CMatrix& X2, bool addG=false) const;
void getDiagGradX(CMatrix& g, const CMatrix& X, bool addG=false) const;
double getWhite() const;
double computeElement(const CMatrix& X1, unsigned int index1, const CMatrix& X2, unsigned int index2) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& cvGrd, bool regularise=true) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd, bool regularise=true) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& cvGrd) const;
private:
void _init();
double variance;
double inverseWidth;
};
// Rational Quadratic Kernel
class CRatQuadKern: public CKern {
public:
CRatQuadKern();
CRatQuadKern(unsigned int inDim);
CRatQuadKern(const CMatrix& X);
~CRatQuadKern();
CRatQuadKern(const CRatQuadKern&);
CRatQuadKern* clone() const
{
return new CRatQuadKern(*this);
}
double getVariance() const;
void setVariance(double val)
{
variance = val;
}
void setInitParam();
double diagComputeElement(const CMatrix& X, unsigned int index) const;
void diagCompute(CMatrix& d, const CMatrix& X) const;
void setParam(double val, unsigned int paramNum);
double getParam(unsigned int paramNum) const;
void getGradX(CMatrix& g, const CMatrix& X, unsigned pointNo, const CMatrix& X2, bool addG=false) const;
void getDiagGradX(CMatrix& g, const CMatrix& X, bool addG=false) const;
double getWhite() const;
double computeElement(const CMatrix& X1, unsigned int index1,
const CMatrix& X2, unsigned int index2) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& cvGrd, bool regularise=true) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd, bool regularise=true) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& cvGrd) const;
void updateX(const CMatrix& X);
private:
void _init();
double variance;
double alpha;
double lengthScale;
mutable CMatrix Xdists;
};
// Matern kernel with dof=3/2.
class CMatern32Kern: public CKern {
public:
CMatern32Kern();
CMatern32Kern(unsigned int inDim);
CMatern32Kern(const CMatrix& X);
~CMatern32Kern();
CMatern32Kern(const CMatern32Kern&);
CMatern32Kern* clone() const
{
return new CMatern32Kern(*this);
}
double getVariance() const;
void setVariance(double val)
{
variance = val;
}
void setInitParam();
double diagComputeElement(const CMatrix& X, unsigned int index) const;
void diagCompute(CMatrix& d, const CMatrix& X) const;
void setParam(double val, unsigned int paramNum);
double getParam(unsigned int paramNum) const;
void getGradX(CMatrix& g, const CMatrix& X, unsigned int pointNo, const CMatrix& X2, bool addG=false) const;
void getDiagGradX(CMatrix& g, const CMatrix& X, bool addG=false) const;
double getWhite() const;
double computeElement(const CMatrix& X1, unsigned int index1,
const CMatrix& X2, unsigned int index2) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& cvGrd, bool regularise=true) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd, bool regularise=true) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& cvGrd) const;
void updateX(const CMatrix& X);
private:
void _init();
double variance;
double lengthScale;
mutable CMatrix Xdists;
};
// Matern kernel with dof=5/2.
class CMatern52Kern: public CKern {
public:
CMatern52Kern();
CMatern52Kern(unsigned int inDim);
CMatern52Kern(const CMatrix& X);
~CMatern52Kern();
CMatern52Kern(const CMatern52Kern&);
CMatern52Kern* clone() const
{
return new CMatern52Kern(*this);
}
double getVariance() const;
void setVariance(double val)
{
variance = val;
}
void setInitParam();
double diagComputeElement(const CMatrix& X, unsigned int index) const;
void diagCompute(CMatrix& d, const CMatrix& X) const;
void setParam(double val, unsigned int paramNum);
double getParam(unsigned int paramNum) const;
void getGradX(CMatrix& g, const CMatrix& X, unsigned int pointNo, const CMatrix& X2, bool addG=false) const;
void getDiagGradX(CMatrix& g, const CMatrix& X, bool addG=false) const;
double getWhite() const;
double computeElement(const CMatrix& X1, unsigned int index1,
const CMatrix& X2, unsigned int index2) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& cvGrd, bool regularise=true) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd, bool regularise=true) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& cvGrd) const;
void updateX(const CMatrix& X);
private:
void _init();
double variance;
double lengthScale;
mutable CMatrix Xdists;
};
// Linear Kernel, also known as the inner product kernel.
class CLinKern: public CKern {
public:
CLinKern();
CLinKern(unsigned int inDim);
CLinKern(const CMatrix& X);
~CLinKern();
CLinKern(const CLinKern&);
CLinKern* clone() const
{
return new CLinKern(*this);
}
double getVariance() const;
void setVariance(double val)
{
variance = val;
}
void setInitParam();
double diagComputeElement(const CMatrix& X, unsigned int index) const;
void setParam(double val, unsigned int paramNum);
double getParam(unsigned int paramNum) const;
void getGradX(CMatrix& g, const CMatrix& X, unsigned int pointNo, const CMatrix& X2, bool addG=false) const;
void getDiagGradX(CMatrix& g, const CMatrix& X, bool addG=false) const;
double getWhite() const;
double computeElement(const CMatrix& X1, unsigned int index1,
const CMatrix& X2, unsigned int index2) const;
void compute(CMatrix& K, const CMatrix& X) const;
void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2) const;
void compute(CMatrix& K, const CMatrix& X, const CMatrix& X2, unsigned int row) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& cvGrd) const;
private:
void _init();
double variance;
};
// MLP Kernel or arcsin kernel. Based on a multi-layer perceptron with infinite hidden nodes. See Williams (1996) "Computing with Infinite Networks" in NIPS 9.
class CMlpKern: public CKern {
public:
CMlpKern();
CMlpKern(unsigned int inDim);
CMlpKern(const CMatrix& X);
~CMlpKern();
CMlpKern(const CMlpKern&);
CMlpKern* clone() const
{
return new CMlpKern(*this);
}
double getVariance() const;
void setVariance(double val)
{
variance = val;
}
void setInitParam();
double diagComputeElement(const CMatrix& X, unsigned int index) const;
void setParam(double val, unsigned int paramNum);
double getParam(unsigned int paramNum) const;
void getGradX(CMatrix& g, const CMatrix& X, unsigned int pointNo, const CMatrix& X2, bool addG=false) const;
void getDiagGradX(CMatrix& g, const CMatrix& X, bool addG=false) const;
double getWhite() const;
double computeElement(const CMatrix& X1, unsigned int index1,
const CMatrix& X2, unsigned int index2) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd, bool regularise=true) const;
void getGradParams(CMatrix& g, const CMatrix& X, const CMatrix& cvGrd, bool regularise=true) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& X2, const CMatrix& cvGrd) const;
double getGradParam(unsigned int index, const CMatrix& X, const CMatrix& cvGrd) const;
private:
void _init();
double weightVariance;
double biasVariance;
double variance;
mutable CMatrix innerProdi;
mutable CMatrix innerProdj;
};
// Polynomial Kernel, not generally recommended as it `extreme behaviour' outside the region where the argument's absolute value is less than 1.
class CPolyKern: public CKern {
public: