Skip to content

Commit 1ad016c

Browse files
author
Thibault Cordier
committed
ADD: coverage validity test
1 parent 5e64851 commit 1ad016c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

mapie/tests/test_regression.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,38 @@ def test_predict_output_shape(
259259
assert y_pis.shape == (X.shape[0], 2, n_alpha)
260260

261261

262+
@pytest.mark.parametrize("delta", [0.5, 0.6, 0.7, 0.8])
263+
@pytest.mark.parametrize("n_calib", [10, 20, 50, 100])
264+
def test_coverage_validity(delta: float, n_calib: int) -> None:
265+
"""
266+
Test that the prefit method provides valid coverage
267+
for different calibration data sizes and coverage targets.
268+
"""
269+
n_split, n_train, n_test = 1000, 100, 100
270+
n_all = n_train + n_calib + n_test
271+
X, y = make_regression(n_all, random_state=random_state)
272+
273+
X_train, X_cal_test, y_train, y_cal_test = \
274+
train_test_split(X, y, train_size=n_train, random_state=random_state)
275+
276+
model = LinearRegression()
277+
model.fit(X_train, y_train)
278+
279+
coverage_list = []
280+
for _ in range(n_split):
281+
mapie_reg = MapieRegressor(estimator=model, method="base", cv="prefit")
282+
X_cal, X_test, y_cal, y_test = \
283+
train_test_split(X_cal_test, y_cal_test, test_size=n_test)
284+
mapie_reg.fit(X_cal, y_cal)
285+
_, y_pis = mapie_reg.predict(X_test, alpha=1-delta)
286+
coverage = \
287+
regression_coverage_score(y_test, y_pis[:, 0, 0], y_pis[:, 1, 0])
288+
coverage_list.append(coverage)
289+
290+
mean_coverage = np.mean(coverage_list)
291+
np.testing.assert_array_less(delta, mean_coverage)
292+
293+
262294
def test_same_results_prefit_split() -> None:
263295
"""
264296
Test checking that if split and prefit method have exactly

0 commit comments

Comments
 (0)