<commit_before>/**
 * 
 */
package blog.distrib;

import blog.common.numerical.MatrixFactory;
import blog.common.numerical.MatrixLib;

/**
 * Inverse Wishart distribution is the conjugate prior distribution for
 * Multivariate Gaussian distribution when the mean is fixed but the variance
 * is unknown. It has two parameters: A p by p square matrix illustrates the
 * scale and a integer mu > p - 1 illustrates the degrees of freedom.
 * 
 * @since July 14, 2014
 */
public class InverseWishart implements CondProbDistrib {

  /**
   * set parameters for Inverse_Wishart distribution
   * 
   * @param params
   *          an array of the form [MatrixLib, MatrixLib]
   *          <ul>
   *          <li>params[0]: <code>scale</code>, as a MatrixLib that serves as a
   *          scale matrix.</li>
   *          <li>params[1]: <code>freeDeg</code>, as a Integer that serves as a
   *          degree of freedom.</li>
   *          </ul>
   * 
   * @see blog.distrib.CondProbDistrib#setParams(java.util.List)
   */
  @Override
  public void setParams(Object[] params) {
    if (params.length != 2) {
      throw new IllegalArgumentException("expected two parameters");
    }
    setParams((MatrixLib) params[0], (Integer) params[1]);
  }

  /**
   * If the method scale parameter is not null and symmetric, then set the scale
   * parameter and the dimension parameter. If the degree parameter is not null
   * and positive, set the degree parameter. If the degree parameter is greater
   * than the dimension parameter - 1, initialize the constants.
   * 
   * @param scale
   *          parameter <code>scale</code>
   * @param freeDeg
   *          parameter <code>freeDeg</code>
   */
  public void setParams(MatrixLib scale, Integer freeDeg) {
    if (scale != null) {
      if (!(scale.numRows() > 0 && scale.isSymmetric())) {
        throw new IllegalArgumentException(
            "The scale given is not symmetric. It has dimensions "
                + scale.numRows() + " by " + scale.numCols() + ".");
      }
      this.scale = scale;
      this.hasScale = true;
    }
    if (freeDeg != null) {
      if (freeDeg <= 0) {
        throw new IllegalArgumentException(
            "The degrees of freedom given should be positive , but given "
                + freeDeg);
      }
      this.freeDeg = freeDeg;
      this.hasFreeDeg = true;
    }
    if (this.hasScale && this.hasFreeDeg) {
      if (freeDeg <= d - 1) {
        throw new IllegalArgumentException(
            "The freeDeg should be greater than the dimention of the scale matrix minus one, but given the freeDeg as "
                + this.freeDeg + " and the dimention as " + d + ".");
      }
      initializeConstants();
    }
  }

  /**
   * Initializes constants for the Inverse Wishart distribution.
   * Precondition: The current scale and degree parameters constitute a legal
   * assignment.
   */
  private void initializeConstants() {
    this.d = scale.numRows();
    this.dimFactor = 1 / (Math.pow(2, this.freeDeg * this.d * 0.5) * multivariategamma(
        this.d, this.freeDeg * 0.5));
    this.logDimFactor = -Math.log(2) * this.freeDeg * this.d * 0.5
        - lgmultivariategamma(this.d, this.freeDeg * 0.5);

    this.normConst = Math.pow(scale.det(), this.freeDeg * 0.5) * dimFactor;
    this.logNormConst = 0.5 * scale.logDet() * this.freeDeg + logDimFactor;
  }

  public static double multivariategamma(int p, double x) {
    return Math.exp(lgmultivariategamma(p, x));
  }

  public static double lgmultivariategamma(int p, double x) {
    double ret = Math.log(Math.PI) * p * (p - 1) * 0.25;
    for (int i = 1; i <= p; i++) {
      ret += Gamma.lgamma(x + (1 - i) * 0.5);
    }
    return ret;
  }<commit_msg>minor modifications<commit_after>/**
 * 
 */
package blog.distrib;

import blog.common.numerical.MatrixFactory;
import blog.common.numerical.MatrixLib;

/**
 * Inverse Wishart distribution is the conjugate prior distribution for
 * Multivariate Gaussian distribution when the mean is fixed but the variance
 * is unknown. It has two parameters: A p by p square matrix illustrates the
 * scale and a integer mu > p - 1 illustrates the degrees of freedom.
 * 
 * @author <NAME>
 * @since July 14, 2014
 */
public class InverseWishart implements CondProbDistrib {

