Skip to content

Commit

Permalink
update tests/test-stats
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinvtran committed Aug 29, 2016
1 parent 7c950d4 commit aaef0ac
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 136 deletions.
2 changes: 1 addition & 1 deletion tests/test-models/test_pythonmodel_log_prob.py
Expand Up @@ -12,7 +12,7 @@


class BetaBernoulli(PythonModel):
"""p(x, z) = Bernoulli(x | z) * Beta(z | 1, 1)"""
"""p(x, p) = Bernoulli(x | p) * Beta(p | 1, 1)"""
def _py_log_prob(self, xs, zs):
# This example is written for pedagogy. We recommend
# vectorizing operations in practice.
Expand Down
6 changes: 3 additions & 3 deletions tests/test-stats/test_bernoulli_entropy.py
Expand Up @@ -12,9 +12,9 @@
class test_bernoulli_entropy_class(tf.test.TestCase):

def _test(self, p):
val_true = stats.bernoulli.entropy(p)
self.assertAllClose(bernoulli.entropy(p).eval(), val_true)
self.assertAllClose(bernoulli.entropy(tf.constant(p)).eval(), val_true)
val_true = stats.bernoulli.entropy(p=p)
self.assertAllClose(bernoulli.entropy(p=p).eval(), val_true)
self.assertAllClose(bernoulli.entropy(p=tf.constant(p)).eval(), val_true)

def test_0d(self):
with self.test_session():
Expand Down
6 changes: 3 additions & 3 deletions tests/test-stats/test_bernoulli_logpmf.py
Expand Up @@ -13,10 +13,10 @@ class test_bernoulli_logpmf_class(tf.test.TestCase):

