A Coding Guide to Build an Autonomous Agentic AI for Time Series Forecasting with Darts and Hugging Face

Building an Autonomous AI Agent for Time Series Forecasting

This guide demonstrates how to develop a sophisticated AI agent capable of independently performing time series forecasting. Leveraging the darts library alongside a streamlined HuggingFace language model, the agent follows a cyclical process of perception, reasoning, and action. It begins by examining data trends, then intelligently selects the most suitable forecasting model, produces predictions, and finally offers clear explanations and visualizations of the outcomes. This approach highlights how agentic AI can seamlessly integrate statistical techniques with natural language understanding to deliver forecasts that are both precise and interpretable.

Setting Up the Environment and Dependencies

To start, install and import the necessary libraries: darts for time series analysis, transformers for natural language reasoning, and essential data manipulation and visualization tools such as pandas, numpy, and matplotlib. These components form the backbone of our autonomous forecasting system.

!pip install darts transformers pandas matplotlib numpy -q

import pandas as pd
import numpy as np
from darts import TimeSeries
from darts.models import ExponentialSmoothing, NaiveSeasonal, LinearRegressionModel
from darts.metrics import mape
from transformers import pipeline
import matplotlib.pyplot as plt

Designing the Time Series Forecasting Agent

We create a TimeSeriesAgent class that embodies the AI’s cognitive cycle. It perceives data characteristics, reasons to choose the optimal forecasting model, executes training and prediction, and finally interprets and visualizes the results.

Perception: Analyzing Data Patterns

The agent converts raw data into a time series format and evaluates key features such as trend direction, volatility, and seasonality. For example, it determines if the data shows an upward or downward trend, measures variability relative to the mean, and detects seasonal patterns using autocorrelation analysis.

Reasoning: Selecting the Best Forecasting Model

Based on the perceived data traits, the agent uses a lightweight language model to simulate reasoning and justify its choice of forecasting method. For instance, if seasonality is present, it opts for a Naive Seasonal model; if volatility is high, it prefers Exponential Smoothing; otherwise, it selects Linear Regression for stable trends.

Action: Training and Forecasting

The chosen model is trained on historical data, and the agent generates forecasts for a specified horizon. It also validates the model’s accuracy by comparing predictions against recent data points, reporting metrics such as Mean Absolute Percentage Error (MAPE).

Explanation and Visualization

To enhance interpretability, the agent summarizes its forecast in plain language, quantifying expected percentage changes and ranges. It then produces a clear plot contrasting historical data with predicted values, making the forecasting process transparent and accessible.

