為每位客戶擬合統計模型

閱讀約需 61 分鐘 分鐘,10 節 中共有 6 個範例。

版次 2026.11.11 範例於 2026年11 月 20 通過測試 41 個範例中有 41 個通過 變更內容

由 AI 翻譯。經人工以英文驗證。 我們如何製作書籍

譯自英文書,版次 2026.11.11。 閱讀原文

本章會針對每位客戶,在相同的回測上擬合 AutoETS 與固定的季節性 ARIMA,將兩者與季節性 naive 比較評分,並學習讓較晚開始的客戶從其第一筆讀數起算。本章假設讀者已讀過前兩章的基準模型與回測內容。

供應商的每位客戶都需要各自的隔日預測,而第 2 章的季節性 naive 對每一位客戶給出相同的答案:昨天的同一個小時。這條規則不會保留對客戶水準或趨勢的任何估計,但各客戶在這兩方面都不相同。因此本章為每位客戶擬合統計模型,並檢驗這些額外的工作是否值得。

此部分尚未翻譯,因此以英文呈現。

Why a statistical model comes next

最近一次變動於第 2026.11.11 版

Chapter 2 set the bar. Seasonal naive forecasts each hour as the same hour yesterday, and the scaled error, MASE, compares clients with different loads on one scale. Chapter 3 backtested that forecast over many cutoffs. A cutoff is the time a forecast is made, and its history ends there. A window is one cutoff and the 24 hours after it, which is the horizon. Each window's history ends at its cutoff, so no hour is forecast with data from its own day.

A statistical model adds to that loop. A series has a state. The level of a series is its typical reading, the trend is the slow drift of the level, the season is the daily shape of the load, and what none of these explain is error. A statistical model holds an estimate of each part of the state of a series, updates the estimates as each reading arrives, and projects them over the horizon. Seasonal naive has no such state. It copies yesterday, so it gets a client's level only as well as yesterday did.

A per-client model is one fit for each client, on that client's history alone, so the cost is one fit per client per window. The subset this chapter uses, SUBSET8, has eight clients. Over the book's three standard cutoffs, Dec 29 to Dec 31, 2014, the last three days of the held-out week and the windows every later chapter reports, that is 24 fits for each model. The code runs the fits in one worker (n_jobs=1), which keeps the output identical from run to run. Every model in this chapter gets the same three cutoffs and the same 1,344 hours of history before each one. The cutoffs fall in the held-out week, and the subset holds the one late starter a later section needs.

Working rule: Fit one model per client only after a baseline and a backtest are in place. Count the fits before you choose the number of windows: clients, times windows, times refits if you run the model every day. Use the same cutoffs and history for every model in a comparison. Otherwise the comparison measures the windows, not the models.

此部分尚未翻譯,因此以英文呈現。

Fit exponential smoothing

最近一次變動於第 2026.11.11 版

Seasonal naive keeps only yesterday, so the first candidate keeps a level instead. Exponential smoothing moves its level toward each new reading. The simplest form updates the level as level_t = alpha * y_t + (1 - alpha) * level_(t-1). The constant alpha, between 0 and 1, sets how fast the level follows the data. The forecast for every hour of the horizon is the last level. Recent readings count for more than older ones, and the weights fall off with age.

ETS, for error, trend and season, extends the simplest form of exponential smoothing. Each part is chosen from a short list. The trend can be none, additive or damped. The error can be additive or multiplicative, and the season can be none, additive or multiplicative. A name such as ETS(A,N,A) reads in that order: additive error, no trend, additive season. ETS(M,Ad,M) has a multiplicative error, a damped trend and a multiplicative season.

AutoETS chooses the ETS form for you. Its default criterion is AICc, the corrected Akaike information criterion, which charges a model for each parameter and corrects for small samples, as the AutoETS docstring in statsforecast 2.1.1 (statsforecast/models.py) says. Two of its defaults matter for hourly demand. The season length defaults to 1, which means no season at all, and the model gives no warning when you leave it that way. The model argument defaults to "ZZZ", which lets the model choose all three parts. For hourly demand with a daily pattern, the call sets season_length=24.

Which form does AutoETS pick for each client, and do its forecasts beat seasonal naive? The lines it prints answer both.