def _test(self, x, p):
xtf = tf.constant(x)
val_true = stats.bernoulli.logpmf(x, p)
val_true = stats.bernoulli.logpmf(x, p=p)
with self.test_session():
self.assertAllClose(bernoulli.logpmf(xtf, p).eval(), val_true)
self.assertAllClose(bernoulli.logpmf(xtf, tf.constant(p)).eval(),
self.assertAllClose(bernoulli.logpmf(xtf, p=p).eval(), val_true)
self.assertAllClose(bernoulli.logpmf(xtf, p=tf.constant(p)).eval(),
val_true)

def test_int_0d(self):
Expand Down
6 changes: 0 additions & 6 deletions tests/test-stats/test_beta_entropy.py
Expand Up @@ -16,12 +16,6 @@ def _test(self, a, b):
self.assertAllClose(beta.entropy(a, b).eval(), val_true, atol=1e-4)
self.assertAllClose(beta.entropy(tf.constant(a), tf.constant(b)).eval(),
val_true, atol=1e-4)
self.assertAllClose(beta.entropy(tf.constant([a]), tf.constant(b)).eval(),
val_true, atol=1e-4)
self.assertAllClose(beta.entropy(tf.constant(a), tf.constant([b])).eval(),
val_true, atol=1e-4)
self.assertAllClose(beta.entropy(tf.constant([a]), tf.constant([b])).eval(),
val_true, atol=1e-4)

def test_0d(self):
with self.test_session():
Expand Down
9 changes: 2 additions & 7 deletions tests/test-stats/test_beta_logpdf.py
Expand Up @@ -18,12 +18,6 @@ def _test(self, x, a, b):
self.assertAllClose(beta.logpdf(xtf, a, b).eval(), val_true)
self.assertAllClose(beta.logpdf(xtf, tf.constant(a),
tf.constant(b)).eval(), val_true)
self.assertAllClose(beta.logpdf(xtf, tf.constant([a]),
tf.constant(b)).eval(), val_true)
self.assertAllClose(beta.logpdf(xtf, tf.constant(a),
tf.constant([b])).eval(), val_true)
self.assertAllClose(beta.logpdf(xtf, tf.constant([a]),
tf.constant([b])).eval(), val_true)

def test_0d(self):
self._test(0.3, a=0.5, b=0.5)
Expand All @@ -42,5 +36,6 @@ def test_1d(self):
self._test([0.5, 0.3, 0.8, 0.1], a=0.5, b=0.5)

def test_2d(self):
self._test(np.array([[0.5, 0.3, 0.8, 0.1], [0.1, 0.7, 0.2, 0.4]]),
self._test(np.array([[0.5, 0.3, 0.8, 0.1], [0.1, 0.7, 0.2, 0.4]],
dtype=np.float32),
a=0.5, b=0.5)
28 changes: 13 additions & 15 deletions tests/test-stats/test_expon_logpdf.py
Expand Up @@ -11,29 +11,27 @@

class test_expon_logpdf_class(tf.test.TestCase):

def _test(self, x, scale=1):
def _test(self, x, lam):
xtf = tf.constant(x)
val_true = stats.expon.logpdf(x, scale=scale)
val_true = stats.expon.logpdf(x, scale=1.0 / lam)
with self.test_session():
self.assertAllClose(expon.logpdf(xtf, scale=tf.constant(scale)).eval(),
self.assertAllClose(expon.logpdf(xtf, lam=tf.constant(lam)).eval(),
val_true)

def test_0d(self):
self._test(0.3)
self._test(0.7)
self._test(0.3, lam=1.0)
self._test(0.7, lam=1.0)

self._test(0.3, scale=1.0)
self._test(0.7, scale=1.0)
self._test(0.3, lam=0.5)
self._test(0.7, lam=0.5)

self._test(0.3, scale=0.5)
self._test(0.7, scale=0.5)

self._test(0.3, scale=5.0)
self._test(0.7, scale=5.0)
self._test(0.3, lam=5.0)
self._test(0.7, lam=5.0)

def test_1d(self):
self._test([0.5, 2.3, 5.8, 10.1], scale=5.0)
self._test([0.5, 2.3, 5.8, 10.1], lam=5.0)

def test_2d(self):
self._test(np.array([[0.5, 2.3, 5.8, 10.1], [0.5, 2.3, 5.8, 10.1]]),
scale=5.0)
self._test(np.array([[0.5, 2.3, 5.8, 10.1], [0.5, 2.3, 5.8, 10.1]],
dtype=np.float32),
lam=5.0)
21 changes: 11 additions & 10 deletions tests/test-stats/test_gamma_entropy.py
Expand Up @@ -20,19 +20,20 @@ def gamma_entropy_vec(a, scale):

class test_gamma_entropy_class(tf.test.TestCase):

def _test(self, a, scale=1):
val_true = gamma_entropy_vec(a, scale=scale)
def _test(self, alpha, beta):
val_true = gamma_entropy_vec(alpha, scale=1.0 / beta)
with self.test_session():
self.assertAllClose(gamma.entropy(a, scale).eval(), val_true)
self.assertAllClose(gamma.entropy(tf.constant(a),
tf.constant(scale)).eval(), val_true)
self.assertAllClose(gamma.entropy(alpha, beta).eval(), val_true)
self.assertAllClose(gamma.entropy(tf.constant(alpha),
tf.constant(beta)).eval(), val_true)

def test_0d(self):
self._test(a=1.0, scale=1.0)
self._test(a=1.0, scale=1.0)
self._test(alpha=1.0, beta=1.0)
self._test(alpha=1.0, beta=1.0)

self._test(a=0.5, scale=5.0)
self._test(a=5.0, scale=0.5)
self._test(alpha=0.5, beta=5.0)
self._test(alpha=5.0, beta=0.5)

def test_1d(self):
self._test(a=[0.5, 1.2, 5.3, 8.7], scale=[0.5, 1.2, 5.3, 8.7])
self._test(alpha=np.array([0.5, 1.2, 5.3, 8.7], dtype=np.float32),
beta=np.array([0.5, 1.2, 5.3, 8.7], dtype=np.float32))
31 changes: 16 additions & 15 deletions tests/test-stats/test_gamma_logpdf.py
Expand Up @@ -11,29 +11,30 @@

class test_gamma_logpdf_class(tf.test.TestCase):

def _test(self, x, a, scale=1):
def _test(self, x, alpha, beta):
xtf = tf.constant(x)
val_true = stats.gamma.logpdf(x, a, scale=scale)
val_true = stats.gamma.logpdf(x, alpha, scale=1.0 / beta)
with self.test_session():
self.assertAllClose(gamma.logpdf(xtf, tf.constant(a),
tf.constant(scale)).eval(), val_true)
self.assertAllClose(gamma.logpdf(xtf, tf.constant(alpha),
tf.constant(beta)).eval(), val_true)

def test_0d(self):
self._test(0.3, a=0.5)
self._test(0.7, a=0.5)
self._test(0.3, alpha=0.5, beta=1.0)
self._test(0.7, alpha=0.5, beta=1.0)

self._test(0.3, a=1.0, scale=1.0)
self._test(0.7, a=1.0, scale=1.0)
self._test(0.3, alpha=1.0, beta=1.0)
self._test(0.7, alpha=1.0, beta=1.0)

self._test(0.3, a=0.5, scale=5.0)
self._test(0.7, a=0.5, scale=5.0)
self._test(0.3, alpha=0.5, beta=5.0)
self._test(0.7, alpha=0.5, beta=5.0)

self._test(0.3, a=5.0, scale=0.5)
self._test(0.7, a=5.0, scale=0.5)
self._test(0.3, alpha=5.0, beta=0.5)
self._test(0.7, alpha=5.0, beta=0.5)

def test_1d(self):
self._test([0.5, 1.2, 5.3, 8.7], a=0.5, scale=0.5)
self._test([0.5, 1.2, 5.3, 8.7], alpha=0.5, beta=0.5)

def test_2d(self):
self._test(np.array([[0.5, 1.2, 5.3, 8.7], [0.5, 1.2, 5.3, 8.7]]),
a=0.5, scale=0.5)
self._test(np.array([[0.5, 1.2, 5.3, 8.7], [0.5, 1.2, 5.3, 8.7]],
dtype=np.float32),
alpha=0.5, beta=0.5)
19 changes: 10 additions & 9 deletions tests/test-stats/test_invgamma_entropy.py
Expand Up @@ -19,19 +19,20 @@ def invgamma_entropy_vec(a, scale):

class test_invgamma_entropy_class(tf.test.TestCase):

def _test(self, a, scale=1):
val_true = invgamma_entropy_vec(a, scale=scale)
def _test(self, alpha, beta):
val_true = invgamma_entropy_vec(alpha, scale=beta)
with self.test_session():
self.assertAllClose(invgamma.entropy(a, scale).eval(), val_true,
self.assertAllClose(invgamma.entropy(alpha, beta).eval(), val_true,
atol=1e-4)
self.assertAllClose(invgamma.entropy(tf.constant(a),
tf.constant(scale)).eval(), val_true,
self.assertAllClose(invgamma.entropy(tf.constant(alpha),
tf.constant(beta)).eval(), val_true,
atol=1e-4)

def test_0d(self):
self._test(a=1.0, scale=1.0)
self._test(a=0.5, scale=5.0)
self._test(a=5.0, scale=0.5)
self._test(alpha=1.0, beta=1.0)
self._test(alpha=0.5, beta=5.0)
self._test(alpha=5.0, beta=0.5)

def test_1d(self):
self._test([0.5, 1.2, 5.3, 8.7], [0.5, 1.2, 5.3, 8.7])
self._test(alpha=np.array([0.5, 1.2, 5.3, 8.7], dtype=np.float32),
beta=np.array([0.5, 1.2, 5.3, 8.7], dtype=np.float32))
33 changes: 17 additions & 16 deletions tests/test-stats/test_invgamma_logpdf.py
Expand Up @@ -11,30 +11,31 @@

class test_invgamma_logpdf_class(tf.test.TestCase):

def _test(self, x, a, scale=1):
def _test(self, x, alpha, beta):
xtf = tf.constant(x)
val_true = stats.invgamma.logpdf(x, a, scale=scale)
val_true = stats.invgamma.logpdf(x, alpha, scale=beta)
with self.test_session():
self.assertAllClose(invgamma.logpdf(xtf, a, scale).eval(), val_true)
self.assertAllClose(invgamma.logpdf(xtf, tf.constant(a),
tf.constant(scale)).eval(), val_true)
self.assertAllClose(invgamma.logpdf(xtf, alpha, beta).eval(), val_true)
self.assertAllClose(invgamma.logpdf(xtf, tf.constant(alpha),
tf.constant(beta)).eval(), val_true)

def test_0d(self):
self._test(0.3, a=0.5)
self._test(0.7, a=0.5)
self._test(0.3, alpha=0.5, beta=1.0)
self._test(0.7, alpha=0.5, beta=1.0)

self._test(0.3, a=1.0, scale=1.0)
self._test(0.7, a=1.0, scale=1.0)
self._test(0.3, alpha=1.0, beta=1.0)
self._test(0.7, alpha=1.0, beta=1.0)

self._test(0.3, a=0.5, scale=5.0)
self._test(0.7, a=0.5, scale=5.0)
self._test(0.3, alpha=0.5, beta=5.0)
self._test(0.7, alpha=0.5, beta=5.0)

self._test(0.3, a=5.0, scale=0.5)
self._test(0.7, a=5.0, scale=0.5)
self._test(0.3, alpha=5.0, beta=0.5)
self._test(0.7, alpha=5.0, beta=0.5)

def test_1d(self):
self._test([0.5, 1.2, 5.3, 8.7], a=0.5, scale=0.5)
self._test([0.5, 1.2, 5.3, 8.7], alpha=0.5, beta=0.5)

def test_2d(self):
self._test(np.array([[0.5, 1.2, 5.3, 8.7], [0.5, 1.2, 5.3, 8.7]]),
a=0.5, scale=0.5)
self._test(np.array([[0.5, 1.2, 5.3, 8.7], [0.5, 1.2, 5.3, 8.7]],
dtype=np.float32),
alpha=0.5, beta=0.5)
12 changes: 4 additions & 8 deletions tests/test-stats/test_norm_entropy.py
Expand Up @@ -20,16 +20,12 @@ def norm_entropy_vec(loc, scale):

class test_norm_entropy_class(tf.test.TestCase):

def _test(self, loc, scale):
val_true = norm_entropy_vec(loc, scale)
def _test(self, mu, sigma):
val_true = norm_entropy_vec(mu, sigma)
with self.test_session():
self.assertAllClose(norm.entropy(loc, scale).eval(), val_true)
self.assertAllClose(norm.entropy(mu, sigma).eval(), val_true)
self.assertAllClose(
norm.entropy(tf.constant(loc), tf.constant(scale)).eval(), val_true)

def test_empty(self):
with self.test_session():
self.assertAllClose(norm.entropy().eval(), stats.norm.entropy())
norm.entropy(tf.constant(mu), tf.constant(sigma)).eval(), val_true)

def test_0d(self):
self._test(1.0, 1.0)
Expand Down
21 changes: 13 additions & 8 deletions tests/test-stats/test_norm_logpdf.py
Expand Up @@ -11,21 +11,26 @@

class test_norm_logpdf_class(tf.test.TestCase):

def _test(self, x, loc=0, scale=1):
def _test(self, x, mu, sigma):
xtf = tf.constant(x)
val_true = stats.norm.logpdf(x, loc, scale)
val_true = stats.norm.logpdf(x, mu, sigma)
with self.test_session():
self.assertAllClose(norm.logpdf(xtf, loc, scale).eval(), val_true)
self.assertAllClose(norm.logpdf(xtf, mu, sigma).eval(), val_true)
self.assertAllClose(
norm.logpdf(xtf, tf.constant(loc), tf.constant(scale)).eval(),
norm.logpdf(xtf, tf.constant(mu), tf.constant(sigma)).eval(),
val_true)

def test_0d(self):
self._test(0.0)
self._test(0.623)
self._test(0.0, 0.0, 1.0)
self._test(0.623, 0.0, 1.0)

def test_1d(self):
self._test([0.0, 1.0, 0.58, 2.3])
self._test([0.0, 1.0, 0.58, 2.3],
np.array([0.0] * 4, dtype=np.float32),
np.array([1.0] * 4, dtype=np.float32))

def test_2d(self):
self._test(np.array([[0.0, 1.0, 0.58, 2.3], [0.1, 1.5, 4.18, 0.3]]))
self._test(np.array([[0.0, 1.0, 0.58, 2.3], [0.1, 1.5, 4.18, 0.3]],
dtype=np.float32),
np.array([0.0] * 4, dtype=np.float32),
np.array([1.0] * 4, dtype=np.float32))
21 changes: 12 additions & 9 deletions tests/test-stats/test_t_logpdf.py
Expand Up @@ -11,20 +11,23 @@

class test_t_logpdf_class(tf.test.TestCase):

def _test(self, x, df, loc=0, scale=1):
def _test(self, x, df, mu, sigma):
xtf = tf.constant(x)
val_true = stats.t.logpdf(x, df, loc, scale)
val_true = stats.t.logpdf(x, df, mu, sigma)
with self.test_session():
self.assertAllClose(t.logpdf(xtf, df, loc, scale).eval(), val_true)
self.assertAllClose(t.logpdf(xtf, df, tf.constant(loc),
tf.constant(scale)).eval(), val_true)
self.assertAllClose(t.logpdf(xtf, df, mu, sigma).eval(), val_true)
self.assertAllClose(t.logpdf(xtf, df, tf.constant(mu),
tf.constant(sigma)).eval(), val_true)

def test_0d(self):
self._test(0.0, df=3)
self._test(0.623, df=3)
self._test(0.0, 3.0, 0.0, 1.0)
self._test(0.623, 3.0, 0.0, 1.0)

def test_1d(self):
self._test([0.0, 1.0, 0.58, 2.3], df=3)
self._test([0.0, 1.0, 0.58, 2.3], 3.0, 0.0, 1.0)

def test_2d(self):
self._test(np.array([[0.0, 1.0, 0.58, 2.3], [0.0, 1.0, 0.58, 2.3]]), df=3)
self._test(np.array([[0.0, 1.0, 0.58, 2.3], [0.0, 1.0, 0.58, 2.3]],
dtype=np.float32),
3.0, np.array([0.0] * 4, dtype=np.float32),
np.array([1.0] * 4, dtype=np.float32))
20 changes: 10 additions & 10 deletions tests/test-stats/test_uniform_entropy.py
Expand Up @@ -20,18 +20,18 @@ def uniform_entropy_vec(loc, scale):

class test_uniform_entropy_class(tf.test.TestCase):

def _test(self, loc=0, scale=1):
val_true = uniform_entropy_vec(loc, scale)
def _test(self, a, b):
val_true = uniform_entropy_vec(a, b - a)
with self.test_session():
self.assertAllClose(uniform.entropy(loc, scale).eval(), val_true)
self.assertAllClose(uniform.entropy(tf.constant(loc),
tf.constant(scale)).eval(), val_true)
self.assertAllClose(uniform.entropy(a, b).eval(), val_true)
self.assertAllClose(uniform.entropy(tf.constant(a),
tf.constant(b)).eval(), val_true)

def test_0d(self):
self._test()
self._test(loc=1.0, scale=1.0)
self._test(loc=0.5, scale=5.0)
self._test(loc=5.0, scale=0.5)
self._test(a=1.0, b=2.0)
self._test(a=0.5, b=5.0)
self._test(a=5.0, b=5.5)

def test_1d(self):
self._test([0.5, 0.3, 0.8, 0.2], [0.5, 0.3, 0.8, 0.2])
self._test(np.array([0.5, 0.3, 0.8, 0.2], dtype=np.float32),
np.array([0.6, 0.4, 0.9, 0.3], dtype=np.float32))

0 comments on commit aaef0ac

Please sign in to comment.