class TimeSeriesAgent:
    """AI agent for autonomous time series forecasting"""

    def __init__(self):
        print("🤖 Initializing AI Agent...")
        self.llm = pipeline("text-generation", model="distilgpt2", max_length=150,
                            do_sample=True, temperature=0.7)
        self.models = {
            'exponential_smoothing': ExponentialSmoothing(),
            'naive_seasonal': NaiveSeasonal(K=12),
            'linear_regression': LinearRegressionModel(lags=12)
        }
        self.selected_model = None
        self.forecast = None

    def perceive(self, data):
        print("n👁️ PERCEPTION PHASE")
        self.ts = TimeSeries.from_dataframe(data, 'date', 'value', freq='M')

        trend = "increasing" if data['value'].iloc[-1] > data['value'].iloc[0] else "decreasing"
        volatility = data['value'].std() / data['value'].mean()
        seasonality = self._detect_seasonality(data['value'])

        analysis = {
            'length': len(data),
            'trend': trend,
            'volatility': f"{volatility:.2f}",
            'has_seasonality': seasonality,
            'mean': f"{data['value'].mean():.2f}",
            'range': f"{data['value'].min():.2f} to {data['value'].max():.2f}"
        }

        print(f"📊 Data Points: {analysis['length']}")
        print(f"📈 Trend: {analysis['trend'].upper()}")
        print(f"🎲 Volatility: {analysis['volatility']}")
        print(f"🔄 Seasonality: {'Present' if seasonality else 'Absent'}")

        return analysis

    def _detect_seasonality(self, series, threshold=0.3):
        if len(series) < 24:
            return False
        acf = np.correlate(series - series.mean(), series - series.mean(), mode='full')
        acf = acf[len(acf)//2:]
        acf /= acf[0]
        return np.max(acf[12:24]) > threshold if len(acf) > 24 else False

    def reason(self, analysis):
        print("n🧠 REASONING PHASE")

        prompt = (f"Time series characteristics: {analysis['length']} points, "
                  f"{analysis['trend']} trend, volatility {analysis['volatility']}, "
                  f"seasonality: {analysis['has_seasonality']}.")
        thought = self.llm(prompt, max_length=100, num_return_sequences=1)[0]['generated_text']
        print(f"💭 Agent Thought: {thought[:150]}...")

        if analysis['has_seasonality']:
            self.selected_model = 'naive_seasonal'
            reason = "Seasonality detected; selecting Naive Seasonal model."
        elif float(analysis['volatility']) > 0.3:
            self.selected_model = 'exponential_smoothing'
            reason = "High volatility observed; choosing Exponential Smoothing."
        else:
            self.selected_model = 'linear_regression'
            reason = "Stable trend identified; opting for Linear Regression."

        print(f"✅ Decision: {reason}")
        return self.selected_model

    def act(self, horizon=12):
        print("n⚡ ACTION PHASE")

        train, val = self.ts[:-12], self.ts[-12:]
        model = self.models[self.selected_model]

        print(f"🎯 Training {self.selected_model} model...")
        model.fit(train)

        self.forecast = model.predict(horizon)

        if len(val) > 0:
            val_pred = model.predict(len(val))
            accuracy = 100 - mape(val, val_pred)
            print(f"📊 Validation Accuracy: {accuracy:.2f}%")

        print(f"🔮 Produced {horizon}-step ahead forecast.")
        return self.forecast

    def explain(self):
        print("n💬 EXPLANATION PHASE")

        forecast_vals = self.forecast.values().flatten()
        hist_vals = self.ts.values().flatten()

        change_pct = ((forecast_vals[-1] - hist_vals[-1]) / hist_vals[-1]) * 100
        direction = "increase" if change_pct > 0 else "decrease"

        explanation = (f"Using the {self.selected_model} model, the forecast indicates a "
                       f"{abs(change_pct):.1f}% {direction} in the upcoming period. "
                       f"Forecast range spans from {forecast_vals.min():.2f} to {forecast_vals.max():.2f}, "
                       f"while historical average was {hist_vals.mean():.2f}.")

        print(f"📝 {explanation}")

        prompt = f"Forecast summary: {explanation} Please elaborate on implications."
        summary = self.llm(prompt, max_length=120)[0]['generated_text']
        print(f"n🤖 Agent Summary: {summary[:200]}...")

        return explanation

    def visualize(self):
        print("n📈 Generating forecast visualization...")

        plt.figure(figsize=(14, 6))
        self.ts.plot(label='Historical Data', lw=2)
        self.forecast.plot(label=f'Forecast ({self.selected_model})', lw=2, linestyle='--')

        plt.title('🤖 Agentic AI Time Series Forecast', fontsize=16, fontweight='bold')
        plt.xlabel('Date', fontsize=12)
        plt.ylabel('Value', fontsize=12)
        plt.legend(loc='best', fontsize=11)
        plt.grid(alpha=0.3)
        plt.tight_layout()
        plt.show()

Generating Synthetic Time Series Data for Testing

To evaluate the agent’s capabilities, we simulate monthly data spanning four years (2020-2023). The synthetic dataset combines a linear upward trend, cyclical seasonal fluctuations modeled by a sine wave, and random noise to mimic real-world variability.

def create_sample_data():
    """Generate synthetic monthly time series data with trend and seasonality"""
    dates = pd.date_range(start='2020-01-01', periods=48, freq='M')
    trend = np.linspace(100, 150, 48)
    seasonality = 8 * np.sin(np.linspace(0, 4 * np.pi, 48))
    noise = np.random.normal(0, 4, 48)
    values = trend + seasonality + noise

    return pd.DataFrame({'date': dates, 'value': values})

Executing the Autonomous Forecasting Pipeline

The main function orchestrates the entire workflow: loading data, initializing the agent, running perception, reasoning, action, explanation, and visualization phases sequentially. This end-to-end process showcases the agent’s ability to independently manage forecasting tasks.

def main():
    print("="*70)
    print("🚀 AGENTIC AI TIME SERIES FORECASTING SYSTEM")
    print("="*70)

    print("n📥 Loading synthetic data...")
    data = create_sample_data()
    print(f"Loaded {len(data)} monthly data points from 2020-01 to 2023-12.")

    agent = TimeSeriesAgent()

    analysis = agent.perceive(data)
    agent.reason(analysis)
    agent.act(horizon=12)
    agent.explain()
    agent.visualize()

    print("n" + "="*70)
    print("✅ AGENT COMPLETED FORECASTING TASK SUCCESSFULLY")
    print("="*70)


if __name__ == "__main__":
    main()

Summary: Harnessing Agentic AI for Transparent Forecasting

This project illustrates how an autonomous AI agent can effectively analyze time series data, intelligently select forecasting models, generate accurate predictions, and communicate insights in natural language. By integrating the darts forecasting toolkit with HuggingFace’s language models, we establish a compact yet powerful framework that enhances both the precision and interpretability of forecasts. The inclusion of visual summaries further empowers users to grasp trends and predictions intuitively, demonstrating the potential of agentic AI to revolutionize time series analysis.

More from this stream

Recomended