# Time Series Forecasting

URL: https://interfaze.ai/docs/compute/time-series-forecasting

Forecast and predict any time series data by for sales, demand, traffic, financial numbers and more in any granularity by day, week, month, year, etc.

## Forecast time series data from CSV file

```csv
date,departure_total_delay_mins
2008-01-01,378031
2008-01-02,470367
2008-01-03,353459
2008-01-04,313734
2008-01-05,333774
2008-01-06,348636
2008-01-07,278903
2008-01-08,243514
2008-01-09,143159
2008-01-10,198799
2008-01-11,238677
2008-01-12,78760
2008-01-13,147519
2008-01-14,150793
2008-01-15,133179
2008-01-16,119011
2008-01-17,374516
2008-01-18,252565
2008-01-19,187702
2008-01-20,161163
2008-01-21,400649
2008-01-22,335202
2008-01-23,235637
2008-01-24,200697
2008-01-25,266815
2008-01-26,132591
2008-01-27,399585
2008-01-28,265513
2008-01-29,237232
2008-01-30,178419
2008-01-31,433207
2008-02-01,489413
2008-02-02,156459
2008-02-03,304508
2008-02-04,372892
2008-02-05,313833
2008-02-06,301864
2008-02-07,212722
2008-02-08,246705
2008-02-09,108724
2008-02-10,271627
2008-02-11,265600
2008-02-12,421826
2008-02-13,442894
2008-02-14,264026
2008-02-15,269081
2008-02-16,200773
2008-02-17,375396
2008-02-18,428998
2008-02-19,247290
2008-02-20,228184
2008-02-21,323296
2008-02-22,499211
2008-02-23,242628
2008-02-24,261066
2008-02-25,264930
2008-02-26,407492
2008-02-27,171874
2008-02-28,156890
2008-02-29,276637
2008-03-01,140787
2008-03-02,215823
2008-03-03,292259
2008-03-04,328213
2008-03-05,248360
2008-03-06,213157
2008-03-07,432107
2008-03-08,478176
2008-03-09,338351
2008-03-10,258571
2008-03-11,161713
2008-03-12,199416
2008-03-13,166095
2008-03-14,233746
2008-03-15,337340
2008-03-16,255591
2008-03-17,213921
2008-03-18,380973
2008-03-19,507369
2008-03-20,321732
2008-03-21,350436
2008-03-22,210375
2008-03-23,166403
2008-03-24,155878
2008-03-25,250814
2008-03-26,158874
2008-03-27,277218
2008-03-28,299229
2008-03-29,166835
2008-03-30,342080
2008-03-31,511312
2008-04-01,337714
2008-04-02,156010
2008-04-03,224781
2008-04-04,568989
2008-04-05,206036
2008-04-06,283556
2008-04-07,179239
2008-04-08,159650
2008-04-09,199709
2008-04-10,404082
2008-04-11,361777
2008-04-12,175466
2008-04-13,174294
2008-04-14,136333
2008-04-15,102687
2008-04-16,125967
2008-04-17,137069
2008-04-18,166045
2008-04-19,124697
2008-04-20,212308
2008-04-21,148696
2008-04-22,101000
2008-04-23,142749
2008-04-24,151886
2008-04-25,299561
2008-04-26,159055
2008-04-27,160645
2008-04-28,342358
2008-04-29,136357
2008-04-30,110746
2008-05-01,155989
2008-05-02,495912
2008-05-03,144236
2008-05-04,148163
2008-05-05,159218
2008-05-06,126368
2008-05-07,199714
2008-05-08,158437
2008-05-09,249442
2008-05-10,101453
2008-05-11,224555
2008-05-12,266230
2008-05-13,127270
2008-05-14,199165
2008-05-15,177012
2008-05-16,298814
2008-05-17,96043
2008-05-18,180781
2008-05-19,179031
2008-05-20,233559
2008-05-21,153795
2008-05-22,293605
2008-05-23,232245
2008-05-24,116993
2008-05-25,68483
2008-05-26,125223
2008-05-27,337000
2008-05-28,136781
2008-05-29,122349
2008-05-30,235455
2008-05-31,232851
2008-06-01,184239
2008-06-02,121768
2008-06-03,250370
2008-06-04,407585
2008-06-05,346043
2008-06-06,358238
2008-06-07,213048
2008-06-08,403606
2008-06-09,371661
2008-06-10,335216
2008-06-11,240100
2008-06-12,245332
2008-06-13,363923
2008-06-14,253204
2008-06-15,369018
2008-06-16,430616
2008-06-17,255261
2008-06-18,234037
2008-06-19,344259
2008-06-20,242161
2008-06-21,221915
2008-06-22,416855
2008-06-23,386194
2008-06-24,235205
2008-06-25,260548
2008-06-26,428710
2008-06-27,379415
2008-06-28,264821
2008-06-29,426092
2008-06-30,217630
2008-07-01,148793
2008-07-02,256315
2008-07-03,180284
2008-07-04,83389
2008-07-05,137595
2008-07-06,404884
2008-07-07,380554
2008-07-08,386291
2008-07-09,299926
2008-07-10,397706
2008-07-11,226386
2008-07-12,222516
2008-07-13,545275
2008-07-14,268835
2008-07-15,192476
2008-07-16,158981
2008-07-17,190439
2008-07-18,240358
2008-07-19,191445
2008-07-20,303036
2008-07-21,243838
2008-07-22,272521
2008-07-23,404564
2008-07-24,410409
2008-07-25,249054
2008-07-26,174479
2008-07-27,458206
2008-07-28,248612
2008-07-29,208409
2008-07-30,220033
2008-07-31,430584
2008-08-01,280923
2008-08-02,334578
2008-08-03,240397
2008-08-04,397401
2008-08-05,293294
2008-08-06,217608
2008-08-07,352340
2008-08-08,283283
2008-08-09,180016
2008-08-10,375855
2008-08-11,400237
2008-08-12,194923
2008-08-13,164443
2008-08-14,304761
2008-08-15,426221
2008-08-16,234661
2008-08-17,181185
2008-08-18,183183
2008-08-19,121396
2008-08-20,208222
2008-08-21,229580
2008-08-22,185158
2008-08-23,121466
2008-08-24,143654
2008-08-25,227878
2008-08-26,225106
2008-08-27,116682
2008-08-28,145522
2008-08-29,195197
2008-08-30,87612
2008-08-31,59485
2008-09-01,84689
2008-09-02,88586
2008-09-03,67733
2008-09-04,146161
2008-09-05,85778
2008-09-06,99116
2008-09-07,104772
2008-09-08,188049
2008-09-09,227704
2008-09-10,78895
2008-09-11,117252
2008-09-12,252262
2008-09-13,126807
2008-09-14,224688
2008-09-15,174558
2008-09-16,88702
2008-09-17,84179
2008-09-18,86266
2008-09-19,100524
2008-09-20,53809
2008-09-21,135987
2008-09-22,98448
2008-09-23,62921
2008-09-24,89997
2008-09-25,137140
2008-09-26,233070
2008-09-27,76256
2008-09-28,145365
2008-09-29,114590
2008-09-30,78756
2008-10-01,68908
2008-10-02,103370
2008-10-03,116519
2008-10-04,66030
2008-10-05,120255
2008-10-06,80214
2008-10-07,101998
2008-10-08,157110
2008-10-09,124526
2008-10-10,118820
2008-10-11,75723
2008-10-12,108076
2008-10-13,98516
2008-10-14,67277
2008-10-15,161586
2008-10-16,159578
2008-10-17,127578
2008-10-18,56201
2008-10-19,103429
2008-10-20,94412
2008-10-21,77083
2008-10-22,142942
2008-10-23,104811
2008-10-24,271700
2008-10-25,116679
2008-10-26,162593
2008-10-27,171405
2008-10-28,171534
2008-10-29,88151
2008-10-30,97112
2008-10-31,95650
2008-11-01,86255
2008-11-02,159172
2008-11-03,108418
2008-11-04,72899
2008-11-05,84189
2008-11-06,207920
2008-11-07,184794
2008-11-08,88502
2008-11-09,166324
2008-11-10,106959
2008-11-11,127670
2008-11-12,137153
2008-11-13,214117
2008-11-14,339143
2008-11-15,105959
2008-11-16,130412
2008-11-17,86408
2008-11-18,70364
2008-11-19,94538
2008-11-20,126414
2008-11-21,160187
2008-11-22,113759
2008-11-23,75872
2008-11-24,90505
2008-11-25,107865
2008-11-26,98192
2008-11-27,45308
2008-11-28,49519
2008-11-29,147211
2008-11-30,642023
2008-12-01,603887
2008-12-02,150219
2008-12-03,118069
2008-12-04,144526
2008-12-05,111734
2008-12-06,80489
2008-12-07,156822
2008-12-08,109980
2008-12-09,170950
2008-12-10,283659
2008-12-11,261031
2008-12-12,185618
2008-12-13,99350
2008-12-14,205691
2008-12-15,358041
2008-12-16,347680
2008-12-17,406280
2008-12-18,463303
2008-12-19,661987
2008-12-20,537477
2008-12-21,629758
2008-12-22,563393
2008-12-23,641760
2008-12-24,491936
2008-12-25,197503
2008-12-26,543688
2008-12-27,561940
2008-12-28,297326
2008-12-29,168919
2008-12-30,227074
2008-12-31,215284
```

