Class ChronosFoundationModel<T>
- Namespace
- AiDotNet.TimeSeries
- Assembly
- AiDotNet.dll
Implements the Chronos foundation model for zero-shot time series forecasting.
public class ChronosFoundationModel<T> : TimeSeriesModelBase<T>, ITimeSeriesModel<T>, IFullModel<T, Matrix<T>, Vector<T>>, IModel<Matrix<T>, Vector<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Matrix<T>, Vector<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Matrix<T>, Vector<T>>>, IGradientComputable<T, Matrix<T>, Vector<T>>, IJitCompilable<T>
Type Parameters
TThe numeric type used for calculations (e.g., float, double).
- Inheritance
-
ChronosFoundationModel<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
What is a Foundation Model? A foundation model is a large neural network pretrained on vast amounts of data that can be applied to new tasks without task-specific training (zero-shot) or with minimal fine-tuning. GPT-3/4 are foundation models for text; Chronos is a foundation model for time series.
The Chronos Approach: Chronos (Ansari et al., 2024) treats time series forecasting as a language modeling task. The key insight is that if we can tokenize continuous time series values into discrete tokens, we can apply the same powerful transformer architectures that work so well for text.
Mean-Scaling Tokenization: Before tokenization, values are normalized by the mean absolute value of the context: x_normalized = x / (mean(|context|) + epsilon) This makes the model scale-invariant - it can handle time series of any magnitude. Normalized values are then mapped to discrete tokens using a fixed vocabulary of uniformly-spaced bins covering a reasonable range (e.g., -15 to 15).
Causal Transformer Architecture: Chronos uses a decoder-only transformer (like GPT) with causal masking. Each position can only attend to itself and previous positions, enabling autoregressive generation. The architecture includes: - Token embeddings mapping discrete tokens to dense vectors - Sinusoidal positional encoding for temporal awareness - Multiple transformer layers with multi-head causal self-attention - Layer normalization and feed-forward networks - Output projection to vocabulary logits
Zero-Shot Forecasting: Once pretrained on diverse time series data (synthetic and real), Chronos can forecast new time series it has never seen. The model learns general patterns of temporal dynamics that transfer across domains - seasonality, trends, noise patterns, etc.
For Beginners: Imagine you've read thousands of different books about weather, stock prices, store sales, and website traffic. After reading all these, you develop an intuition for how numbers change over time. When someone shows you a new sequence of numbers you've never seen, you can make educated guesses about what comes next.
Chronos does exactly this but with neural networks. It "reads" millions of time series during training and learns patterns. Then it can forecast new time series without being specifically trained on that type of data. This is incredibly powerful for real-world applications where you might not have enough historical data to train a specialized model.
Constructors
ChronosFoundationModel(ChronosOptions<T>?)
Initializes a new instance of the Chronos foundation model.
public ChronosFoundationModel(ChronosOptions<T>? options = null)
Parameters
optionsChronosOptions<T>
Properties
ParameterCount
Gets the number of parameters in the model.
public override int ParameterCount { get; }
Property Value
Remarks
This property returns the total count of trainable parameters in the model. It's useful for understanding model complexity and memory requirements.
Methods
CreateInstance()
Creates a new instance of the derived model class.
protected override IFullModel<T, Matrix<T>, Vector<T>> CreateInstance()
Returns
- IFullModel<T, Matrix<T>, Vector<T>>
A new instance of the same model type.
Remarks
This abstract factory method must be implemented by derived classes to create a new instance of their specific type. It's used by Clone and DeepCopy to ensure that the correct derived type is instantiated.
For Beginners: This method creates a new, empty instance of the specific model type. It's used during cloning and deep copying to ensure that the copy is of the same specific type as the original.
For example, if the original model is an ARIMA model, this method would create a new ARIMA model. If it's a TBATS model, it would create a new TBATS model.
DeserializeCore(BinaryReader)
Deserializes model-specific data from the binary reader.
protected override void DeserializeCore(BinaryReader reader)
Parameters
readerBinaryReaderThe binary reader to read from.
Remarks
This abstract method must be implemented by each specific model type to load its unique parameters and state.
For Beginners: This method is responsible for loading the specific details that make each type of time series model unique. It reads exactly what was written by SerializeCore, in the same order, reconstructing the specialized parts of the model.
It's the counterpart to SerializeCore and should read data in exactly the same order and format that it was written.
This separation allows the base class to handle common deserialization tasks while each model type handles its specialized data.
ForecastWithQuantiles(Vector<T>, double[], int)
Generates probabilistic forecasts by sampling from the model.
public Dictionary<double, Vector<T>> ForecastWithQuantiles(Vector<T> history, double[] quantiles, int numSamples = 100)
Parameters
Returns
- Dictionary<double, Vector<T>>
GetModelMetadata()
Gets metadata about the time series model.
public override ModelMetadata<T> GetModelMetadata()
Returns
- ModelMetadata<T>
A ModelMetaData object containing information about the model.
Remarks
This method provides comprehensive metadata about the model, including its type, configuration options, training status, evaluation metrics, and information about which features/lags are most important.
For Beginners: This method provides important information about the model that can help you understand its characteristics and performance.
The metadata includes:
- The type of model (e.g., ARIMA, TBATS, Neural Network)
- Configuration details (e.g., lag order, seasonality period)
- Whether the model has been trained
- Performance metrics from the last evaluation
- Information about which features (time periods) are most influential
This information is useful for documentation, model comparison, and debugging. It's like a complete summary of everything important about the model.
PredictSingle(Vector<T>)
Predicts the next value in a time series.
public override T PredictSingle(Vector<T> input)
Parameters
inputVector<T>
Returns
- T
SerializeCore(BinaryWriter)
Serializes model-specific data to the binary writer.
protected override void SerializeCore(BinaryWriter writer)
Parameters
writerBinaryWriterThe binary writer to write to.
Remarks
This abstract method must be implemented by each specific model type to save its unique parameters and state.
For Beginners: This method is responsible for saving the specific details that make each type of time series model unique. Different models have different internal structures and parameters that need to be saved separately from the common elements.
For example:
- An ARIMA model would save its AR, I, and MA coefficients
- A TBATS model would save its level, trend, and seasonal components
- A neural network model would save its weights and biases
This separation allows the base class to handle common serialization tasks while each model type handles its specialized data.
TrainCore(Matrix<T>, Vector<T>)
Trains the Chronos model using proper backpropagation through all parameters.
protected override void TrainCore(Matrix<T> x, Vector<T> y)
Parameters
xMatrix<T>yVector<T>