Esempio n. 1
0
void main(void)
{
  hp1();
  hp2();
  hp3();
  hp4();
}
/* Returns the normalized MI scores.
 *  
 * Parameters
 *   dx (IN) : x-data sorted in increasing order by dx-values
 *   dy (IN) : y-data sorted in increasing order by dx-values
 *   n (IN) : number of elements in dx and dy
 *   Q_map (IN) : the map Q computed by EquipartitionYAxis() sorted in 
 *                increasing order by dx-values
 *   q (IN) : number of partitions in Q_map
 *   P_map (IN) : the map P computed by GetSuperclumpsPartition() sorted 
 *                in increasing order by Dx-values
 *   p (IN) : number of partitions in P_map
 *   x (IN) : maximum grid size on dx-values
 *   score (OUT) : mutual information scores. score must be a 
 *                 preallocated array of dimension x-1
 * Returns
 *   0 on success, 1 if an error occurs
 */
int OptimizeXAxis(double *dx, double *dy, int n, int *Q_map, int q, 
		  int *P_map, int p, int x, double *score)
{
  int i, s, t, l;
  int *c;
  int **cumhist;
  double **I, **HP2Q;
  double F, F_max, HQ, ct, cs;
  
  
  /* return score=0 if p=1 */
  if (p == 1)
    {
      for (i=0; i<x-1; i++)
	score[i] = 0.0;
      return 0;
    }
 
  /* compute c */
  c = compute_c(P_map, p, n);
  if (c == NULL)
    goto error_c;
  
  /* compute the cumulative histogram matrix along P_map */
  cumhist = compute_cumhist(Q_map, q, P_map, p, n);
  if (cumhist == NULL)
    goto error_cumhist;

  /* I matrix initialization */
  I = init_I(p, x);
  if (I == NULL)
    goto error_I;

  /* Precomputes the HP2Q matrix */
  HP2Q = compute_HP2Q(cumhist, c, q, p);
  if (HP2Q == NULL)
    goto error_HP2Q;

  /* compute H(Q) */
  HQ = hq(cumhist, q, p, n);

  /* 
   * Find the optimal partitions of size 2
   * Algorithm 2 in SOM, lines 3-8
   */
  for (t=2; t<=p; t++)
    {
      F_max = -DBL_MAX;
      for (s=1; s<=t; s++)
	{
	  F = hp3(c, s, t) - hp3q(cumhist, c, q, p, s, t);
	  if (F > F_max)
	    {
	      I[t][2] = HQ + F;
	      F_max = F;
	    }
	}
    }
  
  /* 
   * Inductively build the rest of the table of
   * optimal partitions
   * Algorithm 2 in SOM, lines 10-17
   */
  for (l=3; l<=x; l++)
    {
      for (t=l; t<=p; t++)
	{
	  ct = (double) c[t-1];
	  F_max = -DBL_MAX;
	  for (s=l-1; s<=t; s++)
	    {
	      cs = (double) c[s-1];
	      F = ((cs/ct) * (I[s][l-1]-HQ))
		- (((ct-cs)/ct) * HP2Q[s][t]);
	      
	      if (F > F_max)
		{
		  I[t][l] = HQ + F;
		  F_max = F;
		}
	    }
	}
    }
  
  /* Algorithm 2 in SOM, line 19 */
  for (i=p+1; i<=x; i++)
    I[p][i] = I[p][p];
  
  /* score */
  for (i=2; i<=x; i++)
    score[i-2] = I[p][i] / MIN(log(i), log(q));
 
  /* start frees */
  for (i=0; i<=p; i++)
    free(HP2Q[i]);
  free(HP2Q);

  for (i=0; i<=p; i++)
    free(I[i]);
  free(I);
  
  for (i=0; i<q; i++)
    free(cumhist[i]);
  free(cumhist);
  
  free (c);
  /* end frees*/

  return 0;
  
  /* gotos*/
 error_HP2Q:
  for (i=0; i<=p; i++)
    free(I[i]);
  free(I);
 error_I:
  for (i=0; i<q; i++)
    free(cumhist[i]);
  free(cumhist);
 error_cumhist:
  free(c);
 error_c:
  return 1;
}