PDFMultinomial.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @file PDFMultinomial.cpp
  3. * @brief Normal Distribution / Gaussian PDF
  4. * @author Erik Rodner
  5. * @date 01/29/2008
  6. */
  7. #include <iostream>
  8. #include <assert.h>
  9. #include "vislearning/math/pdf/PDFMultinomial.h"
  10. #include "vislearning/math/pdf/gslRandomNumberGenerator.h"
  11. using namespace OBJREC;
  12. using namespace std;
  13. // refactor-nice.pl: check this substitution
  14. // old: using namespace ice;
  15. using namespace NICE;
  16. PDFMultinomial::PDFMultinomial( int dimension ) : theta(dimension)
  17. {
  18. assert ( dimension > 0 );
  19. N = 1;
  20. for ( int i = 0 ; i < dimension ; i++ )
  21. theta[i] = 1.0 / dimension;
  22. }
  23. // refactor-nice.pl: check this substitution
  24. // old: PDFMultinomial::PDFMultinomial ( const Vector & _theta, int _N )
  25. PDFMultinomial::PDFMultinomial ( const NICE::Vector & _theta, int _N )
  26. : theta(_theta), N(_N)
  27. {
  28. }
  29. PDFMultinomial::~PDFMultinomial()
  30. {
  31. }
  32. // refactor-nice.pl: check this substitution
  33. // old: double PDFMultinomial::getNLogDensity ( const Vector & x ) const
  34. double PDFMultinomial::getNLogDensity ( const NICE::Vector & x ) const
  35. {
  36. #ifdef NICE_USELIB_GSL
  37. assert ( x.size() == theta.size() );
  38. unsigned int *ix = new unsigned int[x.size()];
  39. double r = gsl_ran_multinomial_lnpdf ( theta.size(), theta.getDataPointerConst(), ix );
  40. delete [] ix;
  41. return r;
  42. #else
  43. #warning "PDFMultinomial::getNLogDensity: this function needs the GNU Scientific Library"
  44. fprintf (stderr, "PDFMultinomial::getNLogDensity: this function needs the GNU Scientific Library\n");
  45. exit(-1);
  46. return 0.0;
  47. #endif
  48. }
  49. // refactor-nice.pl: check this substitution
  50. // old: double PDFMultinomial::getProb ( const Vector & x ) const
  51. double PDFMultinomial::getProb ( const NICE::Vector & x ) const
  52. {
  53. #ifdef NICE_USELIB_GSL
  54. assert ( x.size() == theta.size() );
  55. unsigned int *ix = new unsigned int[theta.size()];
  56. double r = gsl_ran_multinomial_pdf ( theta.size(), theta.getDataPointerConst(), ix );
  57. delete [] ix;
  58. return r;
  59. #else
  60. #warning "PDFMultinomial::getProb: this function needs the GNU Scientific Library"
  61. fprintf (stderr, "PDFMultinomial::getProb: this function needs the GNU Scientific Library\n");
  62. exit(-1);
  63. return 0.0;
  64. #endif
  65. }
  66. int PDFMultinomial::getDimension () const
  67. {
  68. return theta.size();
  69. }
  70. int PDFMultinomial::sample () const
  71. {
  72. #ifdef NICE_USELIB_GSL
  73. if ( theta.size() == 1 )
  74. return 0;
  75. initGSLRandom();
  76. double r = gsl_rng_uniform(randomGSL);
  77. NICE::Vector cumDist ( theta.size() );
  78. cumDist[0] = theta[0];
  79. for ( int i = 1 ; i < theta.size() ; i++ )
  80. {
  81. if ( r < cumDist[i-1] ) return i-1;
  82. cumDist[i] += cumDist[i-1] + theta[i];
  83. }
  84. return theta.size()-1;
  85. #else
  86. #warning "PDFMultinomial::sample: this function needs the GNU Scientific Library"
  87. fprintf (stderr, "PDFMultinomial::sample: this function needs the GNU Scientific Library\n");
  88. exit(-1);
  89. return 0;
  90. #endif
  91. }
  92. void PDFMultinomial::sample ( VVector & samples, int count ) const
  93. {
  94. #ifdef NICE_USELIB_GSL
  95. initGSLRandom();
  96. unsigned int *ix = new unsigned int[theta.size()];
  97. for ( int i = 0 ; i < count ; i++ )
  98. {
  99. NICE::Vector x ( theta.size() );
  100. gsl_ran_multinomial (randomGSL, theta.size(), N, theta.getDataPointerConst(), ix );
  101. for ( int j = 0 ; j < theta.size() ; j++ )
  102. x[j] = ix[j];
  103. samples.push_back ( x );
  104. }
  105. delete [] ix;
  106. #else
  107. #warning "PDFMultinomial::sample: this function needs the GNU Scientific Library"
  108. fprintf (stderr, "PDFMultinomial::sample: this function needs the GNU Scientific Library\n");
  109. exit(-1);
  110. #endif
  111. }
  112. double PDFMultinomial::getDiscreteProbability ( int index ) const
  113. {
  114. assert ( (index >= 0) && (index < (int)theta.size()) );
  115. return theta[index];
  116. }