testDirichlet.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * @file testDirichlet.cpp
  3. * @brief functions testing the MAP estimation routines
  4. * @author Erik Rodner
  5. * @date 05/21/2008
  6. */
  7. #ifdef NOVISUAL
  8. #warning "testDirichlet needs ICE with visualization !!"
  9. int main (int argc, char **argv) {};
  10. #else
  11. #include "core/vector/VectorT.h"
  12. #include "core/vector/MatrixT.h"
  13. #include "core/image/ImageT.h"
  14. #include "core/imagedisplay/ImageDisplay.h"
  15. #include <core/vector/Distance.h>
  16. #include <core/image/CrossT.h>
  17. #include <core/basics/Config.h>
  18. #include <vislearning/baselib/cmdline.h>
  19. #include <vislearning/baselib/Gnuplot.h>
  20. #include <vislearning/baselib/ICETools.h>
  21. #include <vislearning/baselib/Conversions.h>
  22. #include <vislearning/math/pdf/PDFDirichlet.h>
  23. #include <vislearning/optimization/mapestimation/MAPMultinomialGaussianPrior.h>
  24. using namespace OBJREC;
  25. // refactor-nice.pl: check this substitution
  26. // old: using namespace ice;
  27. using namespace NICE;
  28. using namespace std;
  29. // refactor-nice.pl: check this substitution
  30. // old: void plotDistributions ( Image & img, const VVector & samples, int color = 1 )
  31. void plotDistributions ( NICE::Image & img, const VVector & samples, int color = 1 )
  32. {
  33. for ( VVector::const_iterator i = samples.begin();
  34. i != samples.end();
  35. i++ )
  36. {
  37. // refactor-nice.pl: check this substitution
  38. // old: const Vector & x = *i;
  39. const NICE::Vector & x = *i;
  40. // refactor-nice.pl: check this substitution
  41. // old: PutVal ( img, (int)(x[0]*(img->xsize-1)), (int)(x[1]*(img->ysize-1)), color );
  42. img.setPixel((int)(x[0]*(img.width()-1)),(int)(x[1]*(img.height()-1)),color);
  43. }
  44. }
  45. // refactor-nice.pl: check this substitution
  46. // old: void estimateDirichlet_Newton ( Vector & alpha,
  47. void estimateDirichlet_Newton ( NICE::Vector & alpha,
  48. const VVector & samples )
  49. {
  50. }
  51. void simulation ( int samplesCount )
  52. {
  53. const int dimension = 100;
  54. // refactor-nice.pl: check this substitution
  55. // old: Vector alpha (dimension);
  56. NICE::Vector alpha (dimension);
  57. #ifdef WIN32
  58. srand(time(NULL));
  59. #else
  60. srand48(time(NULL));
  61. #endif
  62. double s = 0.0;
  63. for ( int i = 0 ; i < dimension ; i++ )
  64. {
  65. #ifdef WIN32
  66. alpha[i] = double( rand() ) / RAND_MAX * 3.0;
  67. #else
  68. alpha[i] = drand48() * 3.0;
  69. #endif
  70. s += alpha[i];
  71. }
  72. cerr << "alpha: " << alpha << endl;
  73. PDFDirichlet pdfdirichlet (alpha);
  74. VVector samples;
  75. pdfdirichlet.sample ( samples, samplesCount );
  76. // refactor-nice.pl: check this substitution
  77. // old: Vector alphaEstimated (alpha.size());
  78. NICE::Vector alphaEstimated (alpha.size());
  79. estimateDirichlet_Newton ( alphaEstimated, samples );
  80. // refactor-nice.pl: check this substitution
  81. // old: Image img ( 400, 400, 255 );
  82. NICE::Image img (400, 400);
  83. // refactor-nice.pl: check this substitution
  84. // old: ClearImg(img);
  85. img.set(0);
  86. if ( dimension == 3 )
  87. {
  88. plotDistributions ( img, samples, 1 );
  89. }
  90. // calculate mean
  91. // refactor-nice.pl: check this substitution
  92. // old: Vector muCGD (dimension);
  93. NICE::Vector muCGD (dimension);
  94. muCGD.set(0);
  95. for ( VVector::const_iterator i = samples.begin();
  96. i != samples.end();
  97. i++ )
  98. muCGD = muCGD + (*i);
  99. muCGD = (1.0/samples.size())*muCGD;
  100. EuclidianDistance<double> euclid;
  101. // refactor-nice.pl: check this substitution
  102. // old: Vector meanDirichlet ( alpha*(1.0/s) );
  103. NICE::Vector meanDirichlet ( alpha*(1.0/s) );
  104. cerr << "mu estimated (cgd): " << muCGD << endl;
  105. cerr << "mean (dirichlet): " << alpha*(1.0/s) << endl;
  106. cerr << "distance: " << euclid(muCGD, meanDirichlet) << endl;
  107. if ( dimension == 3 )
  108. {
  109. Cross cross1 ( Coord( muCGD[0]*(img.width()-1), muCGD[1]*(img.height()-1) ), 10 );
  110. Cross cross2 ( Coord( alpha[0]/s*(img.width()-1), alpha[1]/s*(img.height()-1) ), 10 );
  111. img.draw ( cross1, 2 );
  112. img.draw ( cross2, 3 );
  113. showImageOverlay (img, img );
  114. } else {
  115. getchar();
  116. }
  117. }
  118. int main (int argc, char **argv)
  119. {
  120. #ifndef __clang__
  121. #ifndef __llvm__
  122. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  123. #endif
  124. #endif
  125. char configfile [300];
  126. struct CmdLineOption options[] = {
  127. {"config", "use config file", NULL, "%s", configfile},
  128. {NULL, NULL, NULL, NULL, NULL}
  129. };
  130. int ret;
  131. char *more_options[argc];
  132. ret = parse_arguments( argc, (const char**)argv, options, more_options);
  133. if ( ret != 0 )
  134. {
  135. if ( ret != 1 ) fprintf (stderr, "Error parsing command line !\n");
  136. exit (-1);
  137. }
  138. Config conf ( configfile );
  139. const int samples_count_sim = 100;
  140. simulation ( samples_count_sim );
  141. const int samples_count = 100;
  142. const int dimension = 1000;
  143. // refactor-nice.pl: check this substitution
  144. // old: Vector mu (dimension);
  145. NICE::Vector mu (dimension);
  146. // refactor-nice.pl: check this substitution
  147. // old: Vector mu_noninformative (dimension);
  148. NICE::Vector mu_noninformative (dimension);
  149. // refactor-nice.pl: check this substitution
  150. // old: Vector counts (dimension);
  151. NICE::Vector counts (dimension);
  152. mu.set(0);
  153. counts.set(0);
  154. MAPMultinomialGaussianPrior map;
  155. // simulate gaussian distribution
  156. for ( int i = 0 ; i < samples_count ; i++ )
  157. {
  158. double r = randGaussDouble ( dimension / 6 );
  159. int bin = (int) (r+dimension/3);
  160. if ( (bin >= 0) && (bin < dimension) )
  161. mu[bin]++;
  162. r = randGaussDouble ( dimension / 6 ) * randGaussDouble (dimension/6);
  163. bin = (int) (r+2*dimension/3);
  164. if ( (bin >= 0) && (bin < dimension) )
  165. counts[bin]++;
  166. }
  167. MAPMultinomialGaussianPrior::normalizeProb ( mu );
  168. mu_noninformative = Vector(counts);
  169. MAPMultinomialGaussianPrior::normalizeProb ( mu_noninformative );
  170. const double eps = 10e-11;
  171. for ( int i = 1 ; i < 12 ; i++ )
  172. {
  173. double sigmaq = pow(10,-i);
  174. // refactor-nice.pl: check this substitution
  175. // old: Vector theta ( dimension );
  176. NICE::Vector theta ( dimension );
  177. map.estimate ( theta, counts, mu, sigmaq );
  178. vector<double> tp, mup, munonp;
  179. tp = Conversions::stl_vector ( theta );
  180. mup = Conversions::stl_vector ( mu );
  181. munonp = Conversions::stl_vector ( mu_noninformative );
  182. Gnuplot gp;
  183. gp.set_style ( "boxes" );
  184. gp.plot_x ( mup, "mu" );
  185. gp.plot_x ( munonp, "mu (noninformative)" );
  186. gp.plot_x ( tp, "theta" );
  187. // refactor-nice.pl: check this substitution
  188. // old: GetChar();
  189. getchar();
  190. }
  191. return 0;
  192. }
  193. #endif