Group Aggregation Features
Building features by aggregating a variable within groups — sector, exchange, day, account — such as a stock's return minus its sector's average return, turning group membership into predictive signal without one-hot encoding every group.
Prerequisites: One-Hot Encoding and the High-Cardinality Problem
A single stock's raw return doesn't tell you much on its own — was the whole sector up 3% that day, making this stock's 2% gain actually below average? A raw daily volume of 5 million shares means something completely different for a mega-cap versus a micro-cap. Comparing a value to its own history or to its peers, rather than looking at it in isolation, is often where the real predictive signal lives, and that comparison is built by aggregating within groups.
The idea: summarize the group, then compare the individual to it
Pick a grouping key — sector, exchange, calendar day, account, order type — and a variable you care about. Compute a group-level statistic (mean, median, standard deviation, rank) of that variable within each group, then attach that group statistic back onto every row belonging to the group, so each individual observation can be compared against its peers.
In plain English: take a stock's own value , subtract the average value computed only over the group that row belongs to (its sector, say), and the result is how far above or below its peer group the stock sits — a feature that's often far more informative than the raw level. The same pattern works with group standard deviation (to standardize), group rank (percentile within the group), or group count (how crowded the group is).
Worked example: sector-relative return
Five tech-sector stocks post daily returns of 3%, 1%, -2%, 4%, and 2%; five energy-sector stocks post 5%, 6%, 4%, 5%, and 7%. The tech-sector mean is ; the energy-sector mean is . A tech stock that returned 3% has a sector-relative return of — solidly above its peers. An energy stock that returned 5% has a sector-relative return of — actually below its peers, despite a higher raw return than every tech stock. Without the group aggregation, a raw-return-only model would have ranked the energy stock ahead of the tech stock; the sector-relative feature reverses that ranking, which is often the more economically meaningful comparison for a stock-picking model.
What this means in practice
Group aggregation features are the workhorse behind cross-sectional signals in equity models: sector-neutral returns, industry-relative valuation ratios, exchange-relative bid-ask spreads. They also compress high-cardinality categoricals (sector, exchange) into a small number of numeric summary columns instead of one-hot columns per category. The one danger is computing the group statistic using information that wouldn't have been available at the time — including a stock's own future return in the sector average it's compared against, or computing the aggregate over the full dataset rather than only prior data, both of which leak future information into a feature meant to represent a contemporaneous or historical comparison.
A group aggregation feature compares an individual observation to a summary statistic computed within its own group (sector, day, account), turning group membership and relative standing into a numeric feature without one-hot encoding every group.
Computing the group statistic over the whole sample — including future rows or the row itself when that's not intended — silently leaks information. Aggregate only over the data that was actually knowable at the time the feature would have been used.
Related concepts
Practice in interviews
Further reading
- Kuhn & Johnson, Feature Engineering and Selection, ch. 6