Skip to content

Commit

Permalink
added likelihood example (although a bit unfitting :))
Browse files Browse the repository at this point in the history
  • Loading branch information
fwick-panasonic committed Aug 18, 2023
1 parent bcccc56 commit 68df69b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 68df69b

Please sign in to comment.