DEV Community

Alain Airom
Alain Airom

Posted on

Unlocking the Power of Time: An Introduction to Time Series Analysis

Follow-up on my hands-on experieneces with TimeSeries Anlysis using Granite foundation models

Introduction

In a world where data is constantly being generated, much of it comes with a timestamp. From stock market fluctuations and sales figures to server logs and climate data, these streams of information are more than just numbers — they’re a story unfolding over time. This type of data, known as time series data, holds immense potential for those who know how to listen to its narrative. But what exactly is a time series, and why is its analysis so crucial?

Time series analysis is the process of examining and modeling time series data to extract meaningful statistics and characteristics. Unlike a simple spreadsheet of numbers, a time series has an inherent order, where each data point is dependent on the points that came before it. This dependency allows us to not only understand past patterns but also to forecast future trends. By mastering the art of time series analysis, you can move from a reactive to a proactive mindset, predicting what’s next and making more informed decisions.

A significant development in the field of time series analysis comes from IBM, which is proposing the use of Time Series Foundation Models (TSFMs) under its IBM Granite umbrella. These are pre-trained models designed to handle diverse time series data, and they’re poised to revolutionize how we approach forecasting. The power of these models lies in their ability to perform zero-shot and few-shot forecasting, meaning they can generate impressive predictions on new datasets with little or no additional training. This is a game-changer for businesses that often have limited historical data for a specific product or event. To make this technology accessible, IBM has provided public notebooks, utilities, and serving components for working with these TSFMs, allowing data scientists and developers to quickly integrate these powerful tools into their workflows. This open approach accelerates innovation and makes advanced forecasting capabilities available to a much wider audience.

Test(s)

This marks the second phase of my testing with the IBM Granite Time Series Foundation Model (TSFM) for my current project. To get my local environment ready, I’m leveraging the public GitHub repository provided by IBM. Below are the steps I followed to install the necessary files and libraries to begin these tests.

git clone "https://github.com/ibm-granite/granite-tsfm.git" 
cd granite-tsfm

# prepare your environment

python3 -m venv venv
source venv/bin/activate

pip install --upgrade pip

# installing Granite packages
pip install ".[notebooks]"
pip install ".[demos]"

# installing Jupyter Notebook packages
pip install jupyter
pip install notebook
pip install jupyterlab
Enter fullscreen mode Exit fullscreen mode

Once the GitHub repository is cloned, in order to test / run a notebook (if you’ve cloned the repository);

cd xxxx/granite-tsfm/notebooks/hfdemo
# launch jupyter 
jupyter lab 
Enter fullscreen mode Exit fullscreen mode

From this point you can run any of the sample notebooks provided.

# Getting started with TinyTimeMixer (TTM) Rolling Predictions

This notebooke demonstrates the usage of a pre-trained or finetuned `TinyTimeMixer` model for rolling predictions. 

In this example, we will use a pre-trained TTM-512-96 model. That means the TTM model can take an input of 512 time points (`context_length`), and can forecast upto 96 time points (`forecast_length`) in the future. We then do rolling predictions of this model to keep predicting for longer forecast lengths.

Pre-trained TTM models will be fetched from the [Hugging Face TTM Model Repository](https://huggingface.co/ibm-granite/granite-timeseries-ttm-v1). We also fine-tune a model and use the finetuned model for the same rolling predictions.
import os
import tempfile

from transformers import Trainer, TrainingArguments, set_seed

from tsfm_public import TinyTimeMixerForPrediction, load_dataset
from tsfm_public.toolkit import RecursivePredictor, RecursivePredictorConfig
from tsfm_public.toolkit.visualization import plot_predictions
### Important arguments
# Set seed for reproducibility
SEED = 42
set_seed(SEED)

# DATA ROOT PATH
# Make sure to download the target data (here ettm2) on the `DATA_ROOT_PATH` folder.
# ETT is available at: https://github.com/zhouhaoyi/ETDataset/tree/main
target_dataset = "etth1"

dataset_path = "https://raw.githubusercontent.com/zhouhaoyi/ETDataset/main/ETT-small/ETTh1.csv"

# DATA_ROOT_PATH = "/dccstor/tsfm23/datasets/"

# Results dir
OUT_DIR = "ttm_finetuned_models/"

# TTM model branch
# Use main for 512-96 model
# Use "1024_96_v1" for 1024-96 model
TTM_MODEL_REVISION = "main"

ROLLING_PREDICTION_LENGTH = 192
TTM_MODEL_URL = (
    "ibm-granite/granite-timeseries-ttm-v1"  # POINT TO A ZEROSHOT MODEL OR A FINETUNED MODEL TO DO ROLLING INFERENCE.
)
## LOAD BASE MODEL
base_model = TinyTimeMixerForPrediction.from_pretrained(TTM_MODEL_URL, revision=TTM_MODEL_REVISION)

base_model_context_length = base_model.config.context_length
base_model_prediction_length = base_model.config.prediction_length

print(base_model_context_length, base_model_prediction_length)
## LOAD DATA
_, _, dset_test = load_dataset(
    dataset_name=target_dataset,
    context_length=base_model_context_length,
    forecast_length=ROLLING_PREDICTION_LENGTH,
    fewshot_fraction=1.0,
    dataset_path=dataset_path,
)
## ROLLING PREDICTIONS
rec_config = RecursivePredictorConfig(
    model=base_model,
    requested_prediction_length=ROLLING_PREDICTION_LENGTH,
    model_prediction_length=base_model_prediction_length,
    loss=base_model.config.loss,
)
rolling_model = RecursivePredictor(rec_config)
temp_dir = tempfile.mkdtemp()
# zeroshot_trainer
zeroshot_trainer = Trainer(
    model=rolling_model,
    args=TrainingArguments(
        output_dir=temp_dir,
        per_device_eval_batch_size=32,
        seed=SEED,
    ),
)
# evaluate = zero-shot performance
print("+" * 20, "Test MSE zero-shot", "+" * 20)
zeroshot_output = zeroshot_trainer.evaluate(dset_test)
print(zeroshot_output)
plot_predictions(
    model=zeroshot_trainer.model,
    dset=dset_test,
    plot_dir=os.path.join(OUT_DIR, target_dataset),
    plot_prefix="test_rolling",
    indices=[685, 118, 902, 1984, 894, 967, 304, 57, 265, 1015],
    channel=0,
)
Enter fullscreen mode Exit fullscreen mode

Conclsuion

The rise of models like IBM Granite’s Time Series Foundation Models represents a monumental leap in how we approach forecasting. These aren’t just incremental improvements; they are a fundamental shift, allowing us to move from tedious, dataset-specific modeling to leveraging massive, pre-trained power with minimal effort. What’s most exciting is that this technology isn’t locked away in a lab. With the public GitHub repository and the comprehensive Granite Time Series Cookbook, this cutting-edge capability is now within reach for developers and data scientists everywhere, democratizing access to a new era of predictive analytics.

Links

Top comments (0)