LHCumulativeGauss.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /**
  2. * @brief the cumulative gaussian likelihood function
  3. * @author Erik Rodner
  4. * @date 02/17/2010
  5. */
  6. #include <iostream>
  7. #include <math.h>
  8. #include "LHCumulativeGauss.h"
  9. using namespace OBJREC;
  10. using namespace std;
  11. static const double sqrt2pi = sqrt(2*M_PI);
  12. LHCumulativeGauss::LHCumulativeGauss( double _lengthScale, double _bias )
  13. {
  14. lengthScale = _lengthScale;
  15. bias = _bias;
  16. }
  17. double LHCumulativeGauss::stdNormPDF (double x)
  18. {
  19. return exp(-x*x)/sqrt(M_PI);
  20. }
  21. double LHCumulativeGauss::thirdgrad ( double y, double f ) const
  22. {
  23. /** Computing the third gradient of the LOG likelihood with
  24. * respect to f
  25. * dl / df hessian = hessian * (-2 * (f*lengthScale + bias)) *lengthScale
  26. * + gradient * (-2*lengthScale*lengthScale) - 2 * hessian * gradient
  27. *
  28. **/
  29. double gradient = this->gradient(y,f);
  30. double hessian = this->hessian(y,f);
  31. return -2* ( hessian*lengthScale*(f*lengthScale+bias) + gradient * lengthScale * lengthScale
  32. + hessian * gradient );
  33. }
  34. double LHCumulativeGauss::hessian ( double y, double f ) const
  35. {
  36. /** Computing the hessian of the LOG likelihood with respect to f
  37. * d^2 / (d^2 f) log l(x) = d/df (dl/df * 1/l(f))
  38. * = (d^2 l)/(d^2 f) 1/l(f) - dl/df dl/df / (l(f)^2)
  39. * = (d^2 l)/(d^2 f) 1/l(f) - gradient * gradient
  40. *
  41. * dl/df = y*lengthScale*stdNormPDF(f*lengthScale + bias)
  42. * d/df dl/df = y*lengthScale*stdNormPDF(f*lengthScale + bias)*(-2 * (f*lengthScale + bias)) * lengthScale
  43. * (d^2 l)/(d^2 f) / l(f) = gradient * (-2 * (f*lengthScale + bias)) * lengthScale
  44. *
  45. *
  46. **/
  47. double gradient = this->gradient(y,f);
  48. return ( -2*(f*lengthScale + bias)*lengthScale*gradient - gradient*gradient );
  49. }
  50. double LHCumulativeGauss::gradient ( double y, double f ) const
  51. {
  52. /* Computing the derivation of the LOG likelihood with respect to f
  53. * erf'(x) = 2/pi e^{-x*x}
  54. * phi'(x) = 1/pi e^{-x*x} = stdNormPDF(x)
  55. * x = y*(f*lengthScale + bias)
  56. * dx/dy = lengthScale*y
  57. * d/dx log l(x) = dl/dx * 1/l(x)
  58. * y is assumed to be in {-1, 1}
  59. * stdNormPDF(y*(f*lengthScale + bias)) = stdNormPDF(f*lengthScale + bias)
  60. * All of the above equations yield
  61. */
  62. return ( y*lengthScale*stdNormPDF(f*lengthScale + bias)/likelihood(y,f) );
  63. }
  64. double LHCumulativeGauss::logLike ( double y, double f ) const
  65. {
  66. return log ( likelihood(y, f) );
  67. }
  68. double LHCumulativeGauss::likelihood ( double y, double f ) const
  69. {
  70. /*
  71. * erf(x) = 2/pi \int_0^x e^{-t*t}
  72. * The likelihood is denoted as phi(x) in the following.
  73. * phi(x) = (erf(x) + 1)/2
  74. */
  75. return ( ::erf(y*(f*lengthScale + bias))+1 ) / 2.0;
  76. }
  77. double LHCumulativeGauss::predictAnalytically ( double fmean, double fvariance ) const
  78. {
  79. // Rasmussen, Williams page 74
  80. // \int_{-inf}^{inf} phi~((x-m)/v) N(x | mu, sigma^2) dx
  81. // = phi~( (mu-m)/(v*sqrt(1+sigma^2/v^2)) )
  82. // phi^(f*lengthScale + bias) = phi~(f*sqrt(2)*lengthScale + bias*sqrt(2))
  83. // v = 1 / (sqrt(2) * lengthScale)
  84. // m = - bias*sqrt(2) * v = - bias / lengthScale
  85. //
  86. // phi~(f) = phi^( f/sqrt(2) ) = phi( (f/(sqrt(2)*lengthScale) - bias/lengthScale) )
  87. //
  88. // (mu-m)/(v*sqrt(1+sigma^2/v^2))
  89. // = (fmean + bias/lengthScale) * sqrt(2) * lengthScale / sqrt(1 + sigma^2 * 2 * lssqr)
  90. // -> we would like to put this into our likelihood
  91. // (fmean + bias/lengthScale) * sqrt(2) * lengthScale / sqrt(1 + sigma^2 * 2 * lssqr) / (sqrt(2) * lengthScale) - bias/lengthScale
  92. // = (fmean + bias/lengthScale) / sqrt(1 + sigma^2 * 2 * lssqr) - bias/lengthScale
  93. //
  94. double lssqr = lengthScale * lengthScale;
  95. // variant without bias: return likelihood( 1.0, fmean / sqrt(1.0 + 2.0 * fvariance * lssqr) );
  96. return likelihood( 1.0, (fmean + bias/lengthScale) / sqrt(1.0 + 2.0 * fvariance * lssqr) - bias/lengthScale );
  97. }