设定模型必须超越的基线
阅读约需 66分钟 分钟,7节中含2个示例。
本章为某客户的次日预测计算三种基线,即朴素预测、季节性朴素预测和周平均预测,并用 MASE 衡量该客户的误差,所用的尺度只读取预测周之前的读数。
第 1 章以留出一周上的两个得分作结,但这两个得分目前还无法评判,因为以 kWh 为单位的误差本身没有意义。供电方的采购取决于每个小时的预测,因此它必须把每个模型的误差,与最简单的预测在同样这些小时上的得分相比较,也要与该客户的需求从一天到下一天的变动幅度相比较。本章构建这两种比较。供电方在断言某个模型物有所值之前,需要先有这两种比较。
每个误差都是某一个小时以千瓦时计的偏差,这里的得分是这些偏差的小时平均值。基线的得分是同类数字,它表明最简单的规则在一天中平均每个小时留下多少千瓦时的误差,而无法超过这一数值的模型,并没有证明其额外成本是值得的。因此,每个得分除了与其他各行相比之外,还要与主客户的平均需求对照来读。
这部分尚未翻译,因此以英文显示。
Why a baseline comes first
A baseline is a forecast that needs no fitting. It copies a value the history already holds, so it costs almost nothing to run, and it sets a bar that any model has to clear. A model earns its place only by beating a forecast that cost nothing. Fitting time, tuning and keeping a model running are all costs, and the baseline's score measures what those costs buy.
Three terms matter here. The cutoff is the hour at which a forecast is made. The cutoff is the first hour the forecast covers, and the history is every reading before the cutoff. The horizon is how far ahead a forecast reaches, here 24 hours. A window is one cutoff together with the horizon it covers, and a backtest is the score of many windows, each made as it would have been made on its day.
The comparison has to be fair, or it measures the setup and not the model. A baseline scored on different hours, with a different history or a different scoring routine, says nothing about the model beside it. So every method in this chapter takes its cutoffs from held_out_cutoffs(), the seven daily cutoffs at 00:00 of the held-out week, reads its history through history_before(), which takes the eight weeks of hours before a cutoff, and is scored by the same code. These are the seven windows Chapter 1 used.
Working rule: Score every baseline on the same windows, histories and scoring code as the model it will be compared with, and count each baseline's missing forecasts before you read its score. A model that beats a baseline scored on different hours has not beaten it.
这部分尚未翻译,因此以英文显示。
Three baselines to beat
Each baseline copies the history in a different way. Naive carries the last hour it has seen forward across the whole day. Seasonal naive copies the day before, hour for hour. Average of weeks takes the mean of that hour on that weekday over the four weeks before.
Every rule covers the 24 hours after a cutoff. Call the cutoff T, the hour at which the forecast is made, and let k run from 0 to 23 across the hours the forecast covers. The three rules are these:
- Naive repeats the last reading before the cutoff:
forecast(T + k) = y(T - 1). - Seasonal naive repeats the reading one day earlier:
forecast(T + k) = y(T + k - 24). - Average of weeks takes the mean of the same hour on the same weekday over the four weeks before:
forecast(T + k) = (y(T + k - 168) + y(T + k - 336) + y(T + k - 504) + y(T + k - 672)) / 4.
The lags are in hours, so 168 is one week. Every reading a rule uses comes from before the cutoff: naive's is the hour before it, seasonal naive's are the 24 hours before it, and average of weeks reaches back one to four weeks. Take the window at 00:00 on Dec 25, 2014 as a worked case. Naive gives every hour of Dec 25 the reading at 23:00 on Dec 24, so the forecast is flat. Seasonal naive gives 00:00 on Dec 25 the reading at 00:00 on Dec 24, and so copies the whole previous day's shape. Average of weeks gives 00:00 on Dec 25 the mean of the 00:00 readings on Dec 18, Dec 11, Dec 4 and Nov 27. That mean smooths out one unusual day, but it also averages in any level change across the four weeks.
Each rule fails in a known way. Naive gives every hour of the day the same value, so it cannot follow the daily peak at all. Seasonal naive copies one day, so an unusual day is copied into the forecast for the next day, as the per-day errors later in this section show on the held-out week. Average of weeks needs four clean weeks, and a holiday in any of them enters the mean with the same weight as an ordinary day. A baseline's score therefore describes its rule on the windows it was run on, and these failures show up only on the windows where they happen.
Naive and seasonal naive come from statsforecast (2.1.1), whose classes Naive and SeasonalNaive are in statsforecast/models.py. The seasonal naive rule is the one Hyndman and Athanasopoulos give for seasonal data: the forecast is the last observed value from the same season. Average of weeks is not a library method, so examples/lib/baselines.py computes it in pandas, where the rule can be read, and each window reads only its own history. A lag missing from that history raises an error instead of being skipped. One script scores all three rules through those seven windows, and its first lines count the windows, scored hours and missing forecasts, so you can check them before you read a score.
1"""Score the three baselines for the lead client over the held-out week, on the same seven windows. The point rests on the missing-forecasts line, which must be zero for the scores to compare, and on the three MAE lines."""2import pandas as pd34from lib.backtest import held_out_cutoffs, score_windows, window_mae5from lib.baselines import BASELINES, baseline_forecasts6from lib.electricity import load7from lib.settings import LEAD, show89cutoffs = held_out_cutoffs() # seven daily cutoffs at 00:00; every baseline uses these same windows10df = load([LEAD])[["unique_id", "ds", "y"]] # unique_id, ds and y only: the baselines read no other column11forecasts = baseline_forecasts(df, cutoffs) # one row per forecast hour, one column per baseline; each window reads its own history12labels = ["naive", "seasonal naive", "average of weeks"] # printed names, in the order of BASELINES, which the loops below zip with1314show("windows", len(cutoffs))15show("scored hours", len(forecasts))16show("missing forecasts", int(forecasts[BASELINES].isna().sum().sum())) # NaN marks a forecast that could not be made; the two sums count them over every column17show("mean demand", df[df["ds"].isin(forecasts["ds"])]["y"].mean()) # the mean over the scored hours only, not over the whole history18for model, label in zip(BASELINES, labels):19 show(f"{label} MAE", score_windows(df, forecasts, model)["mae"]) # pooled over every scored hour; the result is keyed by metric name20for model, label in zip(BASELINES, labels):21 per_window = window_mae(df, forecasts, model) # one MAE per window, keyed by the day its forecast hours fall on22 show(f"{label} worst window MAE", per_window.max())23 show(f"{label} worst window", per_window.idxmax().strftime("%b %d, %Y"))24 for day in ["2014-12-25", "2014-12-26"]: # Dec 25 is the holiday and Dec 26 the day after: the two windows the prose reads25 show(f"{label} MAE {pd.Timestamp(day):%b %d} window", per_window[pd.Timestamp(day)]) # windows are keyed by midnight of their day, so a Timestamp at 00:00 finds one
python naive-baselines-held-out-week.pywindows: 7
scored hours: 168
missing forecasts: 0
mean demand: 46.62
naive MAE: 8.16
seasonal naive MAE: 8.36
average of weeks MAE: 8.96
naive worst window MAE: 11.01
naive worst window: Dec 30, 2014
naive MAE Dec 25 window: 5.84
naive MAE Dec 26 window: 10.36
seasonal naive worst window MAE: 12.16
seasonal naive worst window: Dec 26, 2014
seasonal naive MAE Dec 25 window: 9.41
seasonal naive MAE Dec 26 window: 12.16
average of weeks worst window MAE: 22.09
average of weeks worst window: Dec 25, 2014
average of weeks MAE Dec 25 window: 22.09
average of weeks MAE Dec 26 window: 12.33
The three scores are comparable only because they cover the same hours: 168 scored hours in 7 windows, one for each hour of the seven days, and 0 missing forecasts across the three rules. That count is the one the working rule in the first section asks for, and zero is the only value that lets the three be compared on the same hours.
Read each error below against the lead client's mean demand of 46.62 kWh. Each score below is a MAE, the mean absolute miss in kWh that Chapter 1 defines. The three baselines score:
- naive: 8.16 kWh
- seasonal naive: 8.36 kWh
- average of weeks: 8.96 kWh
The simplest rule has the lowest error on this week, and average of weeks, which averages four weeks, is the weakest. Nothing here explains that order, and this is one client and one week, so it is a result for this week only. Chapter 3 is where the book backtests seasonal naive across many cutoffs.
The totals hide where the errors sit. The week begins on Dec 25, 2014, a Portuguese national holiday, and its first window is where the rules part company. Naive has the lowest error on it, at 5.84 kWh, against 9.41 kWh for seasonal naive. Average of weeks is worse still, and its largest error of the week falls on that window, at 22.09 kWh, because its four lags are ordinary days and the holiday has no match among them.
The day after the holiday is where copying costs seasonal naive. Its largest error of the week falls on Dec 26, 2014, at 12.16 kWh, because that window copies the holiday's pattern. On that day naive is still the lowest, at 10.36 kWh.
We recommend reporting all three baselines every time. Use seasonal naive as the main reference for MASE, because it carries the daily cycle that makes hourly demand predictable, and keep naive beside it, because on some weeks the flat rule is the stronger one.
Working rule: 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
A kilowatt-hour error means different things for different clients. A miss of 8.36 kWh an hour is small for a client that uses 46.62 kWh an hour, and the same miss would be large for a hypothetical client that uses 4 kWh an hour. Scaling the error removes that difference. A scaled error divides each absolute error by a scale, the typical error of a simple method on the same client's own history. MASE, the mean absolute scaled error, is the mean of the scaled errors. Hyndman and Koehler introduced it in a 2006 paper in the International Journal of Forecasting. The link is the authors' preprint, dated 2005. They noted that the scaled error does not depend on the units of the data, so the same number can be read across clients of very different size. This chapter scores one client, so that comparison across clients is the reason for the scale, not a result shown here.
The 2006 paper scales by the one-step naive error: the mean absolute difference between each reading and the one before it. That is the natural scale for non-seasonal series. For hourly demand, this book uses the seasonal form that Hyndman and Athanasopoulos give for seasonal data, because the comparison a reader wants is with seasonal naive: under the seasonal scale, the denominator is seasonal naive's error on the client's own history. A MASE below 1 then says the forecast's error on the scored hours is smaller than seasonal naive's error on that history. A one-step scale would compare it with a method this book does not use for hourly demand.
The two formulas are these:
scale = mean of |y(t) - y(t - 24)|over every reading t before the first test day, with the pairs that touch a masked clock-change hour left outMASE = mean of |actual(t) - forecast(t)| / scaleover the scored hours
The scale reads every reading before the first test day, 17352 hours, about two years. That is longer than each window's eight-week history, and a longer history gives a steadier estimate of the client's usual swing. One scale for the week also means every window is read against the same number. The function seasonal_scale in examples/lib/baselines.py computes it for one client or for many, and returns one value per client. To see whether the cut matters, score the three baselines on the held-out week twice: with the scale from history, and with the scale from the whole series, both with the clock-change readings masked.
1"""Score the lead client's held-out week in MASE with a scale from history and with one from the whole series, both with the clock-change readings masked. The point rests on the two last-reading lines, which show where each scale stops, and on the MASE ratio lines."""2import pandas as pd34from lib.backtest import held_out_cutoffs5from lib.baselines import BASELINES, baseline_forecasts, mase_scores, scale_history, seasonal_scale6from lib.electricity import load7from lib.quirks import mask_clock_changes8from lib.settings import HELD_OUT_WEEK, LEAD, show910LABELS = ["naive", "seasonal naive", "average of weeks"] # printed names, in the order of BASELINES11first_day = pd.Timestamp(HELD_OUT_WEEK[0]) # the first test day: the history scale may read only readings before it1213full = load([LEAD]) # every reading of the lead client, with the quirk column that marks clock-change hours14df = full[["unique_id", "ds", "y"]] # the three columns the baselines and mase_scores read; quirk is for masking only15forecasts = baseline_forecasts(df, held_out_cutoffs()) # the same windows and history rule as naive-baselines-held-out-week.py16history = scale_history(full, first_day) # keeps only rows strictly before the first test day, so no test hour reaches this scale17masked_history = mask_clock_changes(history) # clock-change hours set to NaN, so the pairs that touch them drop out of the mean18masked_whole = mask_clock_changes(full) # the same masking on the whole series, so the test week is the only difference19test_rows = len(full[full["ds"] >= first_day]) # readings from the first test day on: the test week's rows2021show("scale rows from history", len(history))22show("last scale reading from history", history["ds"].max().strftime("%b %d, %Y %H:%M")) # this must be the hour before the first test day; the line is the check23show("scale rows from whole series", len(full))24show("last reading of whole series", full["ds"].max().strftime("%b %d, %Y %H:%M"))25show("test week share of whole-series rows in percent", 100 * test_rows / len(full), digits=3)26show("clock-change rows in scale history", int(history["quirk"].ne("normal").sum())) # every quirk but normal; for the lead client these are the clock-change hours, as it has no late start27show("test week own mean day-to-day difference", seasonal_scale(full[full["ds"] >= first_day - pd.Timedelta(hours=24)]).loc[LEAD], digits=4) # starts 24 hours early, so its first pair is the first test day against the day before28show("scale from history as recorded", seasonal_scale(history).loc[LEAD], digits=4) # seasonal_scale returns one value per client, so .loc picks the lead client; the clock-change pairs are kept here29show("scale from history clock changes masked", seasonal_scale(masked_history).loc[LEAD], digits=4) # pairs with a NaN are left out of the mean30show("scale from whole series clock changes masked", seasonal_scale(masked_whole).loc[LEAD], digits=4)3132hist = mase_scores(df, forecasts, BASELINES, masked_history).loc[LEAD] # the scale comes from the rows passed as train, not from df; that choice is what this example tests33whole = mase_scores(df, forecasts, BASELINES, masked_whole).loc[LEAD]34for model, label in zip(BASELINES, LABELS):35 show(f"{label} MASE from history scale", hist[model])36 show(f"{label} MASE from whole-series scale", whole[model])37 show(f"{label} MASE ratio whole-series over history", whole[model] / hist[model], digits=4) # both MASEs share the same errors, so this ratio is the history scale over the whole-series scale38show("seasonal naive MASE from history scale at four decimals", hist["SeasonalNaive"], digits=4) # two decimals hide the leak, so the last two lines print four39show("seasonal naive MASE from whole-series scale at four decimals", whole["SeasonalNaive"], digits=4)
python mase-scale-leak-check.pyscale rows from history: 17352
last scale reading from history: Dec 24, 2014 23:00
scale rows from whole series: 17520
last reading of whole series: Dec 31, 2014 23:00
test week share of whole-series rows in percent: 0.959
clock-change rows in scale history: 4
test week own mean day-to-day difference: 8.3607
scale from history as recorded: 7.1121
scale from history clock changes masked: 7.1005
scale from whole series clock changes masked: 7.1126
naive MASE from history scale: 1.15
naive MASE from whole-series scale: 1.15
naive MASE ratio whole-series over history: 0.9983
seasonal naive MASE from history scale: 1.18
seasonal naive MASE from whole-series scale: 1.18
seasonal naive MASE ratio whole-series over history: 0.9983
average of weeks MASE from history scale: 1.26
average of weeks MASE from whole-series scale: 1.26
average of weeks MASE ratio whole-series over history: 0.9983
seasonal naive MASE from history scale at four decimals: 1.1775
seasonal naive MASE from whole-series scale at four decimals: 1.1755
The scale from history is 7.1005 kWh, the average gap between each hour and the same hour the day before, and it reads against the mean demand the way an MAE does. A scale that is a large share of the mean says the client's own day-to-day swing is large, and every error for that client is read against that swing. Dividing seasonal naive's MAE of 8.36 kWh by the scale gives its MASE, 1.18. utilsforecast's mase, reached through the helper mase_scores, computes this MASE by dividing the same MAE by the same seasonal scale (utilsforecast 0.2.17, utilsforecast/losses.py, function mase), so the division by hand gives the same figure.
utilsforecast (0.2.17) computes MASE with its mase function in utilsforecast/losses.py. The function takes its scale from the train_df it is given, and it shifts that frame by rows, not by time, so a missing hour moves every later lag and nothing reports it. It also needs sorted input. Its time filter runs only when the scored frame has a cutoff column (function _create_train_with_cutoffs). So the library does not decide which readings the scale may read. The caller does, and the next section is about that choice.
Working rule: 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.
这部分尚未翻译,因此以英文显示。
Take the MASE scale from history
A scale describes the history before the first test day, not the series as a whole. If the scale reads the whole series, it also reads the hours the forecast is scored on, so the held-out week enters the denominator of its own error. That is a leak, and it raises no error. The MASE it produces looks like any other. The rule in this book is that a scale reads only the readings before the first test day of the week it scores, which for the held-out week is Dec 25, 2014.
A whole-series scale looks like the safer choice, because it uses more readings. It uses more readings from the wrong period. The extra readings are the ones the forecast is judged on, so the scale is no longer a measure of what was known at the cutoff. More data does not fix that. Only the cut does.
The script in the previous section prints the rows each scale reads, and those lines are the check. The history scale reads 17352 readings, and its last reading is Dec 24, 2014 23:00, the hour before the first test day. A later date there would mean test hours reached the scale.
The whole-series scale reads 17520 readings, through Dec 31, 2014 23:00, which covers the whole held-out week. Both scales have their clock-change readings masked, so the held-out week is the only difference between them.
The whole-series scale is the larger one, so every MASE computed with it is smaller. At two decimals both scales give seasonal naive 1.18, so the leak does not show. At four decimals it does, with seasonal naive's MASE at 1.1775 against 1.1755, and the factor below is the lesson, not the rounding.
The scales are 7.1005 kWh from history and 7.1126 kWh from the whole series, and the same factor, 0.9983, shrinks each of the three rules.
The leak is small on this week because the held-out week is a small share of the series. It makes up 0.959 percent of the whole-series readings, so the whole-series scale is roughly the history scale moved toward the held-out week's own swing by that share. The held-out week's own mean day-to-day difference is 8.3607 kWh, against 7.1005 kWh for the history, so the week swings more than the history does. The share times that gap is the leak, and the ratio above is its size. The leak grows in two ways. It grows when more of the scale's rows are test hours, as in a test period much longer than one week, such as the year of cutoffs in Chapter 3. It also grows when the held-out week's swing sits further from the history's. A quieter week would pull the whole-series scale down and make every MASE larger, so the same leak can flatter a model in one week and penalise it in another.
The clock changes are a second, smaller point, and this chapter handles them as Chapter 3 does. The history holds 4 clock-change readings: the two gap hours and the two repeat hours of the March and October changes in 2013 and 2014. The gap hours read zero, and the repeat hours hold the energy of two, as the data card describes; Chapter 3 shows the effect. Kept as recorded, those pairs give a scale of 7.1121 kWh. Masked, the scale is 7.1005 kWh, because the pairs that touch a masked reading drop out of the mean. A masked reading is neither zero nor a copy; it is missing. The chapter's scale masks them, and Chapter 3 gives the reason, which sets them to missing in every training set and every score.
Working rule: Pass a scale only the readings before the first test day, mask its clock-change readings, and print the last reading it uses. Check that this reading is the hour before the first test day. A small difference between the two scales does not show the procedure is right. The row check does.
解读基线的得分
MASE 把一个预测的误差与季节性朴素法在客户自身历史数据上的误差作比较。低于 1,表示该预测在被评分的那些小时上的误差小于季节性朴素法在历史数据上的误差;高于 1,则表示更大。Hyndman 和 Koehler在论文中针对一步尺度给出了这种解读,而对季节尺度,算法是相同的。本书不给出好成绩的阈值。什么算好,取决于一次预测失误会让供电商付出多少代价,这一点供电商清楚,本书并不知道。
下表把三种基线并排列出,每种一行。MAE 的单位为 kWh,MASE 使用屏蔽了时钟调整的历史尺度。上一节中的泄漏在保留两位小数时看不出来,所以表中只有一列 MASE。
| 基线 | MAE(kWh) | MASE |
|---|---|---|
| 朴素法 | 8.16 | 1.15 |
| 季节性朴素法 | 8.36 | 1.18 |
| 周平均法 | 8.96 | 1.26 |
三个 MASE 值都高于 1,所以每种基线的误差都超过了季节性朴素法在历史数据上的典型误差。就季节性朴素法本身而言,这一周的日间波动大于历史数据的日间波动。它的 MAE 等于留出周自身的日间差值 8.3607 kWh,而历史数据的对应值为 7.1005 kWh。对于朴素法和周平均法,这个尺度是把它们与季节性朴素法相比,而不是与它们自己的过去相比,因此本表并不能说明这两种规则在历史数据上表现如何。这一周以一个法定假日开始,而前面的一节显示,假日窗口是周平均法误差最大的部分。本书在此不检验其余各天。
两种度量下的顺序相同:朴素法第一,季节性朴素法第二,周平均法第三。这种一致并不是一项发现。MASE 用同一个尺度去除每个 MAE,所以对同一个客户而言,MAE 与 MASE 的顺序不可能不同。
这张表也引出了供电商的问题:最简单的规则是否已经好到可以据以购电?这取决于一小时的误差造成多少成本,而 MASE 并不给它定价。MASE 给供电商的是一种可以重复进行的比较。之后的模型可以用同样的辅助函数评分,因此它的 MASE 可以直接与这些行并列,不必换算单位。
阅读这张表可按以下顺序。第一,根据前面的一节确认每一行评分的是同样的小时,没有缺失的预测。第二,对照 1 来读 MASE 一列。第三,把每一行与其他行比较,而不是与教科书中的某个数字比较。一个拟合模型若只和最好的一行相当,就没有证明什么;若比它好的幅度小于打印结果的舍入误差,则什么也没有证明。
工作准则: 把每种基线的 MASE 既与 1 比较,也与其他基线比较,不要凭一周的数据宣布胜者。只有当模型在不止一组窗口上胜过最好的基线,并且优势大于打印得分的舍入误差时,才算它更好。
生产环境中会出什么问题
如果在其他时段上,或用另一段历史来给基线打分,这种比较就不再表达它所声称的含义。一个团队可能用一整年的截断点给模型打分,却只用一周给基线打分,或者给模型的历史比给基线的更长。此时比较衡量的是设置本身,而打印出来的数字看上去却像是证据。检查方法就是本章所用的做法:每种方法的截断点都取自 held_out_cutoffs,历史都取自 history_before,并且每次运行都打印缺失预测的数量。
尺度中的泄漏会留下一个与其他 MASE 看起来无异的 MASE,也不会引发任何错误。在本章中,它使每个季节性朴素 MASE 都乘以 0.9983,这一变化被两位小数掩盖了。换到另一周发生更大的泄漏,在纸面上看起来也会一样。检查办法是打印出的行数,以及尺度所用的最后一个读数,二者位于同一行输出中,评审者无需重新运行任何东西即可读到。
季节滞后中出现缺口,会给出一个错误的尺度,而且没有任何输出标示这一点。缺少一个小时时,24 行的滞后会回溯到 25 小时之前,因此次日的每一对数据比较的都是错误的小时。库中的 mase 自身没有缺口检查。辅助函数 seasonal_scale 会检查每个客户的小时序列是否连续,mase_scores 则在库读取尺度所用的行之前,对这些行执行同样的检查。该检查通过抛出错误而不是使用断言来实现,因为 Python 在 -O 下会移除断言。被评分的小时只检查是否有读数以及是否重复。生产运行应当在计算任何尺度之前先统计数据源中的小时数,因为辅助函数内部的检查是在问题已经进入尺度之后才报告它。
季节滞后也可能为错误的周期而选。24 的滞后适用于具有日周期的逐小时需求。日数据具有周周期,而带有周节律的逐小时数据可能需要 168 的滞后。用错误的季节计算出的尺度仍然是一个数字,因此输出中没有任何东西显示这一错误。检查办法是在正文中于尺度旁写明所用的季节,并在使用该尺度之前对照数据的周期加以确认。
在留出的一周上挑选某项设置,会破坏比较。周平均法中有一个选择:平均多少周。如果这个数字是因为它在留出的一周上得分最好而被选中,那么基线就是在评判它的那一周上拟合出来的,它与各模型的比较也就不再公平。本书在打印任何得分之前就把它定为四周,本章对每个窗口都沿用这一设定。对它的任何改动都应列入本章末尾的决策清单,并在看到得分之前作出。
工作准则: 按以下顺序检查基线的输入及其尺度的输入:每种方法使用相同的窗口;尺度只读取更早的读数,并打印其最后一个读数;季节滞后所用的逐小时历史没有缺口;季节与数据的周期相符。每项检查都是一行输出或一个计数,检查未通过时,比较即告暂停,直到问题修复。
本章的决定
本章做了七项选择,最后一项是与第 3 章的衔接。其中多数位于 examples/lib/baselines.py 这一基线辅助函数模块中,因此在那里修正一处,就会影响到所有调用它们的章。历史长度和预测时域是 examples/lib/settings.py 中的设置,与第 1 章使用的相同,所以这里的任何基线读取的历史,都不会与旁边的预测不同。
- MASE 的尺度取季节性尺度,滞后为 24 小时。不使用 2006 年那篇论文中的一步尺度。
- 尺度对每个客户只取一个数,针对留出的那一周,取自 2014 年 12 月 25 日之前的全部读数。如上文所述,它比每个窗口的历史更长。当数据框带有截断点列时,utilsforecast 可以按每个截断点分别取尺度,但本章不使用这一选项。如果你要加上它,请注意它的过滤条件保留的是
ds <= cutoff,因此截断点那一小时本身也被包含在内。本书中的截断点是预测所覆盖的第一个小时,该小时的读数不属于历史。所以,用本书的截断点构造截断点列时,必须先把它往前移一小时。 - 周平均使用每个预测日之前的四周,在 pandas 中计算,并且只读取它自己的八周历史。
- 朴素法和季节性朴素法使用 statsforecast 中的类。得分在这一周所有参与评分的小时上汇总,并保留两位小数输出。
- 按照第 3 章的规则,尺度会屏蔽历史中 4 个时钟变更读数。此时输出的历史尺度为 7.1005 kWh。
- 这些基线不产生随机数,因此示例不设随机种子。
- 第 3 章保留季节性朴素法和这些辅助函数,并在一整年的周日截断点上运行它们,每个客户的尺度取自第一个截断点之前的全部读数,这正是本章所述的规则。一周的数据无法完成这样的检验。
本章,在本版次中
版次 2026.11.11示例于 2026年11月20日 通过本章 2个示例 个示例中有 2 个通过
译自英文版图书,版次 2026.11.11。