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

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

本文整理了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

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

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

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

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

  1. @Override
  2. public DoubleTensor digammaInPlace() {
  3. value = Gamma.digamma(value);
  4. return this;
  5. }

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

  1. /**
  2. * Overloaded digamma function: returns 0 if argument is zero
  3. *
  4. * @param x
  5. * @return digamma(x) if x nonzero, otherwise zero
  6. */
  7. private double digamma(final double x) {
  8. if (Math.abs(x) < 1e-15)
  9. return 0.0;
  10. else
  11. return Gamma.digamma(x);
  12. }

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

  1. @Test
  2. public void testDigammaShouldMatchCommonsMath() {
  3. for (double d: toIterable(doubles(), 10000)) {
  4. assertThat("digamma of " + d,
  5. Scalars.digamma(d),
  6. closeTo(Gamma.digamma(d), 1.0e-8));
  7. }
  8. for (double d: someDoubles(0, 2.0e-5)) {
  9. assertThat("digamma of " + d,
  10. Scalars.digamma(d),
  11. closeTo(Gamma.digamma(d), 1.0e-8));
  12. }
  13. for (double d: someDoubles(-10, 0)) {
  14. assertThat("digamma of " + d,
  15. Scalars.digamma(d),
  16. closeTo(Gamma.digamma(d), 1.0e-8));
  17. }
  18. for (double d: someDoubles(0, 10)) {
  19. assertThat("digamma of " + d,
  20. Scalars.digamma(d),
  21. closeTo(Gamma.digamma(d), 1.0e-8));
  22. }
  23. }
  24. }

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

  1. @Nonnull
  2. public static float[] digamma(@Nonnull final float[] arr) {
  3. final int k = arr.length;
  4. final float[] ret = new float[k];
  5. for (int i = 0; i < k; i++) {
  6. ret[i] = (float) Gamma.digamma(arr[i]);
  7. }
  8. return ret;
  9. }

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

  1. @Nonnull
  2. public static double[] digamma(@Nonnull final double[] arr) {
  3. final int k = arr.length;
  4. final double[] ret = new double[k];
  5. for (int i = 0; i < k; i++) {
  6. ret[i] = Gamma.digamma(arr[i]);
  7. }
  8. return ret;
  9. }

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

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

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

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

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

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

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

  1. private void updatePhiPerDoc(@Nonnegative final int d,
  2. @Nonnull final Map<String, float[]> eLogBeta_d) {
  3. // Dirichlet expectation (2d) for gamma
  4. final float[] gamma_d = _gamma[d];
  5. final double digamma_gammaSum_d = Gamma.digamma(MathUtils.sum(gamma_d));
  6. final double[] eLogTheta_d = new double[_K];
  7. for (int k = 0; k < _K; k++) {
  8. eLogTheta_d[k] = Gamma.digamma(gamma_d[k]) - digamma_gammaSum_d;
  9. }
  10. // updating phi w/ normalization
  11. final Map<String, float[]> phi_d = _phi.get(d);
  12. final Map<String, Float> doc = _miniBatchDocs.get(d);
  13. for (String label : doc.keySet()) {
  14. final float[] phi_label = phi_d.get(label);
  15. final float[] eLogBeta_label = eLogBeta_d.get(label);
  16. double normalizer = 0.d;
  17. for (int k = 0; k < _K; k++) {
  18. float phiVal = (float) Math.exp(eLogBeta_label[k] + eLogTheta_d[k]) + 1E-20f;
  19. phi_label[k] = phiVal;
  20. normalizer += phiVal;
  21. }
  22. for (int k = 0; k < _K; k++) {
  23. phi_label[k] /= normalizer;
  24. }
  25. }
  26. }

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

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

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

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

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

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

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

  1. @Test
  2. public void calculatesDerivativeOfMatrixElementWiseLogGamma() {
  3. calculatesDerivativeOfMatrixElementWiseOperator(
  4. new double[]{0.1, 0.2, 0.3, 0.4},
  5. toDiagonalArray(new double[]{digamma(0.1), digamma(0.2), digamma(0.3), digamma(0.4)}),
  6. DoubleVertex::logGamma
  7. );
  8. }

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

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

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

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

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

  1. /**
  2. * @param order
  3. * polynomial order
  4. * @param x
  5. * real number
  6. * @return polyGamma_order(x)
  7. */
  8. final public static double polyGamma(NumberValue order, double x) {
  9. int o = (int) order.getDouble();
  10. switch (o) {
  11. case 0:
  12. return Gamma.digamma(x);
  13. case 1:
  14. return Gamma.trigamma(x);
  15. // case 2:
  16. // return PolyGamma.tetragamma(x);
  17. // case 3:
  18. // return PolyGamma.pentagamma(x);
  19. // default:
  20. // return PolyGamma.psigamma(x, o);
  21. default:
  22. return Double.NaN;
  23. }
  24. }

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

  1. @Test
  2. public void calculatesDerivativeOScalarLogGamma() {
  3. calculatesDerivativeOfScalar(
  4. 0.5,
  5. digamma(0.5),
  6. DoubleVertex::logGamma
  7. );
  8. }

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

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

相关文章