Stockformer<T>
Models & Types · AiDotNet.Finance.Trading.Factors
Stockformer: price-volume factor stock selection using a wavelet band split and a dual-frequency spatiotemporal encoder, trained on returns and direction simultaneously.
Inheritance: NeuralNetworkBase<T> → FinancialModelBase<T> → CrossSectionalGraphModelBase<T> → Stockformer<T>
For Beginners
This ranks stocks. It splits each stock’s history into a slow trend and fast wiggles, studies each with machinery suited to it, lets stocks influence one another through a similarity graph, and then predicts both how much a stock will move and which way — learning both at once, which the paper shows works better than either alone.
How It Works
Bohan Ma, Yushan Xue, Yuan Lu and Jing Chen, “Stockformer: A price-volume factor stock selection model based on wavelet transform and multi-task self-attention networks” (arXiv:2401.06139; Expert Systems with Applications 273:126803, 2025). Reference implementation: github.com/Eric991005/Multitask-Stockformer.
REPLACES FactorTransformer. That class cited arXiv:2206.06516 as “FactorFormer: Factor-guided Transformer for Stock Return Prediction”. That identifier is “Gauss-Bonnet black holes in (2+1) dimensions: Perturbative aspects and entropy features”, a general-relativity paper in Physical Review D — the citation was fabricated, and the implementation was a plain transformer with none of the four contributions below.
The four contributions, and where each lives:
Example
var model = new Stockformer<double>(new StockformerOptions<double>
{
NumAssets = 50,
NumFeatures = 16,
HiddenDimension = 32,
SequenceLength = 20,
});
// Rows are stocks, columns are timesteps of the return series.
var returns = new Matrix<double>(50, 20);
var prediction = model.PredictBands(returns);
Console.WriteLine(prediction.Returns.Length); // one predicted return per stock
Console.WriteLine(prediction.DirectionLogits.Length); // stocks x direction classes
Constructors
| Constructor | Summary |
|---|---|
Stockformer(StockformerOptions<T> options) |
Creates a Stockformer. |
Properties
| Property | Summary |
|---|---|
IReadOnlyList<string> TaskNames { get; } |
|
StockformerBands<T> Bands { get; } |
Gets the band splitter. |
StockformerOptions<T> Configuration { get; } |
Gets the Stockformer configuration in use. |
int TaskCount { get; } |
Methods
| Method | Summary |
|---|---|
IReadOnlyList<Tensor<T>> PredictAllTasks(Tensor<T> input) |
|
ModelMetadata<T> GetModelMetadata() |
Gets metadata describing this model. |
Prediction<T> PredictBands(Matrix<T> perStockReturns) |
Runs the full pipeline on a [assets, time, features] factor tensor. |
Prediction<T> PredictBands(Tensor<T> features) |
Runs the full pipeline on a [assets, time, features] factor tensor. |
ValueTuple<T, T, T> ComputeLoss(Matrix<T> perStockReturns, Vector<T> returnTarget, Vector<T> directionTarget) |
The paper’s training objective for one cross-section, computed as a DIAGNOSTIC. |