org.apache.commons.math3.special.Gamma.digamma()方法的使用及代码示例

x33g5p2x  于2022-01-20 转载在 其他  
字(6.6k)|赞(0)|评价(0)|浏览(159)

本文整理了Java中org.apache.commons.math3.special.Gamma.digamma()方法的一些代码示例,展示了Gamma.digamma()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Gamma.digamma()方法的具体详情如下:
包路径:org.apache.commons.math3.special.Gamma
类名称:Gamma
方法名:digamma

Gamma.digamma介绍

[英]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. The result should be accurate to within 10^-8 absolute tolerance for x >= 10^-5 and within 10^-8 relative tolerance for x > 0.

Performance for large negative values of x will be quite expensive (proportional to |x|). Accuracy for negative values of x should be about 10^-8 absolute for results less than 10^5 and 10^-8 relative for results larger than that.
[中]计算x的digamma函数。
这是Jose Bernardo《算法AS 103:Psi(Digamma)函数》中描述的算法的独立书面实现,应用统计学,1976年。
一些常数已被更改,以增加精度,但运行时的开销适中。对于x>=10^-5,结果应精确到10^-8绝对公差范围内,对于x>0,结果应精确到10^-8相对公差范围内。
大负值x的性能将非常昂贵(与| x |成比例)。对于小于10^5的结果,x负值的准确度应为10^-8绝对值,对于大于10^5的结果,准确度应为10^-8相对值。

代码示例

代码示例来源:origin: org.apache.commons/commons-math3

return digamma(x + 1) - 1 / x;

代码示例来源:origin: geogebra/geogebra

final public static double psi(double x) {
  return Gamma.digamma(x);
}

代码示例来源:origin: improbable-research/keanu

@Override
public DoubleTensor digammaInPlace() {
  value = Gamma.digamma(value);
  return this;
}

代码示例来源:origin: mast-group/tassal

/**
 * Overloaded digamma function: returns 0 if argument is zero
 *
 * @param x
 * @return digamma(x) if x nonzero, otherwise zero
 */
private double digamma(final double x) {
  if (Math.abs(x) < 1e-15)
    return 0.0;
  else
    return Gamma.digamma(x);
}

代码示例来源:origin: lenskit/lenskit

@Test
  public void testDigammaShouldMatchCommonsMath() {
    for (double d: toIterable(doubles(), 10000)) {
      assertThat("digamma of " + d,
            Scalars.digamma(d),
            closeTo(Gamma.digamma(d), 1.0e-8));
    }

    for (double d: someDoubles(0, 2.0e-5)) {
      assertThat("digamma of " + d,
            Scalars.digamma(d),
            closeTo(Gamma.digamma(d), 1.0e-8));
    }

    for (double d: someDoubles(-10, 0)) {
      assertThat("digamma of " + d,
            Scalars.digamma(d),
            closeTo(Gamma.digamma(d), 1.0e-8));
    }

    for (double d: someDoubles(0, 10)) {
      assertThat("digamma of " + d,
            Scalars.digamma(d),
            closeTo(Gamma.digamma(d), 1.0e-8));
    }
  }
}

代码示例来源:origin: org.apache.hivemall/hivemall-core

@Nonnull
public static float[] digamma(@Nonnull final float[] arr) {
  final int k = arr.length;
  final float[] ret = new float[k];
  for (int i = 0; i < k; i++) {
    ret[i] = (float) Gamma.digamma(arr[i]);
  }
  return ret;
}

代码示例来源:origin: org.apache.hivemall/hivemall-core

@Nonnull
public static double[] digamma(@Nonnull final double[] arr) {
  final int k = arr.length;
  final double[] ret = new double[k];
  for (int i = 0; i < k; i++) {
    ret[i] = Gamma.digamma(arr[i]);
  }
  return ret;
}

代码示例来源:origin: lenskit/lenskit

double lambdaShpIK = lambdaShp.getEntry(item, k);
double lambdaRteIK = lambdaRte.getEntry(item, k);
double phiUIK = Gamma.digamma(gammaShpUK) - Math.log(gammaRteUK) + Gamma.digamma(lambdaShpIK) - Math.log(lambdaRteIK);
phiUI.setEntry(k, phiUIK);

代码示例来源:origin: broadgsa/gatk-protected

public double[] effectiveMultinomialWeights() {
  final double digammaOfSum = Gamma.digamma(MathUtils.sum(alpha));
  return MathUtils.applyToArray(alpha, a -> Math.exp(Gamma.digamma(a) - digammaOfSum));
}

代码示例来源:origin: broadgsa/gatk-protected

public double[] effectiveLog10MultinomialWeights() {
  final double digammaOfSum = Gamma.digamma(MathUtils.sum(alpha));
  return MathUtils.applyToArray(alpha, a -> (Gamma.digamma(a) - digammaOfSum) * MathUtils.LOG10_OF_E);
}

代码示例来源:origin: org.apache.hivemall/hivemall-core

