package yi.iu.imgdb.learn.em;

import java.util.*;

/**
 * EMTestDataGenerator used to generate data according to Gaussian
 * Mixture Model.  The generated object (eg. image) contains several
 * components.  The probabilities of those components are predefined.  For
 * each component, a feature vector is generated using multi-variate Gaussian.
 */
public class EMTestDataGenerator {
  String[] m_objNames;
  double[] m_objProbs;
  double[][] m_means, m_variances;
  Random m_random;

  public EMTestDataGenerator(
      String[] obj_names, double[] obj_probs, double[][] means, double[][] variances, Random random) {
    m_objNames = obj_names;
    m_objProbs = obj_probs;
    m_means = means;
    m_variances = variances;
    m_random = random;
  }

  public EMTestData generate() {
    HashSet objects = new HashSet();
    LinkedList features = new LinkedList();
    // for each object
    for (int i=0; i<m_objNames.length; i++) {
      if (m_random.nextDouble() > m_objProbs[i]) continue;
      double[] feature = new double[m_means[i].length];
      for (int j=0; j<m_means[i].length; j++) {
        //feature[j] = yi.stat.ProbUtil.normal(m_means[i][j], m_variances[i][j]);
        feature[j] = m_random.nextGaussian() * m_variances[i][j] + m_means[i][j];
      }
      objects.add(m_objNames[i]);
      features.add(feature);
    }
    return new EMTestData(objects, features);
  }
}