autoets-subset-backtest.py Python 3.13.16 · numpy 2.5.5 · pandas 2.3.3 · statsforecast 2.1.1 · utilsforecast 0.2.17 通過
"""Backtest AutoETS and seasonal naive for the eight clients of SUBSET8 over the three standard cutoffs, and score both.The fits lines count the forms AutoETS chose, and the MASE and coverage lines are the scores the comparison table quotes."""from collections import Counter, defaultdictimport numpy as npimport pandas as pdfrom statsforecast import StatsForecastfrom statsforecast.models import AutoETS, SeasonalNaivefrom lib.scoring import book_scalefrom lib.electricity import loadfrom lib.quirks import filled_history, mask_clock_changesfrom lib.settings import EVAL_CUTOFFS, HORIZON, SEED, SUBSET8, showfrom lib.subsets import score_clients, show_client_linesnp.random.seed(SEED)                                        # the seed from lib/settings.py, set before any fitmasked = mask_clock_changes(load(SUBSET8))                  # clock-change hours become NaN, so they are never scored; filled_history fills them from their neighbours before each fitscale = book_scale(load(SUBSET8), EVAL_CUTOFFS[0])        # MASE scale: rows before the first cutoff, from each client's first reading# season_length=24 for hourly demand; the default of 1 would mean no season at all. n_jobs=1 keeps the output the same from run to run.sf = StatsForecast(models=[AutoETS(season_length=24), SeasonalNaive(season_length=24)], freq="h", n_jobs=1)parts, forms, by_client = [], Counter(), defaultdict(list)for c in pd.to_datetime(EVAL_CUTOFFS):    sf.fit(df=filled_history(masked, c)[["unique_id", "ds", "y"]])   # the eight weeks before c, clock hours filled from them    part = sf.predict(h=HORIZON, level=[95])                          # level=[95] asks for the 95% interval the coverage line scores    # the first forecast hour must be the cutoff itself, or the window is misaligned    if part["ds"].min() != c:        raise ValueError(f"forecast for {c:%Y-%m-%d} does not start at the cutoff")    parts.append(part)    for i, uid in enumerate(sf.uids):        form = sf.fitted_[i, 0].model_["method"]                    # model 0 is AutoETS; its method name is the form, such as ETS(A,N,A)        forms[form] += 1        by_client[uid].append(form)forecasts = pd.concat(parts, ignore_index=True)summary, per_client = score_clients(forecasts, masked, ["AutoETS", "SeasonalNaive"], scale, interval_model="AutoETS")show("horizon hours", HORIZON)show("windows", len(parts))for form, n in sorted(forms.items()):    show(f"fits that chose {form}", n)# the form reads ETS(error,trend,season): a multiplicative part is an M in any of the three slotsshow("fits with a multiplicative part", sum(n for form, n in forms.items() if "M" in form[4:-1].split(",")))for label, value in summary.items():    show(label, value, digits=3 if label.startswith("coverage") else 2)   # coverage is a share, to three decimals; the rest are kWh or ratiosshow("clients where AutoETS has lower MASE", int((per_client["AutoETS"] < per_client["SeasonalNaive"]).sum()))show_client_lines(by_client, per_client, ["AutoETS", "SeasonalNaive"])
輸出python autoets-subset-backtest.py
horizon hours: 24
windows: 3
fits that chose ETS(A,Ad,A): 1
fits that chose ETS(A,N,A): 8
fits that chose ETS(A,N,N): 9
fits that chose ETS(M,Ad,M): 2
fits that chose ETS(M,N,A): 3
fits that chose ETS(M,N,M): 1
fits with a multiplicative part: 6
clients: 8
scored hours per client: 72
MAE AutoETS: 362.11
RMSE AutoETS: 537.20
MASE AutoETS: 1.30
MAE SeasonalNaive: 423.68
RMSE SeasonalNaive: 591.25
MASE SeasonalNaive: 1.16
coverage 95 AutoETS: 0.910
clients where AutoETS has lower MASE: 4
forms MT_014, one per cutoff: ETS(A,N,N) ETS(A,N,N) ETS(A,N,N)
MASE MT_014 AutoETS: 1.18
MASE MT_014 SeasonalNaive: 0.97
forms MT_064, one per cutoff: ETS(A,N,N) ETS(A,N,N) ETS(A,N,N)
MASE MT_064 AutoETS: 2.32
MASE MT_064 SeasonalNaive: 1.08
forms MT_115, one per cutoff: ETS(M,N,A) ETS(M,N,A) ETS(M,N,A)
MASE MT_115 AutoETS: 0.95
MASE MT_115 SeasonalNaive: 1.31
forms MT_166, one per cutoff: ETS(A,Ad,A) ETS(A,N,A) ETS(A,N,A)
MASE MT_166 AutoETS: 1.18
MASE MT_166 SeasonalNaive: 1.29
forms MT_217, one per cutoff: ETS(M,N,M) ETS(M,Ad,M) ETS(M,Ad,M)
MASE MT_217 AutoETS: 1.76
MASE MT_217 SeasonalNaive: 1.25
forms MT_268, one per cutoff: ETS(A,N,A) ETS(A,N,A) ETS(A,N,A)
MASE MT_268 AutoETS: 0.94
MASE MT_268 SeasonalNaive: 0.89
forms MT_319, one per cutoff: ETS(A,N,A) ETS(A,N,A) ETS(A,N,A)
MASE MT_319 AutoETS: 1.11
MASE MT_319 SeasonalNaive: 1.26
forms MT_370, one per cutoff: ETS(A,N,N) ETS(A,N,N) ETS(A,N,N)
MASE MT_370 AutoETS: 0.96
MASE MT_370 SeasonalNaive: 1.25

The chosen form changes from client to client, and for some clients from one cutoff to the next. Across the 24 fits, AutoETS chose ETS(A,N,N), with no season, for 9 of them, and ETS(A,N,A), with an additive season, for 8. ETS(A,Ad,A), a damped trend with an additive season, came up in 1 of the fits. The other fits used a multiplicative part, in the error, the season or both. The lines under the totals give each client's three forms. Three clients, MT_014, MT_064 and MT_370, take the flat form at all three cutoffs. MT_014 is the lead client, the one Chapter 1 met.

A flat form winning looks odd when the seasonal form contains it. ETS(A,N,N) is ETS(A,N,A) with the seasonal part switched off, so the best seasonal fit cannot have a lower likelihood than the flat fit on the same history. Fitting both forms on each of the 24 windows shows whether that happens.

flat-form-check.py Python 3.13.16 · numpy 2.5.5 · pandas 2.3.3 · statsforecast 2.1.1 · utilsforecast 0.2.17 通過
"""For each subset window, fit the flat form ETS(A,N,N) and the additive-season form ETS(A,N,A) on the same history, and count where the seasonal fit has the lower likelihood.The two counts printed last are the ones the prose quotes."""import numpy as npimport pandas as pdfrom statsforecast import StatsForecastfrom statsforecast.models import AutoETSfrom lib.electricity import loadfrom lib.quirks import filled_history, mask_clock_changesfrom lib.settings import EVAL_CUTOFFS, SEED, SUBSET8, shownp.random.seed(SEED)                                        # the seed from lib/settings.py, set before any fitmasked = mask_clock_changes(load(SUBSET8))windows = seasonal_lower_loglik = seasonal_higher_aicc = 0for c in pd.to_datetime(EVAL_CUTOFFS):    hist = filled_history(masked, c)[["unique_id", "ds", "y"]]    # ANN is additive error, no trend, no season: the flat form. ANA is the same with an additive season.    sf = StatsForecast(models=[AutoETS(model="ANN", season_length=24, alias="flat"),                               AutoETS(model="ANA", season_length=24, alias="seasonal")], freq="h", n_jobs=1)    sf.fit(df=hist)    for i in range(len(sf.uids)):        flat = sf.fitted_[i, 0].model_                          # models are fitted in the order listed: 0 is the flat fit        seasonal = sf.fitted_[i, 1].model_                      # 1 is the seasonal fit        windows += 1        # ANA contains ANN, so a lower seasonal likelihood points to a fit that stopped short of its best        seasonal_lower_loglik += int(seasonal["loglik"] < flat["loglik"])        # AICc charges each parameter, so this count can differ from the likelihood count        seasonal_higher_aicc += int(seasonal["aicc"] > flat["aicc"])show("windows", windows)show("seasonal fit has the lower log-likelihood", seasonal_lower_loglik)show("seasonal fit has the higher AICc", seasonal_higher_aicc)
輸出python flat-form-check.py
windows: 24
seasonal fit has the lower log-likelihood: 9
seasonal fit has the higher AICc: 9

