-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathtest_bart.py
258 lines (222 loc) · 8.22 KB
/
test_bart.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import numpy as np
import pymc as pm
import pytest
from numpy.testing import assert_almost_equal, assert_array_equal
from pymc.initial_point import make_initial_point_fn
from pymc.logprob.basic import transformed_conditional_logp
import pymc_bart as pmb
def assert_moment_is_expected(model, expected, check_finite_logp=True):
fn = make_initial_point_fn(
model=model,
return_transformed=False,
default_strategy="support_point",
)
moment = fn(0)["x"]
expected = np.asarray(expected)
try:
random_draw = model["x"].eval()
except NotImplementedError:
random_draw = moment
assert moment.shape == expected.shape
assert expected.shape == random_draw.shape
assert np.allclose(moment, expected)
if check_finite_logp:
logp_moment = (
transformed_conditional_logp(
(model["x"],),
rvs_to_values={model["x"]: pm.math.constant(moment)},
rvs_to_transforms={},
rvs_to_total_sizes={},
)[0]
.sum()
.eval()
)
assert np.isfinite(logp_moment)
@pytest.mark.parametrize(
argnames="response",
argvalues=["constant", "linear"],
ids=["constant", "linear-response"],
)
def test_bart_vi(response):
X = np.random.normal(0, 1, size=(250, 3))
Y = np.random.normal(0, 1, size=250)
X[:, 0] = np.random.normal(Y, 0.1)
with pm.Model() as model:
mu = pmb.BART("mu", X, Y, m=10, response=response)
sigma = pm.HalfNormal("sigma", 1)
y = pm.Normal("y", mu, sigma, observed=Y)
idata = pm.sample(tune=200, draws=200, random_seed=3415)
var_imp = (
idata.sample_stats["variable_inclusion"]
.stack(samples=("chain", "draw"))
.mean("samples")
)
var_imp /= var_imp.sum()
assert var_imp[0] > var_imp[1:].sum()
assert_almost_equal(var_imp.sum(), 1)
@pytest.mark.parametrize(
argnames="response",
argvalues=["constant", "linear"],
ids=["constant", "linear-response"],
)
def test_missing_data(response):
X = np.random.normal(0, 1, size=(50, 2))
Y = np.random.normal(0, 1, size=50)
X[10:20, 0] = np.nan
with pm.Model() as model:
mu = pmb.BART("mu", X, Y, m=10, response=response)
sigma = pm.HalfNormal("sigma", 1)
pm.Normal("y", mu, sigma, observed=Y)
pm.sample(tune=100, draws=100, chains=1, random_seed=3415)
@pytest.mark.parametrize(
argnames="response",
argvalues=["constant", "linear"],
ids=["constant", "linear-response"],
)
def test_shared_variable(response):
X = np.random.normal(0, 1, size=(50, 2))
Y = np.random.normal(0, 1, size=50)
with pm.Model() as model:
data_X = pm.Data("data_X", X)
mu = pmb.BART("mu", data_X, Y, m=2, response=response)
sigma = pm.HalfNormal("sigma", 1)
y = pm.Normal("y", mu, sigma, observed=Y, shape=mu.shape)
idata = pm.sample(tune=100, draws=100, chains=2, random_seed=3415)
ppc = pm.sample_posterior_predictive(idata)
pm.set_data({"data_X": X[:3]})
ppc2 = pm.sample_posterior_predictive(idata)
assert ppc.posterior_predictive["y"].shape == (2, 100, 50)
assert ppc2.posterior_predictive["y"].shape == (2, 100, 3)
@pytest.mark.parametrize(
argnames="response",
argvalues=["constant", "linear"],
ids=["constant", "linear-response"],
)
def test_shape(response):
X = np.random.normal(0, 1, size=(250, 3))
Y = np.random.normal(0, 1, size=250)
with pm.Model() as model:
w = pmb.BART("w", X, Y, m=2, response=response, shape=(2, 250))
y = pm.Normal("y", w[0], pm.math.abs(w[1]), observed=Y)
idata = pm.sample(tune=50, draws=10, random_seed=3415)
assert model.initial_point()["w"].shape == (2, 250)
assert idata.posterior.coords["w_dim_0"].data.size == 2
assert idata.posterior.coords["w_dim_1"].data.size == 250
class TestUtils:
X_norm = np.random.normal(0, 1, size=(50, 2))
X_binom = np.random.binomial(1, 0.5, size=(50, 1))
X = np.hstack([X_norm, X_binom])
Y = np.random.normal(0, 1, size=50)
with pm.Model() as model:
mu = pmb.BART("mu", X, Y, m=10)
sigma = pm.HalfNormal("sigma", 1)
y = pm.Normal("y", mu, sigma, observed=Y)
idata = pm.sample(tune=200, draws=200, random_seed=3415)
def test_sample_posterior(self):
all_trees = self.mu.owner.op.all_trees
rng = np.random.default_rng(3)
pred_all = pmb.utils._sample_posterior(all_trees, X=self.X, rng=rng, size=2)
rng = np.random.default_rng(3)
pred_first = pmb.utils._sample_posterior(all_trees, X=self.X[:10], rng=rng)
assert_almost_equal(pred_first[0], pred_all[0, :10], decimal=4)
assert pred_all.shape == (2, 50, 1)
assert pred_first.shape == (1, 10, 1)
@pytest.mark.parametrize(
"kwargs",
[
{},
{
"samples": 2,
"var_discrete": [3],
},
{"instances": 2},
{"var_idx": [0], "smooth": False, "color": "k"},
{"grid": (1, 2), "sharey": "none", "alpha": 1},
{"var_discrete": [0]},
],
)
def test_ice(self, kwargs):
pmb.plot_ice(self.mu, X=self.X, Y=self.Y, **kwargs)
@pytest.mark.parametrize(
"kwargs",
[
{},
{
"samples": 2,
"xs_interval": "quantiles",
"xs_values": [0.25, 0.5, 0.75],
"var_discrete": [3],
},
{"var_idx": [0], "smooth": False, "color": "k"},
{"grid": (1, 2), "sharey": "none", "alpha": 1},
{"var_discrete": [0]},
],
)
def test_pdp(self, kwargs):
pmb.plot_pdp(self.mu, X=self.X, Y=self.Y, **kwargs)
@pytest.mark.parametrize(
"kwargs",
[
{"samples": 50},
{"labels": ["A", "B", "C"], "samples": 2, "figsize": (6, 6)},
],
)
def test_vi(self, kwargs):
samples = kwargs.pop("samples")
vi_results = pmb.compute_variable_importance(
self.idata, bartrv=self.mu, X=self.X, samples=samples
)
pmb.plot_variable_importance(vi_results, **kwargs)
pmb.plot_scatter_submodels(vi_results, **kwargs)
def test_pdp_pandas_labels(self):
pd = pytest.importorskip("pandas")
X_names = ["norm1", "norm2", "binom"]
X_pd = pd.DataFrame(self.X, columns=X_names)
Y_pd = pd.Series(self.Y, name="response")
axes = pmb.plot_pdp(self.mu, X=X_pd, Y=Y_pd)
figure = axes[0].figure
assert figure.texts[0].get_text() == "Partial response"
assert_array_equal([ax.get_xlabel() for ax in axes], X_names)
@pytest.mark.parametrize(
"size, expected",
[
(None, np.zeros(50)),
],
)
def test_bart_moment(size, expected):
X = np.zeros((50, 2))
Y = np.zeros(50)
with pm.Model() as model:
pmb.BART("x", X=X, Y=Y, size=size)
assert_moment_is_expected(model, expected)
@pytest.mark.parametrize(
argnames="separate_trees,split_rule",
argvalues=[
(False, pmb.ContinuousSplitRule),
(False, pmb.OneHotSplitRule),
(False, pmb.SubsetSplitRule),
(True, pmb.ContinuousSplitRule),
],
ids=["continuous", "one-hot", "subset", "separate-trees"],
)
def test_categorical_model(separate_trees, split_rule):
Y = np.array([0, 0, 0, 1, 1, 1, 2, 2, 2])
X = np.concatenate([Y[:, None], np.random.randint(0, 6, size=(9, 4))], axis=1)
with pm.Model() as model:
lo = pmb.BART(
"logodds",
X,
Y,
m=2,
shape=(3, 9),
split_rules=[split_rule] * 5,
separate_trees=separate_trees,
)
y = pm.Categorical("y", p=pm.math.softmax(lo.T, axis=-1), observed=Y)
idata = pm.sample(tune=300, draws=300, random_seed=3415)
idata = pm.sample_posterior_predictive(
idata, predictions=True, extend_inferencedata=True, random_seed=3415
)
# Fit should be good enough so right category is selected over 50% of time
assert (idata.predictions.y.median(["chain", "draw"]) == Y).all()
assert pmb.compute_variable_importance(idata, bartrv=lo, X=X)["preds"].shape == (5, 50, 9, 3)