/**
   * Constructor. The Gaussian mixture model will be learned from the given data with the EM
   * algorithm.
   *
   * @param data the training data.
   * @param k the number of components.
   */
  public GaussianMixture(double[] data, int k) {
    if (k < 2) throw new IllegalArgumentException("Invalid number of components in the mixture.");

    double min = Math.min(data);
    double max = Math.max(data);
    double step = (max - min) / (k + 1);

    for (int i = 0; i < k; i++) {
      Component c = new Component();
      c.priori = 1.0 / k;
      c.distribution = new GaussianDistribution(min += step, step);
      components.add(c);
    }

    EM(components, data);
  }