liveBook · Manning

Overfitting

As explained in Financial AI in Practice

Overfitting occurs when a model captures patterns specific to its training data that do not generalize reliably to new data.

In market prediction, overfitting happens when a model memorizes historical sequences instead of learning patterns that generalize. High accuracy on historical data can therefore exaggerate what the model will achieve on new data. section 10.5.1

Treat impressive accuracy claims cautiously: look-ahead bias, cherry-picked periods, and class imbalance can inflate results independently of a model’s predictive value. Define the model’s intended operating environment and compare performance with a meaningful baseline. section 10.5.1

For a random forest trained on noisy financial data, constrain complexity to reduce the chance of fitting noise:

  • Limit tree depth and require more samples per leaf and split to discourage narrow, noise-driven rules. section 10.5.5
  • Restrict feature sampling to encourage model diversity. section 10.5.5
  • Evaluate predictions on out-of-sample time-series folds; regard the result as predictive evidence, not proof of trading profitability. section 10.5.5
Listing 10.9 Final model training with conservative parameters
from sklearn.ensemble import RandomForestClassifier
print("=== FINAL MODEL TRAINING ===")

# Parameters selected through grid search (code omitted for space constraints)
best_params = {
    'n_estimators': 150,        #1
    'max_depth': 6,              #2
    'min_samples_leaf': 20,     #3
    'max_features': 0.2,           #4
    'min_samples_split': 30      #5
}

print(f"Selected parameters: {best_params}")

# Collect predictions across all time-series folds
all_predictions = []
all_actuals = []

for fold, split in enumerate(splits):
    X_train = features.iloc[split['train_idx']]
    X_test = features.iloc[split['test_idx']]
    y_train = target.iloc[split['train_idx']]
    y_test = target.iloc[split['test_idx']]

    # Create fresh model for each fold to ensure independence
    model = RandomForestClassifier(     #6
        **best_params,
        random_state=42 + fold,    #7
        class_weight='balanced',
        n_jobs=-1
    )

    model.fit(X_train, y_train)   #8
    predictions = model.predict(X_test)

    all_predictions.extend(predictions)   #9
    all_actuals.extend(y_test)

    print(f"Fold {fold+1}: {len(predictions)} predictions generated")

print(f"\nTotal predictions collected: {len(all_predictions)}")

The final random-forest training example shows conservative parameter settings and predictions collected across time-series folds. section 10.5.5

A modest signal still needs scrutiny

A final market-direction model reports 52.2% directional accuracy on qualifying predictions. The result suggests a modest edge, but does not account for transaction costs, slippage, or capital constraints. section 10.5.5

How Financial AI in Practice covers it

How deep it goes
Explains historical memorization and random-forest safeguards, then evaluates out-of-sample predictions without covering full trading economics.
Examples
Contrasts memorized historical market sequences with a conservatively configured random forest reporting 52.2% directional accuracy.
What you can do after
Apply it via scikit-learn by limiting random-forest depth, sample thresholds, and feature sampling, then evaluating out of sample.
How it connects
Connects overfitting with random forests, hyperparameter tuning, time-series validation, and out-of-sample evaluation.

Also explained in

Compare how 24 books explain overfitting →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Financial AI in Practice. The section links point to the parts of the book it draws on.