KernelData.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @file KernelData.h
  3. * @author Erik Rodner
  4. * @date 01/19/2010
  5. */
  6. #ifndef _NICE_OBJREC_KERNELDATAINCLUDE
  7. #define _NICE_OBJREC_KERNELDATAINCLUDE
  8. #include "core/basics/Config.h"
  9. #include "core/algebra/CholeskyRobust.h"
  10. #include "core/vector/MatrixT.h"
  11. namespace OBJREC {
  12. /** @class KernelData
  13. * caching some kernel data
  14. *
  15. * @author Erik Rodner
  16. */
  17. class KernelData
  18. {
  19. public:
  20. enum {
  21. QUADRATIC_DISTANCES = 0
  22. };
  23. protected:
  24. bool verbose;
  25. NICE::CholeskyRobust *cr;
  26. protected:
  27. NICE::Matrix kernelMatrix;
  28. NICE::Matrix inverseKernelMatrix;
  29. NICE::Matrix choleskyMatrix;
  30. std::map<int, NICE::Matrix *> cachedMatrices;
  31. double logdet;
  32. void initFromConfig ( const NICE::Config *conf, const std::string & section );
  33. NICE::Matrix B;
  34. NICE::Matrix U;
  35. NICE::Matrix V;
  36. NICE::Matrix F;
  37. NICE::Matrix F_inv;
  38. public:
  39. /** standard stuff */
  40. KernelData();
  41. /** copy constructor */
  42. KernelData( const KernelData & src );
  43. /** simple constructor using config settings for numerical details */
  44. KernelData( const NICE::Config *conf, const std::string & section = "Kernel" );
  45. /** the config contains information about numerical setting of the cholesky factorization etc. */
  46. KernelData( const NICE::Config *conf, const NICE::Matrix & kernelMatrix, const std::string & section = "Kernel", bool updateCholesky = true );
  47. /** simple destructor */
  48. virtual ~KernelData();
  49. /** update the cholesky factorization necessary to use computeInverseKernelMultiply */
  50. virtual void updateCholeskyFactorization ();
  51. /** in nearly all cases computeInverseKernelMultiply can be used instead */
  52. virtual void updateInverseKernelMatrix ();
  53. /** compute K^{-1} * x */
  54. virtual void computeInverseKernelMultiply ( const NICE::Vector & x, NICE::Vector & result ) const;
  55. /** standard const and non-const get functions */
  56. virtual const NICE::Matrix & getKernelMatrix() const;
  57. virtual NICE::Matrix & getKernelMatrix();
  58. virtual const NICE::Matrix & getInverseKernelMatrix() const;
  59. virtual NICE::Matrix & getInverseKernelMatrix();
  60. virtual const NICE::Matrix & getCholeskyMatrix() const;
  61. /** get the logdet of the current kernel matrix (cholesky factorization has to be computed in advance) */
  62. double getLogDetKernelMatrix () const {
  63. return logdet;
  64. };
  65. /** get the numbers of rows (and columns) of the kernel matrix */
  66. virtual uint getKernelMatrixSize () const;
  67. /** get a pre-cached matrix */
  68. const NICE::Matrix & getCachedMatrix (int i) const;
  69. /** set a pre-cached matrix */
  70. void setCachedMatrix (int i, NICE::Matrix *m);
  71. /** did we already start updateCholeskyFactorization() */
  72. bool hasCholeskyFactorization () const {
  73. return (choleskyMatrix.rows() == kernelMatrix.rows());
  74. };
  75. /** get efficient GP regression loo estimates and corresponding variances, which can be used
  76. * to perform model selection (cf. Rasmussen and Williams) */
  77. void getLooEstimates ( const NICE::Vector & y, NICE::Vector & muLoo, NICE::Vector & sigmaLoo ) const;
  78. /** clone this object */
  79. virtual KernelData *clone(void) const;
  80. void getGPLikelihoodWithOneNewRow( const NICE::Vector & y, const double & oldLogdetK, const int & rowIndex, const NICE::Vector & newRow, const NICE::Vector & oldAlpha, NICE::Vector & newAlpha, double & loglike);
  81. void getGPLikelihoodWithOneNewRow_FirstPart( const int & rowIndex, const NICE::Vector & newRow);
  82. void getGPLikelihoodWithOneNewRow_SecondPart( const NICE::Vector & y, const double & oldLogdetK, const NICE::Vector & oldAlpha, NICE::Vector & newAlpha, double & loglike);
  83. void perform_Rank_2_Update(const int & rowIndex, const NICE::Vector & newRow);
  84. void perform_Rank_2k_Update(const std::vector<int> & rowIndices, const std::vector<NICE::Vector> & newRows);
  85. void delete_one_row(const int & rowIndex);
  86. void delete_multiple_rows(std::vector<int> & indices);
  87. void setKernelMatrix(const NICE::Matrix & k_matrix);
  88. void increase_size_by_One();
  89. void increase_size_by_k(const uint & k);
  90. void set_verbose(const bool & _verbose) {
  91. verbose = _verbose;
  92. };
  93. bool get_verbose() {
  94. return verbose;
  95. };
  96. };
  97. }
  98. #endif