Leak-Free Feature Standardization in Walk-Forward Validation
Problem statement
You have a feature matrix with $T$ rows, each a list of $p$ feature values (a number, or null/None for a missing value), plus a training-window length $L$ and a test horizon $H$. In a walk-forward validation loop, the training window for step $t$ is rows $[t-L+1,\ t]$ and the test window is $[t+1,\ t+H]$.
You standardize each feature to zero mean and unit variance using only training-window statistics (no peeking at the test rows). Implement the function
def walk_forward_stats(X, L, H):
...
For each step $t = L-1,\ L,\ \dots,\ T-H-1$, using only the training window rows $[t-L+1,\ t]$, compute per feature $j$:
- $\mu_j$ = mean of the non-null values in the window
- $\text{var}_j = \tfrac{1}{n}\sum x^2 - \mu_j^2$ (population variance, clamped to $\ge 0$), and $\sigma_j = \sqrt{\text{var}_j}$
Return a list with one entry per step: [mu_vector, sigma_vector], where each vector has length $p$. These are exactly the $(\mu,\sigma)$ you would apply to both the train and test windows.
Edge cases:
- Missing values (
None): excluded from the per-feature mean/variance; track a per-feature valid count, not the window length $L$. - Constant feature ($\sigma_j < 10^{-10}$) or fewer than 2 valid values: set $\sigma_j = 1.0$. When there are fewer than 2 valid values, also set $\mu_j = 0.0$ (there is no meaningful mean to apply).
Example
walk_forward_stats([[1, 10], [2, 10], [3, 10], [4, 5], [5, 3]], 3, 1) -> [[[2.0, 10.0], [0.8164965809277263, 1.0]], [[3.0, 8.333333333333334], [0.8164965809277257, 2.357022603955156]]]
Here $T=5,\ L=3,\ H=1$, so the steps are $t=2$ and $t=3$. At $t=2$ the window is rows $[0,2]$: feature 0 is $[1,2,3]$ ($\mu=2$, $\sigma=\sqrt{2/3}\approx0.8165$) and feature 1 is $[10,10,10]$, which is constant so $\sigma=1.0$. At $t=3$ the window is rows $[1,3]$: feature 1 is $[10,10,5]$ with $\mu=8.333\ldots$ and $\sigma\approx2.357$.
Show hint
Which rows is the standardization allowed to see at a given step if nothing after that step may leak in?
Relevant roles: ML Engineer, Applied Scientist, Data Scientist and MLOps Engineer.
Related free problems
- Multi-Head AttentionMedium
- Masked Self-AttentionMedium