KernelData.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 { return logdet; };
  63. /** get the numbers of rows (and columns) of the kernel matrix */
  64. virtual uint getKernelMatrixSize () const;
  65. /** get a pre-cached matrix */
  66. const NICE::Matrix & getCachedMatrix (int i) const;
  67. /** set a pre-cached matrix */
  68. void setCachedMatrix (int i, NICE::Matrix *m);
  69. /** did we already start updateCholeskyFactorization() */
  70. bool hasCholeskyFactorization () const { return (choleskyMatrix.rows() == kernelMatrix.rows()); };
  71. /** get efficient GP regression loo estimates and corresponding variances, which can be used
  72. * to perform model selection (cf. Rasmussen and Williams) */
  73. void getLooEstimates ( const NICE::Vector & y, NICE::Vector & muLoo, NICE::Vector & sigmaLoo ) const;
  74. /** clone this object */
  75. virtual KernelData *clone(void) const;
  76. 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);
  77. void getGPLikelihoodWithOneNewRow_FirstPart( const int & rowIndex, const NICE::Vector & newRow);
  78. void getGPLikelihoodWithOneNewRow_SecondPart( const NICE::Vector & y, const double & oldLogdetK, const NICE::Vector & oldAlpha, NICE::Vector & newAlpha, double & loglike);
  79. void perform_Rank_2_Update(const int & rowIndex, const NICE::Vector & newRow);
  80. void perform_Rank_2k_Update(const std::vector<int> & rowIndices, const std::vector<NICE::Vector> & newRows);
  81. void delete_one_row(const int & rowIndex);
  82. void delete_multiple_rows(std::vector<int> & indices);
  83. void setKernelMatrix(const NICE::Matrix & k_matrix);
  84. void increase_size_by_One();
  85. void increase_size_by_k(const uint & k);
  86. void set_verbose(const bool & _verbose){ verbose = _verbose;};
  87. bool get_verbose(){ return verbose;};
  88. };
  89. }
  90. #endif