PerVintageActualTransformer¶
yohou.compose.PerVintageActualTransformer
¶
Bases: BaseForecastTransformer
Apply a single-axis transformer to each vintage of an X_forecast frame.
Wraps a BaseActualTransformer and applies it independently to every
vintage: the frame is grouped by "vintage_time", and each group's
single-axis ["time", ...] slice is fitted and transformed on its own,
then re-stacked with "vintage_time" restored. Because a vintage is fitted
using only its own rows, a value-dependent inner self-references per vintage:
a scaler standardizes each vintage to that vintage's own scale, a mean
imputer fills each vintage's gaps from that vintage's own values. Order
dependent transforms (cumulative sums, differences within the horizon) also
stay contained.
The transform is leakage-free: a vintage's output depends only on its own
rows, all of which are known at that vintage's vintage_time. This is
distinct from normalizing X_forecast by the training distribution, which
is done with a scaler inside the estimator pipeline instead.
fit does not fit the inner for the values it will produce. It fits a
single representative clone on one vintage only to expose output feature
names and to measure observation_horizon, both value-independent; the
per-vintage fits happen in transform.
The wrapped transformer may be stateful. A vintage is internally
contiguous (its forecast steps at the series interval), so a lag or a
difference is well defined within one, and a fresh clone per vintage means
its history never reaches across a vintage boundary. Only cross-vintage
memory is impossible, which is why the composite itself stays stateless and
exposes no observe/rewind: it carries no buffer between calls
whatever the inner does inside one vintage.
A stateful inner consumes observation_horizon rows from the start of
every vintage, which are its nearest-term forecast steps. If it consumes the
whole vintage the composite raises, since that is a configuration error. Note
that a lifted lag means "the forecast for step h-1 issued at the same
vintage_time", a trajectory ramp; it is not the previous vintage's
forecast for the same time, which this wrapper does not provide.
A vintage with fewer than two rows cannot be fitted on its own and has no per-vintage statistic to compute. Such vintages, typically the truncated tail of a forecast frame, are dropped from the output with a warning rather than transformed with borrowed parameters.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
transformer
|
BaseActualTransformer
|
The single-axis transformer to apply per vintage. May be stateful, in which case it must leave at least one row per vintage. |
required |
Attributes ¶
| Name | Type | Description |
|---|---|---|
transformer_ |
BaseActualTransformer
|
A representative clone fitted on the first vintage, used only for
|
feature_names_in_ |
list[str]
|
Feature (non-index) column names seen during |
Examples ¶
>>> import polars as pl
>>> from datetime import datetime
>>> from yohou.preprocessing import FunctionTransformer
>>> from yohou.compose import PerVintageActualTransformer
>>>
>>> X_forecast = pl.DataFrame({
... "vintage_time": [datetime(2020, 1, 1)] * 2 + [datetime(2020, 1, 2)] * 2,
... "time": [
... datetime(2020, 1, 2),
... datetime(2020, 1, 3),
... datetime(2020, 1, 3),
... datetime(2020, 1, 4),
... ],
... "load": [100.0, 110.0, 120.0, 130.0],
... "wind": [10.0, 20.0, 15.0, 25.0],
... })
>>> def net_load(df):
... return df.select((pl.col("load") - pl.col("wind")).alias("net_load"))
>>> tx = PerVintageActualTransformer(
... FunctionTransformer(func=net_load, feature_names_out=lambda self, names: ["net_load"])
... )
>>> out = tx.fit_transform(X_forecast)
>>> out.columns
['vintage_time', 'time', 'net_load']
>>> out["net_load"].to_list()
[90.0, 90.0, 105.0, 105.0]
See Also ¶
BaseForecastTransformer: Base class for X_forecast transformers.BaseActualTransformer: Base class for single-axis transformers.
Source Code ¶
Source code in src/yohou/compose/per_vintage.py
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 | |
Methods ¶
min_vintage_rows
property
¶
Get the smallest vintage length that survives both of this wrapper's constraints.
The wrapper enforces two independent constraints on vintage length, and neither implies the other:
- A vintage shorter than
_MIN_VINTAGE_ROWScannot be fitted on its own rows (two timestamps are the minimum to infer a series interval) and is dropped with a warning. - A vintage no longer than the inner's
observation_horizonis consumed entirely, leaving no rows, and raises.
So the smallest surviving vintage is max(_MIN_VINTAGE_ROWS, k + 1)
where k is the inner's horizon, and this reports the stricter of the
two. Reporting only the horizon term would let a stateless inner (k =
0) pass a caller's check at a vintage length of one, and then have that
single-row vintage silently dropped by the first constraint.
Returns ¶
| Type | Description |
|---|---|
int
|
Smallest vintage length yielding non-empty output, at least |
Raises ¶
| Type | Description |
|---|---|
NotFittedError
|
If the transformer has not been fitted yet, consistent with the
inner's own |
get_feature_names_out(input_features=None)
¶
Return the wrapped transformer's output feature names.
Delegates to the representative clone, which was fitted on one vintage,
while transform produces values from a separate clone per vintage.
This relies on output names being value-independent: they follow from the
wrapped transformer's parameters and the frame schema, which every
vintage shares, not from any vintage's data. A wrapped transformer whose
feature_names_out varied with the values would break that invariant
and is not supported.
Parameters ¶
| Name | Type | Description | Default |
|---|---|---|---|
input_features
|
list of str or None
|
Ignored; the fitted wrapped transformer determines the output names. |
None
|
Returns ¶
| Type | Description |
|---|---|
list of str
|
Output feature names, delegated to the wrapped transformer. |
Source Code ¶
Source code in src/yohou/compose/per_vintage.py
Tutorials¶
The following example notebooks use this component:
-
How to Transform Features on the Forecast Channel
Lift transformers onto the vintage axis with PerVintageActualTransformer, including stateful ones such as lags, compose them with FeatureUnion, and feed the result to a forecaster's X_forecast channel.