public static double logpmf(double x,int n,double p){
  if (x < 0 || x > n) {
    return Double.NEGATIVE_INFINITY;
  }
  if (p <= 0.) {
    return x == 0 ? 0. : Double.NEGATIVE_INFINITY;
  }
  if (p >= 1.) {
    return x == n ? 0. : Double.NEGATIVE_INFINITY;
  }
  final double q=1 - p;
  if (x == 0) {
    if (p < .1) {
      return -devianceTerm(n,n * q) - n * p;
    }
 else {
      return n * Math.log(q);
    }
  }
  if (x == n) {
    if (p > .9) {
      return -devianceTerm(n,n * p) - n * q;
    }
 else {
      return n * Math.log(p);
    }
  }
  final double lc=stirlingError(n) - stirlingError(x) - stirlingError(n - x)- devianceTerm(x,n * p)- devianceTerm(n - x,n * q);
  final double f=(MathUtil.TWOPI * x * (n - x)) / n;
  return lc - .5 * Math.log(f);
}

public static double logpmf ( double x , int n , double p ) { if ( x < 0 || x > n ) { return Double . NEGATIVE_INFINITY ; } if ( p <= 0. ) { return x == 0 ? 0. : Double . NEGATIVE_INFINITY ; } if ( p >= 1. ) { return x == n ? 0. : Double . NEGATIVE_INFINITY ; } final double q = 1 - p ; if ( x == 0 ) { if ( p < .1 ) { return - devianceTerm ( n , n * q ) - n * p ; } else { return n * Math . log ( q ) ; } } if ( x == n ) { if ( p > .9 ) { return - devianceTerm ( n , n * p ) - n * q ; } else { return n * Math . log ( p ) ; } } final double lc = stirlingError ( n ) - stirlingError ( x ) - stirlingError ( n - x ) - devianceTerm ( x , n * p ) - devianceTerm ( n - x , n * q ) ; final double f = ( MathUtil . TWOPI * x * ( n - x ) ) / n ; return lc - .5 * Math . log ( f ) ; }
poisson probability mass function ( pmf ) for integer values .
