Revolutionizing Time Series Analysis with TimeGPT and Julia

Manu Francis
6 min readNov 28, 2023

Introduction:

Time series analysis plays a pivotal role in unraveling the mysteries embedded within chronological data, making it an indispensable tool for industries ranging from finance and healthcare to manufacturing and beyond. In a world inundated with data, understanding and predicting temporal patterns are crucial for informed decision-making. Time series data, characterized by its sequential nature, encapsulates a wealth of information crucial for identifying trends, patterns, and anomalies.

In finance, the ability to forecast market trends and predict asset values hinges on analyzing historical price movements. For healthcare professionals, time series analysis aids in tracking patient vitals, predicting disease outbreaks, and optimizing treatment plans. In manufacturing, predicting equipment failures through historical performance data is essential for minimizing downtime and maximizing efficiency. The applications extend to virtually every sector, where leveraging the past to anticipate the future is not just advantageous but often mission-critical.

Nixtla’s TimeGPT stands as a generative pre-trained model designed for forecasting in time series data. Operating without the need for explicit training, TimeGPT excels at generating accurate predictions for novel time series, relying solely on historical values as its input. Its versatility extends across various applications, including demand forecasting, anomaly detection, financial forecasting, and more.

In synergy with the high-performance programming language Julia, TimeGPT emerges as a formidable force, democratizing advanced time series analysis and empowering industries to navigate the challenges of an increasingly dynamic world with unparalleled precision and efficiency. In this blog post, we delve into the transformative alliance between TimeGPT and Julia, exploring how it reshapes the paradigm of time series analysis.

The Power of TimeGPT

Operating akin to how humans read a sentence, the TimeGPT model traverses time series data sequentially, moving from left to right. It scrutinizes windows of past data, conceptualized as “tokens,” and anticipates forthcoming values. This predictive capability stems from the model’s ability to discern patterns within historical data, projecting these patterns into the future.

Facilitated by an API, TimeGPT offers a user-friendly interface, enabling individuals to harness its forecasting prowess for anticipating upcoming events. Beyond forecasting, TimeGPT proves valuable in diverse time series-related tasks, such as exploring hypothetical scenarios, detecting anomalies, and addressing various other analytical challenges.Section 2: Understanding TimeGPT

In its predictive process, TimeGPT utilizes a segment of historical values, enhancing the input with local positional encoding. The model’s architecture adopts an encoder-decoder structure with several layers, each featuring residual connections and layer normalization. Concluding the sequence, a linear layer translates the output from the decoder to match the dimensions of the forecasting window. The underlying rationale is that attention-based mechanisms excel in comprehending the array of past events, proficiently projecting potential future distributions.

Practical Applications

  • Anomaly Detection
  • Forecasting

Using TimeGPT with Julia

Generate Tokens

Prior to engaging with the TimeGPT API, it is necessary to generate tokens through the https://dashboard.nixtla.io/ platform. If you do not have an account on nixla.io, you must first create one. Subsequently, you can proceed to generate a token using the following steps:

Setting Up Environment:

The HTTP.jl package is employed for communication with TimeGPT APIs within the Julia environment. To integrate HTTP.jl, you can execute the installation process as demonstrated below:

julia> using Pkg
julia> Pkg.add("HTTP")

Furthermore, the Plot.jl package is essential for visualizing data points. To incorporate the Plot.jl functionality, follow the installation steps provided below:

julia> using Pkg
julia> Pkg.add("Plots")

JSON3.jl in Julia serves as a high-performance JSON parsing and encoding library, offering efficient handling of JSON data. Its purpose is to provide a fast and reliable tool for working with JSON-formatted information within Julia programs. JSON3.jl helps us to parse Julia data trypes into JSON strings. You can install JSON3.jl as below

julia> using Pkg
julia> Pkg.add("Plots")

Example code for forecasting using TimeGPT

using HTTP
using JSON3

endpoint = "https://dashboard.nixtla.io/api/timegpt"
api_key = "YOUR_TIMEGPT_TOKEN"
headers = Dict(
"Authorization" => "Bearer $api_key",
"accept" => "application/json",
"content-type" => "application/json"
)