  /**
   * set parameters for Inverse_Wishart distribution
   * 
   * @param params
   *          an array of the form [MatrixLib, MatrixLib]
   *          <ul>
   *          <li>params[0]: <code>scale</code>, as a MatrixLib that serves as a
   *          scale matrix.</li>
   *          <li>params[1]: <code>freeDeg</code>, as a Integer that serves as a
   *          degree of freedom.</li>
   *          </ul>
   * 
   * @see blog.distrib.CondProbDistrib#setParams(java.util.List)
   */
  @Override
  public void setParams(Object[] params) {
    if (params.length != 2) {
      throw new IllegalArgumentException("expected two parameters");
    }
    setParams((MatrixLib) params[0], (Integer) params[1]);
  }

  /**
   * If the method scale parameter is not null and symmetric, then set the scale
   * parameter and the dimension parameter. If the degree parameter is not null
   * and positive, set the degree parameter. If the degree parameter is greater
   * than the dimension parameter - 1, initialize the constants.
   * 
   * @param scale
   *          parameter <code>scale</code>
   * @param freeDeg
   *          parameter <code>freeDeg</code>
   */
  public void setParams(MatrixLib scale, Integer freeDeg) {
    if (scale != null) {
      if (!(scale.numRows() > 0 && scale.isSymmetric())) {
        throw new IllegalArgumentException(
            "The scale given is not symmetric. It has dimensions "
                + scale.numRows() + " by " + scale.numCols() + ".");
      }
      this.scale = scale;
      this.hasScale = true;
    }
    if (freeDeg != null) {
      if (freeDeg <= 0) {
        throw new IllegalArgumentException(
            "The degrees of freedom given should be positive , but given "
                + freeDeg);
      }
      this.freeDeg = freeDeg;
      this.hasFreeDeg = true;
    }
    if (this.hasScale && this.hasFreeDeg) {
      if (freeDeg <= d - 1) {
        throw new IllegalArgumentException(
            "The freeDeg should be greater than the dimention of the scale matrix minus one, but given the freeDeg as "
                + this.freeDeg + " and the dimention as " + d + ".");
      }
      initializeConstants();
    }
  }

  /**
   * Initializes constants for the Inverse Wishart distribution.
   * Precondition: The current scale and degree parameters constitute a legal
   * assignment.
   */
  private void initializeConstants() {
    this.d = scale.numRows();
    this.dimFactor = 1 / (Math.pow(2, this.freeDeg * this.d * 0.5) * multivariategamma(
        this.d, this.freeDeg * 0.5));
    this.logDimFactor = -Math.log(2) * this.freeDeg * this.d * 0.5
        - lgmultivariategamma(this.d, this.freeDeg * 0.5);

    this.normConst = Math.pow(scale.det(), this.freeDeg * 0.5) * dimFactor;
    this.logNormConst = 0.5 * scale.logDet() * this.freeDeg + logDimFactor;
  }

  public static double multivariategamma(int p, double x) {
    return Math.exp(lgmultivariategamma(p, x));
  }

  private static double lgmultivariategamma(int p, double x) {
    double ret = Math.log(Math.PI) * p * (p - 1) * 0.25;
    for (int i = 1; i <= p; i++) {
      ret += Gamma.lgamma(x + (1 - i) * 0.5);
    }
    return ret;
  }
{'fraction_non_alphanumeric': 0.11404803434858446, 'fraction_numerical': 0.010465584328458339, 'mean_word_length': 2.473438956197577, 'pattern_counts': {'":': 0, '<': 38, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2971605', 'n_tokens_mistral': 2354, 'n_tokens_neox': 2207, 'n_words': 713}