MMMが将来予測で利用できるかの検討¶
準備¶
モジュールの読み込み¶
In [ ]:
from mmm_functions import *
データセットの読み込み¶
In [ ]:
# データセット読み込み
dataset = 'MMM_ts_manga5.csv'
df = pd.read_csv(
dataset,
parse_dates=['week'],
index_col='week',
)
アドストックを考慮する特徴量のリスト¶
In [ ]:
# アドストックを考慮する特徴量のリストを作成
apply_effects_features = ['traditional', 'internet', 'promotional']
季節性を表現する三角関数特徴量を追加¶
In [ ]:
# 三角関数特徴量を追加したデータフレームを表示して確認
df = add_fourier_terms(df, num=5, seasonal=52.25)
説明変数と目的変数の設定¶
In [ ]:
# 対象データ期間、ここでは直近5年間(52週間×5)を指定
data_term = int(52.25 * 5)
# 説明変数Xと目的変数yに分解
X = df.drop(columns=['sales'])[-data_term:]
y = df['sales'][-data_term:]
学習データとテストデータに分割¶
In [ ]:
# テストデータ期間、ここでは直近1年間(52週間)を指定
term = 52
# 学習データとテストデータに分割
X_train, X_test = X[:-term], X[-term:]
y_train, y_test = y[:-term], y[-term:]
学習データでMMM構築¶
ハイパーパラメータ自動調整¶
In [ ]:
study = run_optimization(
regarima_objective,
X_train, y_train,
apply_effects_features,
n_trials=1000
)
Best trial: 465. Best value: 1.39196e+08: 100%|██████████| 1000/1000 [2:22:07<00:00, 8.53s/it]Best trial: Value: 139195577.01378402 Params: carryover_length_traditional: 2 carryover_peak_traditional: 1 carryover_rate1_traditional: 0.6486743800140834 carryover_rate2_traditional: 0.5714128641590107 carryover_c1_traditional: 1.1023551799789082 carryover_c2_traditional: 1.2471345305654304 saturation_function_traditional: exponential curve_param_d_traditional: 4.754232174124576 carryover_length_internet: 1 carryover_peak_internet: 1 carryover_rate1_internet: 0.7627560266710607 carryover_rate2_internet: 0.38881893338218193 carryover_c1_internet: 1.560448887792296 carryover_c2_internet: 1.1816466447208518 saturation_function_internet: exponential curve_param_d_internet: 5.865583617579571 carryover_length_promotional: 1 carryover_peak_promotional: 1 carryover_rate1_promotional: 0.6736610202821939 carryover_rate2_promotional: 0.5597957240432434 carryover_c1_promotional: 0.8672204210246754 carryover_c2_promotional: 1.9616376745909556 saturation_function_promotional: exponential curve_param_d_promotional: 5.075326716961253 alpha: 0.039424834140418086
In [ ]:
# optunaのstudyの保存
joblib.dump(study, 'train_ridgeMMM_study.joblib')
Out[ ]:
['train_ridgeMMM_study.joblib']
MMM構築¶
In [ ]:
trained_model,model_params,pred = create_model_from_trial_regarima(
study.best_trial,
X_train, y_train,
apply_effects_features
)
RMSE: 58429219.42892168 MAE: 43509340.96651112 MAPE: 0.038400402224866625 R2: 0.9939853574754446
テストデータで検証¶
In [ ]:
pred = test_evaluate_regarima(trained_model, X_test, y_test, term)
RMSE: 63439231.308501214 MAE: 45591859.71753966 MAPE: 0.03413458585900201 R2: 0.9920426478968533