Backtesting

Thiết kế Pipeline Dự báo cho Môi trường Production

Rami Krispin

Senior Manager, Data Science and Engineering

Backtesting

Backtesting - chia dữ liệu thành nhiều phân hoạch (cửa sổ) để huấn luyện và kiểm tra mô hình

Thiết kế Pipeline Dự báo cho Môi trường Production

Backtesting

Các tập huấn luyện

Thiết kế Pipeline Dự báo cho Môi trường Production

Backtesting

Các tập kiểm tra

Thiết kế Pipeline Dự báo cho Môi trường Production

Backtesting

Cách tiếp cận cửa sổ mở rộng: kích thước mẫu tăng dần cho mỗi phân hoạch

Thiết kế Pipeline Dự báo cho Môi trường Production

Backtesting

Cách tiếp cận cửa sổ trượt: kích thước mẫu mỗi phân hoạch giữ nguyên

Thiết kế Pipeline Dự báo cho Môi trường Production

Backtesting

Ở cả hai cách tiếp cận, kích thước tập kiểm tra không đổi

Thiết kế Pipeline Dự báo cho Môi trường Production

Backtesting

Boxplot RMSE cho 5 mô hình: Lasso, LightGBM, Hồi quy tuyến tính, Ridge và XGBoost

Thiết kế Pipeline Dự báo cho Môi trường Production

Thiết lập backtesting

Mô hình

  • XGBoost
  • Light Gradient-Boosting Machine
  • Ridge
  • Lasso
  • Hồi quy tuyến tính
Thiết kế Pipeline Dự báo cho Môi trường Production

Thiết lập backtesting

Thiết lập phân hoạch

Tham số backtesting

Thiết kế Pipeline Dự báo cho Môi trường Production

Thiết lập backtesting

Thiết lập phân hoạch

Tham số backtesting

Thiết kế Pipeline Dự báo cho Môi trường Production

Thiết lập backtesting

Thiết lập phân hoạch

Tham số backtesting

Thiết kế Pipeline Dự báo cho Môi trường Production

Thiết lập backtesting

Thiết lập phân hoạch

Tham số backtesting

Thiết kế Pipeline Dự báo cho Môi trường Production

Thiết lập backtesting

Bất định mô hình

  • Khoảng dự báo conformal
    • Hỗ trợ mô hình phi tham số và tham số
  • Mức ý nghĩa 95%
Thiết kế Pipeline Dự báo cho Môi trường Production

Thư viện cần thiết

from mlforecast import MLForecast
from mlforecast.target_transforms import Differences
from mlforecast.utils import PredictionIntervals
from window_ops.expanding import expanding_mean
from lightgbm import LGBMRegressor
from xgboost import XGBRegressor
from sklearn.linear_model import Lasso, LinearRegression, Ridge
from sklearn.neural_network import MLPRegressor
from sklearn.ensemble import RandomForestRegressor
from utilsforecast.plotting import plot_series


Thiết kế Pipeline Dự báo cho Môi trường Production

Thư viện cần thiết

import pandas as pd
import numpy as np
import requests
import json
import os
import datetime
from statistics import mean
Thiết kế Pipeline Dự báo cho Môi trường Production

Chuẩn bị dữ liệu

ts = pd.read_csv("data/data.csv")
ts["ds"] = pd.to_datetime(ts["ds"])
ts = ts.sort_values("ds")
ts = ts[["unique_id", "ds", "y"]]

end = ts["ds"].max() start = end - datetime.timedelta(hours = 24 * 31 * 25) ts = ts[ts["ds"] >= start]
os.environ['NIXTLA_ID_AS_COL'] = '1'
Thiết kế Pipeline Dự báo cho Môi trường Production

Định nghĩa mô hình dự báo

ml_models = {
    "lightGBM": LGBMRegressor(n_estimators=500, verbosity=-1),
    "xgboost": XGBRegressor(),
    "linear_regression": LinearRegression(),
    "lasso": Lasso(),
    "ridge": Ridge()
}

mlf = MLForecast( models= ml_models, freq='h', lags=list(range(1, 24)), date_features=["month", "day", "dayofweek", "week", "hour"])
Thiết kế Pipeline Dự báo cho Môi trường Production

Đặt tham số backtesting

Thiết lập cửa sổ

partitions = 10  
step_size = 24  
h = 72

Thiết lập khoảng dự báo

n_windows = 5
method = "conformal_distribution"
pi = PredictionIntervals(h=h, n_windows = n_windows , method = method)
levels = [95]
Thiết kế Pipeline Dự báo cho Môi trường Production

Huấn luyện mô hình với backtesting

bkt_df = mlf.cross_validation(
        df = ts,
        h = h,
        step_size = step_size,
        n_windows = partitions,
        prediction_intervals = pi, 
        level = levels)
Thiết kế Pipeline Dự báo cho Môi trường Production

Huấn luyện mô hình với backtesting

print(bkt_df.head())
     unique_id    ds                     cutoff                 y            lightGBM         
0    1            2024-04-22 00:00:00    2024-04-21 23:00:00    421082.60    421089.155837    
1    1            2024-04-22 01:00:00    2024-04-21 23:00:00    429728.30    425700.453391    
2    1            2024-04-22 02:00:00    2024-04-21 23:00:00    430690.96    424382.613668    
3    1            2024-04-22 03:00:00    2024-04-21 23:00:00    420094.58    409967.877157    
4    1            2024-04-22 04:00:00    2024-04-21 23:00:00    403292.36    393175.446116    
Thiết kế Pipeline Dự báo cho Môi trường Production

Kết quả backtesting dạng wide

Thông tin về DataFrame bkt_df, gồm 19 cột như unique ID, ds, y và giá trị mô hình

Thiết kế Pipeline Dự báo cho Môi trường Production

Ayo berlatih!

Thiết kế Pipeline Dự báo cho Môi trường Production

Preparing Video For Download...