Skip to content

Commit

Permalink
fix uniform; add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dustinvtran committed May 14, 2016
1 parent 3b57744 commit fe01657
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 1 deletion.
2 changes: 1 addition & 1 deletion edward/stats/distributions.py
Expand Up @@ -318,7 +318,7 @@ def rvs(self, loc=0, scale=1, size=1):
def logpdf(self, x, loc=0, scale=1):
# Note there is no error checking if x is outside domain.
scale = tf.cast(tf.squeeze(scale), dtype=tf.float32)
return -tf.log(scale)
return tf.squeeze(tf.ones([get_dims(x)[0]]) * -tf.log(scale))

class Wishart:
def rvs(self, df, scale, size=1):
Expand Down
37 changes: 37 additions & 0 deletions tests/test_stats_binom_logpmf.py
@@ -0,0 +1,37 @@
from __future__ import print_function
import numpy as np
import tensorflow as tf

from edward.stats import binom
from scipy import stats

sess = tf.Session()

def _assert_eq(val_ed, val_true):
with sess.as_default():
# NOTE: since Tensorflow has no special functions, the values here are
# only an approximation
assert np.allclose(val_ed.eval(), val_true, atol=1e-4)

def _test_logpmf(x, n, p):
xtf = tf.constant(x)
val_true = stats.binom.logpmf(x, n, p)
_assert_eq(binom.logpmf(xtf, n, p), val_true)
_assert_eq(binom.logpmf(xtf, tf.constant(n), tf.constant(p)), val_true)
_assert_eq(binom.logpmf(xtf, tf.constant([n]), tf.constant([p])), val_true)

def test_logpmf_int_scalar():
_test_logpmf(0, 1, 0.5)
_test_logpmf(1, 2, 0.75)

def test_logpmf_float_scalar():
_test_logpmf(0.0, 1, 0.5)
_test_logpmf(1.0, 2, 0.75)

def test_logpmf_int_1d():
_test_logpmf([0, 1, 0], 1, 0.5)
_test_logpmf([1, 0, 0], 1, 0.75)

def test_logpmf_float_1d():
_test_logpmf([0.0, 1.0, 0.0], 1, 0.5)
_test_logpmf([1.0, 0.0, 0.0], 1, 0.75)
28 changes: 28 additions & 0 deletions tests/test_stats_chi2_logpdf.py
@@ -0,0 +1,28 @@
from __future__ import print_function
import numpy as np
import tensorflow as tf

from edward.stats import chi2
from scipy import stats

sess = tf.Session()

def _assert_eq(val_ed, val_true):
with sess.as_default():
# NOTE: since Tensorflow has no special functions, the values here are
# only an approximation
assert np.allclose(val_ed.eval(), val_true, atol=1e-4)

def _test_logpdf(x, df):
xtf = tf.constant(x)
val_true = stats.chi2.logpdf(x, df)
_assert_eq(chi2.logpdf(xtf, df), val_true)
_assert_eq(chi2.logpdf(xtf, tf.constant(df)), val_true)
_assert_eq(chi2.logpdf(xtf, tf.constant([df])), val_true)

def test_logpdf_scalar():
_test_logpdf(0.2, df=2)
_test_logpdf(0.623, df=2)

def test_logpdf_1d():
_test_logpdf([0.1, 1.0, 0.58, 2.3], df=3)
35 changes: 35 additions & 0 deletions tests/test_stats_geom_logpmf.py
@@ -0,0 +1,35 @@
from __future__ import print_function
import numpy as np
import tensorflow as tf

from edward.stats import geom
from scipy import stats

sess = tf.Session()

def _assert_eq(val_ed, val_true):
with sess.as_default():
assert np.allclose(val_ed.eval(), val_true)

def _test_logpmf(x, p):
xtf = tf.constant(x)
val_true = stats.geom.logpmf(x, p)
_assert_eq(geom.logpmf(xtf, p), val_true)
_assert_eq(geom.logpmf(xtf, tf.constant(p)), val_true)
_assert_eq(geom.logpmf(xtf, tf.constant([p])), val_true)

def test_logpmf_int_scalar():
_test_logpmf(1, 0.5)
_test_logpmf(2, 0.75)

def test_logpmf_float_scalar():
_test_logpmf(1.0, 0.5)
_test_logpmf(2.0, 0.75)

def test_logpmf_int_1d():
_test_logpmf([1, 5, 3], 0.5)
_test_logpmf([2, 8, 2], 0.75)

