Example #1
0
 def logpdf(self, x, df, loc=0, scale=1):
     x = tf.cast(tf.squeeze(x), dtype=tf.float32)
     df = tf.cast(tf.squeeze(df), dtype=tf.float32)
     loc = tf.cast(tf.squeeze(loc), dtype=tf.float32)
     scale = tf.cast(tf.squeeze(scale), dtype=tf.float32)
     z = (x - loc) / scale
     return lgamma(0.5 * (df + 1.0)) - lgamma(0.5 * df) - \
            0.5 * (tf.log(np.pi) + tf.log(df)) - tf.log(scale) - \
            0.5 * (df + 1.0) * tf.log(1.0 + (1.0/df) * tf.square(z))
Example #2
0
 def logpdf(self, x, df, loc=0, scale=1):
     x = tf.cast(x, dtype=tf.float32)
     df = tf.cast(df, dtype=tf.float32)
     loc = tf.cast(loc, dtype=tf.float32)
     scale = tf.cast(scale, dtype=tf.float32)
     z = (x - loc) / scale
     return lgamma(0.5 * (df + 1.0)) - lgamma(0.5 * df) - \
            0.5 * (tf.log(np.pi) + tf.log(df)) - tf.log(scale) - \
            0.5 * (df + 1.0) * tf.log(1.0 + (1.0/df) * tf.square(z))
Example #3
0
 def logpmf(self, x, n, p):
     """
     Parameters
     ----------
     x : np.array or tf.Tensor
         vector of length K, where x[i] is the number of outcomes
         in the ith bucket, or matrix with column length K
     n : int or tf.Tensor
         number of outcomes equal to sum x[i]
     p : np.array or tf.Tensor
         vector of probabilities summing to 1
     """
     x = tf.cast(tf.squeeze(x), dtype=tf.float32)
     n = tf.cast(tf.squeeze(n), dtype=tf.float32)
     p = tf.cast(tf.squeeze(p), dtype=tf.float32)
     if len(get_dims(x)) == 1:
         return lgamma(n + 1.0) - \
                tf.reduce_sum(lgamma(x + 1.0)) + \
                tf.reduce_sum(tf.mul(x, tf.log(p)))
     else:
         return lgamma(n + 1.0) - \
                tf.reduce_sum(lgamma(x + 1.0), 1) + \
                tf.reduce_sum(tf.mul(x, tf.log(p)), 1)
Example #4
0
 def logpmf(self, x, n, p):
     """
     Parameters
     ----------
     x : np.array or tf.Tensor
         vector of length K, where x[i] is the number of outcomes
         in the ith bucket, or matrix with column length K
     n : int or tf.Tensor
         number of outcomes equal to sum x[i]
     p : np.array or tf.Tensor
         vector of probabilities summing to 1
     """
     x = tf.cast(x, dtype=tf.float32)
     n = tf.cast(n, dtype=tf.float32)
     p = tf.cast(p, dtype=tf.float32)
     if len(get_dims(x)) == 1:
         return lgamma(n + 1.0) - \
                tf.reduce_sum(lgamma(x + 1.0)) + \
                tf.reduce_sum(tf.mul(x, tf.log(p)))
     else:
         return lgamma(n + 1.0) - \
                tf.reduce_sum(lgamma(x + 1.0), 1) + \
                tf.reduce_sum(tf.mul(x, tf.log(p)), 1)
Example #5
0
 def logpmf(self, x, n, p):
     x = tf.cast(tf.squeeze(x), dtype=tf.float32)
     n = tf.cast(tf.squeeze(n), dtype=tf.float32)
     p = tf.cast(tf.squeeze(p), dtype=tf.float32)
     return lgamma(n + 1.0) - lgamma(x + 1.0) - lgamma(n - x + 1.0) + \
            tf.mul(x, tf.log(p)) + tf.mul(n - x, tf.log(1.0-p))
