最適投資配分¶
準備¶
モジュールの読み込み¶
In [ ]:
from mmm_functions import *
データセットの読み込み¶
In [ ]:
# データセット読み込み
dataset = 'MMM_ts_manga5.csv'
df = pd.read_csv(
dataset,
parse_dates=['week'],
index_col='week',
)
季節性を表現する三角関数特徴量を追加¶
In [ ]:
# 三角関数特徴量を追加したデータフレームを表示して確認
df = add_fourier_terms(df, num=5, seasonal=52.25)
説明変数と目的変数の設定¶
In [ ]:
# 説明変数Xと目的変数yに分解
X = df.drop(columns=['sales'])
y = df['sales']
MMMの学習済みモデル(外部ファイル)の読み込み¶
In [ ]:
# 学習済みモデルtrained_modelを外部ファイルからロードする
model_path = 'ridgeMMM_trained_model.joblib'
trained_model = joblib.load(model_path)
最適化する期間の設定¶
In [ ]:
# 最適化期間を表す変数、ここでは1年間(52週間)を指定
term = 52
最適化対象の特徴量(マーケ変数)のリスト¶
In [ ]:
# 最適化対象の特徴量のリストを作成
optimized_features = ['traditional', 'internet', 'promotional']
最適化対象期間(未来)分追加した説明変数Xを作成¶
In [ ]:
#
# Xのデータセットにterm期間分のレコードを追加
#
# 最後のインデックスを取得
last_index = X.index[-1]
# 最後のtの値を取得
last_t_value = X['t'].iloc[-1]
# 新しいインデックスを生成(7日間隔でterm期間分)
new_index = pd.date_range(start=last_index + pd.Timedelta(days=7), periods=term, freq='W')
# tの新しい値を生成
new_t_values = range(last_t_value + 1, last_t_value + term + 1)
# 新しいデータフレームを生成(値は0で初期化、tは新しい値で設定)
new_df = pd.DataFrame(index=new_index, columns=X.columns)
new_df['t'] = new_t_values
new_df = new_df.fillna(0)
# 新しいデータフレームを元のXに追加
X_extended = pd.concat([X, new_df])
# 三角関数特徴量を修正・追加
X_extended = add_fourier_terms(X_extended, num=5, seasonal=52.25)
print(X_extended)
traditional internet promotional t sin_1 cos_1 \ 2015-12-27 656100000 0 0 0 0.000000 1.000000 2016-01-03 0 178050000 188587500 1 0.119963 0.992778 2016-01-10 0 150450000 162037500 2 0.238193 0.971218 2016-01-17 0 0 126900000 3 0.352983 0.935630 2016-01-24 0 175500000 0 4 0.462674 0.886528 ... ... ... ... ... ... ... 2024-11-24 0 0 0 465 -0.590215 0.807246 2024-12-01 0 0 0 466 -0.489113 0.872220 2024-12-08 0 0 0 467 -0.380947 0.924597 2024-12-15 0 0 0 468 -0.267279 0.963619 2024-12-22 0 0 0 469 -0.149750 0.988724 sin_2 cos_2 sin_3 cos_3 sin_4 cos_4 \ 2015-12-27 0.000000 1.000000 0.000000 1.000000 0.000000 1.000000 2016-01-03 0.238193 0.971218 0.352983 0.935630 0.462674 0.886528 2016-01-10 0.462674 0.886528 0.660522 0.750806 0.820348 0.571865 2016-01-17 0.660522 0.750806 0.883026 0.469324 0.991849 0.127421 2016-01-24 0.820348 0.571865 0.991849 0.127421 0.938256 -0.345941 ... ... ... ... ... ... ... 2024-11-24 -0.952897 0.303293 -0.948231 -0.317582 -0.578014 -0.816027 2024-12-01 -0.853229 0.521537 -0.999294 0.037570 -0.889981 -0.455998 2024-12-08 -0.704445 0.709759 -0.921708 0.387885 -0.999972 0.007516 2024-12-15 -0.515110 0.857124 -0.725461 0.688264 -0.883026 0.469324 2024-12-22 -0.296123 0.955150 -0.435817 0.900035 -0.565683 0.824623 sin_5 cos_5 2015-12-27 0.000000 1.000000 2016-01-03 0.565683 0.824623 2016-01-10 0.932951 0.360005 2016-01-17 0.972981 -0.230887 2016-01-24 0.671733 -0.740793 ... ... ... 2024-11-24 0.015031 -0.999887 2024-12-01 -0.553225 -0.833032 2024-12-08 -0.927434 -0.373987 2024-12-15 -0.976341 0.216236 2024-12-22 -0.682792 0.730613 [470 rows x 14 columns]
昨年(直近52週間分)のデータ抽出¶
In [ ]:
# 昨年の説明変数Xのデータを抽出
X_last_year = X[-term:]
# 昨年の目的変数yのデータを抽出
y_last_year = y[-term:]
# コスト全体の設定
cost_all = X_last_year[optimized_features].values.ravel().sum()
最適化期間(今後52週間分)の説明面数X ※マーケ変数の値はすべて0¶
In [ ]:
# 最適化期間における対象特徴量Xのデータ
X_actual = X_extended[-term:]
print(X_actual)
traditional internet promotional t sin_1 cos_1 \ 2023-12-31 0 0 0 418 -1.959435e-15 1.000000 2024-01-07 0 0 0 419 1.199627e-01 0.992778 2024-01-14 0 0 0 420 2.381928e-01 0.971218 2024-01-21 0 0 0 421 3.529827e-01 0.935630 2024-01-28 0 0 0 422 4.626743e-01 0.886528 2024-02-04 0 0 0 423 5.656834e-01 0.824623 2024-02-11 0 0 0 424 6.605222e-01 0.750806 2024-02-18 0 0 0 425 7.458210e-01 0.666146 2024-02-25 0 0 0 426 8.203477e-01 0.571865 2024-03-03 0 0 0 427 8.830260e-01 0.469324 2024-03-10 0 0 0 428 9.329505e-01 0.360005 2024-03-17 0 0 0 429 9.694003e-01 0.245485 2024-03-24 0 0 0 430 9.918488e-01 0.127421 2024-03-31 0 0 0 431 9.999718e-01 0.007516 2024-04-07 0 0 0 432 9.936520e-01 -0.112498 2024-04-14 0 0 0 433 9.729806e-01 -0.230887 2024-04-21 0 0 0 434 9.382564e-01 -0.345941 2024-04-28 0 0 0 435 8.899806e-01 -0.455998 2024-05-05 0 0 0 436 8.288507e-01 -0.559470 2024-05-12 0 0 0 437 7.557496e-01 -0.654861 2024-05-19 0 0 0 438 6.717330e-01 -0.740793 2024-05-26 0 0 0 439 5.780144e-01 -0.816027 2024-06-02 0 0 0 440 4.759474e-01 -0.879474 2024-06-09 0 0 0 441 3.670062e-01 -0.930218 2024-06-16 0 0 0 442 2.527643e-01 -0.967528 2024-06-23 0 0 0 443 1.348716e-01 -0.990863 2024-06-30 0 0 0 444 1.503098e-02 -0.999887 2024-07-07 0 0 0 445 -1.050268e-01 -0.994469 2024-07-14 0 0 0 446 -2.235676e-01 -0.974688 2024-07-21 0 0 0 447 -3.388793e-01 -0.940830 2024-07-28 0 0 0 448 -4.492966e-01 -0.893383 2024-08-04 0 0 0 449 -5.532246e-01 -0.833032 2024-08-11 0 0 0 450 -6.491623e-01 -0.760650 2024-08-18 0 0 0 451 -7.357239e-01 -0.677282 2024-08-25 0 0 0 452 -8.116594e-01 -0.584131 2024-09-01 0 0 0 453 -8.758718e-01 -0.482544 2024-09-08 0 0 0 454 -9.274339e-01 -0.373987 2024-09-15 0 0 0 455 -9.656009e-01 -0.260029 2024-09-22 0 0 0 456 -9.898214e-01 -0.142315 2024-09-29 0 0 0 457 -9.997458e-01 -0.022545 2024-10-06 0 0 0 458 -9.952307e-01 0.097550 2024-10-13 0 0 0 459 -9.763412e-01 0.216236 2024-10-20 0 0 0 460 -9.433502e-01 0.331799 2024-10-27 0 0 0 461 -8.967342e-01 0.442570 2024-11-03 0 0 0 462 -8.371665e-01 0.546948 2024-11-10 0 0 0 463 -7.655074e-01 0.643427 2024-11-17 0 0 0 464 -6.827919e-01 0.730613 2024-11-24 0 0 0 465 -5.902147e-01 0.807246 2024-12-01 0 0 0 466 -4.891130e-01 0.872220 2024-12-08 0 0 0 467 -3.809468e-01 0.924597 2024-12-15 0 0 0 468 -2.672786e-01 0.963619 2024-12-22 0 0 0 469 -1.497500e-01 0.988724 sin_2 cos_2 sin_3 cos_3 sin_4 \ 2023-12-31 -3.918870e-15 1.000000 -5.878305e-15 1.000000 -7.837740e-15 2024-01-07 2.381928e-01 0.971218 3.529827e-01 0.935630 4.626743e-01 2024-01-14 4.626743e-01 0.886528 6.605222e-01 0.750806 8.203477e-01 2024-01-21 6.605222e-01 0.750806 8.830260e-01 0.469324 9.918488e-01 2024-01-28 8.203477e-01 0.571865 9.918488e-01 0.127421 9.382564e-01 2024-02-04 9.329505e-01 0.360005 9.729806e-01 -0.230887 6.717330e-01 2024-02-11 9.918488e-01 0.127421 8.288507e-01 -0.559470 2.527643e-01 2024-02-18 9.936520e-01 -0.112498 5.780144e-01 -0.816027 -2.235676e-01 2024-02-25 9.382564e-01 -0.345941 2.527643e-01 -0.967528 -6.491623e-01 2024-03-03 8.288507e-01 -0.559470 -1.050268e-01 -0.994469 -9.274339e-01 2024-03-10 6.717330e-01 -0.740793 -4.492966e-01 -0.893383 -9.952307e-01 2024-03-17 4.759474e-01 -0.879474 -7.357239e-01 -0.677282 -8.371665e-01 2024-03-24 2.527643e-01 -0.967528 -9.274339e-01 -0.373987 -4.891130e-01 2024-03-31 1.503098e-02 -0.999887 -9.997458e-01 -0.022545 -3.005856e-02 2024-04-07 -2.235676e-01 -0.974688 -9.433502e-01 0.331799 4.358174e-01 2024-04-14 -4.492966e-01 -0.893383 -7.655074e-01 0.643427 8.027876e-01 2024-04-21 -6.491623e-01 -0.760650 -4.891130e-01 0.872220 9.875705e-01 2024-04-28 -8.116594e-01 -0.584131 -1.497500e-01 0.988724 9.482309e-01 2024-05-05 -9.274339e-01 -0.373987 2.088918e-01 0.977939 6.936966e-01 2024-05-12 -9.898214e-01 -0.142315 5.406408e-01 0.841254 2.817326e-01 2024-05-19 -9.952307e-01 0.097550 8.027876e-01 0.596265 -1.941688e-01 2024-05-26 -9.433502e-01 0.331799 9.615833e-01 0.274513 -6.260049e-01 2024-06-02 -8.371665e-01 0.546948 9.965845e-01 -0.082579 -9.157733e-01 2024-06-09 -6.827919e-01 0.730613 9.032851e-01 -0.429041 -9.977132e-01 2024-06-16 -4.891130e-01 0.872220 6.936966e-01 -0.720267 -8.532287e-01 2024-06-23 -2.672786e-01 0.963619 3.948014e-01 -0.918766 -5.151097e-01 2024-06-30 -3.005856e-02 0.999548 4.507935e-02 -0.998983 -6.008995e-02 2024-07-07 2.088918e-01 0.977939 -3.104462e-01 -0.950591 4.085668e-01 2024-07-14 4.358174e-01 0.900035 -6.260049e-01 -0.779819 7.845020e-01 2024-07-21 6.376556e-01 0.770322 -8.609715e-01 -0.508653 9.823997e-01 2024-07-28 8.027876e-01 0.596265 -9.850964e-01 -0.172003 9.573485e-01 2024-08-04 9.217077e-01 0.387885 -9.823997e-01 0.186791 7.150334e-01 2024-08-11 9.875705e-01 0.157177 -8.532287e-01 0.521537 3.104462e-01 2024-08-18 9.965845e-01 -0.082579 -6.142127e-01 0.789141 -1.645946e-01 2024-08-25 9.482309e-01 -0.317582 -2.961228e-01 0.955150 -6.022818e-01 2024-09-01 8.452931e-01 -0.534303 6.008995e-02 0.998193 -9.032851e-01 2024-09-08 6.936966e-01 -0.720267 4.085668e-01 0.912728 -9.992940e-01 2024-09-15 5.021680e-01 -0.864770 7.044446e-01 0.709759 -8.685198e-01 2024-09-22 2.817326e-01 -0.959493 9.096320e-01 0.415415 -5.406408e-01 2024-09-29 4.507935e-02 -0.998983 9.977132e-01 0.067590 -9.006704e-02 2024-10-06 -1.941688e-01 -0.980968 9.573485e-01 -0.288936 3.809468e-01 2024-10-13 -4.222398e-01 -0.906484 7.937345e-01 -0.608264 7.655074e-01 2024-10-20 -6.260049e-01 -0.779819 5.279349e-01 -0.849285 9.763412e-01 2024-10-27 -7.937345e-01 -0.608264 1.941688e-01 -0.980968 9.656009e-01 2024-11-03 -9.157733e-01 -0.401695 -1.645946e-01 -0.986361 7.357239e-01 2024-11-10 -9.850964e-01 -0.172003 -5.021680e-01 -0.864770 3.388793e-01 2024-11-17 -9.977132e-01 0.067590 -7.750923e-01 -0.631848 -1.348716e-01 2024-11-24 -9.528973e-01 0.303293 -9.482309e-01 -0.317582 -5.780144e-01 2024-12-01 -8.532287e-01 0.521537 -9.992940e-01 0.037570 -8.899806e-01 2024-12-08 -7.044446e-01 0.709759 -9.217077e-01 0.387885 -9.999718e-01 2024-12-15 -5.151097e-01 0.857124 -7.254606e-01 0.688264 -8.830260e-01 2024-12-22 -2.961228e-01 0.955150 -4.358174e-01 0.900035 -5.656834e-01 cos_4 sin_5 cos_5 2023-12-31 1.000000 -9.797174e-15 1.000000 2024-01-07 0.886528 5.656834e-01 0.824623 2024-01-14 0.571865 9.329505e-01 0.360005 2024-01-21 0.127421 9.729806e-01 -0.230887 2024-01-28 -0.345941 6.717330e-01 -0.740793 2024-02-04 -0.740793 1.348716e-01 -0.990863 2024-02-11 -0.967528 -4.492966e-01 -0.893383 2024-02-18 -0.974688 -8.758718e-01 -0.482544 2024-02-25 -0.760650 -9.952307e-01 0.097550 2024-03-03 -0.373987 -7.655074e-01 0.643427 2024-03-10 0.097550 -2.672786e-01 0.963619 2024-03-17 0.546948 3.246995e-01 0.945817 2024-03-24 0.872220 8.027876e-01 0.596265 2024-03-31 0.999548 9.992940e-01 0.037570 2024-04-07 0.900035 8.452931e-01 -0.534303 2024-04-14 0.596265 3.948014e-01 -0.918766 2024-04-21 0.157177 -1.941688e-01 -0.980968 2024-04-28 -0.317582 -7.150334e-01 -0.699090 2024-05-05 -0.720267 -9.850964e-01 -0.172003 2024-05-12 -0.959493 -9.096320e-01 0.415415 2024-05-19 -0.980968 -5.151097e-01 0.857124 2024-05-26 -0.779819 6.008995e-02 0.998193 2024-06-02 -0.401695 6.142127e-01 0.789141 2024-06-09 0.067590 9.528973e-01 0.303293 2024-06-16 0.521537 9.573485e-01 -0.288936 2024-06-23 0.857124 6.260049e-01 -0.779819 2024-06-30 0.998193 7.508698e-02 -0.997177 2024-07-07 0.912728 -5.021680e-01 -0.864770 2024-07-14 0.620126 -9.032851e-01 -0.429041 2024-07-21 0.186791 -9.875705e-01 0.157177 2024-07-28 -0.288936 -7.254606e-01 0.688264 2024-08-04 -0.699090 -2.088918e-01 0.977939 2024-08-11 -0.950591 3.809468e-01 0.924597 2024-08-18 -0.986361 8.371665e-01 0.546948 2024-08-25 -0.798284 9.997458e-01 -0.022545 2024-09-01 -0.429041 8.116594e-01 -0.584131 2024-09-08 0.037570 3.388793e-01 -0.940830 2024-09-15 0.495655 -2.527643e-01 -0.967528 2024-09-22 0.841254 -7.557496e-01 -0.654861 2024-09-29 0.995936 -9.936520e-01 -0.112498 2024-10-06 0.924597 -8.830260e-01 0.469324 2024-10-13 0.643427 -4.626743e-01 0.886528 2024-10-20 0.216236 1.199627e-01 0.992778 2024-10-27 -0.260029 6.605222e-01 0.750806 2024-11-03 -0.677282 9.694003e-01 0.245485 2024-11-10 -0.940830 9.382564e-01 -0.345941 2024-11-17 -0.990863 5.780144e-01 -0.816027 2024-11-24 -0.816027 1.503098e-02 -0.999887 2024-12-01 -0.455998 -5.532246e-01 -0.833032 2024-12-08 0.007516 -9.274339e-01 -0.373987 2024-12-15 0.469324 -9.763412e-01 0.216236 2024-12-22 0.824623 -6.827919e-01 0.730613
最適解(最適な広告投資)の探索¶
初期値探索(ランダムサーチ)¶
In [ ]:
max_pred_val = 0
opt_allocations = None
iter = int(1e+5)
# tqdmを使用してプログレスバーを表示
with tqdm.tqdm(total=iter, position=0, leave=False) as pbar:
for _ in range(iter):
random_allocations = np.random.dirichlet(np.ones(len(X_actual) * len(optimized_features)), size=1).reshape(len(X_actual), len(optimized_features)) * cost_all
X_actual[optimized_features] = random_allocations
pred_val = trained_model.predict(X_actual, n_periods=term).sum()
if pred_val > max_pred_val:
max_pred_val = pred_val
opt_allocations = random_allocations
# プログレスバーを更新
pbar.update(1)
# 現在の予測の最大値を表示
pbar.set_description(f'Max Predicted Value: {max_pred_val:.2f}', refresh=True)
# X_actualに初期値を設定
X_actual[optimized_features] = opt_allocations
# 予測
pred = trained_model.predict(X_actual, n_periods=term)
pred.sum()
Out[ ]:
85299415766.59154
非線形最適化問題を解く¶
In [ ]:
#
# 最適投資配分
#
opt_results = optimize_investment(
trained_model,
X_actual,
optimized_features,
cost_all,
niter = 10)
Optimal value: -87167226479.74: : 11it [1:28:05, 480.46s/it]
最適解(最適な広告投資案)¶
In [ ]:
#
# 最適解の取得
#
X_optimized = opt_results['X_optimized']
# 最適化後の特徴量を表示
print(X_optimized)
traditional internet promotional t sin_1 \ 2023-12-31 6.422680e+07 1.911496e+06 1.317001e+08 418 -1.959435e-15 2024-01-07 1.237385e+08 3.701697e+07 3.733827e+06 419 1.199627e-01 2024-01-14 1.110666e+07 1.118746e+07 1.579304e+08 420 2.381928e-01 2024-01-21 2.393785e+08 1.309210e+08 4.985978e+07 421 3.529827e-01 2024-01-28 1.411283e+07 2.701994e+07 2.529645e+07 422 4.626743e-01 2024-02-04 3.004361e-12 6.292760e+06 5.826196e+06 423 5.656834e-01 2024-02-11 7.121518e+07 1.010922e+08 2.315352e+07 424 6.605222e-01 2024-02-18 3.752799e+07 5.331879e+06 1.320666e+08 425 7.458210e-01 2024-02-25 2.528533e+08 7.232264e+07 2.368481e+06 426 8.203477e-01 2024-03-03 1.275706e+08 2.816876e+07 9.367396e+07 427 8.830260e-01 2024-03-10 3.927676e+07 1.527016e+07 8.789018e+07 428 9.329505e-01 2024-03-17 2.243547e+08 1.256976e+08 3.143927e+08 429 9.694003e-01 2024-03-24 1.268317e+08 6.824334e+07 3.024564e+08 430 9.918488e-01 2024-03-31 3.489543e+08 6.075750e+07 4.106629e+06 431 9.999718e-01 2024-04-07 3.241903e+06 7.760159e+07 8.788486e+06 432 9.936520e-01 2024-04-14 2.308963e+08 1.313742e+08 1.054828e+08 433 9.729806e-01 2024-04-21 2.926762e+08 1.302348e+08 2.651488e+07 434 9.382564e-01 2024-04-28 1.240468e+08 1.883563e+07 2.840171e+08 435 8.899806e-01 2024-05-05 9.369963e+06 4.757691e+07 2.028846e+07 436 8.288507e-01 2024-05-12 9.859787e+07 8.519091e+07 1.200522e+08 437 7.557496e-01 2024-05-19 5.906206e+08 9.455515e+07 3.682056e+08 438 6.717330e-01 2024-05-26 1.109606e+06 4.017821e+07 3.818937e+08 439 5.780144e-01 2024-06-02 5.463433e+08 1.030711e+08 1.247510e+08 440 4.759474e-01 2024-06-09 4.150601e+07 1.052048e+08 1.192493e+08 441 3.670062e-01 2024-06-16 3.085149e+08 2.986517e+07 3.805357e+08 442 2.527643e-01 2024-06-23 3.703123e+08 1.750829e+08 3.599881e+08 443 1.348716e-01 2024-06-30 1.132699e+08 8.089663e+07 3.455783e+08 444 1.503098e-02 2024-07-07 8.956961e+07 6.222931e+07 4.291516e+07 445 -1.050268e-01 2024-07-14 4.679252e+07 8.968729e+07 1.182092e+08 446 -2.235676e-01 2024-07-21 5.333766e+07 7.065752e+07 2.907277e+08 447 -3.388793e-01 2024-07-28 3.636313e+07 1.005593e+08 2.899988e+08 448 -4.492966e-01 2024-08-04 3.746780e+07 1.106321e+08 2.969194e+08 449 -5.532246e-01 2024-08-11 2.175623e+08 1.184776e+07 6.612656e+07 450 -6.491623e-01 2024-08-18 1.310765e+08 5.101771e+07 7.552555e+07 451 -7.357239e-01 2024-08-25 2.217768e+08 7.562214e+05 3.036005e+08 452 -8.116594e-01 2024-09-01 1.119489e+08 4.074854e+07 5.657508e+07 453 -8.758718e-01 2024-09-08 2.524615e+08 1.179571e+08 2.764061e+08 454 -9.274339e-01 2024-09-15 5.554588e+07 5.632192e+07 2.250331e+08 455 -9.656009e-01 2024-09-22 2.107843e+08 3.626801e+07 3.373126e+07 456 -9.898214e-01 2024-09-29 3.508117e+07 9.035500e+07 8.174741e+07 457 -9.997458e-01 2024-10-06 7.283716e+06 6.532188e+07 1.228082e+08 458 -9.952307e-01 2024-10-13 1.193995e+08 3.186399e+07 6.461004e+07 459 -9.763412e-01 2024-10-20 1.011537e+07 3.771304e+07 6.596654e+07 460 -9.433502e-01 2024-10-27 2.265959e+08 1.069363e+08 2.318156e+08 461 -8.967342e-01 2024-11-03 3.645798e+07 2.649295e+07 7.042132e+07 462 -8.371665e-01 2024-11-10 5.944637e+07 6.717510e+07 4.003195e+07 463 -7.655074e-01 2024-11-17 5.679671e+07 1.200454e+08 5.738821e+07 464 -6.827919e-01 2024-11-24 2.046119e+08 1.058822e+08 2.179263e+08 465 -5.902147e-01 2024-12-01 1.585925e+08 4.159803e+07 3.751177e+07 466 -4.891130e-01 2024-12-08 1.740466e+08 1.097679e+08 1.087992e+07 467 -3.809468e-01 2024-12-15 1.877221e+08 1.436493e+08 5.586987e+07 468 -2.672786e-01 2024-12-22 9.501348e+07 1.114810e+08 1.198322e+08 469 -1.497500e-01 cos_1 sin_2 cos_2 sin_3 cos_3 \ 2023-12-31 1.000000 -3.918870e-15 1.000000 -5.878305e-15 1.000000 2024-01-07 0.992778 2.381928e-01 0.971218 3.529827e-01 0.935630 2024-01-14 0.971218 4.626743e-01 0.886528 6.605222e-01 0.750806 2024-01-21 0.935630 6.605222e-01 0.750806 8.830260e-01 0.469324 2024-01-28 0.886528 8.203477e-01 0.571865 9.918488e-01 0.127421 2024-02-04 0.824623 9.329505e-01 0.360005 9.729806e-01 -0.230887 2024-02-11 0.750806 9.918488e-01 0.127421 8.288507e-01 -0.559470 2024-02-18 0.666146 9.936520e-01 -0.112498 5.780144e-01 -0.816027 2024-02-25 0.571865 9.382564e-01 -0.345941 2.527643e-01 -0.967528 2024-03-03 0.469324 8.288507e-01 -0.559470 -1.050268e-01 -0.994469 2024-03-10 0.360005 6.717330e-01 -0.740793 -4.492966e-01 -0.893383 2024-03-17 0.245485 4.759474e-01 -0.879474 -7.357239e-01 -0.677282 2024-03-24 0.127421 2.527643e-01 -0.967528 -9.274339e-01 -0.373987 2024-03-31 0.007516 1.503098e-02 -0.999887 -9.997458e-01 -0.022545 2024-04-07 -0.112498 -2.235676e-01 -0.974688 -9.433502e-01 0.331799 2024-04-14 -0.230887 -4.492966e-01 -0.893383 -7.655074e-01 0.643427 2024-04-21 -0.345941 -6.491623e-01 -0.760650 -4.891130e-01 0.872220 2024-04-28 -0.455998 -8.116594e-01 -0.584131 -1.497500e-01 0.988724 2024-05-05 -0.559470 -9.274339e-01 -0.373987 2.088918e-01 0.977939 2024-05-12 -0.654861 -9.898214e-01 -0.142315 5.406408e-01 0.841254 2024-05-19 -0.740793 -9.952307e-01 0.097550 8.027876e-01 0.596265 2024-05-26 -0.816027 -9.433502e-01 0.331799 9.615833e-01 0.274513 2024-06-02 -0.879474 -8.371665e-01 0.546948 9.965845e-01 -0.082579 2024-06-09 -0.930218 -6.827919e-01 0.730613 9.032851e-01 -0.429041 2024-06-16 -0.967528 -4.891130e-01 0.872220 6.936966e-01 -0.720267 2024-06-23 -0.990863 -2.672786e-01 0.963619 3.948014e-01 -0.918766 2024-06-30 -0.999887 -3.005856e-02 0.999548 4.507935e-02 -0.998983 2024-07-07 -0.994469 2.088918e-01 0.977939 -3.104462e-01 -0.950591 2024-07-14 -0.974688 4.358174e-01 0.900035 -6.260049e-01 -0.779819 2024-07-21 -0.940830 6.376556e-01 0.770322 -8.609715e-01 -0.508653 2024-07-28 -0.893383 8.027876e-01 0.596265 -9.850964e-01 -0.172003 2024-08-04 -0.833032 9.217077e-01 0.387885 -9.823997e-01 0.186791 2024-08-11 -0.760650 9.875705e-01 0.157177 -8.532287e-01 0.521537 2024-08-18 -0.677282 9.965845e-01 -0.082579 -6.142127e-01 0.789141 2024-08-25 -0.584131 9.482309e-01 -0.317582 -2.961228e-01 0.955150 2024-09-01 -0.482544 8.452931e-01 -0.534303 6.008995e-02 0.998193 2024-09-08 -0.373987 6.936966e-01 -0.720267 4.085668e-01 0.912728 2024-09-15 -0.260029 5.021680e-01 -0.864770 7.044446e-01 0.709759 2024-09-22 -0.142315 2.817326e-01 -0.959493 9.096320e-01 0.415415 2024-09-29 -0.022545 4.507935e-02 -0.998983 9.977132e-01 0.067590 2024-10-06 0.097550 -1.941688e-01 -0.980968 9.573485e-01 -0.288936 2024-10-13 0.216236 -4.222398e-01 -0.906484 7.937345e-01 -0.608264 2024-10-20 0.331799 -6.260049e-01 -0.779819 5.279349e-01 -0.849285 2024-10-27 0.442570 -7.937345e-01 -0.608264 1.941688e-01 -0.980968 2024-11-03 0.546948 -9.157733e-01 -0.401695 -1.645946e-01 -0.986361 2024-11-10 0.643427 -9.850964e-01 -0.172003 -5.021680e-01 -0.864770 2024-11-17 0.730613 -9.977132e-01 0.067590 -7.750923e-01 -0.631848 2024-11-24 0.807246 -9.528973e-01 0.303293 -9.482309e-01 -0.317582 2024-12-01 0.872220 -8.532287e-01 0.521537 -9.992940e-01 0.037570 2024-12-08 0.924597 -7.044446e-01 0.709759 -9.217077e-01 0.387885 2024-12-15 0.963619 -5.151097e-01 0.857124 -7.254606e-01 0.688264 2024-12-22 0.988724 -2.961228e-01 0.955150 -4.358174e-01 0.900035 sin_4 cos_4 sin_5 cos_5 2023-12-31 -7.837740e-15 1.000000 -9.797174e-15 1.000000 2024-01-07 4.626743e-01 0.886528 5.656834e-01 0.824623 2024-01-14 8.203477e-01 0.571865 9.329505e-01 0.360005 2024-01-21 9.918488e-01 0.127421 9.729806e-01 -0.230887 2024-01-28 9.382564e-01 -0.345941 6.717330e-01 -0.740793 2024-02-04 6.717330e-01 -0.740793 1.348716e-01 -0.990863 2024-02-11 2.527643e-01 -0.967528 -4.492966e-01 -0.893383 2024-02-18 -2.235676e-01 -0.974688 -8.758718e-01 -0.482544 2024-02-25 -6.491623e-01 -0.760650 -9.952307e-01 0.097550 2024-03-03 -9.274339e-01 -0.373987 -7.655074e-01 0.643427 2024-03-10 -9.952307e-01 0.097550 -2.672786e-01 0.963619 2024-03-17 -8.371665e-01 0.546948 3.246995e-01 0.945817 2024-03-24 -4.891130e-01 0.872220 8.027876e-01 0.596265 2024-03-31 -3.005856e-02 0.999548 9.992940e-01 0.037570 2024-04-07 4.358174e-01 0.900035 8.452931e-01 -0.534303 2024-04-14 8.027876e-01 0.596265 3.948014e-01 -0.918766 2024-04-21 9.875705e-01 0.157177 -1.941688e-01 -0.980968 2024-04-28 9.482309e-01 -0.317582 -7.150334e-01 -0.699090 2024-05-05 6.936966e-01 -0.720267 -9.850964e-01 -0.172003 2024-05-12 2.817326e-01 -0.959493 -9.096320e-01 0.415415 2024-05-19 -1.941688e-01 -0.980968 -5.151097e-01 0.857124 2024-05-26 -6.260049e-01 -0.779819 6.008995e-02 0.998193 2024-06-02 -9.157733e-01 -0.401695 6.142127e-01 0.789141 2024-06-09 -9.977132e-01 0.067590 9.528973e-01 0.303293 2024-06-16 -8.532287e-01 0.521537 9.573485e-01 -0.288936 2024-06-23 -5.151097e-01 0.857124 6.260049e-01 -0.779819 2024-06-30 -6.008995e-02 0.998193 7.508698e-02 -0.997177 2024-07-07 4.085668e-01 0.912728 -5.021680e-01 -0.864770 2024-07-14 7.845020e-01 0.620126 -9.032851e-01 -0.429041 2024-07-21 9.823997e-01 0.186791 -9.875705e-01 0.157177 2024-07-28 9.573485e-01 -0.288936 -7.254606e-01 0.688264 2024-08-04 7.150334e-01 -0.699090 -2.088918e-01 0.977939 2024-08-11 3.104462e-01 -0.950591 3.809468e-01 0.924597 2024-08-18 -1.645946e-01 -0.986361 8.371665e-01 0.546948 2024-08-25 -6.022818e-01 -0.798284 9.997458e-01 -0.022545 2024-09-01 -9.032851e-01 -0.429041 8.116594e-01 -0.584131 2024-09-08 -9.992940e-01 0.037570 3.388793e-01 -0.940830 2024-09-15 -8.685198e-01 0.495655 -2.527643e-01 -0.967528 2024-09-22 -5.406408e-01 0.841254 -7.557496e-01 -0.654861 2024-09-29 -9.006704e-02 0.995936 -9.936520e-01 -0.112498 2024-10-06 3.809468e-01 0.924597 -8.830260e-01 0.469324 2024-10-13 7.655074e-01 0.643427 -4.626743e-01 0.886528 2024-10-20 9.763412e-01 0.216236 1.199627e-01 0.992778 2024-10-27 9.656009e-01 -0.260029 6.605222e-01 0.750806 2024-11-03 7.357239e-01 -0.677282 9.694003e-01 0.245485 2024-11-10 3.388793e-01 -0.940830 9.382564e-01 -0.345941 2024-11-17 -1.348716e-01 -0.990863 5.780144e-01 -0.816027 2024-11-24 -5.780144e-01 -0.816027 1.503098e-02 -0.999887 2024-12-01 -8.899806e-01 -0.455998 -5.532246e-01 -0.833032 2024-12-08 -9.999718e-01 0.007516 -9.274339e-01 -0.373987 2024-12-15 -8.830260e-01 0.469324 -9.763412e-01 0.216236 2024-12-22 -5.656834e-01 0.824623 -6.827919e-01 0.730613
In [ ]:
# (必要があれば)最適解の保存
X_optimized_path = 'X_optimized.csv'
X_optimized.to_csv(X_optimized_path)
In [ ]:
#
# 最適投資のプロット
#
# 最適化対象の特徴量の数
features_count = len(optimized_features)
# 最適化後の特徴量をプロット
fig, axes = plt.subplots(nrows=features_count, ncols=1, figsize=(12, 15))
fig.suptitle('Optimized Investment')
if features_count == 1:
axes = [axes]
for i, feature in enumerate(optimized_features):
axes[i].plot(X_optimized.index, X_optimized[feature], label='Optimized')
axes[i].set_title(feature)
axes[i].set_xlabel('Date')
axes[i].set_ylabel('Investment Amount')
axes[i].legend()
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
plt.show()
現状(直近52週間)と最適配分時(今後52週間)の比較¶
In [ ]:
#
# 現状と最適配分時の比較(yとマーケティングROI)
#
result = compare_y_and_marketing_roi(
X_optimized,
X_last_year,
y_last_year,
trained_model,
optimized_features)
# 数値の表示
for key,value in result.items():
print(f"{key}: {value}")
y_actual_sum: 73392955000 y_optimized_sum: 88110964946 y_change_percent: +20.05 % roi_actual: 3.06 roi_optimized: 3.87 roi_change_point: +0.81 points
In [ ]:
#
# 投資配分構成比の比較
#
comparison_df = plot_comparative_allocation(
X_last_year,
X_optimized,
optimized_features)
# 数値の表示
print(comparison_df)
Actual Allocation Optimized Allocation traditional 0.400384 0.400464 internet 0.355934 0.199907 promotional 0.243683 0.399629