def test_logpmf_float_1d():
_test_logpmf([1.0, 5.0, 3.0], 0.5)
_test_logpmf([2.0, 8.0, 2.0], 0.75)
26 changes: 26 additions & 0 deletions tests/test_stats_lognorm_logpdf.py
@@ -0,0 +1,26 @@
from __future__ import print_function
import numpy as np
import tensorflow as tf

from edward.stats import lognorm
from scipy import stats

sess = tf.Session()

def _assert_eq(val_ed, val_true):
with sess.as_default():
assert np.allclose(val_ed.eval(), val_true)

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)

def test_logpdf_scalar():
_test_logpdf(2.0)
_test_logpdf(0.623)

def test_logpdf_1d():
_test_logpdf([2.0, 1.0, 0.58, 2.3])
39 changes: 39 additions & 0 deletions tests/test_stats_nbinom_logpmf.py
@@ -0,0 +1,39 @@
from __future__ import print_function
import numpy as np
import tensorflow as tf

from edward.stats import nbinom
from scipy import stats

sess = tf.Session()

def _assert_eq(val_ed, val_true):
with sess.as_default():
# NOTE: since Tensorflow has no special functions, the values here are
# only an approximation
assert np.allclose(val_ed.eval(), val_true, atol=1e-4)

def _test_logpmf(x, n, p):
xtf = tf.constant(x)
val_true = stats.nbinom.logpmf(x, n, p)
_assert_eq(nbinom.logpmf(xtf, n, p), val_true)
_assert_eq(nbinom.logpmf(xtf, tf.constant(n), tf.constant(p)), val_true)
_assert_eq(nbinom.logpmf(xtf, tf.constant(n), tf.constant([p])), val_true)
_assert_eq(nbinom.logpmf(xtf, tf.constant([n]), tf.constant(p)), val_true)
_assert_eq(nbinom.logpmf(xtf, tf.constant([n]), tf.constant([p])), val_true)

def test_logpmf_int_scalar():
_test_logpmf(1, 5, 0.5)
_test_logpmf(2, 5, 0.75)

def test_logpmf_float_scalar():
_test_logpmf(1.0, 5, 0.5)
_test_logpmf(2.0, 5, 0.75)

def test_logpmf_int_1d():
_test_logpmf([1, 5, 3], 5, 0.5)
_test_logpmf([2, 8, 2], 5, 0.75)

def test_logpmf_float_1d():
_test_logpmf([1.0, 5.0, 3.0], 5, 0.5)
_test_logpmf([2.0, 8.0, 2.0], 5, 0.75)
37 changes: 37 additions & 0 deletions tests/test_stats_uniform_logpdf.py
@@ -0,0 +1,37 @@
from __future__ import print_function
import numpy as np
import tensorflow as tf

from edward.stats import uniform
from scipy import stats

sess = tf.Session()

def _assert_eq(val_ed, val_true):
with sess.as_default():
assert np.allclose(val_ed.eval(), val_true)

def _test_logpdf(x, loc=0, scale=1):
xtf = tf.constant(x)
val_true = stats.uniform.logpdf(x, loc, scale)
_assert_eq(uniform.logpdf(xtf, loc, scale), val_true)
_assert_eq(uniform.logpdf(xtf, tf.constant(loc), tf.constant(scale)), val_true)
_assert_eq(uniform.logpdf(xtf, tf.constant([loc]), tf.constant(scale)), val_true)
_assert_eq(uniform.logpdf(xtf, tf.constant(loc), tf.constant([scale])), val_true)
_assert_eq(uniform.logpdf(xtf, tf.constant([loc]), tf.constant([scale])), val_true)

def test_logpdf_scalar():
_test_logpdf(0.3)
_test_logpdf(0.7)

_test_logpdf(1.3, loc=1.0, scale=1.0)
_test_logpdf(1.7, loc=1.0, scale=1.0)

_test_logpdf(2.3, loc=0.5, scale=5.0)
_test_logpdf(2.7, loc=0.5, scale=5.0)

_test_logpdf(5.3, loc=5.0, scale=0.5)
_test_logpdf(5.1, loc=5.0, scale=0.5)

def test_logpdf_1d():
_test_logpdf([0.5, 0.3, 0.8, 0.2], loc=0.1, scale=0.9)

0 comments on commit fe01657

Please sign in to comment.