How to Use TimesFM for Time-Series Forecasting

May 20, 2024 | Educational

Welcome to the guide on utilizing TimesFM, Google’s cutting-edge pretrained Time Series Foundation Model, designed specifically for time-series forecasting. In this article, we’ll walk you through the steps to set up and use TimesFM, discuss its installation, and provide troubleshooting tips. Let’s dive right in!

What is TimesFM?

TimesFM is a specialized model designed to forecast future values based on historical time series data. Think of it as a skilled fortune-teller, equipped with a crystal ball that can read sequential patterns over time. With TimesFM, you’ll be able to make predictions based on data points (like sales numbers, stock prices, etc.) that are fed into the model, helping you prepare for what lies ahead.

Installation

To get started with TimesFM, you first need to install the library. Follow these steps:

  • Visit the GitHub repo for TimesFM.
  • Follow the installation instructions provided there to setup the library on your system.

Important Note: The dependency lingvo does not currently support ARM architectures, so if you’re using an Apple Silicon machine, you may encounter issues. We’re working on a fix, so stay tuned!

Using TimesFM

Once you’ve got TimesFM installed, it’s time to start forecasting! Here’s how you can do that step-by-step:

1. Initialize the Model and Load a Checkpoint

To start using the model, you’ll need to load it along with a checkpoint. Here’s how it works:

import timesfm

tfm = timesfm.TimesFm(
    context_len=,
    horizon_len=,
    input_patch_len=32,
    output_patch_len=128,
    num_layers=20,
    model_dims=1280,
    backend=,
)
tfm.load_from_checkpoint(repo_id="google/timesfm-1.0-200m")

The parameters input_patch_len, output_patch_len, num_layers, and model_dims are fixed for the 200m model. Make sure to set context_len—the maximum length from which the model can utilize historical data.

2. Perform Inference

Now you’re ready to make predictions! There are two ways to input your data: through array inputs or a pandas DataFrame. Here’s a brief rundown:

Array Inputs

import numpy as np

forecast_input = [
    np.sin(np.linspace(0, 20, 100)),
    np.sin(np.linspace(0, 20, 200)),
    np.sin(np.linspace(0, 20, 400)),
]
frequency_input = [0, 1, 2]

point_forecast, experimental_quantile_forecast = tfm.forecast(
    forecast_input,
    freq=frequency_input,
)

Pandas DataFrame

import pandas as pd

# e.g. input_df is
#       unique_id  ds          y
# 0     T1         1975-12-31  697458.0
# 1     T1         1976-01-31  1187650.0
# ...
forecast_df = tfm.forecast_on_df(
    inputs=input_df,
    freq="M",  # monthly
    value_name="y",
    num_jobs=-1,
)

Understanding Frequencies

TimesFM categorizes frequencies into three types:

  • 0: High frequency, suitable for daily granularity.
  • 1: Medium frequency, appropriate for weekly or monthly data.
  • 2: Low frequency, good for quarterly or yearly time series.

The frequency should be provided directly with your input data. However, feel free to adjust this according to your specific data characteristics!

Troubleshooting

If you run into issues while utilizing TimesFM, consider the following troubleshooting steps:

  • Ensure that all dependencies are correctly installed, especially lingvo, if you aren’t using ARM architecture.
  • Verify the dimensionality of your input data matches what TimesFM expects—consistency in context and horizon lengths is crucial.
  • If your predictions aren’t as expected, consider changing the frequency settings as they can significantly influence the model’s forecast capabilities.
  • For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

By now, you should have a solid understanding of how to install, initialize, and use TimesFM for your time-series forecasting needs. Whether you’re looking to predict sales, stock prices, or any other sequential data, TimesFM has you covered.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox