Skip to content

AI analysis

AI trading preprocessing: freeze the fitted state

A five-row spread example shows how test data changes a fitted scale. Preserve category meaning, fold-specific state and one replayable transformed row.

Irregular ivory pebbles enter a lime-rimmed ivory aperture beside a row of square tiles, with spare pebbles behind dark glass.
Illustration for ai analysis; examples in this article are educational.

AI trading feature preprocessing leaks future information when a scaler, imputer, encoder or feature selector learns from observations outside the permitted training set. Split eligible rows first, fit each learned transformation inside that training set, and apply its saved state to later observations. Keep the transformation and model together so every historical prediction can be reconstructed without refitting on future data.

A model can train through March while receiving a feature scale calculated through June. Its inputs already contain information about the later distribution. Audit every operation that learns state.

Separate learning a rule from applying it

Converting a known percentage to basis points uses a fixed arithmetic rule. Estimating a feature's mean, selecting a clipping percentile, filling missing values with a median or discovering category frequencies learns something from the supplied rows. Those learned quantities belong to a particular training window.

Scikit-learn's preprocessing leakage guidance explains the fit/transform separation and recommends keeping learned transformations inside a pipeline. A pipeline helps preserve that boundary only when its fitting inputs and evaluation splits are appropriate.

StepState that can cross the boundaryRecord before evaluation
StandardizationFeature means and scalesFitting rows, units and variance convention
Missing-value imputationMedian or other learned replacementReplacement values and missingness policy
Category encodingVocabulary, order and frequency groupingOrdered output columns and unknown-category rule
ClippingEstimated lower and upper cutoffsFitting window and selected percentiles
Feature selectionChosen columns or componentsSelection objective and fitting membership

The same rule applies to an unsupervised transformation. It can learn from future inputs without ever seeing a future return label. A fixed vocabulary declared before evaluation is different from a vocabulary discovered by scanning the complete historical dataset; retain evidence of when that schema was defined.

A five-row normalization audit

Consider an entirely hypothetical spread feature, measured in basis points. Three eligible training observations are 2, 4 and 6. The next two evaluation observations are 8 and 10. Assume all five raw measurements were available at their respective decision times and that the training membership has already passed the label-availability checks.

Standardization here subtracts the training mean and divides by the population standard deviation. When estimating variance v, x ranges over training values:

m = mean(train)
v = mean((x-m)^2)
s = sqrt(v)
z = (x-m) / s

Scikit-learn's StandardScaler reference documents this ddof=0 variance convention and the storage of fitted statistics for later transformation. Other implementations can use different conventions; record the one actually used.

CalculationTraining-only fitContaminated fit using all five rows
Fitting values, basis points2, 4, 62, 4, 6, 8, 10
Mean46
Variance8/38
Standard deviation1.6329932.828427
Transformed evaluation value 82.4494900.707107
Transformed evaluation value 103.6742351.414214

The later observations raise both the mean and the scale. Consequently, the same 8-basis-point spread appears much less unusual under the contaminated transformation. A predeclared illustrative flag at z > 2 would fire for both evaluation rows with the frozen training scale and neither with the contaminated scale. That flag is a teaching device, not a recommended trading threshold.

The table demonstrates changed inputs. Leakage need not improve every prediction, and a clean scaler cannot establish profitability.

As a practical check, append a later synthetic spread of 100 basis points to a copy of the dataset. Reapply the already saved transformation to the original evaluation rows. Their transformed values must remain unchanged. If appending that row changes them, the code is rebuilding state from data outside the frozen fit. This check catches one dependency error; it cannot prove that all original inputs were timely.

Freeze category meaning as well as column count

Suppose a training window contains venue labels A and B, with output columns explicitly ordered as [A, B]. A later observation arrives from venue C. Refitting the encoder on that evaluation batch creates a different representation, potentially adding columns or changing their meaning.

Choose the unknown-category behavior before evaluation. The OneHotEncoder documentation specifies that handle_unknown='error' raises an error, while handle_unknown='ignore' produces zeros for that feature's category columns. Under the latter policy, this example's C becomes [0, 0]; it does not become a newly learned third column.