The seasonal fit loses on likelihood, not only on AICc. Its log-likelihood is lower in 9 of the 24 windows, and its AICc is higher in 9. Those are the same nine windows in which the default run chose the flat form. AICc charges a model for each parameter, and the seasonal form has 24 more. That charge adds to the gap; it does not cause it, because the seasonal fit's likelihood is already lower before any charge. The seasonal fit most likely stopped short of its best value, and the flat form won by default. The book does not chase the optimiser; it tests the choice with a forced season.

Chapter 1 forced an additive season on the lead client, with model="ZZA", and seasonal naive still had the lower error there. Does that hold across all eight? The forced run covers the same eight clients, beside the default.

autoets-forced-season-backtest.py Python 3.13.16 · numpy 2.5.5 · pandas 2.3.3 · statsforecast 2.1.1 · utilsforecast 0.2.17 通過
"""Backtest AutoETS with the season forced to additive (model ZZA) and seasonal naive, for the eight clients of SUBSET8 over the three standard cutoffs.The ZZA lines are the forced-season scores that the comparison table and the forced-season paragraph quote."""from collections import Counter, defaultdictimport numpy as npimport pandas as pdfrom statsforecast import StatsForecastfrom statsforecast.models import AutoETS, SeasonalNaivefrom lib.scoring import book_scalefrom lib.electricity import loadfrom lib.quirks import filled_history, mask_clock_changesfrom lib.settings import EVAL_CUTOFFS, HORIZON, SEED, SUBSET8, showfrom lib.subsets import score_clients, show_client_linesnp.random.seed(SEED)                                        # the seed from lib/settings.py, set before any fitmasked = mask_clock_changes(load(SUBSET8))                  # clock-change hours become NaN, so they are never scored; filled_history fills them from their neighbours before each fitscale = book_scale(load(SUBSET8), EVAL_CUTOFFS[0])        # MASE scale: rows before the first cutoff, from each client's first reading# ZZA: Z lets the model choose the error and the trend, A forces an additive season. alias names the output columns.sf = StatsForecast(models=[AutoETS(model="ZZA", season_length=24, alias="ZZA"), SeasonalNaive(season_length=24)],                   freq="h", n_jobs=1)parts, forms, by_client = [], Counter(), defaultdict(list)for c in pd.to_datetime(EVAL_CUTOFFS):    sf.fit(df=filled_history(masked, c)[["unique_id", "ds", "y"]])   # the eight weeks before c, clock hours filled from them    part = sf.predict(h=HORIZON, level=[95])                          # level=[95] asks for the 95% interval the coverage line scores    # the first forecast hour must be the cutoff itself, or the window is misaligned    if part["ds"].min() != c:        raise ValueError(f"forecast for {c:%Y-%m-%d} does not start at the cutoff")    parts.append(part)    for i, uid in enumerate(sf.uids):        form = sf.fitted_[i, 0].model_["method"]                    # model 0 is the forced AutoETS; its method name is the form        forms[form] += 1        by_client[uid].append(form)forecasts = pd.concat(parts, ignore_index=True)summary, per_client = score_clients(forecasts, masked, ["ZZA", "SeasonalNaive"], scale, interval_model="ZZA")show("horizon hours", HORIZON)show("windows", len(parts))for form, n in sorted(forms.items()):    show(f"fits that chose {form}", n)for label, value in summary.items():    show(label, value, digits=3 if label.startswith("coverage") else 2)show("clients where ZZA has lower MASE", int((per_client["ZZA"] < per_client["SeasonalNaive"]).sum()))show_client_lines(by_client, per_client, ["ZZA", "SeasonalNaive"])
輸出python autoets-forced-season-backtest.py
horizon hours: 24
windows: 3
fits that chose ETS(A,Ad,A): 1
fits that chose ETS(A,N,A): 17
fits that chose ETS(M,N,A): 6
clients: 8
scored hours per client: 72
MAE ZZA: 365.24
RMSE ZZA: 553.11
MASE ZZA: 1.17
MAE SeasonalNaive: 423.68
RMSE SeasonalNaive: 591.25
MASE SeasonalNaive: 1.16
coverage 95 ZZA: 0.925
clients where ZZA has lower MASE: 4
forms MT_014, one per cutoff: ETS(A,N,A) ETS(A,N,A) ETS(A,N,A)
MASE MT_014 ZZA: 1.08
MASE MT_014 SeasonalNaive: 0.97
forms MT_064, one per cutoff: ETS(A,N,A) ETS(A,N,A) ETS(A,N,A)
MASE MT_064 ZZA: 1.27
MASE MT_064 SeasonalNaive: 1.08
forms MT_115, one per cutoff: ETS(M,N,A) ETS(M,N,A) ETS(M,N,A)
MASE MT_115 ZZA: 0.95
MASE MT_115 SeasonalNaive: 1.31
forms MT_166, one per cutoff: ETS(A,Ad,A) ETS(A,N,A) ETS(A,N,A)
MASE MT_166 ZZA: 1.18
MASE MT_166 SeasonalNaive: 1.29
forms MT_217, one per cutoff: ETS(M,N,A) ETS(M,N,A) ETS(M,N,A)
MASE MT_217 ZZA: 1.78
MASE MT_217 SeasonalNaive: 1.25
forms MT_268, one per cutoff: ETS(A,N,A) ETS(A,N,A) ETS(A,N,A)
MASE MT_268 ZZA: 0.94
MASE MT_268 SeasonalNaive: 0.89
forms MT_319, one per cutoff: ETS(A,N,A) ETS(A,N,A) ETS(A,N,A)
MASE MT_319 ZZA: 1.11
MASE MT_319 SeasonalNaive: 1.26
forms MT_370, one per cutoff: ETS(A,N,A) ETS(A,N,A) ETS(A,N,A)
MASE MT_370 ZZA: 1.05
MASE MT_370 SeasonalNaive: 1.25

