testDirichlet.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. srand48(time(NULL));
  58. double s = 0.0;
  59. for ( int i = 0 ; i < dimension ; i++ )
  60. {
  61. alpha[i] = drand48() * 3.0;
  62. s += alpha[i];
  63. }
  64. cerr << "alpha: " << alpha << endl;
  65. PDFDirichlet pdfdirichlet (alpha);
  66. VVector samples;
  67. pdfdirichlet.sample ( samples, samplesCount );
  68. // refactor-nice.pl: check this substitution
  69. // old: Vector alphaEstimated (alpha.size());
  70. NICE::Vector alphaEstimated (alpha.size());
  71. estimateDirichlet_Newton ( alphaEstimated, samples );
  72. // refactor-nice.pl: check this substitution
  73. // old: Image img ( 400, 400, 255 );
  74. NICE::Image img (400, 400);
  75. // refactor-nice.pl: check this substitution
  76. // old: ClearImg(img);
  77. img.set(0);
  78. if ( dimension == 3 )
  79. {
  80. plotDistributions ( img, samples, 1 );
  81. }
  82. // calculate mean
  83. // refactor-nice.pl: check this substitution
  84. // old: Vector muCGD (dimension);
  85. NICE::Vector muCGD (dimension);
  86. muCGD.set(0);
  87. for ( VVector::const_iterator i = samples.begin();
  88. i != samples.end();
  89. i++ )
  90. muCGD = muCGD + (*i);
  91. muCGD = (1.0/samples.size())*muCGD;
  92. EuclidianDistance<double> euclid;
  93. // refactor-nice.pl: check this substitution
  94. // old: Vector meanDirichlet ( alpha*(1.0/s) );
  95. NICE::Vector meanDirichlet ( alpha*(1.0/s) );
  96. cerr << "mu estimated (cgd): " << muCGD << endl;
  97. cerr << "mean (dirichlet): " << alpha*(1.0/s) << endl;
  98. cerr << "distance: " << euclid(muCGD, meanDirichlet) << endl;
  99. if ( dimension == 3 )
  100. {
  101. Cross cross1 ( Coord( muCGD[0]*(img.width()-1), muCGD[1]*(img.height()-1) ), 10 );
  102. Cross cross2 ( Coord( alpha[0]/s*(img.width()-1), alpha[1]/s*(img.height()-1) ), 10 );
  103. img.draw ( cross1, 2 );
  104. img.draw ( cross2, 3 );
  105. showImageOverlay (img, img );
  106. } else {
  107. getchar();
  108. }
  109. }
  110. int main (int argc, char **argv)
  111. {
  112. std::set_terminate(__gnu_cxx::__verbose_terminate_handler);
  113. char configfile [300];
  114. struct CmdLineOption options[] = {
  115. {"config", "use config file", NULL, "%s", configfile},
  116. {NULL, NULL, NULL, NULL, NULL}
  117. };
  118. int ret;
  119. char *more_options[argc];
  120. ret = parse_arguments( argc, (const char**)argv, options, more_options);
  121. if ( ret != 0 )
  122. {
  123. if ( ret != 1 ) fprintf (stderr, "Error parsing command line !\n");
  124. exit (-1);
  125. }
  126. Config conf ( configfile );
  127. const int samples_count_sim = 100;
  128. simulation ( samples_count_sim );
  129. const int samples_count = 100;
  130. const int dimension = 1000;
  131. // refactor-nice.pl: check this substitution
  132. // old: Vector mu (dimension);
  133. NICE::Vector mu (dimension);
  134. // refactor-nice.pl: check this substitution
  135. // old: Vector mu_noninformative (dimension);
  136. NICE::Vector mu_noninformative (dimension);
  137. // refactor-nice.pl: check this substitution
  138. // old: Vector counts (dimension);
  139. NICE::Vector counts (dimension);
  140. mu.set(0);
  141. counts.set(0);
  142. MAPMultinomialGaussianPrior map;
  143. // simulate gaussian distribution
  144. for ( int i = 0 ; i < samples_count ; i++ )
  145. {
  146. double r = randGaussDouble ( dimension / 6 );
  147. int bin = (int) (r+dimension/3);
  148. if ( (bin >= 0) && (bin < dimension) )
  149. mu[bin]++;
  150. r = randGaussDouble ( dimension / 6 ) * randGaussDouble (dimension/6);
  151. bin = (int) (r+2*dimension/3);
  152. if ( (bin >= 0) && (bin < dimension) )
  153. counts[bin]++;
  154. }
  155. MAPMultinomialGaussianPrior::normalizeProb ( mu );
  156. mu_noninformative = Vector(counts);
  157. MAPMultinomialGaussianPrior::normalizeProb ( mu_noninformative );
  158. const double eps = 10e-11;
  159. for ( int i = 1 ; i < 12 ; i++ )
  160. {
  161. double sigmaq = pow(10,-i);
  162. // refactor-nice.pl: check this substitution
  163. // old: Vector theta ( dimension );
  164. NICE::Vector theta ( dimension );
  165. map.estimate ( theta, counts, mu, sigmaq );
  166. vector<double> tp, mup, munonp;
  167. tp = Conversions::stl_vector ( theta );
  168. mup = Conversions::stl_vector ( mu );
  169. munonp = Conversions::stl_vector ( mu_noninformative );
  170. Gnuplot gp;
  171. gp.set_style ( "boxes" );
  172. gp.plot_x ( mup, "mu" );
  173. gp.plot_x ( munonp, "mu (noninformative)" );
  174. gp.plot_x ( tp, "theta" );
  175. // refactor-nice.pl: check this substitution
  176. // old: GetChar();
  177. getchar();
  178. }
  179. return 0;
  180. }
  181. #endif