**Interfaze SDK · typescript**

```typescript
import { responseFormat } from "interfaze";
import { z } from "zod";

const ForecastSchema = z.object({
	prediction: z.array(
		z.object({
			date: z.string(),
			departure_total_delay_mins: z.string(),
		})
	),
});

const response = await interfaze.chat.completions.create({
	messages: [
		{
			role: "user",
			content:
				"Forecast the next 10 days of this: https://r2public.jigsawstack.com/interfaze/examples/prediction-example.csv",
		},
	],
	response_format: responseFormat(z.toJSONSchema(ForecastSchema), "forecast_schema"),
});

console.log(JSON.parse(response.choices[0]?.message.content ?? "{}"));

console.log("Forecast Results:", response.precontext?.[0]?.result);
```

**Vercel AI SDK · typescript**

```typescript
import { generateObject } from "ai";
import { z } from "zod";

const ForecastSchema = z.object({
	prediction: z.array(
		z.object({
			date: z.string(),
			departure_total_delay_mins: z.string(),
		})
	),
});

const { object, providerMetadata } = await generateObject({
	model: interfaze("interfaze-beta"),
	schema: ForecastSchema,
	messages: [
		{
			role: "user",
			content:
				"Forecast the next 10 days of this: https://r2public.jigsawstack.com/interfaze/examples/prediction-example.csv",
		},
	],
});

console.log(object);
console.log("Forecast Results:", providerMetadata?.interfaze?.precontext?.[0]?.result);
```