On mean MASE, forcing the season brings AutoETS within a hair of seasonal naive, though not below it: 1.17 against 1.30 for the default and 1.16 for seasonal naive.

The forced season helps some clients and hurts others. It lowers the lead client's MASE, from 1.18 to 1.08, and MT_064's too, but it raises MT_370's, from 0.96 to 1.05. So forcing the season is a change to test on each client, not a fix to apply to all of them.

Working rule: Start with AutoETS for hourly demand with season_length=24, and print the chosen forms in every backtest. When a client takes a flat form, check that its seasonal form is not failing. If the seasonal fit has the lower likelihood, force the season with model="ZZA", and score both forms on earlier windows before you keep either: cutoffs before the held-out week that the report does not score, such as the four ordinary days in late November and early December 2014, which Chapter 5 sets aside for choices. The standard cutoffs show the comparison here, but they do not decide it. When a forecast gets worse, look first for a form that changed between cutoffs.

擬合固定的季節性 ARIMA

最近一次變動於第 2026.11.5 版

第二個候選模型描述的是季節性樸素法所遺留的部分。第 3 章的回測則用來檢驗它究竟增加了什麼。ARIMA 模型在差分之後,把下一筆讀數寫成先前讀數與先前誤差的加權和;所謂差分,是將一筆讀數減去前一個季節的讀數。模型記為 ARIMA(p,d,q)(P,D,Q)[m]。非季節性部分有 p 個自迴歸項、d 次差分與 q 個移動平均項。季節性部分同樣有這三者,只是落後期數是季節長度 m 的倍數。

本書採用的模型是 ARIMA(1,0,0)(1,1,0)[24]。它有一個自迴歸項、沒有一般差分、沒有移動平均項、有一次落後 24 期的季節差分,以及一個落後 24 期的季節自迴歸項。令 B 為後移運算子,使 B y_t 等於 y_(t-1),則模型寫成:

(1 - phi1 B)(1 - Phi1 B^24)(1 - B^24) y_t = e_t

(1 - B^24) 這一項把每筆讀數換成它與昨天同一小時相比的變化量。對逐時需求而言,落後 24 期正是這個模型的重點。每個小時都與昨天的同一小時比較,這正是季節性樸素法所做的比較,而自迴歸項接著對這個比較所遺留的部分建模。季節差分會移除水準,因此模型沒有均值項。statsforecast 的 ARIMA 預設對經過差分的序列不加入均值(見 statsforecast/models.py 中 ARIMA 的 docstring)。

我們固定階數,而不去搜尋階數,原因見自動搜尋一節。在把這個階數放進回測之前,先用 statsmodels 0.15.0 對主要客戶在第一個標準截止點之前的歷史資料擬合一次,並印出擬合後的參數名稱。這可以顯示模型是否具有你所寫下的那些項。

arima-summary-look.py Python 3.13.16 · numpy 2.5.5 · pandas 2.3.3 · statsmodels 0.15.0 · utilsforecast 0.2.17 通過
"""Fit the fixed seasonal ARIMA with statsmodels on the lead client's history before the first standard cutoff, and print its parameter names. The parameter lines are what the prose reads."""import pandas as pdfrom statsmodels.tsa.statespace.sarimax import SARIMAXfrom lib.backtest import history_beforefrom lib.electricity import loadfrom lib.settings import EVAL_CUTOFFS, LEAD, showhist = history_before(load([LEAD]), pd.Timestamp(EVAL_CUTOFFS[0]))y = pd.Series(hist["y"].to_numpy(), index=pd.DatetimeIndex(hist["ds"], freq="h"))res = SARIMAX(y, order=(1, 0, 0), seasonal_order=(1, 1, 0, 24)).fit(disp=False)show("hours of history", len(y))for name in res.params.index:    show("parameter", name)
輸出python arima-summary-look.py
hours of history: 1344
parameter: ar.L1
parameter: ar.S.L24
parameter: sigma2

清單中有三個名稱:ar.L1、ar.S.L24 與 sigma2。第一個是自迴歸項,表示某小時相對於昨天同一小時的變化,有多少會延續到下一個小時。第二個是落後 24 期的季節項,表示某一天在某個小時的變化,有多少會延續到隔天的同一小時。第三個是誤差的變異數。清單中沒有截距。statsmodels 只有在設定 trend 時,才會為季節性 ARIMA 加入截距,所以無論序列是否經過差分,清單都不會出現截距,而經過季節差分的模型本來就不需要截距。完整的 statsmodels 摘要也會給出各個數值,本書不引用這些數值。這些數值會隨視窗與函式庫版本而改變,因此讀者更需要理解各項的意義,而不是它們的數字。

這個固定的模型接著就成為回測中的另一個候選,與 AutoETS 和季節性樸素法並列。它的分數見下方的比較一節。

實用原則: 當搜尋慢到無法在每個截止點都執行時,就固定階數,並把它寫在呼叫中。只印出一次摘要,檢查其中列出的項是否就是你想要的。如同重新擬合任何模型,在每個截止點都重新擬合這個固定的階數。階數保持固定,係數則不然。

此部分尚未翻譯,因此以英文呈現。

Compare models on the same cutoffs

最近一次變動於第 2026.11.11 版

Four forecasts now cover the same 24 hours at the same three cutoffs, from the same eight weeks of history, for the same eight clients: seasonal naive, AutoETS, AutoETS with the season forced, and ARIMA. Each backtest is a separate script, so each finishes well inside the 45 seconds the book allows an example on two CPUs.

