Chronos Time Series Model Tutorial: A Comprehensive Guide
Time series forecasting plays a pivotal role in informed decision-making across a multitude of industries, including retail, energy, finance, and healthcare. The ability to accurately predict future trends from historical data empowers organizations to optimize their operations, allocate resources with greater efficiency, and proactively anticipate market dynamics. Traditionally, the development of precise machine-learning-based forecasting models has been a complex and time-consuming endeavor, often necessitating substantial dataset-specific tuning and extensive model customization.
Chronos emerges as a solution to these challenges, offering a family of pre-trained time series models grounded in language model architectures. This innovative approach treats time series data as a language, enabling the utilization of transformer architectures for modeling. The result is a simplified forecasting process that facilitates rapid and accurate predictions without the need for extensive customization.
This article will guide you through the creation of a tool that harnesses the zero-shot capabilities of Chronos for effective time series data forecasting. Leveraging Rio, an open-source Python full-stack framework, we will construct a feature-rich tool encompassing:
- A navigation bar providing seamless access to the home page and other sections.
- File upload functionality, enabling the selection of a time series column and the forecasting of future values.
- Interactive hyperparameter tuning for the Chronos model.
- Visualization of forecasted time series data in comparison to actual values.
- Export options for saving the forecasted time series as a CSV file.
Let's embark on the journey of building a powerful and user-friendly time series forecasting tool with Chronos.
Understanding Chronos
Time series forecasting is undeniably vital for decision-making in various sectors, including retail, energy, finance, and healthcare. Historically, the creation of accurate forecasting models has demanded extensive tuning and customization, presenting a significant hurdle.
Read also: Understanding PLCs
Chronos addresses this challenge as a family of pre-trained time series models built upon language model architectures. Similar to large language models, Chronos operates as a foundation model, learning from vast datasets to generate general representations applicable to a wide array of tasks.
Chronos treats time series data as a language, employing transformer architectures to model it effectively. Real-valued time series observations undergo tokenization through scaling and quantization into uniformly spaced bins. Special tokens, PAD and EOS, are incorporated to handle padding and sequence endings. This ingenious approach allows for the training of standard language models like T5 without necessitating alterations to their fundamental architecture.
To enhance generalization capabilities, Chronos leverages diverse time series data sourced from various domains. Public data sources used for pre-training are augmented with randomly mixed-in real samples (TSMix) and a synthetically generated dataset based on Gaussian processes (KernelSynth).
With its impressive zero-shot capabilities, Chronos serves as a versatile "general-purpose" forecasting solution. It empowers practitioners to generate accurate forecasts swiftly, thereby reducing computational costs and streamlining deployment processes.
Building Our Forecasting Tool
In this section, we will embark on the development of a new tool designed to leverage the zero-shot capabilities of Chronos for time series data forecasting. We will harness the power of Rio, an open-source Python full-stack framework, to bring this tool to life.
Read also: Learning Resources Near You
Navigation Bar with File Upload
A navigation bar equipped with a file upload button is a fundamental feature for any data-driven application. It offers users an intuitive means of uploading their data and navigating seamlessly between different sections of the application. In this section, we will construct a navigation bar that incorporates a file upload button, a switcher bar for effortless page navigation, and a text component to display the name of the uploaded file. By employing asynchronous functions, we ensure that the file upload process does not impede the main thread, thereby maintaining the application's responsiveness.
Explanation:
Attributes:
file: This attribute stores the uploaded file as a pandas DataFrame, facilitating data manipulation and analysis.slider_value_plot: This attribute holds the slider value for the plot, initialized to 1, enabling users to dynamically adjust the data view.file_loaded: A boolean attribute that indicates whether a file has been successfully uploaded, allowing for conditional rendering of UI elements.file_name: This attribute stores the name of the uploaded file, which is displayed in the navigation bar to provide users with clear feedback.
Event Handling:
_on_page_change: An asynchronous method that is triggered when the page changes, ensuring that the UI is refreshed to reflect the new page state._on_change: This method handles the event triggered when the switcher bar value changes, facilitating navigation to the selected page._on_file_upload: An asynchronous method designed to handle file uploads. It opens a file chooser, reads the file as bytes, converts it into a pandas DataFrame, updates the slider value, and sets the file name.
Building the Component:
- The
buildmethod constructs the UI elements of the navigation bar, orchestrating the layout and functionality of the component. - A
rio.Rowis employed to arrange the elements horizontally, including a file upload button, a text component to display the file name, a spacer, and a switcher bar for page navigation.
- The
By implementing this navigation bar, users can effortlessly upload their data files and navigate through different sections of the application. The utilization of asynchronous functions guarantees that the application remains responsive, providing a smooth and efficient user experience.
Read also: Learning Civil Procedure
Chronos Forecasting Parameters
Hyperparameters exert a significant influence on the performance and accuracy of machine learning models, including time series forecasting models such as Chronos. To facilitate experimentation and optimization, we will create a component that empowers users to adjust the hyperparameters of the Chronos model. This component will feature input fields for key parameters, including prediction length, number of samples, temperature, top k, top p, and an option to limit the prediction length. Users can input their desired values for these parameters and observe the resulting impact on the forecasted time series.
Explanation:
Attributes:
prediction_length: This attribute specifies the desired length of the prediction horizon, determining how far into the future the model will forecast.num_samples: This attribute defines the number of samples to generate, influencing the diversity and robustness of the forecast.temperature: This attribute controls the temperature for sampling, affecting the randomness and exploration of the model's predictions.top_k: This attribute sets the number of top tokens to sample from, limiting the model's focus to the most probable candidates.top_p: This attribute determines the cumulative probability threshold for sampling, influencing the balance between exploration and exploitation.limit_prediction_length: A boolean attribute that indicates whether to limit the prediction length, providing control over the forecast horizon.
Building the Component:
- The
buildmethod constructs the UI elements of the parameter input component, arranging the input fields in a structured and intuitive layout. rio.Rowelements, such asrow1androw2, are used to organize the input fields in a visually appealing and logical manner.
- The
The code structure closely mirrors the previous example, with a crucial distinction: the utilization of self.bind().prediction_length instead of self.prediction_length when passing the prediction_length value to the NumberInput component. By employing .bind(), we establish a dynamic connection between the parent and child components. Consequently, if either component updates the prediction_length value, the other component will automatically reflect the new value without requiring any additional code.
Interactive Plots for Visualization
Visualizing forecasted time series data is paramount for comprehending and validating the predictions generated by your model. In this section, we will create interactive plots using Plotly to visualize the forecasted time series and compare it against the actual values. Plotly offers an extensive suite of interactive features, enabling users to zoom, pan, and hover over data points to scrutinize values in detail.
The implementation of an interactive plot using Plotly is presented below. This example generates a line plot that showcases the actual values of the time series alongside the forecasted values, facilitating a clear visual comparison.
The function begins by verifying whether the data file has been loaded. If not, it returns a grey rectangle as a placeholder. Subsequently, it converts the timestamp column to datetime format and partitions the data into historical and future datasets based on the specified prediction_length. A new Plotly figure is initialized, and a green line trace is added to represent the actual historical data. If available, a dashed blue line is included to depict the ground truth, and a solid blue line represents the Chronos model forecast.
Finally, the layout is updated to display the legend, and the function returns a Plotly plot encapsulated within a custom component that incorporates a slider for adjusting the data view.
By integrating this interactive plot into your application, users can effortlessly explore and compare the forecasted time series against the actual values, enhancing their ability to analyze and interpret the forecasting results effectively.
Make Forecasts with Chronos
Forecasting future values of a time series is a critical task with applications spanning diverse domains such as finance, healthcare, and supply chain management. By accurately predicting future trends, organizations can make data-driven decisions, optimize their operations, and proactively anticipate potential challenges. In this section, we will demonstrate the creation of a component that leverages the Chronos model to forecast future values of a time series. The Chronos model, renowned for its zero-shot capabilities, allows us to harness user-defined hyperparameters to "fine-tune" the forecast according to specific needs.
By pressing the "Run FC!" button, users can initiate the forecasting process. The forecasted time series will be generated and displayed alongside the actual values, empowering users to visually compare the predicted outcomes with historical data through interactive plots.
The following Python function illustrates how to retrieve and process the Chronos forecast asynchronously:
This function first sets a loading spinner to indicate that the forecasting process is underway. It then initializes the Chronos pipeline and downloads the model if it hasn't already been downloaded. The data is sliced to prepare it for forecasting, and the Chronos pipeline's predict method is invoked with the appropriate context and hyperparameters. The median forecast is extracted from the generated forecast and stored for subsequent use. Finally, the loading spinner is removed to signal the completion of the forecasting process.
Exporting Forecasted Time Series Data to CSV
In many data science endeavors, the ability to export results for further analysis or sharing is of paramount importance. After generating a forecasted time series, you may wish to save these results into a CSV file for convenient access and manipulation. Below, we provide a Python implementation for exporting forecasted data using the pandas library and saving it asynchronously.
First, we define a method to generate a CSV file from the forecasted time series. This method converts the DataFrame into a CSV format and stores it in a bytes buffer.
Next, we create an asynchronous function to save the generated CSV file. This function leverages an asynchronous session to handle the file saving process efficiently.
By utilizing these methods, you can effectively export and save your forecasted time series data, making it readily available for further analysis or sharing with your team. This approach ensures that your workflow remains smooth and your data is accessible whenever needed.
Putting It All Together: A Comprehensive Forecasting Page
In this section, we will integrate all the components and functionalities we have discussed to create a comprehensive page for generating forecasts using the Chronos model. This page will enable users to upload a file, select a time series column, adjust hyperparameters, visualize the forecast, and export the results.
Explanation:
- Attributes:
data: Stores the uploaded data as a pandas DataFrame, providing a structured representation of the time series data.data_target: Specifies the target column in the data, indicating the time series to be forecasted.data_timestamp: Specifies the timestamp column in the data, enabling temporal alignment and analysis.forecast_chronos_median: Stores the median forecasted values from Chronos, representing the central tendency of the predictions.slider_value_plot: Holds the slider value for the plot, allowing users to dynamically adjust the data view.is_fitting: Indicates whether the model is currently fitting, providing feedback on the forecasting process.file_loaded: Indicates whether a file has been loaded, enabling conditional rendering of UI elements.
Chronos and AutoGluon-TimeSeries (AG-TS)
AutoGluon-TimeSeries (AG-TS) incorporates the Chronos family of forecasting models, providing a robust and user-friendly way to leverage Chronos through the familiar TimeSeriesPredictor API.
Specifically, Chronos models do not actually fit time series data in the traditional sense. Instead, when the predict method is called, they perform a computationally intensive operation that scales linearly with the number of time series in the dataset. In this regard, they resemble local statistical models such as ETS or ARIMA, where the most demanding computations occur during inference.
Alternatively, Chronos can be combined with other time series models using the "medium_quality", "high_quality", and "best_quality" presets.
We can specify settings for the Chronos-Bolt model, including zero-shot and fine-tuned configurations. The TimeSeriesPredictor can perform a lightweight fine-tuning of the pre-trained model on the provided training data.
Chronos is a univariate model, meaning that it relies solely on the historical data of the target time series for making predictions. However, in real-world scenarios, additional exogenous information related to the target series (e.g., holidays, promotions) is often available. A covariate regressor in AG-TS is a tabular regression model that is fit on the known covariates and static features to predict the target column at each time step.
It's important to note that covariates may not always be beneficial; for some datasets, the zero-shot model may achieve better accuracy. Therefore, it's crucial to experiment with multiple models and select the one that achieves the best accuracy on held-out data.
The AutoGluon team are among the core developers of Chronos.
Exploring Chronos with Minimal Code
Chronos is a language modeling framework for time series introduced by Amazon. This probabilistic model treats time series as a sequence and employs Google AI's T5 model (text-to-text transfer transformer) for training. The zero-shot forecasting model exhibits superior performance compared to classical statistical forecasting models and specialized deep learning models after a comprehensive evaluation on more than 40 datasets. Zero-shot forecasting refers to the ability of models to generate forecasts from unseen datasets, achieved by using synthetic data along with real data to train Chronos models.
How Chronos Models Work:
First, the time stamps (hourly, daily, monthly, etc.) are not considered; instead, the entire values are treated as tokens.
Steps:
- Import Necessary Libraries:This includes Chronos Pipeline, matplotlib, numpy, pandas and pytorch.
- Choose Model and Initialize Pipeline:There are five Chronos models available: tiny, mini, small, base, and large, with increasing size and number of parameters. The 'tiny' model is often chosen for smaller datasets and faster response times. The
device_mapcan be set to 'cpu', 'auto', 'cuda', etc., depending on available hardware. - Import and Split the Data:Import your time series data using pandas and split it into training and testing sets.
- Define Variable and Forecasting Steps/Horizon:Specify the variable to be predicted and the forecasting horizon (number of steps into the future).
- Forecast Using Chronos:Use the Chronos pipeline's
predictmethod to generate the forecast. - Visualize Forecast:Since Chronos performs probabilistic forecasting, the predicted values are in the form of a dense tensor. Extract the median (50th percentile), upper bound (90th percentile), and lower bound (10th percentile) for better understanding and visualization.
tags: #chronos #time #series #model #tutorial

