在预测中,我们通常对预测的分布感兴趣,而不仅仅是点预测,因为我们希望了解预测周围的不确定性概念。

为此,我们可以创建预测区间

预测区间具有直观的解释,因为它表示预测分布的一个特定范围。例如,95% 的预测区间意味着在 100 次中,我们期望未来值有 95 次落在估计范围内。因此,更宽的区间表明预测的不确定性更大,而更窄的区间则表明更高的信心。

使用 TimeGPT,我们可以创建预测分布,并提取所需置信水平的预测区间。

TimeGPT 使用保形预测来生成预测区间。

1. 导入软件包

首先,我们导入所需的软件包并初始化 Nixtla 客户端

import pandas as pd
from nixtla import NixtlaClient
nixtla_client = NixtlaClient(
    # defaults to os.environ.get("NIXTLA_API_KEY")
    api_key = 'my_api_key_provided_by_nixtla'
)

👍 使用 Azure AI 端点

要使用 Azure AI 端点,请设置 base_url 参数

nixtla_client = NixtlaClient(base_url="you azure ai endpoint", api_key="your api_key")

2. 加载数据

df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/air_passengers.csv')
df.head()
timestampvalue
01949-01-01112
11949-02-01118
21949-03-01132
31949-04-01129
41949-05-01121

3. 使用预测区间进行预测

在使用 TimeGPT 进行时间序列预测时,您可以根据您的需求设置预测区间的水平(或多个水平)。以下是操作方法

timegpt_fcst_pred_int_df = nixtla_client.forecast(
    df=df, h=12, level=[80, 90, 99.7], 
    time_col='timestamp', target_col='value',
)
timegpt_fcst_pred_int_df.head()
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Inferred freq: MS
INFO:nixtla.nixtla_client:Restricting input...
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...
timestampTimeGPTTimeGPT-lo-99.7TimeGPT-lo-90TimeGPT-lo-80TimeGPT-hi-80TimeGPT-hi-90TimeGPT-hi-99.7
01961-01-01437.837952415.826484423.783737431.987091443.688812451.892166459.849419
11961-02-01426.062744402.833553407.694092412.704956439.420532444.431396449.291935
21961-03-01463.116577423.434092430.316893437.412564488.820590495.916261502.799062
31961-04-01478.244507444.885193446.776764448.726837507.762177509.712250511.603821
41961-05-01505.646484465.736694471.976787478.409872532.883096539.316182545.556275

📘 Azure AI 中可用的模型

如果您正在使用 Azure AI 端点,请务必设置 model="azureai"

nixtla_client.forecast(..., model="azureai")

对于公共 API,我们支持两种模型:timegpt-1timegpt-1-long-horizon

默认情况下使用 timegpt-1。请参阅本教程,了解如何以及何时使用 timegpt-1-long-horizon

nixtla_client.plot(
    df, timegpt_fcst_pred_int_df, 
    time_col='timestamp', target_col='value',
    level=[80, 90],
)

请务必注意,预测区间水平的选择取决于您的具体用例。对于高风险预测,您可能希望选择更宽的区间以考虑更多的不确定性。对于不太重要的预测,较窄的区间可能是可以接受的。

历史预测

您还可以通过添加 add_history=True 参数来计算历史预测的预测区间,如下所示

timegpt_fcst_pred_int_historical_df = nixtla_client.forecast(
    df=df, h=12, level=[80, 90], 
    time_col='timestamp', target_col='value',
    add_history=True,
)
timegpt_fcst_pred_int_historical_df.head()
INFO:nixtla.nixtla_client:Validating inputs...
INFO:nixtla.nixtla_client:Preprocessing dataframes...
INFO:nixtla.nixtla_client:Inferred freq: MS
INFO:nixtla.nixtla_client:Calling Forecast Endpoint...
INFO:nixtla.nixtla_client:Calling Historical Forecast Endpoint...
timestampTimeGPTTimeGPT-lo-80TimeGPT-lo-90TimeGPT-hi-80TimeGPT-hi-90
01951-01-01135.483673111.937767105.262830159.029579165.704516
11951-02-01144.442413120.896508114.221571167.988319174.663256
21951-03-01157.191910133.646004126.971067180.737815187.412752
31951-04-01148.769379125.223473118.548536172.315284178.990221
41951-05-01140.472946116.927041110.252104164.018852170.693789
nixtla_client.plot(
    df, timegpt_fcst_pred_int_historical_df, 
    time_col='timestamp', target_col='value',
    level=[80, 90],
)