由 AI 翻譯。經人工以英文驗證。 閱讀原文
教學套件
版次 2026.11.23 為本書目前的版次,會隨本書更新而變動。可自由使用與改編,但須標示本書出處。
課程大綱
閱讀時間僅計算本章的文字。學生也會在 Lab 或自己的電腦上執行範例。
每週一章:10 週
| 週 | 各章 | 閱讀 | 範例 |
|---|---|---|---|
| 1 | 1. 預測明天的需求 | 51 分鐘 | 4 |
| 2 | 2. 設定模型必須超越的基準 | 56 分鐘 | 3 |
| 3 | 3. 不偷看未來的回測 | 71 分鐘 | 8 |
| 4 | 4. 為每位客戶擬合統計模型 | 59 分鐘 | 9 |
| 5 | 5. 加入日曆與落後特徵 | 69 分鐘 | 4 |
| 6 | 6. 以單一模型預測多位客戶 | 67 分鐘 | 5 |
| 7 | 7. 讓總量加總一致 | 36 分鐘 | 3 |
| 8 | 8. 量化預測的不確定性 | 61 分鐘 | 7 |
| 9 | 9. 比較 Prophet 與基礎模型 | 80 分鐘 | 7 |
| 10 | 10. 讓預測持續運作 | 41 分鐘 | 6 |
5 週後
| 週 | 各章 | 閱讀 | 範例 |
|---|---|---|---|
| 1 | 1. 預測明天的需求 2. 設定模型必須超越的基準 | 107 分鐘 | 7 |
| 2 | 3. 不偷看未來的回測 4. 為每位客戶擬合統計模型 | 130 分鐘 | 17 |
| 3 | 5. 加入日曆與落後特徵 6. 以單一模型預測多位客戶 | 136 分鐘 | 9 |
| 4 | 7. 讓總量加總一致 8. 量化預測的不確定性 | 97 分鐘 | 10 |
| 5 | 9. 比較 Prophet 與基礎模型 10. 讓預測持續運作 | 121 分鐘 | 13 |
學習目標與大綱
每章的學習目標即為其第一句。每份大綱依循該章各節,列出該節定義的名詞、執行範例前要求的預測,以及每節結尾所歸納的規則。
1. 預測明天的需求
閱讀約需 51 分鐘,含 4 個範例。
本章建立第一個明日需求預測,以及之後每個預測都必須勝過的簡單基準。
課程大綱:7 節
-
案例與資料
- 實用原則 在做出任何預測之前,先將節選資料與資料卡核對。檔案中應有 30 位客戶,而範例會計算其中有多少位客戶具備完整兩年的逐小時資料列。這些數量應與資料卡相符。並確認
MT_223確實缺少且已有說明。與資料卡不符的節選資料是另一份資料,由它算出的每個數字描述的是那份資料,而不是本書所用的這一份。
- 實用原則 在做出任何預測之前,先將節選資料與資料卡核對。檔案中應有 30 位客戶,而範例會計算其中有多少位客戶具備完整兩年的逐小時資料列。這些數量應與資料卡相符。並確認
-
Load one client's hourly demand
- 執行
load-lead-client.py - 實用原則 Choose the client and the window first, then count the quirks in that window's history before any model sees it. If a history holds a clock-change label or a
before_first_readingrow, do not hand it to a model unchanged, because the model will read the label's value as demand. Chapter 3 and Chapter 4 give the rules for those rows. A window that holds neither can use its rows as they are, as this chapter does.
- 執行
-
以季節性單純法預測隔天
- 執行
seasonal-naive-next-day.py - 實用原則 對於具有日循環的逐時序列,每次比較都要保留季節性單純法。它不需要擬合,因此在每個窗口上執行都沒有額外成本。在相同窗口上無法勝過它的模型,並未證明其額外的工作是值得的。在查看預測的分數之前,先檢查預測中是否有缺漏值,因為最後一個季節中的缺漏讀數,會變成缺漏的預測。第 3 章提供了在任何模型執行之前填補這些小時的規則。
- 執行
-
Forecast the next day with one model
- 執行
autoets-next-day.py - 實用原則 Add a model only with a baseline it has to beat on the same windows. Refit the model at each cutoff, using history from before that cutoff only. A model fitted once on the whole series and then scored on that series has seen the answers, and its score is not a forecast score.
- 執行
-
以保留的一週評分比較兩個模型
- 執行
figure-held-out-week-forecasts.py - 實用原則 評分時,應使用模型在歷史資料中不含被評分小時的情況下所做的預測,涵蓋不只一天,並報告每項評分背後的視窗數與小時數。只在相同的視窗上比較模型。將 MAE 與 RMSE 並列閱讀。若兩者對哪個模型較佳的看法不一致,應先檢視各視窗中最大的偏差再做選擇,因為少數幾次大幅偏差就可能決定結果。
- 執行
-
正式環境中會出什麼問題
- 實用原則 在相信一個分數之前,依序檢查三件事:每個視窗的第一個預測小時就是其截止點;每段歷史都在其截止點之前結束,並包含完整的歷史長度;沒有任何預測缺漏。輔助函式會對前兩項引發錯誤,而範例會印出第三項。只有三項全部通過,分數才能算是一項結論。
- 本章做出的決定
2. 設定模型必須超越的基準
閱讀約需 56 分鐘,含 3 個範例。
在本章中,你會為某位客戶的日前預測計算三種基準:單純法(naive)、季節性單純法(seasonal naive)與週平均法(average of weeks),並以 MASE 來衡量該客戶的誤差,其中所用的尺度只讀取預測週之前的讀數。
課程大綱:7 節
-
為什麼要先建立基準線
- 實用原則 每個基準線都要使用與其所比較的模型相同的視窗、歷史資料與評分程式碼來計分,並且在查看分數之前,先計算每個基準線缺少的預測數量。若模型勝過的是以不同小時計分的基準線,就不能算是勝過了它。
-
Three baselines to beat
- 執行
naive-baselines-held-out-week.py - 執行
figure-baseline-window-errors.py - 實用原則 Start from naive and seasonal naive, both from the library, and add average of weeks when the series has a weekly rhythm worth testing. Check one forecast by hand against its formula before you rely on a baseline: pick one cutoff, compute one hour from the readings in its history, and compare the result with the forecast.
- 執行
-
Scale errors with MASE
- 執行
mase-scale-leak-check.py - 實用原則 Report MASE beside MAE whenever the clients you compare differ in size, and say in the same sentence which history the scale comes from. Read MASE as a ratio of two mean errors, one from the scored hours and one from the history. It is not a percentage gain over seasonal naive, even when the MASE is well below 1.
- 執行
-
以歷史資料取得 MASE 尺度
- 實用原則 只把第一個測試日之前的讀數交給尺度,遮蔽其中時鐘變更的讀數,並印出它所用的最後一筆讀數。檢查這筆讀數是否為第一個測試日的前一個小時。兩個尺度之間差異很小,並不能證明程序正確,資料列檢查才能證明。
-
解讀基準模型的分數
- 實用原則 將每個基準模型的 MASE 與 1 相比,也與其他基準模型相比,不要僅憑一週的結果指定勝者。只有當模型在不止一組時段上擊敗最佳基準模型,且差距大於所印分數的捨入誤差時,才算較佳。
-
正式環境中會出什麼問題
- 實用原則 依下列順序檢查基準模型的輸入及其尺度的輸入:每個方法使用相同的視窗;尺度只讀取較早的讀數,並印出其最後一筆讀數;季節性延遲所用的每小時歷史資料沒有缺口;季節與資料的週期相符。每項檢查都是一行輸出或一個計數,檢查未通過時,比較就應暫停,直到問題修正為止。
- 本章做出的決定
3. 不偷看未來的回測
閱讀約需 71 分鐘,含 8 個範例。
本章教你在一年份的預測截止時間點上,為「隔日預測」評分,同時不讓未來的資料進入其歷史資料;也教你處理資料中兩個因時鐘調整而出現的小時,並執行一項位移測試,以找出讀取了超過截止時間之資料的特徵。
課程大綱:10 節
-
為什麼只用一週測試會造成誤判
- 實用原則 在信任兩個方法之間的差異之前,優先採用一整年的截止點。若成本迫使你使用較少的截止點,請在每個分數旁註明,如第 4 章至第 9 章所做的那樣。確保樣本涵蓋一年中的特殊日子。報告每個數字背後有多少個視窗與多少個計分小時。只來自一週的數字就是一個樣本,讀者應該能看出這一點。
-
Roll the cutoff forward
- 執行
backtest-sundays-2014.py - 執行
figure-rolling-windows.py - 實用原則 Set the history length explicitly, in hours, and check that it is the same for every window. When a library loops over windows for you, read how it sets the training slice before you trust its output, because its default may be an expanding window rather than the length you chose.
- 執行
-
Keep the future out of the history
- 執行
history-cut-check.py - 實用原則 Slice each window's history by its cutoff, never by the start of its test window, and pass a library only the rows it may use. Check the first and last timestamps of every history, and make sure the last one is an hour before the cutoff.
- 執行
- 認識時鐘變更
-
The March hour that reads zero
- 執行
clock-change-census.py - 執行
clock-gap-daily-cutoffs.py - 實用原則 Treat a 01:00 reading of zero on a March clock-change day as a missing hour, not as zero demand. Keep it out of a model's history and out of the scores, and do not let a seasonal method copy it into the next day.
- 執行
-
容納兩小時的十月那一個小時
- 執行
clock-repeat-daily-cutoffs.py - 實用原則 應將十月的 01:00 標籤視為同一個時段內含兩小時的用電量,絕不可當成一般小時。也不要為了讓它看起來像一小時而將其減半,原因見下一節。
- 執行
-
將時鐘變更的時段標記為缺失值
- 執行
clock-masked-backtest.py - 實用原則 在每一份歷史資料與每一次評分中,都將兩個時鐘變更的時段標記為缺失,在報告中記下被遮罩的時段數,只有在模型無法接受缺失值時,才以截止點之前的歷史資料來填補。如果被遮罩的時段數在不同次執行之間有變化,就表示樣本或檔案的旗標已經改變,重新執行評分之前應先查明原因。
- 執行
-
Check a backtest for leakage
- 執行
leakage-shift-check.py - 實用原則 Keep every lag at least the horizon, 24 hours, so that the reading a lag uses for any target hour was known at the cutoff. Before you trust a backtest with features, run both halves of the shift test on every feature, at several cutoffs spread through the year, and require zero differing rows for every feature the model reads. A feature that differs is reading the future, so fix or remove it before you score anything.
- 執行
-
What goes wrong in production
- 實用原則 Run the clock and shift checks again on every data refresh, and before any change to the sample's dates, the history length or the scale. Keep the masked-hour count, the census counts and the shift-test counts in the run log next to the scores, so that a later change in either can be traced to its cause.
- 本章所做的決定
4. 為每位客戶擬合統計模型
閱讀約需 59 分鐘,含 9 個範例。
供應商的每位客戶都需要各自的隔日預測,而第 2 章的季節性樸素法對每一位客戶都給出相同的答案:昨天同一小時的值。
課程大綱:10 節
-
為什麼接下來是統計模型
- 執行
figure-lead-client-fortnight.py - 實用原則 只有在基準和回測都已就緒之後,才為每個客戶各擬合一個模型。在選定視窗數之前,先計算擬合次數:客戶數乘以視窗數,若每天都重新執行模型,還要再乘以重新擬合的次數。比較中的每個模型都使用相同的截止點與歷史資料。否則,比較衡量的是視窗,而不是模型。
- 執行
-
Fit exponential smoothing
- 執行
figure-smoothing-weights.py - 執行
autoets-subset-backtest.py - 執行
flat-form-check.py - 執行
autoets-forced-season-backtest.py - 實用原則 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 withmodel="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
- 執行
figure-seasonal-difference.py - 執行
arima-summary-look.py - 實用原則 當搜尋慢到無法在每個截止點都執行時,就固定階數,並寫在呼叫中。把摘要印出一次,檢查它列出的項是否就是你原本的意圖。在每個截止點都重新配適這個固定的階數,如同重新配適任何模型一樣。階數維持固定,係數則不然。
- 執行
-
在相同的截止點上比較模型
- 執行
arima-subset-backtest.py - 實用原則 以相同的截止點、相同的歷史資料與相同的尺度為每個候選模型計分,並列報告 MAE 與 MASE。兩者不一致時,要說明決策需要的是哪一個。在兩個模型的 MASE 差距小於約 0.1 時,我們建議先增加窗口數,再在兩者之間做選擇。這個門檻是根據此樣本做出的判斷,而不是實測的結果。
- 執行
- 認識晚開始的客戶
- 以零開頭的歷史資料
-
讓每條序列從第一筆讀數開始
- 實用原則 當客戶的歷史資料含有零值時,先檢查它的第一筆非零讀數是否落在歷史資料之內。若是,無論零值占比多少,都要在任何模型看到歷史資料之前套用起始規則,因為開頭的零值並不是用電量。先對整條序列執行
check_start_rule,若檢查失敗,在修剪任何資料之前先找出原因。列印每個截止點的歷史長度,因為歷史資料過短是這條規則取走過多資料的第一個徵兆。
- 實用原則 當客戶的歷史資料含有零值時,先檢查它的第一筆非零讀數是否落在歷史資料之內。若是,無論零值占比多少,都要在任何模型看到歷史資料之前套用起始規則,因為開頭的零值並不是用電量。先對整條序列執行
-
為何本書在季節週期為 24 時避免使用自動 ARIMA
- 實用原則 在每天重新擬合的排程中,不要對季節週期為 24 的每小時資料執行自動搜尋。投入使用前,先對一位客戶的搜尋計時。如果太慢,就固定階數,並在回測中檢驗該階數。
- 正式環境中會出什麼問題
- 本章的決定
5. 加入日曆與落後特徵
閱讀約需 69 分鐘,含 4 個範例。
讀完本章後,你可以把單一客戶的每小時歷史資料轉成特徵表,加入隔日預測允許使用的落後特徵(lag)、日曆特徵與假日特徵,在相同的視窗上衡量每個特徵帶來的增益,並找出讀取了被預測當天資料的落後特徵。
課程大綱:8 節
-
Turn history into features
- 實用原則 Build every feature row as if the cutoff were the last reading you have. A feature for a forecast hour must be computable from readings at or before the cutoff of its window. Before any shift test, ask whether the feature is known at the cutoff. Then check it with the shift test from Chapter 3, and do not train on a table you have not checked.
-
加入預測時看得到的延遲特徵
- 實用原則 只使用至少與預測期間一樣長的延遲特徵。預測期間為 24 小時時,即為 24 小時或更長,本書使用 24 與 168。較短的延遲特徵需要理由,而使用它的模型在任何人信任其分數之前,必須通過位移測試。延遲特徵要遵守時鐘規則,並計算延遲特徵讀到被遮蔽小時的列數,以及從歷史填補的小時數。
-
Add calendar and holiday features
- 執行
figure-holiday-like-sunday.py - 實用原則 Add the calendar features the case's rhythm needs, and keep each one only if it helps on the same windows, as the next section measures. For holidays, use a calendar that covers every year in your data and every year you will forecast, and print how many holidays it holds for those years. A count of zero for a forecast year means the list is missing that year, not that the year had no holidays.
- 執行
-
以決策窗口進行選擇
- 實用原則 應在早於保留週的窗口上選擇特徵,而且這些窗口須包含該特徵所針對的那類日子:假日特徵需要假日窗口。標準截止點與選擇結果並列報告,但絕不用來做出選擇。
-
衡量每個特徵帶來的增益
- 執行
lgbm-feature-sets.py - 實用原則 只有當某特徵集在相同視窗上,相較於它所擴充的特徵集降低了每位客戶平均 MASE,才保留該特徵集。同時報告彙總 MAE。不要為了讓兩種觀點的分歧消失而調整模型。
- 執行
-
Catch a lag that leaks the future
- 執行
lag-leak-check.py - 執行
figure-leaky-lag-forecast.py - 實用原則 Run the shift test on every lag and every window feature before a model trains on it, and treat any differing row as a leak until you have shown otherwise. When an error falls sharply after you add a feature, check that feature first, before you credit it.
- 執行
-
正式環境中會出什麼問題
- 實用原則 每當資料或函式庫變動時,都要重新檢查各預測年度的假日數量、被遮蔽的小時及其影響的資料列,以及落後期數的長度。對每個落後期數執行位移測試,並在彙總 MAE 旁閱讀每位客戶的 MASE。檢查結果若有變動,就應先停下來讀懂原因,再發布下一次預測。
- Decisions this chapter made
6. 以單一模型預測多位客戶
閱讀約需 67 分鐘,含 5 個範例。
讀完本章,你會得到一個涵蓋全部三十位客戶的全域模型、一條能讓晚起步客戶開頭的零值不進入模型的起始規則,以及一個在相同截斷點上、為每位客戶將此模型與季節性單純法(seasonal naive)並列比較的評分。
課程大綱:7 節
-
One model for many clients
- 實用原則 Fit one global model when your series share a shape and you must forecast many of them on each window. Scale each series' target with statistics from that window's history only, use only lags at least as long as the horizon, and fit once per window, not once per series. Then score the model per series against a baseline on the same windows, as the sections below do, before you trust its pooled error.
-
識別每一位客戶
- 執行
client-identifier-check.py - 實用原則 把客戶識別碼視為第 5 章規則下的又一個特徵:只有在它於決策視窗上降低各客戶平均 MASE,並同時參照彙總 MAE 時,才保留它。如果決策很接近,就要明說,並在其他視窗上重新測量。如果識別碼勝出,就要寫下它的代價:新客戶在有歷史資料之前沒有學到的分裂,模型必須重新擬合,才能預測該客戶。
- 執行
-
Apply the start rule to every client
- 執行
start-rule-leak-check.py - 執行
global-model-raw-starts.py - 實用原則 Apply the start rule to every series that has leading zeros, and find each first reading from the rows before the cutoff. When the rule refuses a cutoff, stop the run and name the client and the cutoff. Do not fill the zeros, and do not publish a panel with a client missing. Judge the rule by the scale and by the windows where zeros enter the history, and expect the MAE effect to be small and mixed.
- 執行
-
Score thirty clients on the same cutoffs
- 執行
global-model-thirty-clients.py - 執行
figure-client-mase-global-vs-naive.py - 實用原則 Score a global model per client on the same cutoffs as the baseline, with the MASE scale fixed before the first scored cutoff. Report the mean per-client MASE beside seasonal naive, and print the counts of clients and scored hours so a reader can check them. State any client left out of MASE and why. Read the pooled MAE and RMSE as the cost in kilowatt hours, not as the measure of model quality.
- 執行
-
規劃執行時間
- 實用原則 執行回測之前,先計算擬合次數:逐客戶方法為客戶數乘以視窗數再乘以方法數,全域方法則為視窗數乘以方法數。若次數超出預算,就縮減子集或視窗數量,切勿削減用來評分的視窗,並將縮減的做法寫在分數旁邊。這樣讀者才能在相同條件下進行比較。
-
正式環境中會出什麼問題
- 實用原則 每次更新時,在讀取任何分數之前,先印出並檢查各項計數(客戶數、視窗數、評分小時數、被拒絕的截止點、未納入 MASE 的客戶數)。接著報告各客戶平均 MASE,並與季節性樸素法並列。遇到任何拒絕就中止執行,不要發布由不完整面板所得出的總量。
- 本章做出的決定
7. 讓總量加總一致
閱讀約需 36 分鐘,含 3 個範例。
你將以兩種方式預測三十位客戶的總需求量,在決策視窗上於兩者之間做出選擇,並且只使用各截止時點之前的歷史資料,將該總量拆分為各客戶的占比。
課程大綱:7 節
-
為何總量需要檢查
- 實用原則 只要有決策依賴總量,就以兩種方式預測總量。在相同的時間窗上,針對總量與各客戶為兩者評分。保留在決策時間窗上表現較佳的方法,絕不因為各部分相加一致就接受某個總量。
-
加總各客戶的預測
- 執行
total-bottom-up-and-direct.py - 實用原則 先確認總量序列在每個沒有時鐘變更旗標的小時都等於其讀數的總和,再逐小時加總各客戶的預測。在加總之前對每個客戶套用起始規則,使任何客戶的模型都不會以其零值擬合。將加總視為點預測,不要把各客戶中位數的總和當作總量的中位數。
- 執行
-
直接預測總量
- 實用原則 以各客戶所用的特徵,並依每個截止點之前總量自身的歷史來直接預測總量,再於決策窗口上選定目標的縮放方式。兩種方法使用相同的窗口,並各自與總量上的季節性單純預測比較評分。兩種方法都要報告,不要只報告自己建構的那一種。
-
比較兩種總量
- 實用原則 依你將要呈報的度量,在決策窗口上選擇方法。在標準截止點上呈報同一度量,並與季節性朴素法並列,同時附上受評分的小時數與尺度。當兩組窗口意見不一時,兩者都要呈報,說明相差多少,不要挑選讀起來較好看的那一組。
-
依占比拆分總量
- 執行
figure-client-daily-profiles.py - 執行
total-split-by-share.py - 實用原則 只在作為基準線,或各客戶的日曲線相近時,才依占比拆分總量。每個占比都由其截止點之前的歷史計算,並檢查截止點之後的讀數改變時沒有任何占比移動,再以相同的尺度將每個客戶與由下而上比較評分。只有當拆分在決策視窗上勝出時,才對該客戶保留這種拆分。
- 執行
-
正式環境中會出什麼問題
- 實用原則 在正式環境中,每次重新配適時都要將總量與其讀數的加總核對,當某個客戶從總量中缺漏時就停止,並對每個占比以及每個視窗的尺度執行中毒檢查。在相同視窗上記錄總量在有識別項與沒有識別項時的誤差。當各視窗的結果不一致時,兩者都要報告,不要選擇對模型有利的那一個。
- 本章的決定
8. 量化預測的不確定性
閱讀約需 61 分鐘,含 7 個範例。
本章會為每一個隔日預測加上一個範圍,讓你能判斷採購是否可以倚賴該預測。
課程大綱:7 節
-
為什麼一個數字還不夠
- 實用原則 凡是兩種誤差的成本不同,就要求給出範圍,並依這些成本來選擇其水準,而不是依某個看起來令人安心的數字。涵蓋率應報告為多個視窗中落在區間帶內的小時所占比例,絕不可說成某一天落在區間內的機率。
-
以殘差建立區間
- 執行
conformal-three-windows.py - 實用原則 以早於截止點的窗口之殘差建立每個區間帶,並在信任其涵蓋率之前,先檢查它在未用於校準的窗口上達到的涵蓋率。先看設計所能達到的水準,再看涵蓋率。每個客戶與小時只有三個殘差,無法達到任一標示水準,所以解法是更大的校準集,而不是換一種方式解讀輸出。
- 執行
-
以較早的視窗進行校準
- 執行
conformal-decision-windows.py - 執行
figure-client-coverage-scaled.py - 執行
conformal-earlier-windows.py - 實用原則 以早於受評分視窗、且不含假日的視窗進行校準,並在合併各客戶的殘差之前先對其縮放。每一項設計選擇都要在不是你所報告的視窗上做出。請報告用於區間帶的視窗數與殘差數,以及它們在受評分日上給出的涵蓋率,即使該涵蓋率未達標示水準也一樣。
- 執行
-
Measure coverage on held-out days
- 執行
total-band-miss-runs.py - 執行
figure-total-band-misses.py - 實用原則 Report coverage on windows that the calibration never used, with the number of windows and the coverage of each. Keep the same measure in a running log once the forecast runs every day, as Chapter 10 does. Read three scored days as a warning, not as a measurement of the level you were promised.
- 執行
-
Choose the level from the cost of error
- 執行
buy-ahead-level-by-cost.py - 實用原則 Set the buy level from the two costs, as
tau = cu / (cu + co), fix it before you score the days, and calibrate the total's bands on the total's own residuals. Show the realised cost at other levels as a check, but never move the level to whichever one would have won on the days you score.
- 執行
- 正式環境中會出什麼問題
- 本章所做的決定
9. 比較 Prophet 與基礎模型
閱讀約需 80 分鐘,含 7 個範例。
讀完本章後,你將能在本書的三個標準截止點上擬合 Prophet,載入一個已記錄的基礎模型(Chronos-2)與一個已記錄的神經網路模型(NHITS),並以同一個 MASE 尺度,對照相同的讀數為它們全部評分。
課程大綱:7 節
-
Prophet 的適用之處
- 實用原則 當讀者需要把 Prophet 的趨勢與季節性設定當作可閱讀的參數,而且其評分預測在你於評分前選定的決策窗口上勝過季節性樸素法時,才對每位客戶各別使用 Prophet。請在程式碼中設定它的區間寬度,並把它預設的 80% 區間視為 80% 區間,絕不當成 95% 的範圍。請對照它所宣稱的水準來檢查其涵蓋率。
-
Run Prophet on the same cutoffs
- 執行
prophet-holiday-decision.py - 執行
prophet-thirty-clients-backtest.py - 實用原則 Set Prophet's seasonality, holidays and interval width in code before you score it, and choose any of them on decision windows, never on the standard cutoffs. Keep the holiday table only if it lowers the MASE on the holiday decision windows. Compare Prophet with seasonal naive and the global model on the same windows and the same scale, and list the clients it loses. Set the seed before each predict, and check that a second run prints the same lines.
- 執行
-
Load a recorded foundation model's forecasts
- 執行
recorded-chronos2-check.py - 實用原則 Record a foundation model only with its script, its pinned revision, its package versions and a manifest that hashes every output. Read the recorded outputs in every example, and check the hashes before any score reads them. Check the licence of the weights, not only the licence of the package, and name the model version in any licence sentence.
- 執行
-
Score the recordings on the same cutoffs
- 執行
recorded-chronos2-score.py - 執行
recorded-nhits-score.py - 執行
figure-chronos-ranges.py - 實用原則 Score a recorded forecast on the same windows, the same scale and the same readings as every other model, and name any interpolated quantile wherever its range is reported. Before you read a lead, check what the model was trained on, because a model that saw the scored readings cannot show skill on them. Do not choose a model or a setting on these windows. A recorded forecast with no forecasts on the decision windows cannot be chosen for production until it has them.
- 執行
-
為何神經模型採用錄製方式
- 執行
recorded-nhits-check.py - 實用原則 當某個模型的相依套件會超出範例執行環境的預算時,就不要把它放進範例的執行環境,改以錄製方式處理,並將其鎖定版本寫在獨立的需求檔中。加入相依套件之前,先在乾淨的環境中安裝,並量測它在大小與套件數量上增加了多少。若相依解析會使某個已鎖定的套件變動,這是改為錄製該模型的理由,而不是更動鎖定版本的理由。
- 執行
-
What goes wrong in production
- 實用原則 Before any Prophet or recorded forecast is reported, check that the holiday table covers the forecast's years when the table is on, that the interval level is printed beside its coverage, that the recording has a window for every cutoff in use, that its quantiles are in order, and that the model revision matches the one scored. A failed check stops the report, and the report says which check failed.
- 本章做出的決定
10. 讓預測持續運作
閱讀約需 41 分鐘,含 6 個範例。
預測一旦每天執行,問題就從「分數好不好」變成「到了使用當天是否仍然可靠」。
課程大綱:7 節
-
依排程重新擬合
- 執行
refit-cadence-decision.py - 實用原則 在保留週之前的一般日子上選擇重新擬合頻率,並以在這些日子的第一天之前求得的同一個尺度評分。選擇平均 MASE 較低的頻率;當兩者接近時,選擇重新擬合次數較少的一種,並明確說明。絕不使用你正在預測當天的讀數進行重新擬合。
- 執行
-
在實際值到達之前記錄每一筆預測
- 執行
rolling-refit-seven-days.py - 實用原則 在每筆預測的截止時間,依據當時已到達的讀數寫入它的各列,並在旁邊附上重新擬合日與兩個到達時間。絕不以較新的模型或實際值重建過去的記錄。若某一天的列遺失,就在記錄中將該日標示為缺漏,不要事後補填。
- 執行
-
隨實際值到達而為記錄評分
- 執行
late-fill-decision.py - 實用原則 在每一天的最後一筆讀數到達時,依該日截止時間所記錄的預測,以本章唯一的尺度為當日評分。讀數延遲到達時,在到達時評分,並在記錄中計數,不更動已記錄的預測。截止時間之後,絕不更改預測,即使更改能讓分數變好也一樣。
- 執行
-
對照回測標記漂移
- 執行
drift-flag-rolling.py - 實用原則 當某一天的每日 MASE 高於先前校準視窗中位數的兩倍時,標記該日,然後詢問是否有日曆上的原因可以解釋。若有原因,請把原因記錄在標記旁。連續兩個無法解釋的標記,應視為汰除檢查的起點,絕不能當作模型已壞的證明。
- 執行
-
判斷何時該淘汰模型
- 執行
drift-after-a-change.py - 執行
figure-drift-flags.py - 實用原則 當旗標連續數日出現且沒有行事曆上的原因,或 80% 區間在數日內涵蓋的讀數遠低於 80% 時,應提前重新擬合,並將觸發條件連同記錄一併留存。若提前重新擬合未能消除旗標,則淘汰該模型。當固定版本的函式庫發布新版本時,重新執行本章的檢查,並保留原有的版本固定,直到檢查在新版本上通過為止。
- 執行
-
正式環境中會出什麼問題
- 實用原則 每次執行都要保留三項檢查:使未來資料不進入每次重新擬合與預測的歷史規則、日誌每一列上的兩項到達檢查,以及第 3 章與第 7 章的洩漏檢查。其中任何一項失敗時,請停止迴圈,並在下一次預測之前修正原因。不要以手動方式修補歷史或日誌,因為手動修補正是這些檢查要攔截的那類變更。
- 本章做出的決定
測驗題庫
每章皆附習題與解答,供經驗證的教育工作者使用。每個解答都對應其所屬的節。 如何取得
正在載入測驗題庫…