Finance-specific models for asset pricing, prediction, and portfolio learning.
Documentation: ml4trading.io/docs/models
This library is one of seven interconnected ML4T libraries supporting the research and production workflow described in Machine Learning for Trading.
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
The architecture guide connects these data contracts, model outputs, and downstream handoffs.
pip install ml4t-modelsOptional 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 capabilitiesSupported interpreters for all model families are Python 3.12, 3.13, and 3.14.
Python 3.15 is stable, but this package currently qualifies only its NumPy-based core
workflows. The deep extra enables Torch only below Python 3.15; use Python 3.12-3.14
for neural models.
Documentation tools are contributor dependencies. From a source checkout, run
uv sync --extra docs before building the site.
The base package can produce a first forecast on a small synthetic panel without credentials or
an accelerator. Run this complete example with pip install ml4t-models:
import numpy as np
from ml4t.models import (
BetaLambdaMapper,
ExpandingMeanFactorForecaster,
LatentFactorForecastPipeline,
PCAConfig,
PCAModel,
PersistentPanelBatch,
)
asset_ids = tuple(f"asset_{i}" for i in range(6))
train = PersistentPanelBatch(
returns=np.random.default_rng(1).normal(scale=0.02, size=(12, 6)),
timestamps=tuple(f"2024-{month:02d}" for month in range(1, 13)),
asset_ids=asset_ids,
)
future = PersistentPanelBatch(timestamps=("2025-01", "2025-02"), asset_ids=asset_ids)
pipeline = LatentFactorForecastPipeline(
model=PCAModel(PCAConfig(n_factors=2)),
forecaster=ExpandingMeanFactorForecaster(),
mapper=BetaLambdaMapper(),
)
pipeline.fit(train)
forecast = pipeline.predict(future).asset_forecast.expected_returns
assert forecast.shape == (2, 6) and np.isfinite(forecast).all()
print(forecast.shape) # (2, 6)The forecast uses the training factor history and preserves the future dates and asset order.
It does not imply trading performance. The Quickstart
explains the result, and the Book Guide links to pinned teaching files.
The deep extra is needed for neural models; the integration extra adds Polars and Specs support.
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 |
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.
SAEModel is a supervised autoencoder signal model. In this library it is treated as a direct predictor, not a latent-factor model.
Portfolio models learn allocations directly:
LinearFeaturePortfolioModelas a deterministic baselineLSTMPortfolioModelas a sequence baselineDeepPortfolioModelas a structured DeePM-style allocator
- 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
Install the locked development environment, then run the repository gates before opening a pull request:
uv sync --locked --dev --extra docs
uv run ruff check src/ tests/ examples/ scripts/
uv run ruff format --check src/ tests/ examples/ scripts/
uv run ty check
uv run pytest tests/ -q --cov-report=json:coverage.json
uv run python scripts/ci/check_coverage.py coverage.json
uv run mkdocs build --strict
uv build