Viyan

Viyan AI

Predicting Stock Returns with LSTMs and XGBoost

Combining LSTM feature extraction with gradient boosting captures both temporal patterns and sharp discontinuities in financial data.

This system uses a two-stage pipeline to predict stock returns by separating feature extraction from regression across a panel of 14 U.S. equities. A stack of two LSTM layers acts as a feature extractor, processing a 60-day sliding window of five sequential market features. The network maps the sequence to a 64-dimensional hidden state vector at the final timestep. You can think of this vector as a compressed coordinate system where each dimension represents a different market phase. Instead of raw price data, these 64 dimensions act as a learned language describing market behaviors such as mean-reversion, regime transitions, or momentum exhaustion. These embeddings are concatenated with 14 hand-crafted technical indicators to form a 78-dimensional vector, which serves as the input for an XGBoost regressor.

Encoding Temporal Dynamics

The LSTM layers utilize weight sharing across the sequence, but the crucial mechanism for capturing dynamics is the recursive update rule. As the LSTM processes each of the 60 steps, it updates its hidden state based on the current input and the previous state. By the final timestep, the hidden state vector acts as a summary of the entire sequence. It discards the specific temporal index of individual events while retaining the trajectory and rate of change of those inputs. This mapping functions as a nonlinear transformation that filters out high-frequency noise. The LSTM is trained independently to produce these embeddings before they are passed to the XGBoost regressor, ensuring that the feature extractor focuses on building a consistent representation of market states rather than chasing final return values.

The Mechanism of Ensemble Regression

XGBoost partitions the input space into hierarchical branches. This allows the model to capture sharp discontinuities that a dense neural layer would otherwise soften through averaging. The LSTM embeddings provide the regressor with high-level summaries of volatility regimes and momentum trends. A tree node splits the 64-dimensional input space, effectively creating a conditional gate. This structure forces the model to treat the same momentum value differently depending on the volatile or stable state represented in the vector. By shifting the complex sequential processing to the LSTM and the threshold-based decision-making to the tree-based regressor, the architecture captures the non-linear dependencies between regime-aware features and target returns. The LSTM handles temporal noise through its smoothing effect while XGBoost handles non-linear if-then discontinuities, preventing the system from over-averaging erratic price signals.

Metric Hybrid LSTM-XGBoost Comparison
Test RMSE (30-day) 0.0949 One-third of baseline RMSE
Directional Accuracy (30-day) 52.1% Near-random chance
Directional Accuracy (365-day) 97.6% Driven by asset base rate

Implementation and Model Generalization

To prevent look-ahead bias, this system requires strictly chronological splitting during training, ensuring that the lookback window for any sample precedes the target return date. The evaluation results reveal a sharp discrepancy in performance across horizons. While the 365-day forecast shows a 97.6% directional accuracy, this figure tracks the high base rate of historical asset appreciation rather than genuine predictive signal. At the 30-day horizon, where market drift is minimized, the directional accuracy drops to 52.1%. The RMSE of 0.0949 represents a threefold reduction compared to the standalone LSTM baseline. This shows that the ensemble regressor effectively gains predictive reach from the regime-aware features extracted by the recurrent layers. A practical example of this utility is a volatility breakout: if the LSTM detects a sudden spike in hidden-state variance, the tree-based model can immediately apply a lower sensitivity multiplier to subsequent momentum inputs, preventing the system from overreacting to false signals that a single monolithic model would likely misinterpret as a sustained trend.

Sources