One score is still missing: the fixed ARIMA's, on the same windows and measures.

arima-subset-backtest.py Python 3.13.16 · numpy 2.5.5 · pandas 2.3.3 · statsforecast 2.1.1 · utilsforecast 0.2.17 通過
"""Backtest the fixed seasonal ARIMA(1,0,0)(1,1,0)[24] and seasonal naive for the eight clients of SUBSET8 over the three standard cutoffs.The MASE gap and the count of clients where ARIMA wins, printed last, are the figures the comparison section quotes."""import numpy as npimport pandas as pdfrom statsforecast import StatsForecastfrom statsforecast.models import ARIMA, SeasonalNaivefrom lib.scoring import book_scalefrom lib.electricity import loadfrom lib.quirks import filled_history, mask_clock_changesfrom lib.settings import EVAL_CUTOFFS, HORIZON, SEED, SUBSET8, showfrom lib.subsets import score_clientsnp.random.seed(SEED)                                        # the seed from lib/settings.py, set before any fitmasked = mask_clock_changes(load(SUBSET8))                  # clock-change hours become NaN, so they are never scored; filled_history fills them from their neighbours before each fitscale = book_scale(load(SUBSET8), EVAL_CUTOFFS[0])        # MASE scale: rows before the first cutoff, from each client's first reading# The order is fixed, not searched: one AR term, one seasonal difference and one seasonal AR term, all at lag 24.# n_jobs=1 keeps the output the same from run to run.sf = StatsForecast(models=[ARIMA(order=(1, 0, 0), season_length=24, seasonal_order=(1, 1, 0)),                           SeasonalNaive(season_length=24)], freq="h", n_jobs=1)parts = []for c in pd.to_datetime(EVAL_CUTOFFS):    sf.fit(df=filled_history(masked, c)[["unique_id", "ds", "y"]])   # the eight weeks before c, clock hours filled from them    part = sf.predict(h=HORIZON)    # the first forecast hour must be the cutoff itself, or the window is misaligned    if part["ds"].min() != c:        raise ValueError(f"forecast for {c:%Y-%m-%d} does not start at the cutoff")    parts.append(part)forecasts = pd.concat(parts, ignore_index=True)summary, per_client = score_clients(forecasts, masked, ["ARIMA", "SeasonalNaive"], scale)show("horizon hours", HORIZON)show("windows", len(parts))for label, value in summary.items():    show(label, value)show("MASE gap, SeasonalNaive minus ARIMA", summary["MASE SeasonalNaive"] - summary["MASE ARIMA"])show("clients where ARIMA has lower MASE", int((per_client["ARIMA"] < per_client["SeasonalNaive"]).sum()))
輸出python arima-subset-backtest.py
horizon hours: 24
windows: 3
clients: 8
scored hours per client: 72
MAE ARIMA: 386.13
RMSE ARIMA: 576.30
MASE ARIMA: 1.09
MAE SeasonalNaive: 423.68
RMSE SeasonalNaive: 591.25
MASE SeasonalNaive: 1.16
MASE gap, SeasonalNaive minus ARIMA: 0.07
clients where ARIMA has lower MASE: 5

MAE is in kilowatt hours, and RMSE, the root mean squared error, punishes large misses more. Both are computed for each client over its 72 scored hours, then averaged over the eight clients, so each client counts once. MASE divides each client's MAE by the MAE that seasonal naive makes in its own history, over every reading before Dec 29, 2014, from each client's first reading and with the clock-change hours left out, then averages over clients. A MASE of 1 means the model is as accurate as seasonal naive was on its own past. The coverage of AutoETS is the share of scored hours that fall inside its 95% prediction interval, the range the model gives for each hour.

Model MAE (kWh) RMSE (kWh) MASE Clients beating seasonal naive on MASE
SeasonalNaive 423.68 591.25 1.16 not applicable
AutoETS 362.11 537.20 1.30 4
AutoETS, season forced (ZZA) 365.24 553.11 1.17 4
ARIMA 386.13 576.30 1.09 5

Read the last column as a count out of the eight clients, not as a score. The AutoETS intervals cover 0.910 of the scored hours, and the forced-season intervals cover 0.925. The nominal share is 0.95, so the intervals are a little narrow on this sample. Coverage checks the intervals, not the point forecast. An interval that covers too little is too narrow, and a planner who buys to it will be short more often than its label says. Chapter 8 returns to intervals and shows how to calibrate them on earlier windows.

The two measures disagree about AutoETS. Its mean MAE is the lowest of the four rows, and its mean MASE is the highest. MAE in kilowatt hours gives more weight to the clients with large loads, because their errors are large. MASE scales each client's error by that client's own history, so every client counts about the same. The per-client lines in the AutoETS run show where the MASE comes from. MT_064 has a MASE of 2.32, against 1.08 for seasonal naive, and it is one of the flat-form clients. MT_217 has 1.76, against 1.25, and its form changes from one cutoff to the next. The ranking depends on the measure, so the choice of measure is part of the decision.

Three windows are a small sample, as Chapter 3 explains. AutoETS beats seasonal naive on 4 of the eight clients, and ARIMA on 5. ARIMA is better than seasonal naive on both measures, and on MASE by 0.07. Three windows can't settle a gap that small.

Working rule: Score every candidate on the same cutoffs, with the same history and the same scale, and report MAE and MASE side by side. When the two disagree, say which one the decision needs. We recommend more windows before choosing between two models that differ by less than about a tenth of MASE. That threshold is a judgement from this sample, not a measurement.

認識晚開始的客戶

最近一次變動於第 2026.11.5 版

子集中有一位客戶較晚才開始有讀數。MT_115 第一筆非零讀數出現在 2014 年 1 月 14 日 00:00。在那之前,電表回報的都是零,資料將這些時段標記為 before_first_reading。這些零並不代表低用電量。模型若讀入這些零,就會學到該客戶實際上沒有的用電水準。晚開始的客戶,是指第一筆讀數落在其模型所讀取的歷史範圍之內的客戶。解決方式是起始規則:數列從它的第一筆讀數開始。這關係到供應商的決策。水準被拉低,意味著為該客戶購電不足,而這是供應商兩種錯誤中成本較高的一種。

