You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

643 lines
21 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /** @file aib.c
  2. ** @brief AIB - Definition
  3. ** @author Brian Fulkerson
  4. ** @author Andrea Vedaldi
  5. **/
  6. /*
  7. Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
  8. All rights reserved.
  9. This file is part of the VLFeat library and is made available under
  10. the terms of the BSD license (see the COPYING file).
  11. */
  12. /**
  13. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  14. @page aib Agglomerative Information Bottleneck (AIB)
  15. @author Brian Fulkerson
  16. @author Andrea Vedaldi
  17. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  18. @ref aib.h implemens the Agglomerative Information Bottleneck (AIB)
  19. algorithm as first described in @cite{slonim99agglomerative}.
  20. AIB takes a discrete valued feature @f$x@f$ and a label @f$c@f$ and
  21. gradually compresses @f$x@f$ by iteratively merging values which
  22. minimize the loss in mutual information @f$I(x,c)@f$.
  23. While the algorithm is equivalent to the one described in
  24. @cite{slonim99agglomerative}, it has some speedups that enable
  25. handling much larger datasets. Let <em>N</em> be the number of feature
  26. values and <em>C</em> the number of labels. The algorithm of
  27. @cite{slonim99agglomerative} is @f$O(N^2)@f$ in space and @f$O(C
  28. N^3)@f$ in time. This algorithm is @f$O(N)@f$ space and @f$O(C N^2)@f$
  29. time in common cases (@f$O(C N^3)@f$ in the worst case).
  30. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  31. @section aib-overview Overview
  32. <!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
  33. Given a discrete feature @f$x \in \mathcal{X} = \{x_1,\dots,x_N\}@f$
  34. and a category label @f$c = 1,\dots,C@f$ with joint probability
  35. @f$p(x,c)@f$, AIB computes a compressed feature @f$[x]_{ij}@f$ by
  36. merging two values @f$x_i@f$ and @f$x_j@f$. Among all the pairs
  37. @f$ij@f$, AIB chooses the one that yields the smallest loss in the
  38. mutual information
  39. @f[
  40. D_{ij} = I(x,c) - I([x]_{ij},c) =
  41. \sum_c p(x_i) \log \frac{p(x_i,c)}{p(x_i)p(c)} +
  42. \sum_c p(x_i) \log \frac{p(x_i,c)}{p(x_i)p(c)} -
  43. \sum_c (p(x_i)+p(x_j)) \log \frac {p(x_i,c)+p(x_i,c)}{(p(x_i)+p(x_j))p(c)}
  44. @f]
  45. AIB iterates this procedure until the desired level of
  46. compression is achieved.
  47. @section aib-algorithm Algorithm details
  48. Computing @f$D_{ij}@f$ requires @f$O(C)@f$ operations. For example, in
  49. standard AIB we need to calculate
  50. @f[
  51. D_{ij} = I(x,c) - I([x]_{ij},c) =
  52. \sum_c p(x_i) \log \frac{p(x_i,c)}{p(x_i)p(c)} +
  53. \sum_c p(x_i) \log \frac{p(x_i,c)}{p(x_i)p(c)} -
  54. \sum_c (p(x_i)+p(x_j)) \log \frac {p(x_i,c)+p(x_i,c)}{(p(x_i)+p(x_j))p(c)}
  55. @f]
  56. Thus in a basic implementation of AIB, finding the optimal pair
  57. @f$ij@f$ of feature values requires @f$O(CN^2)@f$ operations in
  58. total. In order to join all the @f$N@f$ values, we repeat this
  59. procedure @f$O(N)@f$ times, yielding @f$O(N^3 C)@f$ time and
  60. @f$O(1)@f$ space complexity (this does not account for the space need
  61. to store the input).
  62. The complexity can be improved by reusing computations. For instance,
  63. we can store the matrix @f$D = [ D_{ij} ]@f$ (which requires
  64. @f$O(N^2)@f$ space). Then, after joining @f$ij@f$, all of the matrix
  65. <em>D</em> except the rows and columns (the matrix is symmetric) of
  66. indexes <em>i</em> and <em>j</em> is unchanged. These two rows and
  67. columns are deleted and a new row and column, whose computation
  68. requires @f$O(NC)@f$ operations, are added for the merged value
  69. @f$x_{ij}@f$. Finding the minimal element of the matrix still
  70. requires @f$O(N^2)@f$ operations, so the complexity of this algorithm
  71. is @f$O(N^2C + N^3)@f$ time and @f$O(N^2)@f$ space.
  72. We can obtain a much better expected complexity as follows. First,
  73. instead of storing the whole matrix <em>D</em>, we store the smallest
  74. element (index and value) of each row as @f$(q_i, D_i)@f$ (notice that
  75. this is also the best element of each column since <em>D</em> is
  76. symmetric). This requires @f$O(N)@f$ space and finding the minimal
  77. element of the matrix requires @f$O(N)@f$ operations. After joining
  78. @f$ij@f$, we have to efficiently update this representation. This is
  79. done as follows:
  80. - The entries @f$(q_i,D_i)@f$ and @f$(q_j,D_j)@f$ are deleted.
  81. - A new entry @f$(q_{ij},D_{ij})@f$ for the joint value @f$x_{ij}@f$
  82. is added. This requires @f$O(CN)@f$ operations.
  83. - We test which other entries @f$(q_{k},D_{k})@f$ need to
  84. be updated. Recall that @f$(q_{k},D_{k})@f$ means that, before the
  85. merge, the value
  86. closest to @f$x_k@f$ was @f$x_{q_k}@f$ at a distance @f$D_k@f$. Then
  87. - If @f$q_k \not = i@f$, @f$q_k \not = j@f$ and @f$D_{k,ij} \geq D_k@f$, then
  88. @f$q_k@f$ is still the closest element and we do not do anything.
  89. - If @f$q_k \not = i@f$, @f$q_k \not = j@f$ and @f$D_{k,ij} <
  90. D_k@f$, then the closest element is @f$ij@f$ and we update the
  91. entry in constant time.
  92. - If @f$q_k = i@f$ or @f$q_k = j@f$, then we need to re-compute
  93. the closest element in @f$O(CN)@f$ operations.
  94. This algorithm requires only @f$O(N)@f$ space and @f$O(\gamma(N) C
  95. N^2)@f$ time, where @f$\gamma(N)@f$ is the expected number of times we
  96. fall in the last case. In common cases one has @f$\gamma(N) \approx
  97. \mathrm{const.}@f$, so the time saving is significant.
  98. **/
  99. #include "aib.h"
  100. #include <stdio.h>
  101. #include <stdlib.h>
  102. #include <float.h>
  103. #include <math.h>
  104. /* The maximum value which beta may take */
  105. #define BETA_MAX DBL_MAX
  106. /** ------------------------------------------------------------------
  107. ** @internal
  108. ** @brief Normalizes an array of probabilities to sum to 1
  109. **
  110. ** @param P The array of probabilities
  111. ** @param nelem The number of elements in the array
  112. **
  113. ** @return Modifies P to contain values which sum to 1
  114. **/
  115. void vl_aib_normalize_P (double * P, vl_uint nelem)
  116. {
  117. vl_uint i;
  118. double sum = 0;
  119. for(i=0; i<nelem; i++)
  120. sum += P[i];
  121. for(i=0; i<nelem; i++)
  122. P[i] /= sum;
  123. }
  124. /** ------------------------------------------------------------------
  125. ** @internal
  126. ** @brief Allocates and creates a list of nodes
  127. **
  128. ** @param nentries The size of the list which will be created
  129. **
  130. ** @return an array containing elements 0...nentries
  131. **/
  132. vl_uint *vl_aib_new_nodelist (vl_uint nentries)
  133. {
  134. vl_uint * nodelist = (vl_uint *) vl_malloc(sizeof(vl_uint)*nentries);
  135. vl_uint n;
  136. for(n=0; n<nentries; n++)
  137. nodelist[n] = n;
  138. return nodelist;
  139. }
  140. /** ------------------------------------------------------------------
  141. ** @internal
  142. ** @brief Allocates and creates the marginal distribution Px
  143. **
  144. ** @param Pcx A two-dimensional array of probabilities
  145. ** @param nvalues The number of rows in Pcx
  146. ** @param nlabels The number of columns in Pcx
  147. **
  148. ** @return an array of size @a nvalues which contains the marginal
  149. ** distribution over the rows.
  150. **/
  151. double * vl_aib_new_Px(double * Pcx, vl_uint nvalues, vl_uint nlabels)
  152. {
  153. double * Px = (double *) vl_malloc(sizeof(double)*nvalues);
  154. vl_uint r,c;
  155. for(r=0; r<nvalues; r++)
  156. {
  157. double sum = 0;
  158. for(c=0; c<nlabels; c++)
  159. sum += Pcx[r*nlabels+c];
  160. Px[r] = sum;
  161. }
  162. return Px;
  163. }
  164. /** ------------------------------------------------------------------
  165. ** @internal @brief Allocates and creates the marginal distribution Pc
  166. **
  167. ** @param Pcx A two-dimensional array of probabilities
  168. ** @param nvalues The number of rows in Pcx
  169. ** @param nlabels The number of columns in Pcx
  170. **
  171. ** @return an array of size @a nlabels which contains the marginal distribution
  172. ** over the columns
  173. **/
  174. double * vl_aib_new_Pc(double * Pcx, vl_uint nvalues, vl_uint nlabels)
  175. {
  176. double * Pc = (double *) vl_malloc(sizeof(double)*nlabels);
  177. vl_uint r, c;
  178. for(c=0; c<nlabels; c++)
  179. {
  180. double sum = 0;
  181. for(r=0; r<nvalues; r++)
  182. sum += Pcx[r*nlabels+c];
  183. Pc[c] = sum;
  184. }
  185. return Pc;
  186. }
  187. /** ------------------------------------------------------------------
  188. ** @internal @brief Find the two nodes which have minimum beta.
  189. **
  190. ** @param aib A pointer to the internal data structure
  191. ** @param besti The index of one member of the pair which has mininum beta
  192. ** @param bestj The index of the other member of the pair which
  193. ** minimizes beta
  194. ** @param minbeta The minimum beta value corresponding to (@a i, @a j)
  195. **
  196. ** Searches @a aib->beta to find the minimum value and fills @a minbeta and
  197. ** @a besti and @a bestj with this information.
  198. **/
  199. void vl_aib_min_beta
  200. (VlAIB * aib, vl_uint * besti, vl_uint * bestj, double * minbeta)
  201. {
  202. vl_uint i;
  203. *minbeta = aib->beta[0];
  204. *besti = 0;
  205. *bestj = aib->bidx[0];
  206. for(i=0; i<aib->nentries; i++)
  207. {
  208. if(aib->beta[i] < *minbeta)
  209. {
  210. *minbeta = aib->beta[i];
  211. *besti = i;
  212. *bestj = aib->bidx[i];
  213. }
  214. }
  215. }
  216. /** ------------------------------------------------------------------
  217. ** @internal
  218. ** @brief Merges two nodes i,j in the internal datastructure
  219. **
  220. ** @param aib A pointer to the internal data structure
  221. ** @param i The index of one member of the pair to merge
  222. ** @param j The index of the other member of the pair to merge
  223. ** @param new The index of the new node which corresponds to the union of
  224. ** (@a i, @a j).
  225. **
  226. ** Nodes are merged by replacing the entry @a i with the union of @c
  227. ** ij, moving the node stored in last position (called @c lastnode)
  228. ** back to jth position and the entry at the end.
  229. **
  230. ** After the nodes have been merged, it updates which nodes should be
  231. ** considered on the next iteration based on which beta values could
  232. ** potentially change. The merged node will always be part of this
  233. ** list.
  234. **/
  235. void
  236. vl_aib_merge_nodes (VlAIB * aib, vl_uint i, vl_uint j, vl_uint nw)
  237. {
  238. vl_uint last_entry = aib->nentries - 1 ;
  239. vl_uint c, n ;
  240. /* clear the list of nodes to update */
  241. aib->nwhich = 0;
  242. /* make sure that i is smaller than j */
  243. if(i > j) { vl_uint tmp = j; j = i; i = tmp; }
  244. /* -----------------------------------------------------------------
  245. * Merge entries i and j, storing the result in i
  246. * -------------------------------------------------------------- */
  247. aib-> Px [i] += aib->Px[j] ;
  248. aib-> beta [i] = BETA_MAX ;
  249. aib-> nodes[i] = nw ;
  250. for (c = 0; c < aib->nlabels; c++)
  251. aib-> Pcx [i*aib->nlabels + c] += aib-> Pcx [j*aib->nlabels + c] ;
  252. /* -----------------------------------------------------------------
  253. * Move last entry to j
  254. * -------------------------------------------------------------- */
  255. aib-> Px [j] = aib-> Px [last_entry];
  256. aib-> beta [j] = aib-> beta [last_entry];
  257. aib-> bidx [j] = aib-> bidx [last_entry];
  258. aib-> nodes [j] = aib-> nodes [last_entry];
  259. for (c = 0 ; c < aib->nlabels ; c++)
  260. aib-> Pcx[j*aib->nlabels + c] = aib-> Pcx [last_entry*aib->nlabels + c] ;
  261. /* delete last entry */
  262. aib-> nentries -- ;
  263. /* -----------------------------------------------------------------
  264. * Scan for entries to update
  265. * -------------------------------------------------------------- */
  266. /*
  267. * After mergin entries i and j, we need to update all other entries
  268. * that had one of these two as closest match. We also need to
  269. * update the renewend entry i. This is added by the loop below
  270. * since bidx [i] = j exactly because i was merged.
  271. *
  272. * Additionaly, since we moved the last entry back to the entry j,
  273. * we need to adjust the valeus of bidx to reflect this.
  274. */
  275. for (n = 0 ; n < aib->nentries; n++) {
  276. if(aib->bidx[n] == i || aib->bidx[n] == j) {
  277. aib->bidx [n] = 0;
  278. aib->beta [n] = BETA_MAX;
  279. aib->which [aib->nwhich++] = n ;
  280. }
  281. else if(aib->bidx[n] == last_entry) {
  282. aib->bidx[n] = j ;
  283. }
  284. }
  285. }
  286. /** ------------------------------------------------------------------
  287. ** @internal
  288. ** @brief Updates @c aib->beta and @c aib->bidx according to @c aib->which
  289. **
  290. ** @param aib AIB data structure.
  291. **
  292. ** The function calculates @c beta[i] and @c bidx[i] for the nodes @c
  293. ** i listed in @c aib->which. @c beta[i] is the minimal variation of mutual
  294. ** information (or other score) caused by merging entry @c i with another entry
  295. ** and @c bidx[i] is the index of this best matching entry.
  296. **
  297. ** Notice that for each entry @c i that we need to update, a full
  298. ** scan of all the other entries must be performed.
  299. **/
  300. void
  301. vl_aib_update_beta (VlAIB * aib)
  302. {
  303. #define PLOGP(x) ((x)*log((x)))
  304. vl_uint i;
  305. double * Px = aib->Px;
  306. double * Pcx = aib->Pcx;
  307. double * tmp = (double *) vl_malloc(sizeof(double)*aib->nentries);
  308. vl_uint a, b, c ;
  309. /*
  310. * T1 = I(x,c) - I([x]_ij) = A + B - C
  311. *
  312. * A = \sum_c p(xa,c) \log ( p(xa,c) / p(xa) )
  313. * B = \sum_c p(xb,c) \log ( p(xb,c) / p(xb) )
  314. * C = \sum_c (p(xa,c)+p(xb,c)) \log ((p(xa,c)+p(xb,c)) / (p(xa)+p(xb)))
  315. *
  316. * C = C1 + C2
  317. * C1 = \sum_c (p(xa,c)+p(xb,c)) \log (p(xa,c)+p(xb,c))
  318. * C2 = - (p(xa)+p(xb) \log (p(xa)+p(xb))
  319. */
  320. /* precalculate A and B */
  321. for (a = 0; a < aib->nentries; a++) {
  322. tmp[a] = 0;
  323. for (c = 0; c < aib->nlabels; c++) {
  324. double Pac = Pcx [a*aib->nlabels + c] ;
  325. if(Pac != 0) tmp[a] += Pac * log (Pac / Px[a]) ;
  326. }
  327. }
  328. /* for each entry listed in which */
  329. for (i = 0 ; i < aib->nwhich; i++) {
  330. a = aib->which[i];
  331. /* for each other entry */
  332. for(b = 0 ; b < aib->nentries ; b++) {
  333. double T1 = 0 ;
  334. if (a == b || Px [a] == 0 || Px [b] == 0) continue ;
  335. T1 = PLOGP ((Px[a] + Px[b])) ; /* - C2 */
  336. T1 += tmp[a] + tmp[b] ; /* + A + B */
  337. for (c = 0 ; c < aib->nlabels; ++ c) {
  338. double Pac = Pcx [a*aib->nlabels + c] ;
  339. double Pbc = Pcx [b*aib->nlabels + c] ;
  340. if (Pac == 0 && Pbc == 0) continue;
  341. T1 += - PLOGP ((Pac + Pbc)) ; /* - C1 */
  342. }
  343. /*
  344. * Now we have beta(a,b). We check wether this is the best beta
  345. * for entries a and b.
  346. */
  347. {
  348. double beta = T1 ;
  349. if (beta < aib->beta[a])
  350. {
  351. aib->beta[a] = beta;
  352. aib->bidx[a] = b;
  353. }
  354. if (beta < aib->beta[b])
  355. {
  356. aib->beta[b] = beta;
  357. aib->bidx[b] = a;
  358. }
  359. }
  360. }
  361. }
  362. vl_free(tmp);
  363. }
  364. /** ------------------------------------------------------------------
  365. ** @internal @brief Calculates the current information and entropy
  366. **
  367. ** @param aib A pointer to the internal data structure
  368. ** @param I The current mutual information (out).
  369. ** @param H The current entropy (out).
  370. **
  371. ** Calculates the current mutual information and entropy of Pcx and sets
  372. ** @a I and @a H to these new values.
  373. **/
  374. void vl_aib_calculate_information(VlAIB * aib, double * I, double * H)
  375. {
  376. vl_uint r, c;
  377. *H = 0;
  378. *I = 0;
  379. /*
  380. * H(x) = - sum_x p(x) \ log p(x)
  381. * I(x,c) = sum_xc p(x,c) \ log (p(x,c) / p(x)p(c))
  382. */
  383. /* for each entry */
  384. for(r = 0 ; r< aib->nentries ; r++) {
  385. if (aib->Px[r] == 0) continue ;
  386. *H += -log(aib->Px[r]) * aib->Px[r] ;
  387. for(c=0; c<aib->nlabels; c++) {
  388. if (aib->Pcx[r*aib->nlabels+c] == 0) continue;
  389. *I += aib->Pcx[r*aib->nlabels+c] *
  390. log (aib->Pcx[r*aib->nlabels+c] / (aib->Px[r]*aib->Pc[c])) ;
  391. }
  392. }
  393. }
  394. /** ------------------------------------------------------------------
  395. ** @brief Allocates and initializes the internal data structure
  396. **
  397. ** @param Pcx A pointer to a 2D array of probabilities
  398. ** @param nvalues The number of rows in the array
  399. ** @param nlabels The number of columns in the array
  400. **
  401. ** Creates a new @a VlAIB struct containing pointers to all the data that
  402. ** will be used during the AIB process.
  403. **
  404. ** Allocates memory for the following:
  405. ** - Px (nvalues*sizeof(double))
  406. ** - Pc (nlabels*sizeof(double))
  407. ** - nodelist (nvalues*sizeof(vl_uint))
  408. ** - which (nvalues*sizeof(vl_uint))
  409. ** - beta (nvalues*sizeof(double))
  410. ** - bidx (nvalues*sizeof(vl_uint))
  411. ** - parents ((2*nvalues-1)*sizeof(vl_uint))
  412. ** - costs (nvalues*sizeof(double))
  413. **
  414. ** Since it simply copies to pointer to Pcx, the total additional memory
  415. ** requirement is:
  416. **
  417. ** (3*nvalues+nlabels)*sizeof(double) + 4*nvalues*sizeof(vl_uint)
  418. **
  419. ** @returns An allocated and initialized @a VlAIB pointer
  420. **/
  421. VlAIB * vl_aib_new(double * Pcx, vl_uint nvalues, vl_uint nlabels)
  422. {
  423. VlAIB * aib = (VlAIB *) vl_malloc(sizeof(VlAIB));
  424. vl_uint i ;
  425. aib->verbosity = 0 ;
  426. aib->Pcx = Pcx ;
  427. aib->nvalues = nvalues ;
  428. aib->nlabels = nlabels ;
  429. vl_aib_normalize_P (aib->Pcx, aib->nvalues * aib->nlabels) ;
  430. aib->Px = vl_aib_new_Px (aib->Pcx, aib->nvalues, aib->nlabels) ;
  431. aib->Pc = vl_aib_new_Pc (aib->Pcx, aib->nvalues, aib->nlabels) ;
  432. aib->nentries = aib->nvalues ;
  433. aib->nodes = vl_aib_new_nodelist(aib->nentries) ;
  434. aib->beta = (double*) vl_malloc(sizeof(double) * aib->nentries) ;
  435. aib->bidx = (vl_uint*) vl_malloc(sizeof(vl_uint) * aib->nentries) ;
  436. for(i = 0 ; i < aib->nentries ; i++)
  437. aib->beta [i] = BETA_MAX ;
  438. /* Initially we must consider all nodes */
  439. aib->nwhich = aib->nvalues;
  440. aib->which = vl_aib_new_nodelist (aib->nwhich) ;
  441. aib->parents = (vl_uint*) vl_malloc(sizeof(vl_uint)*(aib->nvalues*2-1));
  442. /* Initially, all parents point to a nonexistent node */
  443. for (i = 0 ; i < 2 * aib->nvalues - 1 ; i++)
  444. aib->parents [i] = 2 * aib->nvalues ;
  445. /* Allocate cost output vector */
  446. aib->costs = (double*) vl_malloc (sizeof(double) * (aib->nvalues - 1 + 1)) ;
  447. return aib ;
  448. }
  449. /** ------------------------------------------------------------------
  450. ** @brief Deletes AIB data structure
  451. ** @param aib data structure to delete.
  452. **/
  453. void
  454. vl_aib_delete (VlAIB * aib)
  455. {
  456. if (aib) {
  457. if (aib-> nodes) vl_free (aib-> nodes);
  458. if (aib-> beta) vl_free (aib-> beta);
  459. if (aib-> bidx) vl_free (aib-> bidx);
  460. if (aib-> which) vl_free (aib-> which);
  461. if (aib-> Px) vl_free (aib-> Px);
  462. if (aib-> Pc) vl_free (aib-> Pc);
  463. if (aib-> parents) vl_free (aib-> parents);
  464. if (aib-> costs) vl_free (aib-> costs);
  465. vl_free (aib) ;
  466. }
  467. }
  468. /** ------------------------------------------------------------------
  469. ** @brief Runs AIB on Pcx
  470. **
  471. ** @param aib AIB object to process
  472. **
  473. ** The function runs Agglomerative Information Bottleneck (AIB) on
  474. ** the joint probability table @a aib->Pcx which has labels along the
  475. ** columns and feature values along the rows. AIB iteratively merges
  476. ** the two values of the feature @c x that causes the smallest
  477. ** decrease in mutual information between the random variables @c x
  478. ** and @c c.
  479. **
  480. ** Merge operations are arranged in a binary tree. The nodes of the
  481. ** tree correspond to the original feature values and any other value
  482. ** obtained as a result of a merge operation. The nodes are indexed
  483. ** in breadth-first order, starting from the leaves. The first index
  484. ** is zero. In this way, the leaves correspond directly to the
  485. ** original feature values. In total there are @c 2*nvalues-1 nodes.
  486. **
  487. ** The results may be accessed through vl_aib_get_parents which
  488. ** returns an array with one element per tree node. Each
  489. ** element is the index the parent node. The root parent is equal to
  490. ** zero. The array has @c 2*nvalues-1 elements.
  491. **
  492. ** Feature values with null probability are ignored by the algorithm
  493. ** and their nodes have parents indexing a non-existent tree node (a
  494. ** value bigger than @c 2*nvalues-1).
  495. **
  496. ** Then the function will also compute the information level after each
  497. ** merge. vl_get_costs will return a vector with the information level
  498. ** after each merge. @a
  499. ** cost has @c nvalues entries: The first is the value of the cost
  500. ** functional before any merge, and the others are the cost after the
  501. ** @c nvalues-1 merges.
  502. **
  503. **/
  504. VL_EXPORT
  505. void vl_aib_process(VlAIB *aib)
  506. {
  507. vl_uint i, besti, bestj, newnode, nodei, nodej;
  508. double I, H;
  509. double minbeta;
  510. /* Calculate initial value of cost function */
  511. vl_aib_calculate_information (aib, &I, &H) ;
  512. aib->costs[0] = I;
  513. /* Initially which = all */
  514. /* For each merge */
  515. for(i = 0 ; i < aib->nvalues - 1 ; i++) {
  516. /* update entries in aib-> which */
  517. vl_aib_update_beta(aib);
  518. /* find best pair of nodes to merge */
  519. vl_aib_min_beta (aib, &besti, &bestj, &minbeta);
  520. if(minbeta == BETA_MAX)
  521. /* only null-probability entries remain */
  522. break;
  523. /* Add the parent pointers for the new node */
  524. newnode = aib->nvalues + i ;
  525. nodei = aib->nodes[besti];
  526. nodej = aib->nodes[bestj];
  527. aib->parents [nodei] = newnode ;
  528. aib->parents [nodej] = newnode ;
  529. aib->parents [newnode] = 0 ;
  530. /* Merge the nodes which produced the minimum beta */
  531. vl_aib_merge_nodes (aib, besti, bestj, newnode) ;
  532. vl_aib_calculate_information (aib, &I, &H) ;
  533. aib->costs[i+1] = I;
  534. if (aib->verbosity > 0) {
  535. VL_PRINTF ("aib: (%5d,%5d)=%5d dE: %10.3g I: %6.4g H: %6.4g updt: %5d\n",
  536. nodei,
  537. nodej,
  538. newnode,
  539. minbeta,
  540. I,
  541. H,
  542. aib->nwhich) ;
  543. }
  544. }
  545. /* fill ignored entries with NaNs */
  546. for(; i < aib->nvalues - 1 ; i++)
  547. aib->costs[i+1] = VL_NAN_D ;
  548. }