Example #6
0
 def entropy(self, a, scale=1):
     a = tf.cast(a, dtype=tf.float32)
     scale = tf.cast(scale, dtype=tf.float32)
     return a + tf.log(scale*tf.exp(lgamma(a))) - \
            (1.0 + a) * digamma(a)
Example #7
0
 def logpmf(self, x, mu):
     x = tf.cast(tf.squeeze(x), dtype=tf.float32)
     mu = tf.cast(tf.squeeze(mu), dtype=tf.float32)
     return x * tf.log(mu) - mu - lgamma(x + 1.0)
Example #8
0
 def logpdf(self, x, a, scale=1):
     x = tf.cast(tf.squeeze(x), dtype=tf.float32)
     a = tf.cast(tf.squeeze(a), dtype=tf.float32)
     scale = tf.cast(tf.squeeze(scale), dtype=tf.float32)
     return (a - 1.0) * tf.log(x) - x/scale - a * tf.log(scale) - lgamma(a)
Example #9
0
 def logpmf(self, x, mu):
     x = tf.cast(x, dtype=tf.float32)
     mu = tf.cast(mu, dtype=tf.float32)
     return x * tf.log(mu) - mu - lgamma(x + 1.0)
Example #10
0
 def logpdf(self, x, a, scale=1):
     x = tf.cast(x, dtype=tf.float32)
     a = tf.cast(a, dtype=tf.float32)
     scale = tf.cast(scale, dtype=tf.float32)
     return tf.mul(a, tf.log(scale)) - lgamma(a) + \
            tf.mul(-a-1, tf.log(x)) - tf.truediv(scale, x)
Example #11
0
 def entropy(self, a, scale=1):
     a = tf.cast(a, dtype=tf.float32)
     scale = tf.cast(scale, dtype=tf.float32)
     return a + tf.log(scale*tf.exp(lgamma(a))) - \
            (1.0 + a) * digamma(a)
Example #12
0
 def logpmf(self, x, mu):
     x = tf.cast(x, dtype=tf.float32)
     mu = tf.cast(mu, dtype=tf.float32)
     return x * tf.log(mu) - mu - lgamma(x + 1.0)
Example #13
0
 def logpdf(self, x, a, scale=1):
     x = tf.cast(x, dtype=tf.float32)
     a = tf.cast(a, dtype=tf.float32)
     scale = tf.cast(scale, dtype=tf.float32)
     return tf.mul(a, tf.log(scale)) - lgamma(a) + \
            tf.mul(-a-1, tf.log(x)) - tf.truediv(scale, x)
Example #14
0
 def logpdf(self, x, a, scale=1):
     x = tf.cast(x, dtype=tf.float32)
     a = tf.cast(a, dtype=tf.float32)
     scale = tf.cast(scale, dtype=tf.float32)
     return (a -
             1.0) * tf.log(x) - x / scale - a * tf.log(scale) - lgamma(a)
Example #15
0
 def logpmf(self, x, n, p):
     x = tf.cast(tf.squeeze(x), dtype=tf.float32)
     n = tf.cast(tf.squeeze(n), dtype=tf.float32)
     p = tf.cast(tf.squeeze(p), dtype=tf.float32)
     return lgamma(n + 1.0) - lgamma(x + 1.0) - lgamma(n - x + 1.0) + \
            tf.mul(x, tf.log(p)) + tf.mul(n - x, tf.log(1.0-p))
Example #16
0
 def logpmf(self, x, mu):
     x = tf.cast(tf.squeeze(x), dtype=tf.float32)
     mu = tf.cast(tf.squeeze(mu), dtype=tf.float32)
     return x * tf.log(mu) - mu - lgamma(x + 1.0)
Example #17
0
 def logpdf(self, x, alpha, scale=1):
     x = tf.cast(tf.squeeze(x), dtype=tf.float32)
     alpha = tf.cast(tf.squeeze(alpha), dtype=tf.float32)
     scale = tf.cast(tf.squeeze(scale), dtype=tf.float32)
     return tf.mul(alpha, tf.log(scale)) - lgamma(alpha) + \
            tf.mul(-alpha-1, tf.log(x)) - tf.truediv(scale, x)