在 12 月的截止時點,每個截止時點之前的八週都是讀數,因此本節改用 2 月的截止時點:2014 年 2 月 1 日、2014 年 2 月 15 日與 2014 年 2 月 28 日。這些截止時點的八週歷史仍然會回溯到第一筆讀數之前,所以這個情況就包含在其中。第 6 章會將同樣的規則套用到全部三十位客戶。

從零值開始的歷史資料

最近一次變動於第 2026.11.5 版

先看原始歷史資料,也就是每個 2 月截止日之前的八週資料,並保留其中的零值。這段歷史資料中零值小時的占比,在 2014 年 2 月 1 日為 68%,在 2014 年 2 月 15 日為 43%,在 2014 年 2 月 28 日為 20%。截止日越過第一筆讀數之後,這個占比就會下降。

讓歷史資料從第一筆讀數開始,或許能解決問題。在每個 2 月截止日對 AutoETS 擬合兩次,一次使用原始的八週資料,一次使用起始歷史資料,後者從 MT_115 的第一筆讀數開始,並以各截止日之後的 24 小時評分,即可得知是否如此。

late-starter-trim.py Python 3.13.16 · numpy 2.5.5 · pandas 2.3.3 · statsforecast 2.1.1 · utilsforecast 0.2.17 通過
"""Backtest AutoETS for MT_115, a late starter, on three February cutoffs: once on its raw eight weeks and once on its history from its first reading.The start-rule counts, printed first, and the three-window MAE lines are the figures the point rests on."""import numpy as npimport pandas as pdfrom statsforecast import StatsForecastfrom statsforecast.models import AutoETSfrom lib.backtest import history_beforefrom lib.electricity import loadfrom lib.quirks import fill_clock_change_hours, mask_clock_changesfrom lib.settings import HORIZON, SEED, SUBSET8, showfrom lib.starts import check_start_rule, history_from_startLATE = "MT_115"                                             # the one late starter in SUBSET8, first non-zero reading on Jan 14, 2014# February cutoffs: at the December cutoffs every hour of the history is a reading, so the rule changes nothing thereCUTOFFS = pd.to_datetime(["2014-02-01", "2014-02-15", "2014-02-28"])np.random.seed(SEED)                                        # the seed from lib/settings.py, set before any fitdropped, flagged = check_start_rule(load(SUBSET8))           # raises unless the rule drops exactly the flagged rows, so the counts agreeshow("start rule drops, SUBSET8", dropped)show("flagged before_first_reading, SUBSET8", flagged)masked = mask_clock_changes(load([LATE]))[["unique_id", "ds", "y", "quirk"]]def fit_and_score(hist, c):    """Fit AutoETS on one history, forecast the 24 hours from c, and score it on those hours."""    sf = StatsForecast(models=[AutoETS(season_length=24)], freq="h", n_jobs=1)    sf.fit(df=fill_clock_change_hours(hist)[["unique_id", "ds", "y"]])   # clock-change hours filled from their neighbouring hours, as elsewhere    fc = sf.predict(h=HORIZON)    actual = masked[(masked["ds"] >= c) & (masked["ds"] < c + pd.Timedelta(hours=HORIZON))]   # the day's 24 readings, to score against    joined = fc.merge(actual[["ds", "y"]], on="ds", how="left")    # the form's method name, the mean forecast, the day's MAE, and the mean of the readings    return sf.fitted_[0, 0].model_["method"], float(joined["AutoETS"].mean()), float((joined["y"] - joined["AutoETS"]).abs().mean()), float(joined["y"].mean())maes = {"raw": [], "start": []}for c in CUTOFFS:    raw_hist = history_before(masked, c)                     # the eight weeks before c, zeros included; it insists on exactly 1,344 rows    # the share of hours that read zero, including the leading zeros before the first reading    show(f"zero percent before {c:%Y-%m-%d}", round(100 * (raw_hist["y"] == 0).mean()))    # the start history begins at the first reading, so it is shorter wherever the client starts late    for label, hist in [("raw", raw_hist), ("start", history_from_start(masked, c))]:        form, fc_mean, window_mae, actual_mean = fit_and_score(hist, c)        show(f"{label} history hours {c:%Y-%m-%d}", len(hist))        show(f"{label} form {c:%Y-%m-%d}", form)        show(f"{label} mean forecast {c:%Y-%m-%d}", fc_mean)        show(f"{label} mean actual {c:%Y-%m-%d}", actual_mean)        show(f"MAE {label} {c:%Y-%m-%d}", window_mae)        maes[label].append(window_mae)for label in maes:    show(f"MAE {label}, three windows", np.mean(maes[label]))    show(f"MAE {label}, 2014-02-15 and 2014-02-28", np.mean(maes[label][1:]))   # [1:] skips Feb 1, the first cutoff
輸出python late-starter-trim.py
start rule drops, SUBSET8: 9072
flagged before_first_reading, SUBSET8: 9072
zero percent before 2014-02-01: 68
raw history hours 2014-02-01: 1344
raw form 2014-02-01: ETS(A,N,N)
raw mean forecast 2014-02-01: 16.43
raw mean actual 2014-02-01: 24.06
MAE raw 2014-02-01: 8.81
start history hours 2014-02-01: 432
start form 2014-02-01: ETS(M,Ad,A)
start mean forecast 2014-02-01: 23.88
start mean actual 2014-02-01: 24.06
MAE start 2014-02-01: 3.72
zero percent before 2014-02-15: 43
raw history hours 2014-02-15: 1344
raw form 2014-02-15: ETS(A,N,A)
raw mean forecast 2014-02-15: 24.54
raw mean actual 2014-02-15: 27.11
MAE raw 2014-02-15: 4.29
start history hours 2014-02-15: 768
start form 2014-02-15: ETS(M,Ad,A)
start mean forecast 2014-02-15: 23.84
start mean actual 2014-02-15: 27.11
MAE start 2014-02-15: 4.77
zero percent before 2014-02-28: 20
raw history hours 2014-02-28: 1344
raw form 2014-02-28: ETS(A,N,A)
raw mean forecast 2014-02-28: 23.73
raw mean actual 2014-02-28: 23.54
MAE raw 2014-02-28: 2.93
start history hours 2014-02-28: 1080
start form 2014-02-28: ETS(M,Ad,A)
start mean forecast 2014-02-28: 22.48
start mean actual 2014-02-28: 23.54
MAE start 2014-02-28: 2.73
MAE raw, three windows: 5.34
MAE raw, 2014-02-15 and 2014-02-28: 3.61
MAE start, three windows: 3.74
MAE start, 2014-02-15 and 2014-02-28: 3.75

