Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

The Complexities of Trend Detection in Time Series Data
Data Science   Latest   Machine Learning

The Complexities of Trend Detection in Time Series Data

Author(s): Lily Chen

Originally published on Towards AI.

One common use case of working with time series data is trend detection, or trend analysis. A real life example is looking at memory data to see if a service is experiencing a memory leak.

Some time ago, my team at Datadog released a Memory Leak Wizard, whose aim was to help users solve memory leak issues. When the feature was first released, we asked the users to manually answer a set of questions before bringing them to what they need to solve their potential memory leak issue.

We first ask if there is a goroutine leak.

If there isn’t (as in the example above), we ask about Live Go Memory.

In this case, there is growth, and after users selects β€œYes”, we show the relevant Comparison Flame Graph that they can use to investigate where the growth in memory is from.

Of course, in this modern age of AI, you might be wondering β€œwhy does a human need to answer this?” And you are correct, this could very much be automated.

Fast forward a few months, we released the automated version of the Memory Leak workflow.

Automatic trend analysis

However, the process of capturing an upward trend in a time series data has some complexities involved. Here are some pitfalls I ran into and lessons I learned that might help others or help my future self if she were to do this kind of work again.

First of all, is this even an LLM problem? My conclusion after working on this project is β€œno.” This is not a problem best solved by the current generation of LLMs like ChatGPT-4o. But before we get to why that’s the case, let’s delve first into some of the complexities of working with time series data.

The necessity of scaling the data

The way to identify linear growth in data is by performing linear regression. Linear regression finds the slope of the line of best fit, and the R-Squared value. Typically, to assess whether a dataset has a linear upward trend, we first define a threshold for slope and a threshold for the R-Squared value. A reasonable threshold for the slope could be 0.1. If both values are greater or equal to the threshold, we say the dataset has a linear growth.

Linear regression, however, is sensitive to the magnitude of the data. When performing time series analysis, it is important to take that into consideration.

For all examples below, the x-axis values are in milliseconds since epoch, or what Date.now() would return in Javascript.

Let’s look at a simple dataset with 2 points. In both examples below, the y doubled. But in the first case, a linear regression will return a slope that’s pretty much 0. A slope of zero (or below) means no upward trend.

Case 1: y axis values go from 10 to 20

If you try to fit a linear regression line through this data for an hour time frame, you will get a slope close to zero, i.e. no growth, even though the data doubled.

Case 2: y axis value go from 1 million to 2 million

When looking at memory usage in bytes, the y-axis values could be in the order of millions or billions. In this case, you might not want to scale the x-axis, depending on the actual time frame and memory values are.

The caveat with scaling data

If you scale the x-axis, the value of the magnitude of the slope kind of becomes meaningless on their own, and not to be relied on alone.

Let’s look at this following example. Let’s say the threshold for positive growth is 0.1, and you have 2 points where the y-values go from 100 to 101. Let’s say they’re taken 1 minute apart so the deltaX (without scaling) is 60 * 1000.

Without scaling, the effective slope is 1 / 60,000 or 0.000017 (i.e. very close to zero), so it would not pass check for linear growth. Good.

But let’s say we scale the x-axis. Let’s set t1 to be 0, and t2 to be t1 + 60000. Or in other words, if t2 is Date.now(), t1 would be t2 - 60000.

To scale the timestamp, let’s divide the timestamps by 6000. The delta X after scaling would be 10.

In this case, the effective slope is 1 / 10, or 0.1, and would pass a threshold of >=0.1 for linear growth.

Thus, slope alone is not reliable when scaling of the axis is involved.

To get around this, we also need to look at the min(y) (minimum of the y-values) and max(y) (maximum of the y-values) of the entire series. If the max(y) is not a certain threshold greater than min(y), we do not label the graph as trending upward. For example, if max(y) <= min(y) * 1.05, i.e. if the data has not increased by 5% between the min and max, we can call it not increasing in a significant manner, even if linear regression after scaling gives us a large slope with high confidence.

This is not foolproof, because what if max(y) is actually before min(y) in the time series? We’re using the ideal scenario that max(y) is towards the end and min(y) towards the beginning to filter out non significant growth. Hopefully it is enough in the context of what we’re doing at Datadog in the context of the Memory Leak Workflow. However, it’s something to keep an eye out on, and we’ll deal with this problem if we ever cross the bridge.

So far I’ve only talked about linear regression, which is good at detecting linear data. However, there could be many shapes of growth in the context of a memory leak, such as quadratic or exponential growth.

I’ll write in a later blog post the challenges of working with other regressions in the context of real world trend analysis, but first, the big question you might be wondering:

Can LLMs do better?

What would happen if we give the data to LLMs like ChatGPT-4o and ask it to tell us if the graph is increasing?

I fed the model some goroutines data and asked if there’s an upward trend, and it answered:

While the trend is technically increasing, the low R-squared value suggests a lot of variability in the data, meaning the upward movement isn’t strong or consistent

To us humans, this is clearly an increase β€” not just linear, but what appears to be exponential. Yet, ChatGPT-4o labeled this as an upward movement that isn’t strong or consistent. Fail.

You can tell from the image ChatGPT generated that it did a linear regression on the dataset. If you expand to view more, you can see it using scipy.stats.

import pandas as pd
import matplotlib.pyplot as plt
from scipy.stats import linregress
# makes pandas DataFrame from the given data
...
# Perform linear regression to determine trend
slope, intercept, r_value, p_value, std_err = linregress(df['timestamp'], df['value'])

The aforementioned goroutines data needs scaling; notice how the slope is almost zero? The data would also much better fit an exponential or quadratic curve. Yet ChatGPT-4o isn’t β€œsmart enough” to know those things on its own.

We learned 2 things from just this one example:

  1. LLMs aren’t magic, and in this case, it used statistics under the hood.
  2. LLMs don’t solve this type of problem well (at least not without prompt engineering).

I’ve written previously about how the current generation of LLMs don’t handle context-dependent problems well. When it comes to working with time series data in the real world, it’s all about the context.

Perhaps we could train an LLM to do this through prompt engineering, telling it to run exponential and quadratic regressions if linear regression fails to pass the threshold. But is doing that really better than coding it up? I’d have to know exactly what to do to detect an upward trend, whereas in the ideal world, the LLM can do it automatically, the LLM knows all the pitfalls and edge cases to look out for.

I have to admit that when I undertook the task of automating the Datadog Memory Leak Workflow, my first instinct was β€œsurely ChatGPT can do this.” But now that I know ChatGPT merely do regressions under the hood, without even taking into account the intricacies of working with time series data, I know it can’t be trusted to solve this kind of problem.

It seems nowadays, many people treat any problem as an LLM problem. I often see on LinkedIn posts from people that are along the line of β€œwe won’t need to hire software engineers soon because I had [insert LLM model] code up some [insert prototype functionality] for me.”

While I’m certain optimistic about the accelerating improvements we’re making to LLMs, we’ve observed here that to blindly use LLMs to solve any problem can be dangerous. I know this is true when it comes to code generation, as of Feb 2025. If you trust these models blindly, you’d either get lucky or get it wrong.

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.

Published via Towards AI

Feedback ↓