Skip to content

Commit

Permalink
check for valid quantiles for QPD, do not remove price ratio feature …
Browse files Browse the repository at this point in the history
…in integration tests
  • Loading branch information
FelixWick committed Oct 28, 2023
1 parent 3682f59 commit 8f5d039
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
3 changes: 3 additions & 0 deletions cyclic_boosting/quantile_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ def __init__(
else:
raise Exception("Invalid version.")

if (qv_low > qv_median) or (qv_high < qv_median):
raise ValueError("The SPT values need to be monotonically increasing.")

self.l = l

self.c = self.phi.ppf(1 - alpha)
Expand Down
31 changes: 17 additions & 14 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,8 +254,8 @@ def test_nbinom_regression_ndarray(prepare_data, default_features, feature_prope

@pytest.fixture(scope="function")
def cb_exponential_regressor_model(features, feature_properties):
features = features
features.remove("price_ratio")
features_noprice = features.copy()
features_noprice.remove("price_ratio")
price_features = [
"L_ID",
"PG_ID_1",
Expand All @@ -278,7 +278,7 @@ def cb_exponential_regressor_model(features, feature_properties):

CB_pipeline = pipeline_CBExponential(
feature_properties=feature_properties,
standard_feature_groups=features,
standard_feature_groups=features_noprice,
external_feature_groups=price_features,
external_colname="price_ratio",
observers=plobs,
Expand Down Expand Up @@ -464,7 +464,7 @@ def test_multiplicative_quantile_regression_median(is_plot, prepare_data, featur
np.testing.assert_almost_equal(quantile_acc, 0.5043, 3)

mad = np.nanmean(np.abs(y - yhat))
np.testing.assert_almost_equal(mad, 1.6575, 3)
np.testing.assert_almost_equal(mad, 1.6559, 3)


def test_multiplicative_quantile_regression_90(is_plot, prepare_data, features, feature_properties):
Expand Down Expand Up @@ -506,13 +506,16 @@ def test_multiplicative_quantile_regression_pdf_J_QPD_S(is_plot, prepare_data, f
cdf_truth_list = []
n_samples = len(X)
for i in range(n_samples):
j_qpd_s = J_QPD_S(0.2, quantile_values[0, i], quantile_values[1, i], quantile_values[2, i])
try:
j_qpd_s = J_QPD_S(0.2, quantile_values[0, i], quantile_values[1, i], quantile_values[2, i])
except ValueError:
continue
np.testing.assert_almost_equal(j_qpd_s.ppf(0.2), quantile_values[0, i], 3)
np.testing.assert_almost_equal(j_qpd_s.ppf(0.5), quantile_values[1, i], 3)
np.testing.assert_almost_equal(j_qpd_s.ppf(0.8), quantile_values[2, i], 3)
if i == 24:
np.testing.assert_almost_equal(j_qpd_s.ppf(0.1), 0.592, 3)
np.testing.assert_almost_equal(j_qpd_s.ppf(0.9), 5.783, 3)
np.testing.assert_almost_equal(j_qpd_s.ppf(0.1), 0.457, 3)
np.testing.assert_almost_equal(j_qpd_s.ppf(0.9), 5.509, 3)

if is_plot:
plt.plot([0.2, 0.5, 0.8], [quantile_values[0, i], quantile_values[1, i], quantile_values[2, i]], "ro")
Expand Down Expand Up @@ -552,9 +555,9 @@ def test_multiplicative_quantile_regression_spline(is_plot, prepare_data, featur

i = 24
spl_fit = quantile_fit_spline(quantiles, quantile_values[:, i])
np.testing.assert_almost_equal(spl_fit(0.2), 0.679, 3)
np.testing.assert_almost_equal(spl_fit(0.5), 2.202, 3)
np.testing.assert_almost_equal(spl_fit(0.8), 4.297, 3)
np.testing.assert_almost_equal(spl_fit(0.2), 0.527, 3)
np.testing.assert_almost_equal(spl_fit(0.5), 2.193, 3)
np.testing.assert_almost_equal(spl_fit(0.8), 4.21, 3)

if is_plot:
plt.plot(quantiles, quantile_values[:, i], "ro")
Expand Down Expand Up @@ -587,9 +590,9 @@ def test_multiplicative_quantile_regression_pdf_gamma(is_plot, prepare_data, fea
for i in range(n_samples):
if i == 24:
gamma_fit = quantile_fit_gamma(quantiles, quantile_values[:, i])
np.testing.assert_almost_equal(gamma_fit(0.2), 0.829, 3)
np.testing.assert_almost_equal(gamma_fit(0.5), 2.049, 3)
np.testing.assert_almost_equal(gamma_fit(0.8), 4.147, 3)
np.testing.assert_almost_equal(gamma_fit(0.2), 0.779, 3)
np.testing.assert_almost_equal(gamma_fit(0.5), 1.986, 3)
np.testing.assert_almost_equal(gamma_fit(0.8), 4.097, 3)

if is_plot:
plt.plot(quantiles, quantile_values[:, i], "ro")
Expand Down Expand Up @@ -824,4 +827,4 @@ def test_classification_logloss(is_plot, prepare_data, cb_classifier_logloss_mod
yhat = CB_est.predict(X.copy())

mad = np.nanmean(np.abs(y - yhat))
np.testing.assert_almost_equal(mad, 0.408, 3)
np.testing.assert_almost_equal(mad, 0.404, 3)

0 comments on commit 8f5d039

Please sign in to comment.