Skip to content

DemandForecastModel

TÓM TẮT

Mô hình core sử dụng two-stage segmented approach: Stage 1 (Day Classifier) phân loại ngày → Stage 2 (Segment Regressors) dự báo theo segment. Giải quyết vấn đề intermittent demand trong e-commerce.

File: forecast_engine/src/core/model.py (~800 dòng)

Kiến trúc Two-Stage

Stage 1: Day Classifier

Phân loại mỗi ngày prediction vào 1 trong 5 segments dựa trên historical patterns:

SegmentMô tảVí dụ
zeroKhông có nhu cầuDead SKU, ngày lễ
lowNhu cầu thấpNgày thường, SKU intermittent
midNhu cầu trung bìnhNgày thường, SKU smooth
highNhu cầu caoPayday, mini-spike
extremeNhu cầu cực caoDouble-day, Mega sale

LightGBM Classifier sử dụng tất cả 40+ features để quyết định segment.

Stage 2: Segment Regressors

Mỗi segment có LightGBM Regressor riêng được tối ưu cho demand regime đó:

  • zero segment: Configurable threshold — nếu probability > threshold → pass cho regressor, ngược lại predict 0
  • low/mid/high/extreme: Mỗi cái có hyperparameters riêng (learning_rate, num_leaves, etc.)

Quantile Prediction

Model output bao gồm 3 quantile predictions:

QuantileMô tảSử dụng
P10Phân vị 10% (pessimistic)Lower confidence bound
P50Phân vị 50% (median)Primary forecast
P90Phân vị 90% (optimistic)Upper confidence bound, staffing buffer

Training Flow

python
# Simplified training flow
model = DemandForecastModel()

# Stage 1: Train classifier
model.fit_classifier(X_train, y_segments)

# Stage 2: Train segment regressors
for segment in ['low', 'mid', 'high', 'extreme']:
    mask = y_segments == segment
    model.fit_regressor(segment, X_train[mask], y_train[mask])

# Threshold tuning
model.tune_thresholds(X_val, y_val)

Prediction Flow

python
# Prediction
y_pred = model.predict(X_test)

# Internal steps:
# 1. Classify each row into segment
# 2. Route to appropriate regressor
# 3. Apply threshold for 'zero' segment
# 4. Generate P10/P50/P90 quantiles

Config Parameters

Main hyperparameters (configurable via config.py or AutoML):

GroupParameters
CLF_PARAMSlearning_rate, num_leaves, max_depth, n_estimators, min_child_samples
REG_PARAMSTương tự CLF + alpha (quantile loss parameter)
ThresholdsPer-segment probability thresholds

LƯU Ý KHI REFACTOR

Config params là module-level mutable dicts. Khi chạy concurrent improvement cycles, có thể gây data corruption. Xem IMP-01.

Tài liệu liên quan

BoxMe Forecast — Tài liệu kỹ thuật nội bộ