private void updatePhiPerDoc(@Nonnegative final int d,
    @Nonnull final Map<String, float[]> eLogBeta_d) {
  // Dirichlet expectation (2d) for gamma
  final float[] gamma_d = _gamma[d];
  final double digamma_gammaSum_d = Gamma.digamma(MathUtils.sum(gamma_d));
  final double[] eLogTheta_d = new double[_K];
  for (int k = 0; k < _K; k++) {
    eLogTheta_d[k] = Gamma.digamma(gamma_d[k]) - digamma_gammaSum_d;
  }
  // updating phi w/ normalization
  final Map<String, float[]> phi_d = _phi.get(d);
  final Map<String, Float> doc = _miniBatchDocs.get(d);
  for (String label : doc.keySet()) {
    final float[] phi_label = phi_d.get(label);
    final float[] eLogBeta_label = eLogBeta_d.get(label);
    double normalizer = 0.d;
    for (int k = 0; k < _K; k++) {
      float phiVal = (float) Math.exp(eLogBeta_label[k] + eLogTheta_d[k]) + 1E-20f;
      phi_label[k] = phiVal;
      normalizer += phiVal;
    }
    for (int k = 0; k < _K; k++) {
      phi_label[k] /= normalizer;
    }
  }
}

代码示例来源:origin: improbable-research/keanu

public static Diff dlnPdf(double alpha, double beta, double x) {
  double dPdx = ((alpha - 1) / x) - ((beta - 1) / (1 - x));
  double dPda = digamma(alpha + beta) - digamma(alpha) + Math.log(x);
  double dPdb = digamma(alpha + beta) - digamma(beta) + Math.log(1 - x);
  return new Diff(dPda, dPdb, dPdx);
}

代码示例来源:origin: apache/opennlp-sandbox

prob+=mult1*mult*Gamma.digamma(n+1)/Gamma.digamma(k+1)/Gamma.digamma(n-k+1);

代码示例来源:origin: net.rapture/Reflex

public ReflexValue digamma(List<ReflexValue> params) {
  if (params.size() != 1) {
    throw new ReflexException(-1, "digamma needs one number parameter");
  }
  if (!params.get(0).isNumber()) {
    throw new ReflexException(-1, "digamma needs one number parameter");
  }
  double value = params.get(0).asDouble();
  return new ReflexValue(Gamma.digamma(value));
}

代码示例来源:origin: improbable-research/keanu

@Test
public void calculatesDerivativeOfMatrixElementWiseLogGamma() {
  calculatesDerivativeOfMatrixElementWiseOperator(
    new double[]{0.1, 0.2, 0.3, 0.4},
    toDiagonalArray(new double[]{digamma(0.1), digamma(0.2), digamma(0.3), digamma(0.4)}),
    DoubleVertex::logGamma
  );
}

代码示例来源:origin: improbable-research/keanu

public static Diff dlnPdf(double a, double b, double x) {
  double dPda = -digamma(a) + Math.log(b) - Math.log(x);
  double dPdb = (a / b) - (1 / x);
  double dPdx = (b - (a + 1) * x) / Math.pow(x, 2);
  return new Diff(dPda, dPdb, dPdx);
}

代码示例来源:origin: improbable-research/keanu

public static Diff dlnPdf(double theta, double k, double x) {
  double dPdx = (k - 1) / x - (1 / theta);
  double dPdtheta = -(theta * k - x) / Math.pow(theta, 2);
  double dPdk = Math.log(x) - Math.log(theta) - digamma(k);
  return new Diff(dPdtheta, dPdk, dPdx);
}

代码示例来源:origin: geogebra/geogebra

/**
 * @param order
 *            polynomial order
 * @param x
 *            real number
 * @return polyGamma_order(x)
 */
final public static double polyGamma(NumberValue order, double x) {
  int o = (int) order.getDouble();
  switch (o) {
  case 0:
    return Gamma.digamma(x);
  case 1:
    return Gamma.trigamma(x);
  // case 2:
  // return PolyGamma.tetragamma(x);
  // case 3:
  // return PolyGamma.pentagamma(x);
  // default:
  // return PolyGamma.psigamma(x, o);
  default:
    return Double.NaN;
  }
}

代码示例来源:origin: improbable-research/keanu

@Test
public void calculatesDerivativeOScalarLogGamma() {
  calculatesDerivativeOfScalar(
    0.5,
    digamma(0.5),
    DoubleVertex::logGamma
  );
}

代码示例来源:origin: improbable-research/keanu

@Override
public Diffs dLogProb(DoubleTensor x) {
  final DoubleTensor dLogPdc = x.log()
    .minusInPlace(concentration.digamma())
    .plusInPlace(org.apache.commons.math3.special.Gamma.digamma(concentration.sum()));
  final DoubleTensor dLogPdx = concentration.minus(1).divInPlace(x);
  return new Diffs()
    .put(C, dLogPdc)
    .put(X, dLogPdx);
}

相关文章