MAPMultinomialGaussianPrior.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**
  2. * @file MAPMultinomialGaussianPrior.cpp
  3. * @brief MAP estimation of multinomial with gaussian prior
  4. * @author Erik Rodner
  5. * @date 05/26/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 <limits>
  14. #include "vislearning/optimization/mapestimation/MAPMultinomialGaussianPrior.h"
  15. using namespace OBJREC;
  16. using namespace std;
  17. // refactor-nice.pl: check this substitution
  18. // old: using namespace ice;
  19. using namespace NICE;
  20. MAPMultinomialGaussianPrior::MAPMultinomialGaussianPrior()
  21. {
  22. }
  23. MAPMultinomialGaussianPrior::~MAPMultinomialGaussianPrior()
  24. {
  25. }
  26. #undef DEBUG_OPTIMIZATION
  27. #undef DEBUG_ANALYZEOPTIMIZATION
  28. double MAPMultinomialGaussianPrior::objective (
  29. // refactor-nice.pl: check this substitution
  30. // old: const Vector & theta,
  31. const NICE::Vector & theta,
  32. // refactor-nice.pl: check this substitution
  33. // old: const Vector & counts,
  34. const NICE::Vector & counts,
  35. // refactor-nice.pl: check this substitution
  36. // old: const Vector & mu,
  37. const NICE::Vector & mu,
  38. double sigmaq )
  39. {
  40. // calculate - log
  41. double sum_likelihood = 0.0;
  42. for ( size_t i = 0 ; i < counts.size() ; i++ )
  43. {
  44. double ti = (theta[i] < 10e-7) ? 10e-7 : theta[i];
  45. sum_likelihood += counts[i] * log( ti );
  46. }
  47. double sum_prior = 0.0;
  48. for ( size_t i = 0 ; i < counts.size() ; i++ )
  49. {
  50. double d = (theta[i] - mu[i]);
  51. sum_prior -= d*d*0.5;
  52. }
  53. sum_prior /= sigmaq;
  54. #ifdef DEBUG_OPTIMIZATION
  55. fprintf (stderr, "log likelihood : %f\n", sum_likelihood );
  56. fprintf (stderr, "log prior : %f\n", sum_prior );
  57. #endif
  58. return sum_likelihood + sum_prior;
  59. }
  60. // refactor-nice.pl: check this substitution
  61. // old: void MAPMultinomialGaussianPrior::normalizeProb ( Vector & x )
  62. void MAPMultinomialGaussianPrior::normalizeProb ( NICE::Vector & x )
  63. {
  64. double sum = 0.0;
  65. for ( size_t i = 0 ; i < x.size() ; i++ )
  66. sum += x[i];
  67. for ( size_t i = 0 ; i < x.size() ; i++ )
  68. x[i] /= sum;
  69. }
  70. double MAPMultinomialGaussianPrior::estimateLambdaNonLinear (
  71. // refactor-nice.pl: check this substitution
  72. // old: const Vector & counts,
  73. const NICE::Vector & counts,
  74. // refactor-nice.pl: check this substitution
  75. // old: const Vector & mu,
  76. const NICE::Vector & mu,
  77. double sigmaq )
  78. {
  79. // calculate lagrange multiplier
  80. const int maxIterations = 10000;
  81. const double eps = 10e-9;
  82. double count = 0;
  83. for ( size_t i = 0 ; i < counts.size() ; i++ )
  84. count += counts[i];
  85. double lambda = - count;
  86. int m = mu.size();
  87. int iterations = 0;
  88. double diff = 0.0;
  89. do {
  90. // calculate f(\lambda) = 0.5 m \lambda \sigma^2 + \sum_l \sqrt{ g_l(\lambda) } - 0.5
  91. double f = 0.5 * m * lambda * sigmaq - 0.5;
  92. double fabl = 0.5 * m * sigmaq;
  93. for ( size_t i = 0 ; i < mu.size() ; i++ )
  94. {
  95. double phalf = 0.5 * (mu[i] + lambda * sigmaq);
  96. double undersqrt = phalf*phalf + sigmaq * counts[i];
  97. assert ( undersqrt >= 0.0 );
  98. // undersqrt = g_l(\lambda)
  99. double gabl = (mu[i] + lambda * sigmaq) * sigmaq;
  100. fabl -= gabl / sqrt(undersqrt);
  101. f += sqrt(undersqrt);
  102. }
  103. double lambdanew = lambda - f / fabl;
  104. diff = fabs(f / fabl);
  105. #ifdef DEBUG_OPTIMIZATION
  106. fprintf (stderr, "(%d) %f %f\n", iterations, lambdanew, diff );
  107. #endif
  108. lambda = lambdanew;
  109. #ifdef DEBUG_ANALYZEOPTIMIZATION
  110. // refactor-nice.pl: check this substitution
  111. // old: Vector theta (m);
  112. NICE::Vector theta (m);
  113. // calculate theta estimation
  114. double sum = 0.0;
  115. for ( size_t i = 0 ; i < mu.size() ; i++ )
  116. {
  117. double phalf = 0.5 * (mu[i] + lambda*sigmaq);
  118. theta[i] = phalf + sqrt( phalf*phalf + sigmaq * counts[i] );
  119. assert ( theta[i] >= 0.0 );
  120. sum += theta[i];
  121. }
  122. fprintf (stderr, "OPT %f %d %f\n", sigmaq, iterations,
  123. objective ( theta, counts, mu, sigmaq ) );
  124. #endif
  125. iterations++;
  126. } while ( (iterations < maxIterations) && (diff > eps) );
  127. fprintf (stderr, "MAPMultinomialGaussianPrior: optimization finished (%d/%d iterations, %f last delta)\n",
  128. iterations, maxIterations, diff );
  129. fprintf (stderr, "MAPMultinomialGaussianPrior: final lambda: %f\n", lambda );
  130. return lambda;
  131. }
  132. double MAPMultinomialGaussianPrior::estimateLambda (
  133. // refactor-nice.pl: check this substitution
  134. // old: const Vector & counts,
  135. const NICE::Vector & counts,
  136. // refactor-nice.pl: check this substitution
  137. // old: const Vector & mu,
  138. const NICE::Vector & mu,
  139. double sigmaq )
  140. {
  141. // calculate lagrange multiplier
  142. const int maxIterations = 10000;
  143. //const double eps = 10e-5;
  144. const double eps = 10e-9;
  145. int m = mu.size();
  146. double count = 0;
  147. for ( size_t i = 0 ; i < counts.size() ; i++ )
  148. count += counts[i];
  149. // using iterative formula, hopefully this converges
  150. double lambda = - count;
  151. double diff;
  152. int iterations = 0;
  153. do {
  154. // calculate evil sqrt sum
  155. double sqrsum = 0.0;
  156. for ( size_t i = 0 ; i < mu.size() ; i++ )
  157. {
  158. double phalf = 0.5 * (mu[i] + lambda * sigmaq);
  159. double undersqrt = phalf*phalf + sigmaq * counts[i];
  160. assert ( undersqrt >= 0.0 );
  161. sqrsum += sqrt( undersqrt );
  162. }
  163. double lambdanew = - (sqrsum - 0.5) * 2 / (sigmaq * m);
  164. diff = fabs(lambda-lambdanew);
  165. #ifdef DEBUG_OPTIMIZATION
  166. fprintf (stderr, "(%d) %f %f\n", iterations, lambdanew, diff );
  167. #endif
  168. lambda = lambdanew;
  169. #ifdef DEBUG_ANALYZEOPTIMIZATION
  170. // refactor-nice.pl: check this substitution
  171. // old: Vector theta (m);
  172. NICE::Vector theta (m);
  173. // calculate theta estimation
  174. double sum = 0.0;
  175. for ( size_t i = 0 ; i < mu.size() ; i++ )
  176. {
  177. double phalf = 0.5 * (mu[i] + lambda*sigmaq);
  178. theta[i] = phalf + sqrt( phalf*phalf + sigmaq * counts[i] );
  179. assert ( theta[i] >= 0.0 );
  180. sum += theta[i];
  181. }
  182. fprintf (stderr, "OPT %f %d %f\n", sigmaq, iterations,
  183. objective ( theta, counts, mu, sigmaq ) );
  184. #endif
  185. iterations++;
  186. } while ( (iterations < maxIterations) && (diff > eps) );
  187. fprintf (stderr, "MAPMultinomialGaussianPrior: optimization finished (%d/%d iterations, %f last delta)\n",
  188. iterations, maxIterations, diff );
  189. fprintf (stderr, "MAPMultinomialGaussianPrior: final lambda: %f\n", lambda );
  190. return lambda;
  191. }
  192. void MAPMultinomialGaussianPrior::estimate (
  193. // refactor-nice.pl: check this substitution
  194. // old: Vector & theta,
  195. NICE::Vector & theta,
  196. // refactor-nice.pl: check this substitution
  197. // old: const Vector & counts,
  198. const NICE::Vector & counts,
  199. // refactor-nice.pl: check this substitution
  200. // old: const Vector & mu,
  201. const NICE::Vector & mu,
  202. double sigmaq )
  203. {
  204. theta = Vector(counts);
  205. normalizeProb ( theta );
  206. #ifdef DEBUG_OPTIMIZATION
  207. fprintf (stderr, "objective (non informative) : %f\n",
  208. objective ( theta, counts, mu, sigmaq ) );
  209. #endif
  210. double lambda = 1.0;
  211. if ( sigmaq < std::numeric_limits<double>::epsilon() )
  212. {
  213. fprintf (stderr, "MAPMultinomialGaussianPrior: skipping lambda optimization (sigmaq = 0.0)\n");
  214. } else {
  215. //lambda = estimateLambdaNonLinear ( counts, mu, sigmaq );
  216. lambda = estimateLambda ( counts, mu, sigmaq );
  217. }
  218. // calculate theta estimation
  219. double sum = 0.0;
  220. for ( size_t i = 0 ; i < mu.size() ; i++ )
  221. {
  222. double phalf = 0.5 * (mu[i] + lambda*sigmaq);
  223. theta[i] = phalf + sqrt( phalf*phalf + sigmaq * counts[i] );
  224. assert ( theta[i] >= 0.0 );
  225. sum += theta[i];
  226. }
  227. #ifdef DEBUG_OPTIMIZATION
  228. fprintf (stderr, "objective (informative prior) : %f\n",
  229. objective ( theta, counts, mu, sigmaq ) );
  230. fprintf (stderr, "Sum over theta = %f\n", sum );
  231. #endif
  232. }
  233. // refactor-nice.pl: check this substitution
  234. // old: void MAPMultinomialGaussianPrior::estimate ( Vector & mapEstimate,
  235. void MAPMultinomialGaussianPrior::estimate ( NICE::Vector & mapEstimate,
  236. const VVector & likelihoodDistributionSamples,
  237. const VVector & priorDistributionSamples,
  238. double sigmaq )
  239. {
  240. assert ( likelihoodDistributionSamples.size() == 1 );
  241. // refactor-nice.pl: check this substitution
  242. // old: const Vector & mlEstimate = likelihoodDistributionSamples[0];
  243. const NICE::Vector & mlEstimate = likelihoodDistributionSamples[0];
  244. // refactor-nice.pl: check this substitution
  245. // old: Vector mu;
  246. NICE::Vector mu;
  247. for ( VVector::const_iterator i = priorDistributionSamples.begin();
  248. i != priorDistributionSamples.end(); i++ )
  249. {
  250. // refactor-nice.pl: check this substitution
  251. // old: const Vector & x = *i;
  252. const NICE::Vector & x = *i;
  253. if ( mu.size() == 0 ) mu = x;
  254. else mu = mu + x;
  255. }
  256. mu = mu * (1.0/priorDistributionSamples.size());
  257. mapEstimate.resize(mlEstimate.size());
  258. mapEstimate.set(0);
  259. estimate ( mapEstimate, mlEstimate, mu, sigmaq );
  260. double sum = 0.0;
  261. for ( int i = 0 ; i < (int)mapEstimate.size() ; i++ )
  262. sum += mapEstimate[i];
  263. assert ( fabs(sum-1) < 10e-4 );
  264. }