-
Notifications
You must be signed in to change notification settings - Fork 252
/
Copy pathtest_basic.py
177 lines (149 loc) · 5.83 KB
/
test_basic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import numpy as np
import pandas as pd
import pymc as pm
import pytest
from arviz import InferenceData, from_dict
from pymc_marketing.clv.models.basic import CLVModel
from tests.conftest import set_model_fit
class CLVModelTest(CLVModel):
_model_type = "CLVModelTest"
def __init__(self, data=None, **kwargs):
if data is None:
data = pd.DataFrame({"y": np.random.randn(10)})
super().__init__(data=data, **kwargs)
@property
def default_model_config(self):
return {
"x": {"dist": "Normal", "kwargs": {"mu": 0, "sigma": 1}},
}
def build_model(self):
x_prior = self._create_distribution(self.model_config["x"])
with pm.Model() as self.model:
x = self.model.register_rv(x_prior, name="x")
pm.Normal("y", mu=x, sigma=1, observed=self.data["y"])
@pytest.fixture(scope="module")
def posterior():
# Create a random numpy array for posterior samples
posterior_samples = np.random.randn(
4, 100, 2
) # shape convention: (chain, draw, *shape)
# Create a dictionary for posterior
posterior_dict = {"theta": posterior_samples}
return from_dict(posterior=posterior_dict)
class TestCLVModel:
def test_repr(self):
model = CLVModelTest()
assert model.__repr__() == "CLVModelTest"
model.build_model()
assert model.__repr__() == "CLVModelTest\nx ~ Normal(0, 1)\ny ~ Normal(x, 1)"
def test_create_distribution_from_wrong_prior(self):
model = CLVModelTest()
with pytest.raises(
ValueError,
match="Distribution definitely_not_PyMC_dist does not exist in PyMC",
):
model._create_distribution(
{"dist": "definitely_not_PyMC_dist", "kwargs": {"alpha": 1, "beta": 1}}
)
def test_fit_mcmc(self):
model = CLVModelTest()
model.build_model()
idata = model.fit(
tune=5,
chains=2,
draws=10,
compute_convergence_checks=False,
)
assert isinstance(idata, InferenceData)
assert len(idata.posterior.chain) == 2
assert len(idata.posterior.draw) == 10
assert model.fit_result is idata.posterior
def test_fit_map(self):
model = CLVModelTest()
model.build_model()
idata = model.fit(fit_method="map")
assert isinstance(idata, InferenceData)
assert len(idata.posterior.chain) == 1
assert len(idata.posterior.draw) == 1
assert model.fit_result is idata.posterior
# Check that summary only includes single value
summ = model.fit_summary()
assert isinstance(summ, pd.Series)
assert summ.name == "value"
def test_wrong_fit_method(self):
model = CLVModelTest()
with pytest.raises(
ValueError,
match=r"Fit method options are \['mcmc', 'map'\], got: wrong_method",
):
model.fit(fit_method="wrong_method")
def test_fit_result_error(self):
model = CLVModelTest()
with pytest.raises(RuntimeError, match="The model hasn't been fit yet"):
model.fit_result
def test_load(self):
model = CLVModelTest()
model.build_model()
model.fit(tune=0, chains=2, draws=5)
model.save("test_model")
model2 = model.load("test_model")
assert model2.fit_result is not None
assert model2.model is not None
os.remove("test_model")
def test_default_sampler_config(self):
model = CLVModelTest()
assert model.sampler_config == {}
def test_set_fit_result(self):
model = CLVModelTest()
model.build_model()
model.idata = None
fake_fit = pm.sample_prior_predictive(
samples=50, model=model.model, random_seed=1234
)
fake_fit.add_groups(dict(posterior=fake_fit.prior))
model.fit_result = fake_fit
with pytest.warns(UserWarning, match="Overriding pre-existing fit_result"):
model.fit_result = fake_fit
model.idata = None
model.fit_result = fake_fit
def test_fit_summary_for_mcmc(self):
model = CLVModelTest()
model.build_model()
model.fit(tune=0, chains=2, draws=5)
summ = model.fit_summary()
assert isinstance(summ, pd.DataFrame)
def test_serializable_model_config(self):
model = CLVModelTest()
serializable_config = model._serializable_model_config
assert isinstance(serializable_config, dict)
assert serializable_config == model.model_config
def test_fail_id_after_load(self, monkeypatch):
# This is the new behavior for the property
def mock_property(self):
return "for sure not correct id"
# Now create an instance of MyClass
mock_basic = CLVModelTest()
mock_basic.fit(tune=0, chains=2, draws=5)
mock_basic.save("test_model")
# Apply the monkeypatch for the property
monkeypatch.setattr(CLVModelTest, "id", property(mock_property))
with pytest.raises(
ValueError,
match="Inference data not compatible with CLVModelTest",
):
CLVModelTest.load("test_model")
os.remove("test_model")
def test_thin_fit_result(self):
data = pd.DataFrame(dict(y=[-3, -2, -1]))
model = CLVModelTest(data=data)
model.build_model()
fake_idata = from_dict(dict(x=np.random.normal(size=(4, 1000))))
set_model_fit(model, fake_idata)
thin_model = model.thin_fit_result(keep_every=20)
assert thin_model is not model
assert thin_model.idata is not model.idata
assert len(thin_model.idata.posterior["x"].chain) == 4
assert len(thin_model.idata.posterior["x"].draw) == 50
assert thin_model.data is not model.data
assert np.all(thin_model.data == model.data)