示例#1
0
double
dhyper (double x, double r, double b, double n, int give_log)
{
  double p, q, p1, p2, p3;

  if (isnan (x) || isnan (r) || isnan (b) || isnan (n))
    return x + r + b + n;

  if (R_D_negInonint (r) || R_D_negInonint (b) || R_D_negInonint (n)
      || n > r + b)
    return(NAN);
  if (x < 0)
    return (R_D__0);
  R_D_nonint_check (x);         // incl warning

  x = round (x);
  r = round (r);
  b = round (b);
  n = round (n);

  if (n < x || r < x || n - x > b)
    return (R_D__0);
  if (n == 0)
    return ((x == 0) ? R_D__1 : R_D__0);

  p = ((double) n) / ((double) (r + b));
  q = ((double) (r + b - n)) / ((double) (r + b));

  p1 = dbinom_raw (x, r, p, q, give_log);
  p2 = dbinom_raw (n - x, b, p, q, give_log);
  p3 = dbinom_raw (n, r + b, p, q, give_log);

  return ((give_log) ? p1 + p2 - p3 : p1 * p2 / p3);
}
示例#2
0
double dhyper(double x, double r, double b, double n, int give_log)
{
    double p, q, p1, p2, p3;

#ifdef IEEE_754
    if (ISNAN(x) || ISNAN(r) || ISNAN(b) || ISNAN(n))
        return x + r + b + n;
#endif

    if (R_D_negInonint(r) || R_D_negInonint(b) || R_D_negInonint(n) || n > r+b)
        ML_ERR_return_NAN;
    if (R_D_negInonint(x))
        return(R_D__0);

    x = R_D_forceint(x);
    r = R_D_forceint(r);
    b = R_D_forceint(b);
    n = R_D_forceint(n);

    if (n < x || r < x || n - x > b) return(R_D__0);
    if (n == 0) return((x == 0) ? R_D__1 : R_D__0);

    p = ((double)n)/((double)(r+b));
    q = ((double)(r+b-n))/((double)(r+b));

    p1 = dbinom_raw(x,	r, p,q,give_log);
    p2 = dbinom_raw(n-x,b, p,q,give_log);
    p3 = dbinom_raw(n,r+b, p,q,give_log);

    return( (give_log) ? p1 + p2 - p3 : p1*p2/p3 );
}
示例#3
0
文件: dbinom.c 项目: Bgods/r-source
double dbinom(double x, double n, double p, int give_log)
{
#ifdef IEEE_754
    /* NaNs propagated correctly */
    if (ISNAN(x) || ISNAN(n) || ISNAN(p)) return x + n + p;
#endif

    if (p < 0 || p > 1 || R_D_negInonint(n))
	ML_ERR_return_NAN;
    R_D_nonint_check(x);
    if (x < 0 || !R_FINITE(x)) return R_D__0;

    n = R_forceint(n);
    x = R_forceint(x);

    return dbinom_raw(x, n, p, 1-p, give_log);
}