FactorVAE<T>
Models & Types · AiDotNet.Finance.Trading.Factors
Variational autoencoder for learning disentangled financial factors.
Inheritance: NeuralNetworkBase<T> → FinancialModelBase<T> → FactorVAE<T>
Implements: IFactorModel<T>
For Beginners
the model explains a group of stocks with a few hidden “factors”. While
training it is allowed to look at what actually happened, to work out what the factors must have
been. At prediction time the future is unknown, so it has to infer them from observable data alone —
and the KL term is what forces those two routes to agree. Returns are then rebuilt as
alpha + beta * factors: alpha is a stock’s own baseline return and beta its sensitivity to
each factor.
How It Works
FactorVAE integrates a dynamic factor model with a variational autoencoder, and learns through PRIOR-POSTERIOR training: a posterior factor distribution inferred with the benefit of future returns guides a prior that must work without them.
Reference: Duan, Wang, Zhang & Li (2022). “FactorVAE: A Probabilistic Dynamic Factor Model Based on Variational Autoencoder for Predicting Cross-Sectional Stock Returns”, AAAI 36(4):4468-4476.
Note this is NOT Kim & Mnih’s “Disentangling by Factorising”, a different paper that shares the
FactorVAE name and uses a discriminator to penalize total correlation. This class previously
documented and partly implemented that one instead; Gamma is the
surviving knob from it, kept distinct from
KlWeight.
Example
// Define architecture for disentangled factor learning via VAE (50 assets, 10 features, 5 latent factors)
var architecture = new NeuralNetworkArchitecture<double>(
inputType: InputType.OneDimensional,
taskType: NeuralNetworkTaskType.Regression,
inputHeight: 60, inputWidth: 10, inputDepth: 1, outputSize: 5);
// Training mode: VAE learns independent latent factors with disentanglement penalty
var model = new FactorVAE<double>(architecture);
// ONNX inference mode: load pre-trained FactorVAE model
var onnxModel = new FactorVAE<double>(architecture, "factor_vae.onnx");
Constructors
| Constructor | Summary |
|---|---|
FactorVAE(NeuralNetworkArchitecture<T> architecture, FactorVAEOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) |
Initializes a new FactorVAE in native mode for training and inference. |
FactorVAE(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, FactorVAEOptions<T> options, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>> optimizer, ILossFunction<T> lossFunction) |
Initializes a new FactorVAE in ONNX mode for inference. |
Properties
| Property | Summary |
|---|---|
bool SupportsTraining { get; } |
Gets whether training is supported in the current mode. |
bool UseNativeMode { get; } |
Gets whether the model is using native layers (true) or ONNX inference (false). |
int LatentDimension { get; } |
Gets the dimension of the latent space. |
int NumAssets { get; } |
Gets the number of assets covered by the model. |
int NumFactors { get; } |
Gets the number of latent factors learned by the model. |
int NumFeatures { get; } |
Gets the number of input features. |
int PredictionHorizon { get; } |
Gets the prediction horizon. |
int SequenceLength { get; } |
Gets the input sequence length. |
Methods
| Method | Summary |
|---|---|
Dictionary<string, T> GetFactorMetrics() |
Gets factor model metrics. |
Dictionary<string, T> GetFinancialMetrics() |
Gets financial metrics for the model. |
ModelInputShapeConstraint GetInputShapeConstraint() |
Declares the cross-sectional geometry a factor model accepts. |
ModelMetadata<T> GetModelMetadata() |
Gets metadata describing this model instance. |
Tensor<T> ComputeAlpha(Tensor<T> returns, Tensor<T> factorReturns) |
Computes alpha (excess return) for each asset. |
Tensor<T> ExtractFactors(Tensor<T> returns) |
Extracts latent factors from asset returns. |
Tensor<T> ForwardForTraining(Tensor<T> input) |
|
Tensor<T> GetFactorCovariance(Tensor<T> returns) |
Computes the factor covariance matrix. |
Tensor<T> GetFactorLoadings(Tensor<T> returns) |
Computes factor loadings for each asset. |
Tensor<T> PredictReturns(Tensor<T> factorExposures) |
Predicts expected returns from factor exposures. |