**LangChain SDK · typescript**

```typescript
import { z } from "zod";

const ForecastSchema = z.object({
	prediction: z.array(
		z.object({
			date: z.string(),
			departure_total_delay_mins: z.string(),
		})
	),
});

const structuredModel = interfaze.withStructuredOutput(ForecastSchema, { includeRaw: true });

const response = await structuredModel.invoke([
	{
		role: "user",
		content:
			"Forecast the next 10 days of this: https://r2public.jigsawstack.com/interfaze/examples/prediction-example.csv",
	},
]);

console.log(response.parsed);

console.log("Forecast Results:", response.raw.response_metadata.precontext?.[0]?.result);
```

**Interfaze SDK · python**

```python
from pydantic import BaseModel

class Prediction(BaseModel):
    date: str
    departure_total_delay_mins: str

class ForecastSchema(BaseModel):
    prediction: list[Prediction]

response = interfaze.chat.completions.parse(
    messages=[
        {
            "role": "user",
            "content": "Forecast the next 10 days of this: https://r2public.jigsawstack.com/interfaze/examples/prediction-example.csv",
        }
    ],
    response_format=ForecastSchema,
)

print(response.choices[0].message.parsed)

print("Forecast Results:", response.precontext[0].result if response.precontext else None)
```

**LangChain SDK · python**

```python
from langchain_core.messages import HumanMessage
from pydantic import BaseModel

class Prediction(BaseModel):
    date: str
    departure_total_delay_mins: str

class ForecastSchema(BaseModel):
    prediction: list[Prediction]

structured_llm = interfaze.with_structured_output(ForecastSchema, include_raw=True)

response = structured_llm.invoke([
    HumanMessage(content="Forecast the next 10 days of this: https://r2public.jigsawstack.com/interfaze/examples/prediction-example.csv")
])

print(response["parsed"])

print("Forecast Results:", response["raw"].response_metadata.get("precontext"))
```

**JSON output**