損害出現在水準(level)上,且以 2 月 1 日最為嚴重。AutoETS 擬合原始歷史資料時沒有出現錯誤,預測時也沒有任何警告。在 2014 年 2 月 1 日,它選擇了平坦形式 ETS(A,N,N),其預測在當天平均為每小時 16.43 kWh。隨後的讀數平均為 24.06 kWh。當天的 MAE 為 8.81 kWh。可能的原因在於這些零值:它們把水準拉低,而模型無從得知它們並不是用電量。

在另外兩天,原始歷史資料的誤差小得多,2014 年 2 月 15 日為 4.29 kWh,2014 年 2 月 28 日為 2.93 kWh。因此,損害的程度取決於視窗中含有多少零值。無論零值占比多少,這項規則都是正確的,因為開頭的零值並不是用電量。

讓每條序列從第一筆讀數開始

最近一次變動於第 2026.11.5 版

起始規則規定,一條序列從它的第一筆讀數開始。它會捨棄客戶第一筆非零讀數之前的所有讀數,並保留其後的所有讀數,包括零值,因為起始之後的零值是真實的低用電量,模型需要這些資料。若視窗從第一筆讀數之後才開始,它會保留所有小時,因為這條規則只移除開頭的零值。這條規則寫在 examples/lib/starts.py 中,因此後續各章會呼叫同一份程式碼。其函式 history_from_start(df, cutoff) 會回傳在此規則下某一個截止點的歷史資料:即截止點之前八週內、從第一筆讀數起算的讀數。它只依截止點之前的資料列來找出第一筆讀數,因此截止點之後的讀數不會進入模型。若某位客戶在截止點之前沒有任何讀數,或歷史資料不足 48 小時,函式會拋出錯誤。

在使用這條規則之前,要先檢查它是否與旗標所標示的一致。check_start_rule 會計算這條規則在整條序列上會捨棄的資料列數,除非這些資料列恰好就是標記為 before_first_reading 的資料列,否則會拋出錯誤。在子集上,這條規則捨棄 9072 小時,而旗標標記了 9072。兩者相符。子集中唯一的晚起始客戶是 MT_115。

這條規則在第一個截止點縮短歷史資料最多,為 432 小時,而原本是 1,344 小時;隨著截止點越過第一筆讀數,縮短的幅度變小:2 月 15 日為 768 小時,2 月 28 日為 1080 小時。

採用起始規則的歷史資料在三個截止點都選出 ETS(M,Ad,A),這是一種具有乘法誤差、阻尼趨勢與加法季節性的形式。原始歷史資料的形式則逐日改變,從 2 月 1 日的平坦形式,變為 2014 年 2 月 15 日的 ETS(A,N,A),以及 2014 年 2 月 28 日的 ETS(A,N,A)。

三個視窗的 MAE 從 5.34 kWh 降到 3.74 kWh。各視窗的改善並不均勻,所有的改善都來自 2 月 1 日,該日的 MAE 從 8.81 降到 3.72 kWh。

在 2 月 15 日,這條規則的表現較差;在 2 月 28 日,則稍微好一些。把這兩天合併來看,原始歷史資料的表現略佳,平均 MAE 為 3.61 kWh,而採用起始規則者為 3.75。

擬合結果顯示了這條規則在 2 月 15 日為何較差。起始後的歷史資料仍然很短,只有 768 小時,其阻尼趨勢的預測比原始擬合的預測低:平均每小時 23.84 kWh,而原始擬合為 24.54,同時讀數平均為 27.11 kWh。這個偏低的預測,就是該案例所警告的代價。一位客戶與三天只是很小的樣本,因此這些數字是用來說明這條規則,而不是證明它。

實用原則: 當客戶的歷史資料含有零值時,要檢查其第一筆非零讀數是否落在該歷史資料之內。若是,無論零值所占比例多少,都要在任何模型看到歷史資料之前套用起始規則,因為開頭的零值並不是用電量。先對整條序列執行 check_start_rule,若未通過,應先查明原因,再進行任何修剪。要印出每個截止點的歷史長度,因為歷史資料變短是這條規則裁掉過多資料的第一個徵兆。

為什麼本書在季節長度為 24 時避免使用自動 ARIMA

最近一次變動於第 2026.11.5 版

AutoARIMA 是搜尋階數,而不是固定階數。它對候選階數進行逐步搜尋,以預設的 AICc 挑選最佳者,最多在 94 個候選模型之後停止(statsforecast 2.1.1、statsforecast/models.py,以及 statsforecast/arima.py 中的停止訊息)。每個候選模型都是一次完整的狀態空間擬合。季節長度為 24 時,每個在延遲 24 含有季節項的候選模型都很慢,而搜尋會嘗試數十個這類模型,因此耗時遠超過本章中的各項擬合。

在本書固定的環境中,使用兩顆 CPU 與 statsforecast 2.1.1,為單一客戶擬合一個視窗約需 75 秒。這是在第一個標準截止點對 MT_014 進行的一次執行。該子集需要 24 個這樣的視窗,因此對它做一次回測約需半小時,而本書對每個範例容許的時間是 45 秒。這是本書自己的量測結果,並非書中的範例,且會隨環境而異,所以在依此安排排程之前,請先在你自己的機器上計時。固定階數每個視窗只需一次擬合,這正是此處關鍵的差異。它的弱點在於某位客戶的模式與該階數的假設不同。回測就是檢查這一點的方法,可查看比較一節中的分數。

