Skip to content

Commit

Permalink
Improve tests (#757)
Browse files Browse the repository at this point in the history
* start moving tests

* modifications

* Replace numpy.round_ with numpy.round

* Keep porting tests..

* Add testcategorical

* Finalize test migration

* fixes
  • Loading branch information
tomicapretto authored Nov 17, 2023
1 parent dcd879b commit 8dd25b1
Show file tree
Hide file tree
Showing 8 changed files with 1,563 additions and 1,508 deletions.
2 changes: 1 addition & 1 deletion bambi/priors/prior.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __repr__(self):

def format_arg(value, decimals):
try:
outcome = np.round_(value, decimals)
outcome = np.round(value, decimals)
except: # pylint: disable = bare-except
try:
outcome = value.name
Expand Down
38 changes: 38 additions & 0 deletions tests/test_aliases.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import pytest

import bambi as bmb
import numpy as np
import pandas as pd


@pytest.fixture(scope="module")
Expand All @@ -13,6 +15,26 @@ def anes():
return bmb.load_data("ANES")


@pytest.fixture(scope="module")
def data_100():
size = 100
rng = np.random.default_rng(121195)
data = pd.DataFrame(
{
"n1": rng.normal(size=size),
"n2": rng.normal(size=size),
"n3": rng.normal(size=size),
"b0": rng.binomial(n=1, p=0.5, size=size),
"b1": rng.choice(["a", "b"], size=size),
"count1": rng.poisson(lam=2, size=size),
"count2": rng.poisson(lam=2, size=size),
"cat1": rng.choice(list("MNOP"), size=size),
"cat2": rng.choice(list("FGHIJK"), size=size),
}
)
return data


def test_non_distributional_model(my_data):
# Plain model
formula = bmb.Formula("y ~ x")
Expand Down Expand Up @@ -126,3 +148,19 @@ def test_set_alias_warnings(my_data):
print(model.constant_components)
assert len(record) == 1
assert str(record[0].message) == expected_warning


# FIXME: Move somewhere
def test_set_alias(data_100):
model = bmb.Model("n1 ~ n2 + (n2|cat1)", data_100)
aliases = {
"Intercept": "α",
"n2": "β",
"1|cat1": "α_group",
"n2|cat1": "β_group",
"sigma": "σ",
}
model.set_alias(aliases)
model.build()
new_names = set(["α", "β", "α_group", "α_group_σ", "β_group", "β_group_σ", "σ"])
assert new_names.issubset(set(model.backend.model.named_vars))
78 changes: 78 additions & 0 deletions tests/test_alternative_samplers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import bambi as bmb
import numpy as np
import pandas as pd

import pytest


@pytest.fixture(scope="module")
def data_n100():
size = 100
rng = np.random.default_rng(121195)
data = pd.DataFrame(
{
"b1": rng.binomial(n=1, p=0.5, size=size),
"n1": rng.poisson(lam=2, size=size),
"n2": rng.poisson(lam=2, size=size),
"y1": rng.normal(size=size),
"y2": rng.normal(size=size),
"y3": rng.normal(size=size),
"cat2": rng.choice(["a", "b"], size=size),
"cat4": rng.choice(list("MNOP"), size=size),
"cat5": rng.choice(list("FGHIJK"), size=size),
}
)
return data


def test_laplace():
data = pd.DataFrame(np.repeat((0, 1), (30, 60)), columns=["w"])
priors = {"Intercept": bmb.Prior("Uniform", lower=0, upper=1)}
model = bmb.Model("w ~ 1", data=data, family="bernoulli", priors=priors, link="identity")
results = model.fit(inference_method="laplace")
mode_n = results.posterior["Intercept"].mean().item()
std_n = results.posterior["Intercept"].std().item()
mode_a = data.mean()
std_a = data.std() / len(data) ** 0.5
np.testing.assert_array_almost_equal((mode_n, std_n), (mode_a.item(), std_a.item()), decimal=2)


def test_vi():
data = pd.DataFrame(np.repeat((0, 1), (30, 60)), columns=["w"])
priors = {"Intercept": bmb.Prior("Uniform", lower=0, upper=1)}
model = bmb.Model("w ~ 1", data=data, family="bernoulli", priors=priors, link="identity")
results = model.fit(inference_method="vi", method="advi")
samples = results.sample(1000).posterior["Intercept"]
mode_n = samples.mean()
std_n = samples.std()
mode_a = data.mean()
std_a = data.std() / len(data) ** 0.5
np.testing.assert_array_almost_equal(
(mode_n.item(), std_n.item()), (mode_a.item(), std_a.item()), decimal=2
)


@pytest.mark.parametrize(
"args",
[
("mcmc", {}),
("nuts_numpyro", {"chain_method": "vectorized"}),
("nuts_blackjax", {"chain_method": "vectorized"}),
],
)
def test_logistic_regression_categoric_alternative_samplers(data_n100, args):
model = bmb.Model("b1 ~ n1", data_n100, family="bernoulli")
model.fit(tune=50, draws=50, inference_method=args[0], **args[1])


@pytest.mark.parametrize(
"args",
[
("mcmc", {}),
("nuts_numpyro", {"chain_method": "vectorized"}),
("nuts_blackjax", {"chain_method": "vectorized"}),
],
)
def test_regression_alternative_samplers(data_n100, args):
model = bmb.Model("n1 ~ n2", data_n100)
model.fit(tune=50, draws=50, inference_method=args[0], **args[1])
Loading

0 comments on commit 8dd25b1

Please sign in to comment.