Example usage for org.apache.commons.math3.special Gamma digamma

List of usage examples for org.apache.commons.math3.special Gamma digamma

Introduction

In this page you can find the example usage for org.apache.commons.math3.special Gamma digamma.

Prototype

public static double digamma(double x) 

Source Link

Document

Computes the digamma function of x.

This is an independently written implementation of the algorithm described in Jose Bernardo, Algorithm AS 103: Psi (Digamma) Function, Applied Statistics, 1976.

Some of the constants have been changed to increase accuracy at the moderate expense of run-time.

Usage

From source file:edu.byu.nlp.stats.SymmetricDirichletMultinomialMatrixMAPOptimizable.java

private static double computeNumerator(double[][] data, double alpha, int N, int K) {
    double total = 0;
    for (int k = 0; k < K; k++) {
        for (int i = 0; i < N; i++) {
            total += Gamma.digamma(data[i][k] + alpha);
        }/*from  w  ww  .j a  v a2s.c  om*/
    }
    total -= Gamma.digamma(alpha) * N * K; // pulled out of the loop for efficiency
    return total;
}

From source file:edu.byu.nlp.stats.SymmetricDirichletMultinomialMatrixMAPOptimizable.java

private static double computeDenominator(double[] perIDataSums, double alpha, double K) {
    double total = 0;
    double alphaK = alpha * K;
    for (int i = 0; i < perIDataSums.length; i++) {
        total += Gamma.digamma(perIDataSums[i] + alphaK);
    }//from w ww.ja  v  a2s. c o  m
    total -= Gamma.digamma(alphaK) * perIDataSums.length;
    total *= K;
    return total;
}

From source file:eu.amidst.core.exponentialfamily.EF_Gamma.java

/**
 * {@inheritDoc}//www  . j a v  a2  s  .c  o  m
 */
@Override
public void updateMomentFromNaturalParameters() {
    double alpha = this.naturalParameters.get(0) + 1;
    double beta = -this.naturalParameters.get(1);
    this.momentParameters.set(0, Gamma.digamma(alpha) - Math.log(beta));
    this.momentParameters.set(1, alpha / beta);
}

From source file:eu.amidst.core.exponentialfamily.EF_InverseGamma.java

/**
 * {@inheritDoc}/*from  www.  j av  a  2  s .co m*/
 */
@Override
public void updateMomentFromNaturalParameters() {
    double alpha = -this.naturalParameters.get(0) - 1;
    double beta = -this.naturalParameters.get(1);
    this.momentParameters.set(0, Math.log(beta) - Gamma.digamma(alpha));
    this.momentParameters.set(1, alpha / beta);
}

From source file:edu.byu.nlp.stats.SymmetricDirichletMultinomialDiagonalMatrixMAPOptimizable.java

private double computeDiagNumerator(double[][][] data, double bdiag, int J, int K) {
    double total = 0;
    for (int j = 0; j < J; j++) {
        for (int k = 0; k < K; k++) {
            total += Gamma.digamma(data[j][k][k] + bdiag);
        }/*  w ww.j  a v a  2s  .c o  m*/
    }

    total -= J * K * Gamma.digamma(bdiag);

    return total;
}

From source file:eu.amidst.core.exponentialfamily.EF_JointNormalGamma.java

/**
 * {@inheritDoc}/* w w  w  .  ja  v a  2s  .c om*/
 */
@Override
public void updateMomentFromNaturalParameters() {
    double alpha = this.naturalParameters.get(EF_JointNormalGamma.LOGGAMMA) + 0.5;
    double precision = this.naturalParameters.get(EF_JointNormalGamma.MUSQUARE_GAMMA) / (-0.5);
    double mean = this.naturalParameters.get(EF_JointNormalGamma.MU_GAMMA) / precision;
    double beta = -this.naturalParameters.get(EF_JointNormalGamma.GAMMA) - 0.5 * precision * mean * mean;

    this.momentParameters.set(EF_JointNormalGamma.MU_GAMMA, mean * alpha / beta);
    this.momentParameters.set(EF_JointNormalGamma.MUSQUARE_GAMMA, 1 / precision + mean * mean * alpha / beta);
    this.momentParameters.set(EF_JointNormalGamma.GAMMA, alpha / beta);
    this.momentParameters.set(EF_JointNormalGamma.LOGGAMMA, Gamma.digamma(alpha) - Math.log(beta));

}

From source file:eu.amidst.core.exponentialfamily.EF_Dirichlet.java

/**
 * {@inheritDoc}/*  w  w w. j  a  va2  s.  co m*/
 */
@Override
public void updateMomentFromNaturalParameters() {

    double sumOfU_i = 0;
    for (int i = 0; i < nOfStates; i++) {
        sumOfU_i += this.naturalParameters.get(i);
    }

    for (int i = 0; i < nOfStates; i++) {
        this.momentParameters.set(i, Gamma.digamma(this.naturalParameters.get(i)) - Gamma.digamma(sumOfU_i));
    }
}

From source file:edu.byu.nlp.stats.SymmetricDirichletMultinomialDiagonalMatrixMAPOptimizable.java

private double computeDiagDenominator(double[][][] data, double bdiag, double offdiag, int J, int K) {
    double total = 0;
    double alphasum = bdiag + (K - 1) * offdiag;

    for (int j = 0; j < J; j++) {
        double[] nj = Matrices.sumOverSecond(data[j]);
        for (int k = 0; k < K; k++) {
            total += Gamma.digamma(nj[k] + alphasum);
        }/*from w  w w .  j a  va  2 s.  com*/
    }

    total -= J * K * Gamma.digamma(alphasum);

    return total;
}

From source file:eu.amidst.core.exponentialfamily.EF_Gamma.java

/**
 * {@inheritDoc}//from w  w w.j av  a 2  s  .  c o  m
 */
@Override
public SufficientStatistics createInitSufficientStatistics() {

    ArrayVector vector = new ArrayVector(this.sizeOfSufficientStatistics());

    double alpha = 1;
    double beta = 1;
    vector.set(0, Gamma.digamma(alpha) - Math.log(beta));
    vector.set(1, alpha / beta);

    return vector;
}

From source file:eu.amidst.core.exponentialfamily.EF_SparseDirichlet.java

/**
 * {@inheritDoc}// www . j  a  va2 s. c o  m
 */
@Override
public void updateMomentFromNaturalParameters() {

    double sumOfU_i = this.naturalParameters.sum();

    this.momentParameters = this.createZeroMomentParameters();

    this.momentParameters.copy(this.naturalParameters);

    this.getSparseMomentParameters().apply(new Function<Double, Double>() {
        @Override
        public Double apply(Double aDouble) {
            return Gamma.digamma(aDouble) - Gamma.digamma(sumOfU_i);
        }
    });

}