```json
{
  "object": {
    "prediction": [
      { "date": "2009-01-01 00:00:00", "departure_total_delay_mins": "229913" },
      { "date": "2009-01-02 00:00:00", "departure_total_delay_mins": "243934" },
      { "date": "2009-01-03 00:00:00", "departure_total_delay_mins": "209254" },
      { "date": "2009-01-04 00:00:00", "departure_total_delay_mins": "227642" },
      { "date": "2009-01-05 00:00:00", "departure_total_delay_mins": "224595" },
      { "date": "2009-01-06 00:00:00", "departure_total_delay_mins": "210210" },
      { "date": "2009-01-07 00:00:00", "departure_total_delay_mins": "205004" },
      { "date": "2009-01-08 00:00:00", "departure_total_delay_mins": "223907" },
      { "date": "2009-01-09 00:00:00", "departure_total_delay_mins": "236490" },
      { "date": "2009-01-10 00:00:00", "departure_total_delay_mins": "200850" }
    ]
  },
  "response": {
    "id": "req-5b4f9b83-5157-47b5-b3e7-abd5e2117952",
    "modelId": "interfaze-beta",
    "body": {
      "id": "req-5b4f9b83-5157-47b5-b3e7-abd5e2117952",
      "object": "chat.completion",
      "model": "interfaze-beta",
      "usage": {
        "prompt_tokens": 20769,
        "completion_tokens": 4676,
        "total_tokens": 25445
      },
      "precontext": [
        {
          "name": "forecast",
          "result": {
            "predictions": [
              { "date": "2009-01-01 00:00:00", "value": 229913 },
              { "date": "2009-01-02 00:00:00", "value": 243934 },
              { "date": "2009-01-03 00:00:00", "value": 209254 },
              { "date": "2009-01-04 00:00:00", "value": 227642 },
              { "date": "2009-01-05 00:00:00", "value": 224595 },
              { "date": "2009-01-06 00:00:00", "value": 210210 },
              { "date": "2009-01-07 00:00:00", "value": 205004 },
              { "date": "2009-01-08 00:00:00", "value": 223907 },
              { "date": "2009-01-09 00:00:00", "value": 236490 },
              { "date": "2009-01-10 00:00:00", "value": 200850 }
            ]
          }
        }
      ]
    }
  },
  "finishReason": "stop",
  "usage": {
    "inputTokens": 20769,
    "outputTokens": 4676,
    "totalTokens": 25445
  }
}
```

`precontext` contains the forecast computation used by the model to produce the final structured response.

## Run timeseries forecast task with raw output

Running forecast as a single task with `<task>forecast</task>` in the system message makes it cheaper and faster with a fixed structured output that's pre-defined.

