def _test(self, x, mu): xtf = tf.constant(x) val_true = stats.poisson.logpmf(x, mu) with self.test_session(): self.assertAllClose(poisson.logpmf(xtf, mu).eval(), val_true) self.assertAllClose( poisson.logpmf(xtf, tf.constant(mu)).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
def log_prob(self, xs, zs): """Return a vector [log p(xs, zs[1,:]), ..., log p(xs, zs[S,:])].""" if self.prior == 'Lognormal': zs = tf.exp(zs) elif self.prior != 'Gaussian': raise NotImplementedError("prior not available.") log_prior = -self.prior_variance * tf.reduce_sum(zs * zs) z = tf.reshape(zs, [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)) 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
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
def log_prob(self, xs, zs): """Returns a vector [log p(xs, zs[1,:]), ..., log p(xs, zs[S,:])].""" if self.prior == 'Lognormal': zs = tf.exp(zs) elif self.prior != 'Gaussian': raise NotImplementedError("prior not available.") log_prior = -self.prior_variance * tf.reduce_sum(zs * zs) s = tf.reshape(zs[:, :self.n_rows * self.K], [self.n_rows, self.K]) t = tf.reshape(zs[:, 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)) 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
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
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
def log_prob(self, xs, zs): """Returns a vector [log p(xs, zs[1,:]), ..., log p(xs, zs[S,:])].""" if self.prior == 'Lognormal': zs = tf.exp(zs) elif self.prior != 'Gaussian': raise NotImplementedError("prior not available.") log_prior = -self.prior_variance * tf.reduce_sum(zs*zs) s = tf.reshape(zs[:,:self.n_rows*self.K], [self.n_rows,self.K]) t = tf.reshape(zs[:,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)) 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
def log_prob(self, xs, zs): """Return a vector [log p(xs, zs[1,:]), ..., log p(xs, zs[S,:])].""" if self.prior == 'Lognormal': zs = tf.exp(zs) elif self.prior != 'Gaussian': raise NotImplementedError("prior not available.") log_prior = -self.prior_variance * tf.reduce_sum(zs*zs) z = tf.reshape(zs, [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)) 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
def _test_logpmf(x, mu): xtf = tf.constant(x) val_true = stats.poisson.logpmf(x, mu) _assert_eq(poisson.logpmf(xtf, mu), val_true) _assert_eq(poisson.logpmf(xtf, tf.constant(mu)), val_true) _assert_eq(poisson.logpmf(xtf, tf.constant([mu])), val_true)
def _test(self, x, mu): xtf = tf.constant(x) val_true = stats.poisson.logpmf(x, mu) with self.test_session(): self.assertAllClose(poisson.logpmf(xtf, mu).eval(), val_true) self.assertAllClose(poisson.logpmf(xtf, tf.constant(mu)).eval(), val_true)
def _test(self, x, lam): val_true = stats.poisson.logpmf(x, lam) with self.test_session(): self.assertAllClose(poisson.logpmf(x, lam=lam).eval(), val_true)