PDFMultinomial.cpp 3.7 KB

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