Example #18
0
 def logpdf(self, x, df):
     x = tf.cast(tf.squeeze(x), dtype=tf.float32)
     df = tf.cast(tf.squeeze(df), dtype=tf.float32)
     return tf.mul(0.5*df - 1, tf.log(x)) - 0.5*x - \
            tf.mul(0.5*df, tf.log(2.0)) - lgamma(0.5*df)
Example #19
0
 def entropy(self, a, scale=1):
     a = tf.cast(a, dtype=tf.float32)
     scale = tf.cast(scale, dtype=tf.float32)
     return a + tf.log(scale) + lgamma(a) + \
            tf.mul(1.0 - a, digamma(a))
Example #20
0
 def logpmf(self, x, n, p):
     x = tf.cast(x, dtype=tf.float32)
     n = tf.cast(n, dtype=tf.float32)
     p = tf.cast(p, dtype=tf.float32)
     return lgamma(n + 1.0) - lgamma(x + 1.0) - lgamma(n - x + 1.0) + \
            tf.mul(x, tf.log(p)) + tf.mul(n - x, tf.log(1.0-p))
Example #21
0
 def logpdf(self, x, a, scale=1):
     x = tf.cast(x, dtype=tf.float32)
     a = tf.cast(a, dtype=tf.float32)
     scale = tf.cast(scale, dtype=tf.float32)
     return (a - 1.0) * tf.log(x) - x/scale - a * tf.log(scale) - lgamma(a)
Example #22
0
 def logpdf(self, x, a, scale=1):
     x = tf.cast(tf.squeeze(x), dtype=tf.float32)
     a = tf.cast(tf.squeeze(a), dtype=tf.float32)
     scale = tf.cast(tf.squeeze(scale), dtype=tf.float32)
     return (a -
             1.0) * tf.log(x) - x / scale - a * tf.log(scale) - lgamma(a)
Example #23
0
 def logpmf(self, x, n, p):
     x = tf.cast(x, dtype=tf.float32)
     n = tf.cast(n, dtype=tf.float32)
     p = tf.cast(p, dtype=tf.float32)
     return lgamma(n + 1.0) - lgamma(x + 1.0) - lgamma(n - x + 1.0) + \
            tf.mul(x, tf.log(p)) + tf.mul(n - x, tf.log(1.0-p))
Example #24
0
 def entropy(self, a, scale=1):
     a = tf.cast(a, dtype=tf.float32)
     scale = tf.cast(scale, dtype=tf.float32)
     return a + tf.log(scale) + lgamma(a) + \
            tf.mul(1.0 - a, digamma(a))
Example #25
0
 def logpdf(self, x, alpha, scale=1):
     x = tf.cast(tf.squeeze(x), dtype=tf.float32)
     alpha = tf.cast(tf.squeeze(alpha), dtype=tf.float32)
     scale = tf.cast(tf.squeeze(scale), dtype=tf.float32)
     return tf.mul(alpha, tf.log(scale)) - lgamma(alpha) + \
            tf.mul(-alpha-1, tf.log(x)) - tf.truediv(scale, x)
Example #26
0
def _test(x):
    xtf = tf.constant(x)
    val_true = special.gammaln(x)
    _assert_eq(lgamma(xtf), val_true)
Example #27
0
 def logpdf(self, x, df):
     x = tf.cast(x, dtype=tf.float32)
     df = tf.cast(df, dtype=tf.float32)
     return tf.mul(0.5*df - 1, tf.log(x)) - 0.5*x - \
            tf.mul(0.5*df, tf.log(2.0)) - lgamma(0.5*df)
Example #28
0
def _test(x):
    xtf = tf.constant(x)
    val_true = special.gammaln(x)
    _assert_eq(lgamma(xtf), val_true)