package yi.stat.em;

/**
 * For zero variance on some dimension, simply use a fixed constant to overwrite
 * zero.
 */

public class SingularGaussianModelConstantFixer implements SingularGaussianModelFixable, java.io.Serializable {

  public double SMALL_VARIANCE = 1e-40;
  /**
   * The fake covariance matrix entry (only along the diagonal of the matrix)
   */
  double[] m_fakeCov;

  /**
   * Construct with the specified fake covariance matrix entry
   * @param fake_cov the specified fake covariance matrix entry (only along the
   *                 diagonal of the matrix)
   */
  public SingularGaussianModelConstantFixer(double[] fake_cov) {
    m_fakeCov = fake_cov;
  }

  /**
   * Fix the covariance matrix use the fake data.
   * @param covariance_matrix the covarince matrix to be fixed
   * @param data the data from where the covarince matrix was calculated
   * @param data_num the number of the data
   * @param data_dim the data dimension
   * @return whether fix is successful.
   */
  public boolean fix(double[][] covariance_matrix, double[][] data, double weight) {
    if (weight < covariance_matrix.length) return false;
    for (int i= 0; i< covariance_matrix.length; i++) {
      if (covariance_matrix[i][i] <= SMALL_VARIANCE) {
        covariance_matrix[i][i] = SMALL_VARIANCE;
      }
    }
    return true;
  }
}