An all-zero vector is an encoding outcome, not evidence that an unfamiliar venue behaves like known venues. Decide separately whether an unknown category permits prediction, requires a warning or blocks the downstream decision. If a reference category is dropped, the all-zero representation can also describe that reference category. Preserve the drop setting and distinguish unknownness in an additional validated field when the decision needs it.

Frequency-based grouping also learns state. Determining which venues are rare from the complete dataset can reveal future participation. Save the fitted group membership rather than recalculating it when producing each evaluation report.

Refit inside each permitted training window

Every historical refit needs its own preprocessing state. Reusing a scaler fitted for the final fold contaminates earlier folds, even if each fold trains a fresh estimator. Conversely, fitting a separate scaler to each test batch changes the coordinate system expected by the frozen model.

First establish eligible training membership. The purged-label and embargo worksheet handles overlapping outcome intervals; this article addresses transformations fitted after that membership is determined. Fit the imputer, scaler, encoder and model using the permitted rows, then retain the resulting bundle for that fold's evaluation.

For a past-only deployment reconstruction, the walk-forward testing procedure adds historical model-freeze dates and label receipt times. A trailing feature may legitimately use earlier observations. Its rolling calculation must still avoid later rows, centered windows and backward filling from future values.

An adaptive online scaler is a separate declared procedure. One reproducible policy predicts with the current saved state, records the prediction, and then updates the state from newly permitted inputs. Replay the same ordering during evaluation. Retrospectively replacing earlier transformed values with statistics learned later would describe a different procedure.

Store a transformation receipt with the model

Keep a receipt that identifies the exact bundle behind each decision:

FieldQuestion it must answer
Training membershipWhich exact row IDs were permitted to influence the fit?
Feature contractWhat are the names, order, units, types and availability rules?
Learned stateWhich means, scales, replacements, vocabularies and cutoffs were stored?
Fit identityWhich code, configuration, data snapshot and library versions produced it?
PairingWhich model artifact and preprocessing artifact must load together?
Replay sampleDoes one saved raw row reproduce its recorded transformed vector?

Scikit-learn's model persistence guidance recommends retaining training-data references, source code and dependency versions for reproducibility. Loading an artifact under a different library version is not a supported compatibility guarantee.

Keep evidence freshness separate from artifact identity. The retrieval cache freshness guide addresses whether retrieved material remains usable for a decision. Refreshing that material must not silently replace the fitted transformation associated with an earlier prediction. Record both the input snapshot and the transformation version.

If a feature stage calls an external embedding or research service, its invocations also need declared accounting. The AI request cost-budget guide addresses request admission and retry costs. Passing a budget check cannot establish that the resulting features respected the historical fit boundary.

Handle failed transformations explicitly

A constant training feature has zero standard deviation, so the displayed division is undefined. StandardScaler uses a scale factor of 1 for zero variance; with centering enabled, an unchanged constant becomes zero. Specify that convention or a declared feature-removal policy rather than silently dividing by zero. A newly varying evaluation feature may then be far outside its training behavior.

An all-missing training column presents a different failure: a usable median cannot be estimated from it. Predeclare whether the pipeline rejects the fit, removes the column under a stable schema or applies a fixed fallback. Inspect the estimator's missing-data support too; a successful scaling call is not proof that prediction can proceed.

Finally, run the receipt's replay sample after loading the bundle. A changed column order, unit conversion or missing encoder can invalidate a prediction even when the artifact loads successfully. Trace questionable raw fields with the AI input provenance guide before interpreting the model's explanation.

Method and limits

This AI-assisted educational article uses the linked primary software documentation, an original five-row calculation and a proposed state receipt. The spread values, venue labels and alert threshold are hypothetical. The means, population variances, transformed values and threshold outcomes were recomputed. No market dataset, trained trading model, KLINEO preprocessing feature or live order was tested. The protocol makes fit boundaries inspectable; it does not establish predictive accuracy or future returns.

Keep the context with the example.

Each worked example is illustrative, with its sources and assumptions in the article. Read our editorial standards for authorship, verification and corrections.