Esempio n. 1
0
 def _test(self, x, s):
     xtf = tf.constant(x)
     val_true = stats.lognorm.logpdf(x, s)
     with self.test_session():
         self.assertAllClose(lognorm.logpdf(xtf, s).eval(), val_true)
         self.assertAllClose(
             lognorm.logpdf(xtf, tf.constant(s)).eval(), val_true)
    def log_prob(self, xs, zs):
        """Return scalar, the log joint density log p(xs, zs)."""
        if self.prior == 'Lognormal':
            log_prior = tf.reduce_sum(lognorm.logpdf(zs['z'], self.prior_std))
        elif self.prior == 'Gaussian':
            log_prior = tf.reduce_sum(norm.logpdf(zs['z'], 0.0,
                                                  self.prior_std))
        else:
            raise NotImplementedError("prior not available.")

        s = tf.reshape(zs['z'][:self.n_rows * self.K], [self.n_rows, self.K])
        t = tf.reshape(zs['z'][self.n_cols * self.K:], [self.n_cols, self.K])

        xp = tf.matmul(s, t, transpose_b=True)
        if self.interaction == 'multiplicative':
            xp = tf.exp(xp)
        elif self.interaction != 'additive':
            raise NotImplementedError("interaction type unknown.")

        if self.like == 'Gaussian':
            log_lik = tf.reduce_sum(norm.logpdf(xs['x'], xp, 1.0))
        elif self.like == 'Poisson':
            if not (self.interaction == "additive"
                    or self.prior == "Lognormal"):
                raise NotImplementedError(
                    "Rate of Poisson has to be nonnegatve.")

            log_lik = tf.reduce_sum(poisson.logpmf(xs['x'], xp))
        else:
            raise NotImplementedError("likelihood not available.")

        return log_lik + log_prior
Esempio n. 3
0
    def log_prob(self, xs, zs):
        """Return scalar, the log joint density log p(xs, zs)."""
        if self.prior == 'Lognormal':
            log_prior = tf.reduce_sum(lognorm.logpdf(zs['z'], self.prior_std))
        elif self.prior == 'Gaussian':
            log_prior = tf.reduce_sum(norm.logpdf(zs['z'], 0.0,
                                                  self.prior_std))
        else:
            raise NotImplementedError("prior not available.")

        z = tf.reshape(zs['z'], [self.N, self.K])
        if self.dist == 'euclidean':
            xp = tf.matmul(tf.ones([1, self.N]),
                           tf.reduce_sum(z * z, 1, keep_dims=True))
            xp = xp + tf.transpose(xp) - 2 * tf.matmul(z, z, transpose_b=True)
            xp = 1.0 / xp
        elif self.dist == 'cosine':
            xp = tf.matmul(z, z, transpose_b=True)

        if self.like == 'Gaussian':
            log_lik = tf.reduce_sum(norm.logpdf(xs['x'], xp, 1.0))
        elif self.like == 'Poisson':
            if not (self.dist == 'euclidean' or self.prior == "Lognormal"):
                raise NotImplementedError(
                    "Rate of Poisson has to be nonnegatve.")

            log_lik = tf.reduce_sum(poisson.logpmf(xs['x'], xp))
        else:
            raise NotImplementedError("likelihood not available.")

        return log_lik + log_prior
Esempio n. 4
0
  def log_prob(self, xs, zs):
    """Return scalar, the log joint density log p(xs, zs)."""
    if self.prior == 'Lognormal':
      log_prior = tf.reduce_sum(lognorm.logpdf(zs['z'], self.prior_std))
    elif self.prior == 'Gaussian':
      log_prior = tf.reduce_sum(norm.logpdf(zs['z'], 0.0, self.prior_std))
    else:
      raise NotImplementedError("prior not available.")

    s = tf.reshape(zs['z'][:self.n_rows * self.K], [self.n_rows, self.K])
    t = tf.reshape(zs['z'][self.n_cols * self.K:], [self.n_cols, self.K])

    xp = tf.matmul(s, t, transpose_b=True)
    if self.interaction == 'multiplicative':
      xp = tf.exp(xp)
    elif self.interaction != 'additive':
      raise NotImplementedError("interaction type unknown.")

    if self.like == 'Gaussian':
      log_lik = tf.reduce_sum(norm.logpdf(xs['x'], xp, 1.0))
    elif self.like == 'Poisson':
      if not (self.interaction == "additive" or self.prior == "Lognormal"):
        raise NotImplementedError("Rate of Poisson has to be nonnegatve.")

      log_lik = tf.reduce_sum(poisson.logpmf(xs['x'], xp))
    else:
      raise NotImplementedError("likelihood not available.")

    return log_lik + log_prior
Esempio n. 5
0
  def log_prob(self, xs, zs):
    """Return scalar, the log joint density log p(xs, zs)."""
    if self.prior == 'Lognormal':
      log_prior = tf.reduce_sum(lognorm.logpdf(zs['z'], self.prior_std))
    elif self.prior == 'Gaussian':
      log_prior = tf.reduce_sum(norm.logpdf(zs['z'], 0.0, self.prior_std))
    else:
      raise NotImplementedError("prior not available.")

    z = tf.reshape(zs['z'], [self.N, self.K])
    if self.dist == 'euclidean':
      xp = tf.tile(tf.reduce_sum(tf.pow(z, 2), 1, keep_dims=True), [1, self.N])
      xp = xp + tf.transpose(xp) - 2 * tf.matmul(z, z, transpose_b=True)
      xp = 1.0 / tf.sqrt(xp + tf.diag(tf.zeros(self.N) + 1e3))
    elif self.dist == 'cosine':
      xp = tf.matmul(z, z, transpose_b=True)

    if self.like == 'Gaussian':
      log_lik = tf.reduce_sum(norm.logpdf(xs['x'], xp, 1.0))
    elif self.like == 'Poisson':
      if not (self.dist == 'euclidean' or self.prior == "Lognormal"):
        raise NotImplementedError("Rate of Poisson has to be nonnegatve.")

      log_lik = tf.reduce_sum(poisson.logpmf(xs['x'], xp))
    else:
      raise NotImplementedError("likelihood not available.")

    return log_lik + log_prior
Esempio n. 6
0
 def _test(self, x, s):
     xtf = tf.constant(x)
     val_true = stats.lognorm.logpdf(x, s)
     with self.test_session():
         self.assertAllClose(lognorm.logpdf(xtf, s).eval(), val_true)
         self.assertAllClose(lognorm.logpdf(xtf, tf.constant(s)).eval(), val_true)
 def _test(self, x, s):
   val_true = stats.lognorm.logpdf(x, s)
   with self.test_session():
     self.assertAllClose(lognorm.logpdf(x, s=s).eval(), val_true)
def _test(x, s):
    xtf = tf.constant(x)
    val_true = stats.lognorm.logpdf(x, s)
    _assert_eq(lognorm.logpdf(xtf, s), val_true)
    _assert_eq(lognorm.logpdf(xtf, tf.constant(s)), val_true)
    _assert_eq(lognorm.logpdf(xtf, tf.constant([s])), val_true)
Esempio n. 9
0
def _test_logpdf(x, s=1):
    xtf = tf.constant(x)
    val_true = stats.lognorm.logpdf(x, s)
    _assert_eq(lognorm.logpdf(xtf, s), val_true)
    _assert_eq(lognorm.logpdf(xtf, tf.constant(s)), val_true)
    _assert_eq(lognorm.logpdf(xtf, tf.constant([s])), val_true)