Choosing the Lookback Window, Lags and Inputs of a Forecaster
This post shows how the autocorrelation function sets the lookback window and the lags of a forecaster, and how six measures of relevance rank lags, calendar variables and weather, first within subgroups and then together, on household, EV, PV and net-load series with twiga.
Before a model predicts tomorrow’s load, someone has to decide what it may look at: how many recent observations to give it as a window, which older values to add beside them, and which other inputs, such as the hour, the weekday or the temperature, to include. These choices usually come from habit, a week of history because a week feels natural, or from trial and error on the test set, which lets the test period shape a model that is meant to be judged on it.
This post makes all three choices from the training data alone, using functions of the open-source library twiga.[1] The autocorrelation sets the window and proposes the lags. The lags, the calendar variables and the weather inputs are then ranked by six measures of relevance, first within each group and then together. A column of random noise sits among the candidates as a control, because a ranking that places noise high cannot be trusted. Four series with different character serve as examples: the half-hourly demand of one London household, the hourly charging of electric vehicles (EV) across Los Angeles, the output of a photovoltaic (PV) array, and the net load of the site that the array feeds.
No forecaster is trained, so the post makes no claim about accuracy. Its claim is narrower: the choices can be made without touching the test period, every step can be inspected, and the result behaves sensibly on a control. The code is folded under each figure, and every selection function is imported from twiga 1.1.8 and not rewritten.
Code
import warningswarnings.filterwarnings("ignore")from importlib.metadata import versionimport refrom pathlib import Pathimport tempfileimport urllib.requestfrom lets_plot import ( aes, arrow, gggrid, element_blank, element_line, element_rect, element_text, facet_wrap, geom_abline, geom_band, geom_blank, geom_hline, geom_line, geom_point, geom_rect, geom_ribbon, geom_segment, geom_text, geom_tile, geom_vline, ggplot, ggsize, labs, scale_color_manual, scale_fill_gradient, scale_fill_manual, scale_size_manual, scale_x_continuous, scale_x_discrete, scale_y_continuous, scale_y_discrete, scale_y_reverse, theme,)import numpy as npimport pandas as pdfrom twiga.core.data import AutoregressTransformer, add_fourier_features, select_top_featuresfrom twiga.core.data.feature import add_temporal_featurefrom twiga.core.data.selection import diversity_weightsfrom twiga.core.stats.seasonality import get_acf_valuesfrom twiga.utils.data_loading import get_best_autoregessive_featuresfrom IPython.display import HTML, displaypd.set_option("display.max_colwidth", None)# Colours by role: subject blue, highlight orange, reference slate, context grey.BLUE, ORANGE, SLATE, INK ="#0369A1", "#E26029", "#64748B", "#1E293B"GREY, GREY_DARK, FONT ="#CED4DA", "#6B7280", "'Libre Franklin', 'Segoe UI', Roboto, Helvetica, Arial, sans-serif"def tint(colour: str, amount: float=0.85) ->str:"""A colour mixed with white, for bands and fills.""" rgb = np.array([int(colour[i : i +2], 16) for i in (1, 3, 5)])return"#"+"".join(f"{int(v):02X}"for v in rgb + (255- rgb) * amount)def house() -> theme:"""Thin dark axes on the left and bottom, no grid, no legend, Libre Franklin type."""return theme( legend_position="none", plot_background=element_rect(fill="white", color="white"), panel_background=element_blank(), panel_border=element_blank(), panel_grid=element_blank(), strip_background=element_blank(), strip_text=element_text(face="bold", size=14, hjust=0), axis_line=element_line(size=0.5, color=INK), axis_ticks=element_line(size=0.5, color=INK), axis_ticks_length=4, text=element_text(family=FONT, size=13, color=INK), plot_margin=[8, 18, 4, 4], )PHONE_WIDTH =420LAYOUT = {"phone": False}def columns(desktop: int, phone: int=1) ->int:"""Panels in a row: ``desktop`` on a wide screen, ``phone`` in the narrow variant of a figure."""return phone if LAYOUT["phone"] else desktopdef size(width: int, height: int, panels: int=1, ncol: int=1, phone_ncol: int=1) -> ggsize:"""Canvas of a figure: as given on a wide screen, narrow with the panels re-stacked in the phone variant."""ifnot LAYOUT["phone"]:return ggsize(width, height) per_panel = (height -60) /-(-panels // ncol) ratio = (PHONE_WIDTH / phone_ncol) / (width / ncol)return ggsize(PHONE_WIDTH, int(60+-(-panels // phone_ncol) * per_panel * ratio *1.15))def inline(plot, kind: str) ->str:"""The SVG of a figure as a block that scales with the page; ``kind`` marks the wide or the narrow variant.""" svg = plot.to_svg() width, height = (float(v) for v in re.search(r'width="([\d.]+)" height="([\d.]+)"', svg).groups()) svg = svg.replace(f'width="{width}" height="{height}"', f'viewBox="0 0 {width:g}{height:g}" width="100%"', 1)returnf'<div class="letsplot letsplot-{kind}">{svg}</div>'def show(build) ->None:"""Draw a figure twice, wide and narrow, so that the page shows the variant that fits the width of the screen.""" LAYOUT["phone"] =False wide = inline(build(), "desktop") LAYOUT["phone"] =Truetry: narrow = inline(build(), "phone")finally: LAYOUT["phone"] =False display(HTML(wide + narrow))def note(frame: pd.DataFrame, colour: str= INK, hjust: float=0.0, size: float=5.6):"""Direct labels from a frame with columns x, y, label (and the facet column), with a white halo over the marks."""return geom_text( aes("x", "y", label="label"), data=frame, color=colour, hjust=hjust, size=size, family=FONT, halo_width=2, halo_color="white", inherit_aes=False, )print("twiga", version("twiga"), "| lets-plot", version("lets-plot"))
Series of Different Structure
The household is one customer of the Low Carbon London trial, taken from the Monash collection on Hugging Face.[2] The EV series adds up the energy charged each hour at every charging site in Los Angeles over six months, from the CHARGED dataset.[3] The PV and net-load series come from a single site in Plymouth, United Kingdom, recorded every half-hour together with local weather.[4] Net load is the demand of the site minus the output of the array, so the two Plymouth series share their weather but measure different things.
Figure 1 shows one week of each. Inputs known in advance, such as the irradiance, are treated as available over the forecast period, as a weather forecast would supply them, and the error of a real forecast is not examined. The household and Plymouth data are stored beside the notebook in small files. The EV series is downloaded when the notebook runs, because its dataset card states no licence for redistribution.
Code
DATA = Path("data")EV_URL ="https://huggingface.co/datasets/yzzpz/Charged-data/resolve/main/LOA/volume.csv"household = pd.read_parquet(DATA /"household.parquet").rename(columns={"demand_kwh": "household"})plymouth = pd.read_parquet(DATA /"plymouth.parquet")plymouth["netload"] = plymouth["demand_mw"] - plymouth["pv_power_mw"]ev_file = Path(tempfile.gettempdir()) /"charged-los-angeles-volume.csv"ifnot ev_file.exists(): urllib.request.urlretrieve(EV_URL, ev_file)charging = pd.read_csv(ev_file, index_col=0, parse_dates=True).sum(axis=1)SERIES = {"Household demand": {"data": household, "target": "household", "known": [], "samples": 48, "unit": "kWh per half-hour"},"EV charging": {"data": pd.DataFrame({"timestamp": charging.index, "ev": charging.to_numpy()}), "target": "ev", "known": [], "samples": 24, "unit": "kWh per hour"},"PV output": {"data": plymouth[["timestamp", "pv_power_mw", "irradiance_wm2", "panel_temp_c"]].rename(columns={"pv_power_mw": "pv"}),"target": "pv", "known": ["irradiance_wm2", "panel_temp_c"], "samples": 48, "unit": "MW", },"Net load": {"data": plymouth[["timestamp", "netload", "air_temp_c", "solar_wm2"]], "target": "netload", "known": ["air_temp_c", "solar_wm2"], "samples": 48, "unit": "MW"},}TAGS = {name: f"({chr(97+ i)}) {name}"for i, name inenumerate(SERIES)} # panel tags, which also fix the panel orderfor series in SERIES.values(): series["data"] = series["data"].ffill() # twiga's own loader forward-fills short gaps in the same wayfor name, s in SERIES.items(): d = s["data"]print(f"{name}: {len(d):,} rows, {d['timestamp'].min():%Y-%m-%d} to {d['timestamp'].max():%Y-%m-%d}, {s['samples']} steps a day, known inputs: {', '.join(s['known']) or'none'}")
Code
rows = []for name, spec in SERIES.items(): data, n = spec["data"], spec["samples"] start =int(len(data) *0.4) start += (7- data["timestamp"].iloc[start].dayofweek) %7* n # begin on a Monday week = data.iloc[start : start +7* n] rows.append(pd.DataFrame({"series": f"{TAGS[name]} ({spec['unit']})", "day": np.arange(len(week)) / n, "value": week[spec["target"]].to_numpy()}))weeks = pd.concat(rows)show(lambda: ggplot(weeks, aes("day", "value"))+ geom_line(color=BLUE, size=0.6)+ facet_wrap("series", ncol=columns(2), scales="free_y")+ scale_x_continuous(breaks=list(range(8)))+ labs(x="Days from Monday", y="")+ house() + size(900, 520, 4, 2))
Figure 1: One week of each series, from a Monday of its training window. The daily cycle is clear in the charging, the PV output and the net load, and weak in the household.
Lookback Window and Lags
A forecaster that predicts the next day from what is known at the forecast origin draws on the history of its target in two ways. The lookback window is a block of the most recent observations, ending at the origin. A lag of steps adds the value , which reaches further back than the window without making it longer. A lag can be used only if its value exists at the origin, so every lag is at least one step. Figure 2 shows a window, the horizon and three lags for a forecast one day ahead.
Each choice has a price. A window that is too short hides the daily shape, while a window that is too long, or a lag that repeats what the window already holds, hands the model the same information twice and raises its variance without adding signal. The autocorrelation function (ACF) gives a principled starting point.[5] It is the correlation of the series with itself steps earlier,
and its peaks fall at the periods in which the series repeats. The shortest significant peak sets the window, because it is the shortest span over which the dynamics repeat, and the longer peaks become lag candidates. A lag as long as the shortest period would only repeat the window, so it is left out. A value of counts as significant when it lies outside the band , the range expected for a series of length with no autocorrelation.
Figure 2: A forecast one day ahead. The lookback window covers the last day before the origin, the horizon the next day, and each lag adds the value two, three or seven days earlier than each step of the window.
Everything below reads the first 70% of each series in time, so nothing from the period to be forecast reaches the choice. twiga’s function for autoregressive features returns the window and builds the lag candidates as calendar multiples of the period that it detects.
Code
TRAIN_SHARE =0.7def training(name: str, share: float= TRAIN_SHARE) -> pd.DataFrame:"""The first ``share`` of a series in time: the only part that any selection below may read.""" data = SERIES[name]["data"]return data.iloc[: int(len(data) * share)].copy()def lookback_and_lags(name: str, share: float= TRAIN_SHARE) ->tuple[int, list[int]]:"""Lookback window in steps and lag candidates in days, from twiga's autocorrelation analysis of the training window.""" series = SERIES[name] candidates, lookback = get_best_autoregessive_features( training(name, share), series["target"], n_samples=series["samples"], top_k=50 )return lookback, sorted({int(feature.rsplit("_", 1)[1]) // series["samples"] for feature in candidates})hints = {name: lookback_and_lags(name) for name in SERIES}for name, (lookback, days) in hints.items():print(f"{name}: {len(training(name)):,} training rows, lookback {lookback} steps ({lookback / SERIES[name]['samples']:.0f} day), lag candidates {days} days")
Code
frames = {"acf": [], "band": [], "window": [], "dots": []}for name, spec in SERIES.items(): n, target = spec["samples"], spec["target"] values = training(name)[target].to_numpy() acf, _ = get_acf_values(values, maxlag=7* n + n //2, seasonal_length=n) days = np.arange(1, len(acf) +1) / n lookback, lags = hints[name] band =1.96/ np.sqrt(len(values)) frames["acf"].append(pd.DataFrame({"series": name, "lag": days, "acf": acf})) frames["band"].append(pd.DataFrame({"series": name, "lag": [0, 7.5], "lo": -band, "hi": band})) frames["window"].append(pd.DataFrame({"series": [name], "x0": [0.0], "x1": [lookback / n]})) frames["dots"].append(pd.DataFrame({"series": name, "x": lags, "y": [acf[lag * n -1] for lag in lags], "label": [f"{lag} d: {acf[lag * n -1]:.2f}"for lag in lags]}))acf_df, band_df, window_df, dots_df = (pd.concat(frames[k]) for k in ("acf", "band", "window", "dots"))for frame in (acf_df, band_df, window_df, dots_df): frame["series"] = frame["series"].map(TAGS)label_df = dots_df.assign(y=dots_df["y"] +0.12)show(lambda: ggplot()+ geom_band(aes(xmin="x0", xmax="x1"), data=window_df, fill=tint(BLUE, 0.88), size=0)+ geom_ribbon(aes("lag", ymin="lo", ymax="hi"), data=band_df, fill=tint(SLATE, 0.6), size=0)+ geom_line(aes("lag", "acf"), data=acf_df, color=SLATE, size=0.6)+ geom_point(aes("x", "y"), data=dots_df, shape=21, fill="white", color=BLUE, size=3.2, stroke=1.2)+ note(label_df.rename(columns={"x": "x"}), colour=BLUE, hjust=0.5, size=5.6)+ facet_wrap("series", ncol=columns(2), scales="fixed")+ scale_x_continuous(breaks=list(range(8)))+ labs(x="Lag (days)", y="Autocorrelation")+ house() + size(900, 520, 4, 2))
Figure 3: Autocorrelation of the training window over seven days. The blue band is the lookback window, the grey band the range of no autocorrelation, and the dots the lag candidates with their values.
All four series give the same answer, shown in Figure 3: a window of one day, the blue band, and lag candidates of two, three and seven days, the dots. The daily cycle that every series has produces it. Charging repeats most closely, with an autocorrelation of 0.88 at one day and at half a day, where the load sits on the opposite side of the cycle. The PV array and the net load follow at 0.77 and 0.79. A single household repeats its day only loosely, at 0.35, and its autocorrelation at seven days, 0.42, is higher than at one day, which makes the week its stronger rhythm. All four stay well above the band of no autocorrelation, which is to .
This step has a limit that is worth stating plainly. The candidates are calendar multiples of the detected period, so any series with a daily cycle receives the same list. The analysis points to where to look and does not search for a lag particular to one series. Choosing among the candidates, and between them and the other inputs, is the job of the ranking that follows.
Six Measures of Relevance
An input is relevant to the extent that it tells something about the target, and no single measure sees every kind of dependence. twiga therefore scores each candidate in six ways:
the absolute Spearman rank correlation, which detects any monotone relation;
the F statistic of a linear fit, which detects a linear relation;
the mutual information, which detects any relation visible in the joint histogram of the two variables;
Chatterjee’s , a rank-based coefficient that is zero under independence and one when one variable is a function of the other, whatever its shape;[6]
the predictive power score, the gain of a small decision tree fitted on the candidate alone over predicting the mean of the target;
the impurity importance of a random forest, which also picks up interactions between inputs.[7]
Consider a load that rises when the temperature departs from a comfortable value in either direction. The rank correlation and the F statistic cannot see that relation and the other four can, so a ranking built on one measure inherits the blind spot of that measure.
The six scores lie on different scales, so each is converted into a ranking and the rankings are combined, which is known as rank aggregation.[8] Under the Borda count a measure gives the best of candidates points, the next best points, and so on, and the score of a candidate is its total over the measures. Writing for the points that measure gives candidate ,
A sum like this counts measures that repeat each other twice. The rank correlation and the F statistic, for instance, usually order candidates alike. twiga therefore also offers a weighted sum, , in which the weight is proportional to , where is the mean absolute Spearman correlation between the scores of measure and those of the other five. The weights have a floor of 0.05 and sum to one. The default of the library is the equal-weight sum, and the figures below use the weighted one, the option weighted_borda.
Two more choices complete the set-up. Each cyclic calendar variable enters as a sine and a cosine, so that 23:00 lies next to 00:00,[9] and a column of standard normal noise joins the candidates, since any measure that ranks it above a real input overstates relevance.
Code
from scipy.stats import spearmanrfrom sklearn.feature_selection import f_regression, mutual_info_regressionfrom twiga.core.stats.ppscore import compute_ppscorefrom twiga.core.stats.xicorr import _xicor_vectorgenerator = np.random.default_rng(3)observations =300x = generator.uniform(-1.0, 1.0, observations)season = np.arange(observations) %2RELATIONS = {"Monotone": (x, 0.8* x + generator.normal(0.0, 0.2, observations), None),"U-shaped": (x, 1.6* x**2-0.5+ generator.normal(0.0, 0.15, observations), None),"Confounded by the season": ( np.where(season ==1, -0.55, 0.55) + generator.normal(0.0, 0.2, observations), np.where(season ==1, 0.55, -0.55) + generator.normal(0.0, 0.2, observations), season, ),}MEASURES = ["Rank correlation", "F statistic", "Mutual information", "Chatterjee xi", "Predictive power", "Within the season"]def xi_of(a: np.ndarray, b: np.ndarray) ->float:"""Chatterjee's coefficient, floored at zero."""returnmax(float(_xicor_vector(a, b)[0]), 0.0)def measure_scores(a: np.ndarray, b: np.ndarray, group: np.ndarray |None) ->dict[str, float]:"""Five relevance measures of one input on a scale from zero to one, and xi within each group when there are groups.""" f =float(f_regression(a[:, None], b)[0][0]) information =float(mutual_info_regression(a[:, None], b, random_state=0)[0]) predictive = compute_ppscore(pd.DataFrame({"x": a, "y": b}), x="x", y="y", random_seed=0)["ppscore"] scores = {"Rank correlation": abs(float(spearmanr(a, b)[0])),"F statistic": f / (f +len(a) -2),"Mutual information": float(np.sqrt(1.0- np.exp(-2.0* information))),"Chatterjee xi": xi_of(a, b),"Predictive power": float(predictive), }if group isnotNone: scores["Within the season"] =float(np.mean([xi_of(a[group == g], b[group == g]) for g in np.unique(group)]))return scoresSEASONS = pd.DataFrame({"x": [-0.55, 0.55], "y": [1.4, -1.4], "label": ["Winter", "Summer"], "colour": ["winter", "summer"]})scatters, bars = [], []for tag, (name, (a, b, group)) inzip("abc", RELATIONS.items()): points = pd.DataFrame({"x": a, "y": b, "colour": np.where(group ==1, "winter", "summer") if group isnotNoneelse"single"}) plot = ( ggplot(points, aes("x", "y", color="colour"))+ geom_point(size=2.2, shape=21, fill="white", stroke=0.8)+ scale_color_manual(values={"single": BLUE, "winter": BLUE, "summer": SLATE})+ scale_x_continuous(breaks=[])+ scale_y_continuous(breaks=[], limits=[-1.5, 1.5] if group isnotNoneelseNone)+ (geom_text(aes("x", "y", label="label", color="colour"), data=SEASONS, size=5.6, family=FONT, hjust=0.5, inherit_aes=False) if group isnotNoneelse geom_blank())+ labs(title=f"({tag}) {name}", x="Input", y="Load")+ house()+ theme(plot_title=element_text(face="bold", size=12, hjust=0), axis_title=element_text(size=11)) ) scatters.append(plot) scores = measure_scores(a, b, group) rows = pd.DataFrame({"measure": list(scores), "score": list(scores.values())}) rows["label"] = rows["score"].map("{:.2f}".format) rows["fill"] = np.where(rows["measure"] =="Within the season", SLATE, BLUE) shown = MEASURES if group isnotNoneelse MEASURES[:-1] + [" "] rows["end"] = rows["score"] +0.03 rows["zero"] =0.0 track = pd.DataFrame({"measure": list(rows["measure"]), "zero": 0.0, "one": 1.0}) plot = ( ggplot()+ geom_segment(aes(x="zero", xend="one", y="measure", yend="measure"), data=track, size=9, color=tint(GREY, 0.45))+ geom_segment(aes(x="zero", xend="score", y="measure", yend="measure", color="fill"), data=rows, size=9)+ scale_color_manual(values={BLUE: BLUE, SLATE: SLATE})+ geom_text(aes("end", "measure", label="label"), data=rows, hjust=0, size=5.4, family=FONT, color=INK)+ scale_x_continuous(limits=[0, 1.18], breaks=[])+ scale_y_discrete(limits=shown[::-1])+ labs(x="", y="")+ house()+ theme(axis_line_x=element_blank(), axis_ticks=element_blank(), axis_text_y=element_text(size=10.5)) ) bars.append(plot)show(lambda: gggrid(scatters + bars ifnot LAYOUT["phone"] else [p for pair inzip(scatters, bars, strict=True) for p in pair], ncol=columns(3)) + size(900, 520, 6, 3))
(a) MonotoneLoadInput0.910.820.910.590.43Predictive powerChatterjee xiMutual informationF statisticRank correlation(b) U-shapedLoadInput0.050.000.930.650.57Predictive powerChatterjee xiMutual informationF statisticRank correlationWinterSummer(c) Confounded by the seasonLoadInput0.740.790.860.470.600.01Within the seasonPredictive powerChatterjee xiMutual informationF statisticRank correlation
Figure 4: Five relevance measures scored from zero to one on three relationships between an input and a load: monotone, U-shaped and confounded by the season. In the last, xi within each season is zero although every pooled score is high.
Figure 4 scores three relationships between an input and a load with five of the measures; the importance of a forest is left out because it divides a total among several inputs and has no value for one input alone. On the monotone relationship the five measures score the input between 0.43 and 0.91. On the U-shaped relationship the rank correlation is 0.05 and the F statistic 0.00, so a ranking built on either would discard an input that the mutual information scores at 0.93. In the third relationship the input and the load both follow the season, and every pooled score is high, with a rank correlation of 0.74. Within each season, however, is 0.01: the dependence is real in the pooled data and disappears once the season is known. A temperature that rises and falls with the season behaves partly in this way, which is why the shortlist is checked against a noise column and against the stability of the ranking.
Code
RANKERS = ["abs_corr", "f_score", "mi_score", "xi_score", "pps_score", "rf_importance"]SHORT = {"abs_corr": "Rank correlation", "f_score": "F statistic", "mi_score": "Mutual information", "xi_score": "Chatterjee xi", "pps_score": "Predictive power", "rf_importance": "Forest importance"}NOUN = {"Household demand": "Demand", "EV charging": "Charging", "PV output": "PV output", "Net load": "Net load"}FIELDS = {"hour_sin": "Hour, sine", "hour_cos": "Hour, cosine", "wday_sin": "Weekday, sine", "wday_cos": "Weekday, cosine","yday_sin": "Day of year, sine", "yday_cos": "Day of year, cosine", "noise": "Noise (control)","solar_wm2": "Solar radiation", "air_temp_c": "Air temperature", "irradiance_wm2": "Irradiance", "panel_temp_c": "Panel temperature",}def label(feature: str, name: str) ->str:"""Sentence-case label of a candidate column.""" days =lambda steps: f"{int(steps) // SERIES[name]['samples']} days"# noqa: E731if"_lag_"in feature:returnf"{NOUN[name]}{days(feature.rsplit('_', 1)[1])} earlier"if"_rolling_mean_win_"in feature:returnf"Mean of the last {days(feature.rsplit('_', 1)[1])}"return FIELDS.get(feature, feature)def design(name: str, share: float= TRAIN_SHARE) ->tuple[pd.DataFrame, dict[str, list[str]], str]:"""Candidate inputs of a series on its training window, in three subgroups, with a noise column as control.""" series = SERIES[name] n, target = series["samples"], series["target"] _, days = lookback_and_lags(name, share) frame = AutoregressTransformer( n_samples=n, lags=days, windows=[days[0], days[-1]], window_funcs=["mean"], value_column=target ).fit_transform(training(name, share)) frame = add_temporal_feature(frame, "timestamp") frame = add_fourier_features(frame, ["hour", "wday", "yday"], [24, 7, 366]) frame["noise"] = np.random.default_rng(0).standard_normal(len(frame)) groups = {"Lags and means": [c for c in frame.columns if"_lag_"in c or"_rolling_"in c],"Calendar": [f"{field}_{kind}"for field in ("hour", "wday", "yday") for kind in ("sin", "cos")],"Known inputs": series["known"], }return frame.dropna(subset=[c for g in groups.values() for c in g] + ["noise", target]), groups, targetdef rank(frame: pd.DataFrame, features: list[str], target: str, aggregation: str="weighted_borda") ->tuple[pd.DataFrame, np.ndarray]:"""twiga's six measures and their fusion for the given candidates; also the diversity weights of the measures.""" _, scores = select_top_features( frame, features, target, top_k=len(features), alpha=None, include_chi2=False, rank_aggregation=aggregation, return_scores=True, ) scores = scores.set_index("feature")return scores, diversity_weights(scores[RANKERS].fillna(0))def pooled(name: str, share: float= TRAIN_SHARE) ->tuple[pd.DataFrame, np.ndarray]:"""One ranking of all the subgroups together and the noise column.""" frame, groups, target = design(name, share)return rank(frame, [c for g in groups.values() for c in g] + ["noise"], target)results = {name: pooled(name) for name in SERIES}for name, (scores, weights) in results.items():print(f"{name}: {len(scores)} candidates; weights "+", ".join(f"{SHORT[m].lower()}{w:.2f}"for m, w inzip(RANKERS, weights)))
Figure 5: Rank correlation between the six measures on the net load, with the weight of each measure at the right. The darker the cell, the more two measures order the inputs alike.
Figure 5 shows how far the six measures agree on the net load. Mutual information and agree most, at 0.98, and the predictive power and the forest importance least, at 0.65, so the weights stay between 0.14 and 0.19 here and between 0.13 and 0.25 across the four series, the largest being that of the forest importance on the PV output. Because the measures overlap only in part, the weights differ little from equal ones, and a later section measures how little.
Ranking within Subgroups
The candidates fall into three kinds that answer different questions: the lags and rolling means of the target, the calendar variables, and the known inputs such as the weather. It is natural to rank each kind on its own first, because the question within a kind is which one is best, which lag or which calendar encoding. This is also how twiga’s dataset loader selects inputs. Each subgroup is ranked by the six measures with the equal-weight Borda count, and the noise column joins every subgroup as a control.
Figure 6 shows the three rankings for the net load. Each tile gives the position that one measure assigns, with 1 the best and the darkest blue, and the last column is the Borda total, in which a higher number is better.
Code
def short(feature: str) ->str:"""Compact row label for the subgroup panels: 2-day lag, 7-day mean, and the calendar and input labels.""" days =lambda steps: int(steps) // SERIES["Net load"]["samples"] # noqa: E731if"_lag_"in feature:returnf"{days(feature.rsplit('_', 1)[1])}-day lag"if"_rolling_mean_win_"in feature:returnf"{days(feature.rsplit('_', 1)[1])}-day mean"return FIELDS.get(feature, feature)def group_panel(frame: pd.DataFrame, group: str, features: list[str], target: str, tag: str, height: int):"""Rank within one subgroup, with the noise column as its control: a heat panel of the rank under each measure.""" scores, _ = rank(frame, features + ["noise"], target, aggregation="borda_count") ranks = scores[RANKERS].fillna(0).rank(ascending=False, method="first").astype(int) order = scores["borda_count"].sort_values(ascending=False).index.tolist() m =len(order) rows = [ (short(f), {"abs_corr": "Corr", "f_score": "F", "mi_score": "MI", "xi_score": "Xi", "pps_score": "PPS", "rf_importance": "RF"}[r], int(ranks.loc[f, r]), 1- (ranks.loc[f, r] -1) / (m -1))for f in order for r in RANKERS ] cells = pd.DataFrame(rows, columns=["row", "column", "rank", "shade"]) cells["ink"] = np.where(cells["shade"] >0.55, "white", INK) columns =list(dict.fromkeys(cells["column"])) fused = pd.DataFrame({"row": [short(f) for f in order], "column": "Sum", "label": [f"{scores.loc[f, 'borda_count']:.0f}"for f in order]}) names = [short(f) for f in order] pad = ["\u200b"* (i +1) for i inrange(height - m)] control = cells[cells["row"] == short("noise")] sums = fused.assign(shade=0.0) plot = ( ggplot(cells, aes("column", "row"))+ geom_tile(aes(fill="shade"), color="white", size=1.0)+ scale_fill_gradient(low=tint(BLUE, 0.94), high=BLUE, limits=[0, 1])+ geom_tile(data=sums, mapping=aes("column", "row"), fill=tint(SLATE, 0.88), color="white", size=1.0, inherit_aes=False)+ geom_tile(data=control, mapping=aes("column", "row"), fill="rgba(0,0,0,0)", color=ORANGE, size=1.1, inherit_aes=False)+ geom_text(aes(label="rank", color="ink"), size=5.4, family=FONT)+ scale_color_manual(values={"white": "white", INK: INK})+ geom_text(aes("column", "row", label="label"), data=fused, size=5.4, family=FONT, color=INK, fontface="bold", inherit_aes=False)+ scale_x_discrete(limits=columns + ["Sum"], position="top", expand=[0.0, 0.8])+ scale_y_discrete(limits=pad + names[::-1])+ labs(title=f"({tag}) {group}", x="", y="")+ house()+ theme(axis_line=element_blank(), axis_ticks=element_blank(), plot_title=element_text(face="bold", size=12, hjust=0), axis_text_x=element_text(size=9.5, angle=0)) )return plot, scoresframe, groups, target = design("Net load")panels = []tallest =max(len(features) for features in groups.values()) +1for tag, (group, features) inzip("abc", groups.items()): plot, scores = group_panel(frame, group, features, target, tag, tallest) panels.append(plot)show(lambda: gggrid(panels, ncol=columns(3)) + size(900, 360, 3, 3))
11111123222232333444554355445566666636292416147CorrFMIXiPPSRFSumNoise (control)7-day mean2-day mean3-day lag7-day lag2-day lag(a) Lags and means22211311332133123244444465565656656777777537373422141410CorrFMIXiRFNoise (control)Weekday, cosineWeekday, sineDay of year, sineHour, cosineDay of year, cosineHour, sine(b) Calendar21111112222333333217127CorrFMIXiPPSRFSumNoise (control)Air temperatureSolar radiation(c) Known inputs
11111123222232333444554355445566666636292416147CorrFMIXiPPSRFSumNoise (control)7-day mean2-day mean3-day lag7-day lag2-day lag(a) Lags and means22211311332133123244444465565656656777777537373422141410CorrFMIXiPPSRFSumNoise (control)Weekday, cosineWeekday, sineDay of year, sineHour, cosineDay of year, cosineHour, sine(b) Calendar21111112222333333217127CorrFMIXiPPSRFSumNoise (control)Air temperatureSolar radiation(c) Known inputs
Figure 6: Ranking of the net load inputs within three subgroups, with the noise column as a control. Each tile is the position under one measure (Corr for the rank correlation, F, MI for the mutual information, Xi, PPS for the predictive power, RF for the forest importance), 1 is best and darkest, and the last column is the Borda total, where higher is better.
For the net load, the lag of two days heads the lags and means, ahead of the lags of seven and three days and the two means, with noise last. Among the calendar variables the sine of the hour is first, the cosine of the day of the year second and the cosine of the hour third. Both weekday encodings fall below the day of the year, and noise is last again. Among the known inputs the solar radiation comes first and the air temperature second, both ahead of the noise.
The three rankings could be used as they stand, with a fixed quota from each subgroup: the best two lags or means, the best two calendar variables and the best known input. Set against the single ranking of all candidates, which the next section builds, that rule selects the same inputs for the household and the EV charging, where it picks four, and shares three of five for the PV output and the net load. In those two series the quota keeps two calendar variables that rank seventh to ninth of fourteen once everything competes. It drops the lag of three days for the net load, which ranks third, and the panel temperature for the PV array, which ranks fourth. Every input it keeps still ranks above the noise column.
Code
def per_group_rule(name: str) ->list[str]:"""The best two lags or means, the best two calendar inputs and the best known input, each ranked within its subgroup.""" frame, groups, target = design(name) picks = []for group, features in groups.items():ifnot features:continue scores, _ = rank(frame, features + ["noise"], target, aggregation="borda_count") ordered = [f for f in scores["borda_count"].sort_values(ascending=False).index if f !="noise"] picks += ordered[: 1if group =="Known inputs"else2]return picksfor name, (scores, _) in results.items(): pooled_order = scores["weighted_borda"].sort_values(ascending=False).index.tolist() picks = per_group_rule(name) position = {f: pooled_order.index(f) +1for f in picks} below_noise = [label(f, name) for f in picks if position[f] > pooled_order.index("noise") +1]print(f"{name}: per-subgroup picks {[label(f, name) for f in picks]}; positions in the pooled order {list(position.values())}; shared with the pooled top {len(picks)}: {len(set(picks) &set(pooled_order[: len(picks)]))}; below the noise column: {below_noise or'none'}")
Pooled Ranking
Ranking everything together lets a lag compete with a calendar variable and a weather column on one scale. The ranking reads the first 70% of each series and uses the diversity-weighted sum. It covers twelve candidates for the household and the charging, and fourteen for the PV output and the net load.
Figure 7 draws the score of every candidate, best at the top, with the five best in blue and the noise column in orange. Five is only a convenient length. The dashed line marks the cut after the fifth, and the label gives the fall in score across it as a share of the range from best to worst.
Demand 7 days earlierDemand 7 days earlierHour, sineHour, sineHour, cosineHour, cosineDemand 2 days earlierDemand 2 days earlierDemand 3 days earlierDemand 3 days earlierDay of year, cosineDay of year, cosineMean of the last 2 daysMean of the last 2 daysDay of year, sineDay of year, sineMean of the last 7 daysMean of the last 7 daysWeekday, sineWeekday, sineWeekday, cosineWeekday, cosineNoise (control)Noise (control)Gap 2.2% of rangeGap 2.2% of range(a) Household demandCharging 7 days earlierCharging 7 days earlierCharging 2 days earlierCharging 2 days earlierHour, sineHour, sineHour, cosineHour, cosineCharging 3 days earlierCharging 3 days earlierMean of the last 2 daysMean of the last 2 daysMean of the last 7 daysMean of the last 7 daysDay of year, sineDay of year, sineDay of year, cosineDay of year, cosineWeekday, cosineWeekday, cosineWeekday, sineWeekday, sineNoise (control)Noise (control)Gap 12.7% of rangeGap 12.7% of range(b) EV chargingIrradianceIrradiancePV output 2 days earlierPV output 2 days earlierPV output 3 days earlierPV output 3 days earlierPanel temperaturePanel temperaturePV output 7 days earlierPV output 7 days earlierMean of the last 2 daysMean of the last 2 daysMean of the last 7 daysMean of the last 7 daysHour, cosineHour, cosineDay of year, cosineDay of year, cosineHour, sineHour, sineDay of year, sineDay of year, sineNoise (control)Noise (control)Weekday, sineWeekday, sineWeekday, cosineWeekday, cosineGap 3.5% of rangeGap 3.5% of range051015(c) PV outputNet load 2 days earlierNet load 2 days earlierNet load 7 days earlierNet load 7 days earlierNet load 3 days earlierNet load 3 days earlierSolar radiationSolar radiationMean of the last 2 daysMean of the last 2 daysMean of the last 7 daysMean of the last 7 daysDay of year, cosineDay of year, cosineHour, sineHour, sineAir temperatureAir temperatureHour, cosineHour, cosineDay of year, sineDay of year, sineWeekday, sineWeekday, sineWeekday, cosineWeekday, cosineNoise (control)Noise (control)Gap 4.8% of rangeGap 4.8% of range051015(d) Net loadFused score (higher is more relevant)
Demand 7 days earlierDemand 7 days earlierHour, sineHour, sineHour, cosineHour, cosineDemand 2 days earlierDemand 2 days earlierDemand 3 days earlierDemand 3 days earlierDay of year, cosineDay of year, cosineMean of the last 2 daysMean of the last 2 daysDay of year, sineDay of year, sineMean of the last 7 daysMean of the last 7 daysWeekday, sineWeekday, sineWeekday, cosineWeekday, cosineNoise (control)Noise (control)Gap 2.2% of rangeGap 2.2% of range(a) Household demandCharging 7 days earlierCharging 7 days earlierCharging 2 days earlierCharging 2 days earlierHour, sineHour, sineHour, cosineHour, cosineCharging 3 days earlierCharging 3 days earlierMean of the last 2 daysMean of the last 2 daysMean of the last 7 daysMean of the last 7 daysDay of year, sineDay of year, sineDay of year, cosineDay of year, cosineWeekday, cosineWeekday, cosineWeekday, sineWeekday, sineNoise (control)Noise (control)Gap 12.7% of rangeGap 12.7% of range(b) EV chargingIrradianceIrradiancePV output 2 days earlierPV output 2 days earlierPV output 3 days earlierPV output 3 days earlierPanel temperaturePanel temperaturePV output 7 days earlierPV output 7 days earlierMean of the last 2 daysMean of the last 2 daysMean of the last 7 daysMean of the last 7 daysHour, cosineHour, cosineDay of year, cosineDay of year, cosineHour, sineHour, sineDay of year, sineDay of year, sineNoise (control)Noise (control)Weekday, sineWeekday, sineWeekday, cosineWeekday, cosineGap 3.5% of rangeGap 3.5% of range(c) PV outputNet load 2 days earlierNet load 2 days earlierNet load 7 days earlierNet load 7 days earlierNet load 3 days earlierNet load 3 days earlierSolar radiationSolar radiationMean of the last 2 daysMean of the last 2 daysMean of the last 7 daysMean of the last 7 daysDay of year, cosineDay of year, cosineHour, sineHour, sineAir temperatureAir temperatureHour, cosineHour, cosineDay of year, sineDay of year, sineWeekday, sineWeekday, sineWeekday, cosineWeekday, cosineNoise (control)Noise (control)Gap 4.8% of rangeGap 4.8% of range051015(d) Net loadFused score (higher is more relevant)
Figure 7: Fused score of every input, best at the top. The dashed line marks the cut after five inputs, blue dots are the shortlist, and the orange dot is the noise control.
Code
for name, (scores, _) in results.items(): n =len(scores) by_measure = {SHORT[m].lower(): int(scores[m].fillna(0).rank(ascending=False, method="first")["noise"]) for m in RANKERS} fused =int(scores["weighted_borda"].rank(ascending=False, method="first")["noise"])print(f"{name}: position of the noise column among {n}, by measure {by_measure}; fused {fused}")
The four orders differ in ways that follow the physics of each series.
In the household, the lag of seven days is first, the sine and cosine of the hour are second and third, and the lags of two and three days follow. The weekday encodings rank tenth and eleventh, so the household repeats by the week through its own past and not through a weekday variable. Noise is last of twelve under every measure except the forest importance, which puts it eighth.
For EV charging the order starts with the lags of seven and two days, then the sine of the hour, the cosine of the hour and the lag of three days. The two weekday encodings and the noise are the last three.
The irradiance leads the PV output under all six measures. The lags of two and three days come next, then the panel temperature and the lag of seven days. Noise ranks twelfth of fourteen, above both weekday encodings, which suits an array whose output does not depend on the day of the week.
In the net load the lags of two, seven and three days are followed by the solar radiation. The air temperature is only ninth, behind two means of the past and two calendar variables, and noise is last of fourteen.
Across the four series noise sits last or within the lowest three. The forest importance misplaces it most often, ranking it above real inputs in the household, the PV output and the net load, and the fusion corrects that, which is the practical case for combining measures. The cut at five is not equally firm everywhere. The score falls by 12.7% of the range after the fifth input for the EV charging, a real break, but by only 2.2%, 3.5% and 4.8% for the household, the PV output and the net load, where the sixth input is almost as relevant as the fifth. Inputs that score near the noise column, as the weekday encodings do, can be dropped without loss. Relevance alone does not remove redundancy, however: the sine and cosine of the hour sit side by side in two of the four shortlists, although a model needs the pair only to place the hour on a circle.
Equal and Diversity Weights
Figure 8 sets the default order of twiga, the equal-weight Borda count, against the diversity-weighted one. The two orders select the same five inputs in all four series, and their rank correlation lies between 0.99 and 1.00. On this evidence the weighting changes little, and equal weights are enough.
Code
pairs = []for name, (scores, _) in results.items(): equal = scores["borda_count"].rank(ascending=False, method="first") weighted = scores["weighted_borda"].rank(ascending=False, method="first") group = np.where(scores.index =="noise", "Noise", np.where(weighted <=5, "Shortlist", "Other")) pairs.append(pd.DataFrame({"series": name, "equal": equal.to_numpy(), "weighted": weighted.to_numpy(), "group": group}))pairs = pd.concat(pairs)pairs["series"] = pairs["series"].map(TAGS)def agreement_label(scores: pd.DataFrame) ->str: same =set(scores["borda_count"].nlargest(5).index) ==set(scores["weighted_borda"].nlargest(5).index)returnf"Same five inputs: {'yes'if same else'no'}; rank correlation {scores['borda_count'].corr(scores['weighted_borda'], method='spearman'):.3f}"agree = pd.DataFrame( {"series": [TAGS[name] for name in results], "x": 1.0, "y": [len(r[0]) +0.6for r in results.values()], "label": [agreement_label(r[0]) for r in results.values()]})show(lambda: ggplot(pairs, aes("equal", "weighted"))+ geom_abline(slope=1, intercept=0, color=GREY_DARK, linetype="dotted")+ geom_point(aes(color="group", fill="group"), shape=21, size=4, stroke=1.1)+ scale_color_manual(values={"Shortlist": BLUE, "Other": GREY_DARK, "Noise": ORANGE})+ scale_fill_manual(values={"Shortlist": BLUE, "Other": "white", "Noise": ORANGE})+ note(agree, colour=INK, hjust=0.0, size=5.6)+ facet_wrap("series", ncol=columns(2))+ scale_x_continuous(breaks=[1, 5, 10, 14])+ scale_y_continuous(breaks=[1, 5, 10, 14])+ labs(x="Position under equal weights (1 is best)", y="Position under diversity weights")+ house() + size(900, 700, 4, 2))
Same five inputs: yes; rank correlation 0.993Same five inputs: yes; rank correlation 0.993151014(a) Household demandSame five inputs: yes; rank correlation 1.000Same five inputs: yes; rank correlation 1.000(b) EV chargingSame five inputs: yes; rank correlation 0.990Same five inputs: yes; rank correlation 0.990151014151014(c) PV outputSame five inputs: yes; rank correlation 0.995Same five inputs: yes; rank correlation 0.995151014(d) Net loadPosition under diversity weightsPosition under equal weights (1 is best)
Same five inputs: yes; rank correlation 0.993Same five inputs: yes; rank correlation 0.993151014(a) Household demandSame five inputs: yes; rank correlation 1.000Same five inputs: yes; rank correlation 1.000151014(b) EV chargingSame five inputs: yes; rank correlation 0.990Same five inputs: yes; rank correlation 0.990151014(c) PV outputSame five inputs: yes; rank correlation 0.995Same five inputs: yes; rank correlation 0.995151014151014(d) Net loadPosition under diversity weightsPosition under equal weights (1 is best)
Figure 8: Position of each input under equal weights and under diversity weights. Points on the dotted line keep their position, blue points are the shortlist, and the orange point is the noise control.
Stability across Training Windows
A ranking helps little if it changes when a few more weeks of data arrive. Figure 9 repeats the whole analysis on the first 50%, 70% and 90% of each series, finding the lookback and the lags again each time. The best input is the same in all three windows for every series, and the top five are identical. The windows are nested, though, so they share data, and the check shows stability as data accumulate and not independence across periods. A later, disjoint period would be a stronger test, and a longer record would allow one. Noise is last in the household and the charging at every window, within the two lowest for the net load, and within the three lowest for the PV output, where two weekday encodings fall below it at 70% and 90%.
Code
WINDOWS = (0.5, 0.7, 0.9)orders = {}for name in SERIES:for share in WINDOWS: scores, _ = results[name] if share == TRAIN_SHARE else pooled(name, share) orders[(name, share)] = scores["weighted_borda"].sort_values(ascending=False).index.tolist()lines, ends = [], []for name in SERIES: keep = orders[(name, TRAIN_SHARE)][:5]for share in WINDOWS:for position, feature inenumerate(orders[(name, share)], start=1): group ="Noise"if feature =="noise"else"Shortlist"if feature in keep else"Other" lines.append((name, feature, int(share *100), position, group))for feature in keep + ["noise"]: ends.append((name, 92.0, orders[(name, WINDOWS[-1])].index(feature) +1, label(feature, name)))rank_df = pd.DataFrame(lines, columns=["series", "feature", "window", "rank", "group"])end_df = pd.DataFrame(ends, columns=["series", "x", "y", "label"])for frame in (rank_df, end_df): frame["series"] = frame["series"].map(TAGS)show(lambda: ggplot(rank_df, aes("window", "rank"))+ geom_line(aes(group="feature", color="group", size="group"))+ geom_point(aes(color="group", fill="group"), shape=21, size=2.6, stroke=1.0)+ scale_color_manual(values={"Shortlist": BLUE, "Other": GREY, "Noise": ORANGE})+ scale_fill_manual(values={"Shortlist": BLUE, "Other": "white", "Noise": ORANGE})+ scale_size_manual(values={"Shortlist": 1.0, "Other": 0.5, "Noise": 1.0})+ note(end_df, colour=INK, hjust=0.0, size=5.4)+ facet_wrap("series", ncol=columns(2))+ scale_x_continuous(breaks=[50, 70, 90], labels=["50%", "70%", "90%"], limits=[46, 122])+ scale_y_reverse(breaks=[1, 5, 10, 14])+ labs(x="Share of the series used for training", y="Position in the fused order")+ house() + size(900, 640, 4, 2))
Demand 7 days earlierDemand 7 days earlierHour, sineHour, sineHour, cosineHour, cosineDemand 2 days earlierDemand 2 days earlierDemand 3 days earlierDemand 3 days earlierNoise (control)Noise (control)151014(a) Household demandCharging 7 days earlierCharging 7 days earlierCharging 2 days earlierCharging 2 days earlierHour, sineHour, sineHour, cosineHour, cosineCharging 3 days earlierCharging 3 days earlierNoise (control)Noise (control)(b) EV chargingIrradianceIrradiancePV output 2 days earlierPV output 2 days earlierPV output 3 days earlierPV output 3 days earlierPanel temperaturePanel temperaturePV output 7 days earlierPV output 7 days earlierNoise (control)Noise (control)50%70%90%151014(c) PV outputNet load 2 days earlierNet load 2 days earlierNet load 7 days earlierNet load 7 days earlierNet load 3 days earlierNet load 3 days earlierSolar radiationSolar radiationMean of the last 2 daysMean of the last 2 daysNoise (control)Noise (control)50%70%90%(d) Net loadPosition in the fused orderShare of the series used for training
Demand 7 days earlierDemand 7 days earlierHour, sineHour, sineHour, cosineHour, cosineDemand 2 days earlierDemand 2 days earlierDemand 3 days earlierDemand 3 days earlierNoise (control)Noise (control)151014(a) Household demandCharging 7 days earlierCharging 7 days earlierCharging 2 days earlierCharging 2 days earlierHour, sineHour, sineHour, cosineHour, cosineCharging 3 days earlierCharging 3 days earlierNoise (control)Noise (control)151014(b) EV chargingIrradianceIrradiancePV output 2 days earlierPV output 2 days earlierPV output 3 days earlierPV output 3 days earlierPanel temperaturePanel temperaturePV output 7 days earlierPV output 7 days earlierNoise (control)Noise (control)151014(c) PV outputNet load 2 days earlierNet load 2 days earlierNet load 7 days earlierNet load 7 days earlierNet load 3 days earlierNet load 3 days earlierSolar radiationSolar radiationMean of the last 2 daysMean of the last 2 daysNoise (control)Noise (control)50%70%90%151014(d) Net loadPosition in the fused orderShare of the series used for training
Figure 9: Position of each input in the fused order when the analysis uses the first 50%, 70% and 90% of the series. Blue lines are the shortlist at 70%, orange is the noise control, grey are the other inputs.
Code
# Numbers that the text quotes. The cell prints them and is hidden on the page.net_frame, net_groups, net_target = design("Net load")for group, features in net_groups.items(): ordered = rank(net_frame, features + ["noise"], net_target, aggregation="borda_count")[0]["borda_count"].sort_values(ascending=False)print(f"{group}: {[label(f, 'Net load') for f in ordered.index]}")print(f"Net load, agreement of the measures: smallest off-diagonal correlation {corr.to_numpy()[np.triu_indices(6, 1)].min():.2f}, largest {corr.to_numpy()[np.triu_indices(6, 1)].max():.2f}; weights {dict(zip(order, weights.round(2)))}")for name, (scores, _) in results.items(): fused = scores["weighted_borda"].sort_values(ascending=False) share = (fused.iloc[4] - fused.iloc[5]) / (fused.iloc[0] - fused.iloc[-1])print(f"{name}: fifth {fused.iloc[4]:.2f}, sixth {fused.iloc[5]:.2f}, gap {share:.1%} of the range; noise at position {int(fused.index.get_loc('noise')) +1} of {len(fused)}; {agreement_label(scores)}")for name in SERIES: top5 = [set(orders[(name, share)][:5]) for share in WINDOWS] noise = [orders[(name, share)].index("noise") +1for share in WINDOWS]print(f"{name}: top five identical in all three windows: {top5[0] == top5[1] == top5[2]}; noise position {noise} of {len(orders[(name, 0.7)])}")
Limits of the Method
Relevance is not usefulness. Each measure scores one input at a time, so two inputs that carry the same information both rank high, and a shortlist can hold redundant inputs. The ranking is a shortlist for a forecaster to test and not a final choice.
Accuracy is untested. No forecaster is fitted here, so nothing shows that the shortlist forecasts better than all the candidates. A comparison on held-out periods is the obvious next step, and its answer may differ by series.
The weather is assumed known. The weather inputs are treated as available over the forecast period. With a real weather forecast their relevance would be lower.
The pool and the cut are fixed. The lags are calendar multiples of the detected period, so others, such as half-day multiples, are not in the pool, and the length of five is a convention.
The selection reads the training window only. twiga’s load_dataset applies the same functions to a whole file, which suits choosing a configuration for a dataset. A backtest has to call them on the training window, as the code here does, or the ranking sees the period that it is later tested on.
The sample is small. The findings describe one household, one site and one city, and say nothing about how typical they are.
Recipe for Application
Every step uses the training window only.
Hold out the period to be forecast, and keep the first part of the series in time.
Find the periods of the series from its autocorrelation. The shortest sets the lookback window, and the longer ones become lag candidates, as get_best_autoregessive_features returns them.
List the candidates by kind: the lags and rolling means, the sine and cosine of each calendar cycle, and the inputs known in advance. Add a column of noise.
Rank each kind, then everything together. Keep the inputs that rank above the noise column and above a visible gap in the scores.
Pass the shortlist to a forecaster, and test it against the full set of candidates on held-out periods.
Summary of the Method
For all four series the autocorrelation gives a window of one day and lags of two, three and seven days, which the daily cycle explains. The ranking then separates them. The lag of seven days is the best input for the household and the EV charging, the lag of two days for the net load, and the irradiance for the PV output. The hour is the only calendar variable to reach a shortlist, and the weekday encodings are among the lowest everywhere. Noise ranks last or within the lowest three in every series, although the forest importance puts it above real inputs in three, more often than any other measure. The top five are the same under equal and diversity weights and across the first 50%, 70% and 90% of each series, though those windows are nested. Ranking each subgroup first and applying a fixed quota selects the same inputs as the combined ranking for the household and the EV charging, and weaker calendar inputs for the PV output and the net load.
The ranking is a shortlist to test, and its length is better read from the gaps between scores than fixed in advance. The notebook and the two small data files are in the repository of this site. The EV series is downloaded when the notebook runs, and the code needs twiga[ml]==1.1.8 and lets-plot.
References
[1] A. Faustine, “twiga: probabilistic time series forecasting,” version 1.1.8, Python Package Index. [Online]. Available: https://sambaiga.github.io/twiga-docs/
[4] “Electricity demand and solar generation data from Plymouth, UK,” Zenodo, record 5500457, CC BY 4.0. [Online]. Available: https://zenodo.org/records/5500457
[5] G. E. P. Box, G. M. Jenkins, G. C. Reinsel and G. M. Ljung, Time Series Analysis: Forecasting and Control, 5th ed. Hoboken, NJ, USA: Wiley, 2015.
[6] S. Chatterjee, “A new coefficient of correlation,” J. Amer. Statist. Assoc., vol. 116, no. 536, pp. 2009-2022, 2021.
[7] L. Breiman, “Random forests,” Mach. Learn., vol. 45, no. 1, pp. 5-32, 2001.
[8] C. Dwork, R. Kumar, M. Naor and D. Sivakumar, “Rank aggregation methods for the Web,” in Proc. 10th Int. Conf. World Wide Web, 2001, pp. 613-622.
[9] R. J. Hyndman and G. Athanasopoulos, Forecasting: Principles and Practice, 3rd ed. Melbourne, Australia: OTexts, 2021. [Online]. Available: https://otexts.com/fpp3/
Citation
BibTeX citation:
@online{faustine2026,
author = {Faustine, Anthony},
title = {Choosing the {Lookback} {Window,} {Lags} and {Inputs} of a
{Forecaster}},
date = {2026-11-21},
url = {https://sambaiga.github.io/pages/blog/posts/2026/11/2026-11-21-lookback-lags-and-input-ranking.html},
langid = {en}
}