This project implements a conditional Denoising Diffusion Implicit Model (DDIM) for the complex task of financial time series forecasting. Traditional forecasting models often struggle to capture the stochastic and non-linear nature of financial markets. This project moves beyond standard approaches by leveraging a generative model that can produce a distribution of plausible future price sequences, conditioned on specific contextual information.
The core innovation lies in its conditional architecture. The model's predictions are not generated in a vacuum; they are guided by:
The DDIM is a type of generative model that learns to create data by reversing a gradual noising process. This is broken into two key phases.
In the forward process, we take a clean data sample (a future sequence of prices) and systematically add Gaussian noise over a series of discrete time steps. By the end of this process, the original structured data is transformed into pure, random noise. The ForwardProcess class manages this, pre-calculating a noise schedule (betas, alphas) to control the noise level at each step. The model learns the underlying data distribution by being trained to reverse this very process.
The reverse process is where prediction occurs. We start with random noise and iteratively denoise it to generate a clean, structured data sample. The DDIM is "implicit" and deterministic, meaning it follows a direct path from noise to a clean signal, making it much faster than traditional Denoising Diffusion Probabilistic Models (DDPMs).
The ddim_reverse_step function orchestrates this. At each step, the trained model predicts the noise present in the current sample, given the context (historical data, ticker ID). The DDIM formula then uses this noise prediction to calculate the slightly-less-noisy sample for the previous step, progressively refining the output until a clean prediction is formed.
The heart of the denoising process is a Transformer-based model (TransformerModel). Its sole purpose is not to predict the future directly, but to predict the noise that was added to a corrupted future sample.
The model takes as input:
t of the diffusion process.The model operates on a simplified, multi-feature representation of the market at each time step. The input features include:
To manage potentially terabytes of financial data, the project uses a pre-processing step (prepare_dataset.py) to convert raw CSV files into a single, compressed HDF5 file. The HDF5Dataset class then allows the PyTorch DataLoader to read batches directly from disk, keeping memory usage minimal and enabling fast, parallel data loading.
The training script (train.py) orchestrates the learning process. In each step, it takes a batch of data, adds a random amount of noise to the target sequence, and tasks the Transformer model with predicting that noise. The loss is calculated as the Mean Squared Error between the true noise and the predicted noise, with an additional smoothness penalty to encourage more realistic price trajectories.
The prediction script (predict.py) uses the trained model to generate new price sequences. It takes a historical context, feeds it to the reverse diffusion process, and generates multiple sample predictions to form a probabilistic forecast.
The model's performance is assessed in evaluate_model.py using metrics suited for probabilistic forecasts:
Figure 1: Sample prediction for the SPY ticker. The model takes the last 30 minutes of price data (blue line) as context and generates a distribution of 100 possible future price paths (light gray lines) for the next 30 minutes.
Key observations from this example include:
This project successfully demonstrates the application of a conditional Denoising Diffusion Implicit Model to the challenging domain of financial forecasting. By combining a powerful Transformer architecture with a principled, generative framework, the model captures the complex dynamics of financial time series.
The key strengths of this approach are its probabilistic nature, providing a distribution of outcomes rather than a single point forecast, and its conditional design, which allows for more nuanced and asset-specific predictions. The use of an efficient HDF5 data pipeline makes the system scalable and robust for large-scale financial modeling.