From 68df69b1f98f08dc86588dd6326a4b162589bfec Mon Sep 17 00:00:00 2001 From: Felix Wick Date: Fri, 18 Aug 2023 12:44:00 +0200 Subject: [PATCH] added likelihood example (although a bit unfitting :)) --- tests/test_integration.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/test_integration.py b/tests/test_integration.py index e2a85d5..41a3a17 100644 --- a/tests/test_integration.py +++ b/tests/test_integration.py @@ -3,6 +3,7 @@ import pytest from sklearn.preprocessing import OrdinalEncoder +from scipy.special import factorial from cyclic_boosting import flags, common_smoothers, observers from cyclic_boosting.smoothing.onedim import SeasonalSmoother, IsotonicRegressor @@ -722,6 +723,38 @@ def test_multiplicative_regression_mse(): np.testing.assert_almost_equal(mad, 1.7171, 3) +def poisson_likelihood(prediction, y, weights): + negative_log_likelihood = np.nanmean(prediction + np.log(factorial(y)) - np.log(prediction) * y) + return negative_log_likelihood + +# commented out due to rather long runtime +# def test_multiplicative_regression_likelihood(): +# np.random.seed(42) + +# df = pd.read_csv("./tests/integration_test_data.csv") + +# X, y = prepare_data(df) +# X = X[["dayofweek", "L_ID", "PG_ID_3", "P_ID", "PROMOTION_TYPE", "price_ratio", "dayofyear"]] + +# fp = feature_properties() +# # plobs = [ +# # observers.PlottingObserver(iteration=-1) +# # ] +# CB_est = pipeline_CBMultiplicativeRegressor( +# # observers=plobs, +# feature_properties=fp, +# costs=poisson_likelihood, +# ) +# CB_est.fit(X.copy(), y) +# # plot_CB('analysis_CB_iterlast', +# # [CB_est[-1].observers[-1]], CB_est[-2]) + +# yhat = CB_est.predict(X.copy()) + +# mad = np.nanmean(np.abs(y - yhat)) +# np.testing.assert_almost_equal(mad, 1.9310, 3) + + def costs_logloss(prediction, y, weights): prediction = np.where(prediction < 0.001, 0.001, prediction) prediction = np.where(prediction > 0.999, 0.999, prediction)