y = Dict{String, Any}(
"2015-12-19" => 7.17165682276851,
"2015-12-15" => 8.23668532271246,
"2015-12-09" => 7.77779262633883,
"2016-01-07" => 8.29579811063615,
"2015-12-21" => 8.36007143564403,
"2016-01-08" => 8.29029259122431,
"2015-12-14" => 8.31947369244219,
"2015-12-18" => 7.52886925664225,
"2015-12-29" => 9.24792513230345,
"2015-12-05" => 7.45876269238096,
"2015-12-10" => 8.2602342916073,
"2016-01-10" => 8.28172399041139,
"2015-12-06" => 8.01400499477946,
"2015-12-27" => 8.87877607170755,
"2015-12-13" => 7.71824095195932,
"2016-01-01" => 7.58933582317062,
"2015-12-04" => 8.08147504013705,
"2015-12-30" => 8.39140318535794,
"2016-01-11" => 8.4707303170059,
"2015-12-02" => 8.71177264560569,
"2015-12-26" => 7.12044437239249,
"2015-12-25" => 7.30182234213793,
"2016-01-03" => 8.24931374626064,
"2015-12-28" => 9.25061821847475,
"2015-12-12" => 7.31055015853442,
"2015-12-11" => 7.86633892304654,
"2015-12-08" => 7.98104975966596,
"2015-12-17" => 7.59186171488993,
"2016-01-06" => 8.35255436947459,
"2015-12-22" => 8.11042723757502,
"2015-12-24" => 7.34729970074316,
"2015-12-31" => 8.00469951054955,
"2015-12-07" => 8.49678638163858,
"2016-01-05" => 8.56826646160024,
"2015-12-23" => 7.77527584648686,
"2015-12-20" => 7.89133075766189,
"2016-01-13" => 8.06714903991011,
"2016-01-02" => 7.82524529143177,
"2015-12-03" => 8.05610965954506,
"2016-01-12" => 8.13505390861157,
"2015-12-16" => 7.80751004221619,
"2016-01-09" => 7.78572089653462,
"2016-01-04" => 9.29514097366865
)
body = Dict(
"model" => "timegpt-1",
"freq" => "D",
"fh" => 7,
"clean_ex_first" => true,
"finetune_steps" => 0,
"y" => y
)

response = HTTP.post(endpoint, headers, JSON3.write(body))

data = JSON3.read(response.body, Dict)

using Dates
# Extract keys and values from the dictionary
k_val = collect(keys(y))
date_format = dateformat"y-m-dTH:M:SZ"
k_val_formatted = map(x->(DateTime(x, date_format)), k_val)
v_val = collect(values(y))

using Plots

# Plot the data
scatter(k_val_formatted, v_val, seriestype=:scatter, xlabel="Time", ylabel="Value", label="Actual")

split_times = [split(x)[1] for x in data["data"]["timestamp"]]

for_timestampe = map(x->(DateTime(x, date_format)), split_times)
# Add a second scatter plot with a different color
scatter!(for_timestampe, data["data"]["value"], label="Prediction", color=:red)

The above example script is for forecasting based on previous time series data. The body of the HTTP request contains the data and model configuration details:

body = Dict(
"model" => "timegpt-1",
"freq" => "D",
"fh" => 7,
"clean_ex_first" => true,
"finetune_steps" => 0,
"y" => y
)
  • model: Specify the desired TimeGPT model using the ‘model’ parameter, choosing between ‘timegpt-1’ and ‘timegpt-1-long-horizon,’ with the latter recommended for forecasting spanning more than one seasonal period.
  • freq: Define the data frequency with the ‘freq’ parameter, selecting from options like ‘D’ for daily, ‘M’ for monthly, ‘H’ for hourly, or ‘W’ for weekly frequencies.
  • level: Set the prediction intervals using the ‘level’ parameter, providing a list of percentage values (e.g., [80, 90] for 80% and 90% prediction intervals).
  • y: Include the historical time series data with the ‘y’ parameter, presented as a dictionary with timestamps (YYYY-MM-DD format) as keys and corresponding observations as values.
  • x: For exogenous variables, use the ‘x’ parameter, providing a dictionary with timestamps as keys and lists of exogenous variable values at those time points.
  • fh: additional timestamps for the forecasting horizon (fh).
  • clean_ex_first: when set to ‘true,’ indicates that the API should preprocess (clean) the exogenous signal before applying the large time model, while ‘false’ applies exogenous variables after the large time model.

The forecatsed results after running above example is visualising Plots.jl package is as below:

Conclusion

In conclusion, TimeGPT emerges as a groundbreaking force in time series analysis, bridging the gap between historical data and future predictions. Its unique ability to generate precise forecasts without explicit training transforms industries, offering a versatile solution for tasks ranging from financial forecasting to anomaly detection. The fusion of TimeGPT with Julia amplifies its impact, democratizing advanced analytics with unparalleled efficiency. Operating like a human reader, TimeGPT’s predictive prowess, coupled with an intuitive API, simplifies complex analyses. As a transformative ally in diverse applications, TimeGPT redefines how we approach time series data, navigating uncertainties with precision and reshaping the landscape of predictive analytics.

References:

--

--

Manu Francis

Researcher, Machine Learning Engineer, Software Developer