Learn more about [running a task](https://interfaze.ai/docs/run-tasks).

**Interfaze SDK · typescript**

```typescript
// for a hosted CSV, use interfaze.tasks.forecast(csvUrl, { periods: 7, unit: "days" })
const salesData = `date,sales_revenue
2024-06-01,18450
2024-06-02,16720
2024-06-03,9840
2024-06-04,10230
2024-06-05,11150
2024-06-06,12480
2024-06-07,15630
2024-06-08,19240
2024-06-09,17010
2024-06-10,9560
2024-06-11,10870
2024-06-12,11320
2024-06-13,12940
2024-06-14,16110
2024-06-15,20330
2024-06-16,17650
2024-06-17,10120
2024-06-18,10640
2024-06-19,11890
2024-06-20,13260
2024-06-21,16780
2024-06-22,21040
2024-06-23,18220
2024-06-24,10440
2024-06-25,11230
2024-06-26,12070
2024-06-27,13580
2024-06-28,17250
2024-06-29,21580
2024-06-30,18790`;

const response = await interfaze.chat.completions.create({
	task: "forecast",
	messages: [
		{
			role: "user",
			content: `Forecast the next 7 days of daily sales revenue for this retail store:\n${salesData}`,
		},
	],
});

const { result } = JSON.parse(response.choices[0]?.message.content ?? "{}");
console.log(result);
```

**Vercel AI SDK · typescript**

```typescript
import { generateObject } from "ai";

const salesData = `date,sales_revenue
2024-06-01,18450
2024-06-02,16720
2024-06-03,9840
2024-06-04,10230
2024-06-05,11150
2024-06-06,12480
2024-06-07,15630
2024-06-08,19240
2024-06-09,17010
2024-06-10,9560
2024-06-11,10870
2024-06-12,11320
2024-06-13,12940
2024-06-14,16110
2024-06-15,20330
2024-06-16,17650
2024-06-17,10120
2024-06-18,10640
2024-06-19,11890
2024-06-20,13260
2024-06-21,16780
2024-06-22,21040
2024-06-23,18220
2024-06-24,10440
2024-06-25,11230
2024-06-26,12070
2024-06-27,13580
2024-06-28,17250
2024-06-29,21580
2024-06-30,18790`;

const { object } = await generateObject({
	model: interfaze("interfaze-beta"),
	output: "no-schema",
	system: "<task>forecast</task>",
	messages: [
		{
			role: "user",
			content: `Forecast the next 7 days of daily sales revenue for this retail store:\n${salesData}`,
		},
	],
});

console.log(object);
```

**LangChain SDK · typescript**

```typescript
import { HumanMessage, SystemMessage } from "@langchain/core/messages";

const salesData = `date,sales_revenue
2024-06-01,18450
2024-06-02,16720
2024-06-03,9840
2024-06-04,10230
2024-06-05,11150
2024-06-06,12480
2024-06-07,15630
2024-06-08,19240
2024-06-09,17010
2024-06-10,9560
2024-06-11,10870
2024-06-12,11320
2024-06-13,12940
2024-06-14,16110
2024-06-15,20330
2024-06-16,17650
2024-06-17,10120
2024-06-18,10640
2024-06-19,11890
2024-06-20,13260
2024-06-21,16780
2024-06-22,21040
2024-06-23,18220
2024-06-24,10440
2024-06-25,11230
2024-06-26,12070
2024-06-27,13580
2024-06-28,17250
2024-06-29,21580
2024-06-30,18790`;

const response = await interfaze.invoke([
	new SystemMessage("<task>forecast</task>"),
	new HumanMessage(`Forecast the next 7 days of daily sales revenue for this retail store:\n${salesData}`),
]);

console.log(JSON.parse(response.content as string));
```

**Interfaze SDK · python**

```python
import json

# for a hosted CSV, use interfaze.tasks.forecast(csv_url, periods=7, unit="days")
sales_data = """date,sales_revenue
2024-06-01,18450
2024-06-02,16720
2024-06-03,9840
2024-06-04,10230
2024-06-05,11150
2024-06-06,12480
2024-06-07,15630
2024-06-08,19240
2024-06-09,17010
2024-06-10,9560
2024-06-11,10870
2024-06-12,11320
2024-06-13,12940
2024-06-14,16110
2024-06-15,20330
2024-06-16,17650
2024-06-17,10120
2024-06-18,10640
2024-06-19,11890
2024-06-20,13260
2024-06-21,16780
2024-06-22,21040
2024-06-23,18220
2024-06-24,10440
2024-06-25,11230
2024-06-26,12070
2024-06-27,13580
2024-06-28,17250
2024-06-29,21580
2024-06-30,18790"""

response = interfaze.chat.completions.create(
    task="forecast",
    messages=[
        {
            "role": "user",
            "content": f"Forecast the next 7 days of daily sales revenue for this retail store:\n{sales_data}",
        },
    ],
)

print(json.loads(response.choices[0].message.content or "{}").get("result"))
```

**LangChain SDK · python**

```python
import json
from langchain_core.messages import SystemMessage, HumanMessage

sales_data = """date,sales_revenue
2024-06-01,18450
2024-06-02,16720
2024-06-03,9840
2024-06-04,10230
2024-06-05,11150
2024-06-06,12480
2024-06-07,15630
2024-06-08,19240
2024-06-09,17010
2024-06-10,9560
2024-06-11,10870
2024-06-12,11320
2024-06-13,12940
2024-06-14,16110
2024-06-15,20330
2024-06-16,17650
2024-06-17,10120
2024-06-18,10640
2024-06-19,11890
2024-06-20,13260
2024-06-21,16780
2024-06-22,21040
2024-06-23,18220
2024-06-24,10440
2024-06-25,11230
2024-06-26,12070
2024-06-27,13580
2024-06-28,17250
2024-06-29,21580
2024-06-30,18790"""

response = interfaze.invoke([
    SystemMessage(content="<task>forecast</task>"),
    HumanMessage(content=f"Forecast the next 7 days of daily sales revenue for this retail store:\n{sales_data}"),
])

print(json.loads(response.content))
```

**Output**

The response is the raw task result with the `name` of the task and a fixed `result` schema.

```json
{
  "object": {
    "name": "forecast",
    "result": {
      "predictions": [
        { "date": "2024-07-01 00:00:00", "value": 10680 },
        { "date": "2024-07-02 00:00:00", "value": 11460 },
        { "date": "2024-07-03 00:00:00", "value": 12310 },
        { "date": "2024-07-04 00:00:00", "value": 13820 },
        { "date": "2024-07-05 00:00:00", "value": 17540 },
        { "date": "2024-07-06 00:00:00", "value": 21930 },
        { "date": "2024-07-07 00:00:00", "value": 19060 }
      ]
    }
  }
}
```
