예제 #1
0
    public double evaluate(double gam) {
      if (gam <= 0) return 1.0e100;

      double sum = 0.0;
      for (int j = 0; j < m; j++) sum += Num.digamma(gam + x[j]);
      return sum / m + Math.log(p) - Num.digamma(gam);
    }
예제 #2
0
  /** Computes the probability <SPAN CLASS="MATH"><I>p</I>(<I>x</I>)</SPAN>. */
  public static double prob(double n, double p, int x) {
    final int SLIM = 15; // To avoid overflow
    final double MAXEXP = (Num.DBL_MAX_EXP - 1) * Num.LN2; // To avoid overflow
    final double MINEXP = (Num.DBL_MIN_EXP - 1) * Num.LN2; // To avoid underflow
    double y;

    if (p < 0.0 || p > 1.0) throw new IllegalArgumentException("p not in [0, 1]");
    if (n <= 0.0) throw new IllegalArgumentException("n <= 0.0");
    if (x < 0) return 0.0;
    if (p >= 1.0) { // In fact, p == 1
      if (x == 0) return 1.0;
      else return 0.0;
    }
    if (p <= 0.0) // In fact, p == 0
    return 0.0;

    y =
        Num.lnGamma(n + x)
            - (Num.lnFactorial(x) + Num.lnGamma(n))
            + n * Math.log(p)
            + x * Math.log1p(-p);

    if (y >= MAXEXP) throw new IllegalArgumentException("term overflow");
    return Math.exp(y);
  }