固定階數是一種判斷,而不是發現。我們選擇季節長度為 24 的 (1,0,0)(1,1,0),是因為資料具有每日模式,而且本章的回測需要一個能跑完的模型。階數寫在呼叫之中,讀者可以看到並加以修改。後續各章會在相同的視窗上與它比較,因此階數的變動會反映在分數上。

實用原則: 在每天重新擬合的排程中,不要對季節長度為 24 的逐時資料執行自動搜尋。在採用之前,先對一位客戶的搜尋計時。如果太慢,就固定階數,並在回測中測試該階數。

正式環境中會出什麼問題

  • 缺少季節性。 季節長度若停留在 1,得到的模型就沒有每日模式,而且不會出現錯誤。徵兆是每位客戶選出的形式都沒有季節項,例如 ETS(A,N,N)。若只有少數客戶如此,則是另一種情況:季節性模型擬合失敗,這可用指數平滑一節中的平坦形式檢查來測試。每次重新擬合時,都要記錄所選出的形式。
  • 晚開始者的零值。 若客戶是在歷史資料之中才開始,其水準會被拉低,直到第一筆讀數離開視窗為止。徵兆是開始之後的數天內預測值偏低。每次重新擬合之前,先計算每位客戶歷史資料中零值所占的比例,並對任何第一筆讀數落在歷史資料之內的客戶套用起始規則。
  • 形式的改變。 同一位客戶所選出的形式,可能在不同次重新擬合之間改變。這本身並不算錯,但若從季節性形式切換成沒有季節項的形式,就值得對照讀數檢查。請把這些形式保留在執行記錄中。
  • 累加的成本。 擬合次數會隨客戶數、視窗數與重新擬合次數相乘而成長。在把客戶加入排程之前,先計算這個數字。
  • 未涵蓋的區間。 在這份樣本上,AutoETS 的區間涵蓋了 0.910 的小時,低於名目值 0.95。第 8 章會說明如何以先前的視窗校準區間帶;在區間帶以這種方式校準之前,不要把它當成 95% 的範圍來呈現。
  • 如何察覺。 在每天的視窗上,把季節性單純法與每個統計模型並列執行,並記錄 MASE 的差距。若差距的正負號逐日變動,這是該檢視模型的訊號,而不是可以報告的結果。

本章做出的決定

最近一次變動於第 2026.11.5 版
  • **季節長度為 24 的 AutoETS。**預設的 model="ZZZ" 與強制指定季節的執行 model="ZZA" 都會報告,因為平坦的選擇可能是失敗的季節性擬合。本章不在這兩種形式之間做選擇:標準截止點的比較只作報告,不用來選擇,選擇應當在更早的視窗上進行。第 1 章曾對主要客戶強制指定 ZZA,本章則對全部八個客戶呈現強制指定的結果。
  • **固定的 ARIMA(1,0,0)(1,1,0)[24],不做搜尋。**這是一項明確陳述的判斷,回測正是用來檢驗它。
  • **八個客戶,而非三十個。**第 6 章以單一模型擬合全部三十個客戶。在固定的環境設定下,對三十個客戶逐一擬合 AutoETS 並跨三個視窗執行,約需 97 秒,而本書允許每個範例使用 45 秒,因此本章使用子集。
  • 子集、截止點與尺度。SUBSET8、三個標準截止點、2014 年 12 月 29 日至 12 月 31 日、1,344 小時的歷史資料,以及在每項比較中作為基準的季節性樸素法。MASE 的尺度從每個客戶的第一筆讀數起算,並排除時鐘調整的時數。
  • **時鐘調整的時數。**每個子集範例都會將這些時數遮蔽,並以歷史資料填補,如第 3 章所述。三個視窗都不含時鐘調整的時數,因此遮蔽不會改變任何預測,也不會改變任何計分的時數。但它確實會改變 MASE 的尺度,因為尺度會讀取 2014 年 12 月 29 日之前的全部歷史資料,而 MT_115 是從 2014 年 1 月 14 日起算。
  • **較晚開始的客戶:二月截止點與起始規則。**十二月的截止點遇不到 MT_115 的較晚起始,所以改用二月的截止點。第 6 章會將此規則套用到全部三十個客戶。
  • **只對兩個 AutoETS 執行計算涵蓋率。**此處的 ARIMA 回測在計分時不含預測區間。
  • **不引用擬合後的數值。**ARIMA 腳本只印出參數名稱,平坦形式的檢查只印出計數。誤差四捨五入至小數點後兩位,涵蓋率則至三位。
  • **作為第 5 章的統計基準。**這些視窗上各客戶的 AutoETS 與 ARIMA 分數,就是梯度提升模型必須超越的門檻。
  • **往後要帶著的事。**平坦的形式可能是失敗的季節性擬合,因此在每項分數旁都要查看所選的形式。MAE 與 MASE 對模型的排序可能不同,所以要說明決定需要的是哪一個。每個序列都從第一筆讀數開始,並在相近的模型之間做選擇之前,先增加視窗。

本章,於本版次中

版次 2026.11.11範例於 2026年11 月 20 通過測試本章 6 個範例 個範例中有 6 個通過

譯自英文書,版次 2026.11.11。

引用本節

格式

各種格式皆引用英文原書,本譯本即依循該原書。

每種格式都會標示本版。其連結會開啟目前版次,節標題會說明該節內文最後一次變動的時間。存取日期與擷取日期即為你開啟此頁的當日。

本頁內容

第 4 章,共 10 章: 為每位客戶擬合統計模型

  1. Why a statistical model comes next
  2. Fit exponential smoothing
  3. 擬合固定的季節性 ARIMA
  4. Compare models on the same cutoffs
  5. 認識晚開始的客戶
  6. 從零值開始的歷史資料
  7. 讓每條序列從第一筆讀數開始
  8. 為什麼本書在季節長度為 24 時避免使用自動 ARIMA
  9. 正式環境中會出什麼問題
  10. 本章做出的決定

返回頂端所有章節