ml4t-models
Finance-native model implementations for latent-factor estimation, stochastic discount factor learning, direct asset prediction, and end-to-end portfolio learning.
Documentation: https://ml4trading.io/docs/models/
Part of the ML4T Library Ecosystem
This library is one of seven interconnected ML4T libraries supporting the research and production workflow described in Machine Learning for Trading.
What This Library Does
ml4t-models packages paper-faithful model families that are common in modern empirical asset pricing and portfolio learning:
- Latent-factor estimators with explicit structural outputs:
PCAModelRPPCAModelIPCAModelCAEModel
- Weight-native stochastic discount factor modeling:
StochasticDiscountFactorModel
- Direct asset prediction:
SAEModel(SAE= supervised autoencoder)
- End-to-end portfolio learning:
LinearFeaturePortfolioModelLSTMPortfolioModelDeepPortfolioModel
The library is built around finance-native contracts rather than generic tensor trainers:
PersistentPanelBatchfor stable-ID panelsCrossSectionBatchfor ragged dated cross-sectionsPortfolioSequenceBatchfor sequence-to-allocation models
It also keeps the predictive steps explicit:
- structural extraction
- factor-premium forecasting
- asset mapping
- downstream prediction and weight frames for
ml4t-backtestandml4t-diagnostic
Installation
pip install ml4t-models
Optional extras:
pip install ml4t-models[deep] # torch-backed neural models
pip install ml4t-models[integration] # polars + ml4t-specs bridges
pip install ml4t-models[all] # all runtime capabilities
Supported stable interpreters are Python 3.12, 3.13, and 3.14. Python 3.15 prereleases run a separate compatibility gate and are not yet part of the stable support range.
Documentation tools are contributor dependencies. From a source checkout, run
uv sync --extra docs before building the site.
Quick Start
1. Latent-Factor Forecast Pipeline
import numpy as np
from ml4t.models import (
BetaLambdaMapper,
CrossSectionBatch,
ExpandingMeanFactorForecaster,
IPCAConfig,
IPCAModel,
LatentFactorForecastPipeline,
)
batch = CrossSectionBatch(
characteristics=np.random.randn(24, 200, 12),
returns=np.random.randn(24, 200),
timestamps=tuple(range(24)),
)
pipeline = LatentFactorForecastPipeline(
model=IPCAModel(IPCAConfig(n_factors=3)),
forecaster=ExpandingMeanFactorForecaster(),
mapper=BetaLambdaMapper(),
)
pipeline.fit(batch)
prediction = pipeline.predict(batch)
print(prediction.asset_forecast.expected_returns.shape)
# (24, 200)
2. Weight-Native Stochastic Discount Factor
import numpy as np
from ml4t.models import (
CrossSectionBatch,
StochasticDiscountFactorConfig,
StochasticDiscountFactorModel,
)
batch = CrossSectionBatch(
characteristics=np.random.randn(36, 300, 16),
returns=np.random.randn(36, 300),
context_features=np.random.randn(36, 8),
timestamps=tuple(range(36)),
)
model = StochasticDiscountFactorModel(
StochasticDiscountFactorConfig(checkpoint_epochs=(256, 512, 768, 1024))
)
model.fit(batch)
state = model.extract(batch, checkpoint=1280)
print(state.asset_weights.shape)
# (36, 300)
3. End-to-End Portfolio Learning
import numpy as np
from ml4t.models import LSTMPortfolioConfig, LSTMPortfolioModel, PortfolioSequenceBatch
batch = PortfolioSequenceBatch(
features=np.random.randn(8, 63, 20, 10),
returns=np.random.randn(8, 63, 20),
timestamps=tuple(range(63)),
asset_ids=tuple(f"asset_{i}" for i in range(20)),
)
model = LSTMPortfolioModel(LSTMPortfolioConfig(max_iters=20, checkpoint_every=5))
model.fit(batch)
weights = model.predict(batch, checkpoint=20)
print(weights.weights.shape)
# (8, 63, 20)
4. Hand Off Predictions To The Rest Of ML4T
from ml4t.models import predictions_frame_from_asset_forecast, write_backtest_frames
frame = predictions_frame_from_asset_forecast(prediction.asset_forecast)
write_backtest_frames("artifacts/run_001", predictions=frame)
Model Families
Latent Factors
These models estimate a structural representation first, then let a separate forecaster produce ex ante factor premia.
| Model | Contract | Native output | Predictive step |
|---|---|---|---|
PCAModel |
PersistentPanelBatch |
static loadings, factor returns | factor-premium forecaster + mapper |
RPPCAModel |
PersistentPanelBatch |
risk-premium-aware latent factors | factor-premium forecaster + mapper |
IPCAModel |
CrossSectionBatch |
characteristic-implied betas, factor history | factor-premium forecaster + mapper |
CAEModel |
CrossSectionBatch |
nonlinear characteristic betas, factor history | factor-premium forecaster + mapper |
Stochastic Discount Factor
StochasticDiscountFactorModel is not a beta × lambda latent-factor model. It learns a weight-native no-arbitrage object and exposes:
- asset weights
- SDF series
- checkpointed phase-aware training state
Optional return projections are handled by separate mappers.
Direct Asset Prediction
SAEModel is a supervised autoencoder signal model. In this library it is treated as a direct predictor, not a latent-factor model.
Portfolio Learning
Portfolio models learn allocations directly:
LinearFeaturePortfolioModelas a deterministic baselineLSTMPortfolioModelas a sequence baselineDeepPortfolioModelas a structured DeePM-style allocator
Design Principles
- Finance-native data contracts rather than generic dataloaders
- Explicit structural and predictive stages
- Checkpoint-aware neural training
- Clear separation between:
- model estimation
- forecasting
- backtest and diagnostic integration
- Integration boundaries with sibling libraries instead of duplicated evaluation logic
Documentation
Release files for ml4t-models 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ml4t_models-0.1.3.tar.gz | 13.0 MB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ml4t_models-0.1.3-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 13.1 MB
Release files / ml4t_models-0.1.3.tar.gz
| Download URL | ml4t_models-0.1.3.tar.gz |
|---|---|
| Size | 13.0 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
0ca2fe6f7cbb90170ebc7d1f5c492fb5e37eecb23563f4945745bb8bdf53802f
|
|
BLAKE2b-256 checksum How to use checksums |
f5c813fb2123519ed72808f8b99454b9d3d372acfde407cfb33fb19cf7eba54d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.
Transparency logRelease files / ml4t_models-0.1.3-py3-none-any.whl
| Download URL | ml4t_models-0.1.3-py3-none-any.whl |
|---|---|
| Size | 103.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ba7a5b89bc9de5a81832091c9fd35f520fd98fcf32a7df95fe825e6fd688da3b
|
|
BLAKE2b-256 checksum How to use checksums |
b14f52b69c3ac9e4128fd887b83f8dd2e68dca9650cb8a919768351ac0b006c9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Sep 19, 2026.
Transparency log