
处理时间序列数据总会带来一套难题。每个数据科学家最终都会遇到那个墙,传统方法开始让人觉得......限制。
但如果你能突破这些极限,通过构建、调试和验证高级预测模型,使用恰到好处的提示,会怎样?
大型语言模型(LLM)正在改变时间序列建模的格局。当你将它们与智能、结构化的提示工程结合时,可以帮助你探索大多数分析师尚未考虑的方法。
他们可以指导你完成ARIMA的设置、Prophet调优,甚至是深度学习架构,比如LSTM和变压器。
本指南介绍了用于模型开发、验证和解释的高级提示技术。最终,你会得到一套实用的提示,帮助你更快、更有信心地构建、比较和微调模型。
这里的内容都基于研究和真实案例,所以你会带着现成可用的工具离开。
这是两部分系列文章中的第二篇,探讨提示工程如何提升你的时间序列分析:
第一部分:时间序列核心策略提示 第二部分:高级模型开发提示(本文)
本文及前一篇文章中的所有提示都可以在本文末尾作为速查表查阅
本文内容:
高级模型开发提示 模型验证与解释提示 现实世界实现示例 最佳实践与高级技巧 提示工程速查表!
1. 高级模型开发提示
我们先从重量级人物说起。你可能知道,ARIMA和Prophet依然非常适合结构化且可解释的工作流程,而LSTM和变换器则非常适合复杂、非线性动力学。
最棒的是?有了合适的提示,你能节省大量时间,因为大型语言模型就像你的私人助手,可以设置、调试和检查每一步,不会迷失方向。
1.1 ARIMA模型选择与验证
在继续之前,先确认经典基线是稳固的。请使用下面的提示来识别正确的ARIMA结构,验证假设,并锁定一个可信赖的预测流程,以便与其他数据进行比较。
全面的ARIMA建模提示:
"You are an expert time series modeler. Help me build and validate an ARIMA model:
Dataset: [desc ription]
Data: [sample of time series]
Phase 1 - Model Identification:
1. Test for stationarity (ADF, KPSS tests)
2. Apply differencing if needed
3. Plot ACF/PACF to determine initial (p,d,q) parameters
4. Use information criteria (AIC, BIC) for model selection
Phase 2 - Model Estimation:
1. Fit ARIMA(p,d,q) model
2. Check parameter significance
3. Validate model assumptions:
- Residual analysis (white noise, normality)
- Ljung-Box test for autocorrelation
- Jarque-Bera test for normality
Phase 3 - Forecasting & Evaluation:
1. Generate forecasts with confidence intervals
2. Calculate forecast accuracy metrics (MAE, MAPE, RMSE)
3. Perform walk-forward validation
Provide complete Python code with explanations."
1.2 先知模型配置
有明确的节日、明确的季节节奏,或者想要“优雅应对”的变更点吗?先知是你的朋友。
下面的提示框定了业务背景,调整季节性,构建了一个经过交叉验证的设置,使你能够信任生产环境中的输出。
先知模型设置提示:
"As a Facebook Prophet expert, help me configure and tune a Prophet model:
Business context: [specify domain]
Data characteristics:
- Frequency: [daily/weekly/etc.]
- Historical period: [time range]
- Known seasonalities: [daily/weekly/yearly]
- Holiday effects: [relevant holidays]
- Trend changes: [known changepoints]
Configuration tasks:
1. Data preprocessing for Prophet format
2. Seasonality configuration:
- Yearly, weekly, daily seasonality settings
- Custom seasonal components if needed
3. Holiday modeling for [country/region]
4. Changepoint detection and prior settings
5. Uncertainty interval configuration
6. Cross-validation setup for hyperparameter tuning
Sample data: [provide time series]
Provide Prophet model code with parameter explanations and validation approach."
1.3 LSTM与深度学习模型指导
当你的系列混乱、非线性或多变量且有长距离相互作用时,就是升级的时候了。
请使用下方的LSTM提示,打造一个端到端深度学习流水线,从预处理到训练技巧,这些技巧可以从概念验证扩展到生产。
LSTM架构设计提示:
"You are a deep learning expert specializing in time series. Design an LSTM architecture for my forecasting problem:
Problem specifications:
- Input sequence length: [lookback window]
- Forecast horizon: [prediction steps]
- Features: [number and types]
- Dataset size: [training samples]
- Computational constraints: [if any]
Architecture considerations:
1. Number of LSTM layers and units per layer
2. Dropout and regularization strategies
3. Input/output shapes for multivariate series
4. Activation functions and optimization
5. Loss function selection
6. Early stopping and learning rate scheduling
Provide:
- TensorFlow/Keras implementation
- Data preprocessing pipeline
- Training loop with validation
- Evaluation metrics calculation
- Hyperparameter tuning suggestions"
2. 模型验证与解释
你知道优秀的模型既准确、可靠又可解释。
本节帮助你对性能进行压力测试,分析模型真正学习的内容。先进行强有力的交叉验证,然后深入诊断,这样你才能信任数字背后的故事。
2.1 时间序列交叉验证
前进验证提示:
"Design a robust validation strategy for my time series model:
Model type: [ARIMA/Prophet/ML/Deep Learning]
Dataset: [size and time span]
Forecast horizon: [short/medium/long term]
Business requirements: [update frequency, lead time needs]
Validation approach:
1. Time series split (no random shuffling)
2. Expanding window vs sliding window analysis
3. Multiple forecast origins testing
4. Seasonal validation considerations
5. Performance metrics selection:
- Scale-dependent: MAE, MSE, RMSE
- Percentage errors: MAPE, sMAPE
- Scaled errors: MASE
- Distributional accuracy: CRPS
Provide Python implementation for:
- Cross-validation splitters
- Metrics calculation functions
- Performance comparison across validation folds
- Statistical significance testing for model comparison"
2.2 模型解释与诊断
残差是干净的吗?音程有校准吗?哪些功能重要?下面的提示为你提供了详尽的诊断路径,让你的模型承担责任。
全面模型诊断提示:
"Perform thorough diagnostics for my time series model:
Model: [specify type and parameters]
Predictions: [forecast results]
Residuals: [model residuals]
Diagnostic tests:
1. Residual Analysis:
- Autocorrelation of residuals (Ljung-Box test)
- Normality tests (Shapiro-Wilk, Jarque-Bera)
- Heteroscedasticity tests
- Independence assumption validation
2. Model Adequacy:
- In-sample vs out-of-sample performance
- Forecast bias analysis
- Prediction interval coverage
- Seasonal pattern capture assessment
3. Business Validation:
- Economic significance of forecasts
- Directional accuracy
- Peak/trough prediction capability
- Trend change detection
4. Interpretability:
- Feature importance (for ML models)
- Component analysis (for decomposition models)
- Attention weights (for transformer models)
Provide diagnostic code and interpretation guidelines."
3. 现实世界实现示例
所以,我们探讨了提示如何指导你的建模工作流程,但你如何真正使用它们呢?
我现在给你展示一个快速且可复现的例子,说明你如何在训练时间序列模型后,直接在自己的笔记本中使用其中一个提示。
这个想法很简单:我们将使用本文中的一个提示(前进验证提示),发送到 OpenAI API,让大型语言模型直接在您的分析工作流程中提供反馈或代码建议。
步骤1:创建一个小型辅助函数,向API发送提示
这个函数 ,,通过你的 API 密钥连接到 OpenAI 的 Responses API,并发送提示内容。ask_llm()
别忘了你的!运行前你应该先把它保存在环境变量里。OPENAI_API_KEY
之后,你可以放下文章中的任何提示,获取建议甚至代码,准备运行。
# %pip -q install openai # Only if you don't already have the SDK
import os
from openai import OpenAI
def ask_llm(prompt_text, model="gpt-4.1-mini"):
"""
Sends a single-user-message prompt to the Responses API and returns text.
Switch 'model' to any available text model in your account.
"""
api_key = os.getenv("OPENAI_API_KEY")
if not api_key:
print("Set OPENAI_API_KEY to enable LLM calls. Skipping.")
return None
client = OpenAI(api_key=api_key)
resp = client.responses.create(
model=model,
input=[{"role": "user", "content": prompt_text}]
)
return getattr(resp, "output_text", None)
假设你的模型已经训练好了,所以你可以用通俗易懂的英语描述你的设置,并通过提示模板发送。
在这种情况下,我们将使用“向前行验证提示”,让大型语言模型生成一个稳健的验证方法和相关代码思路。
walk_forward_prompt = f"""
Design a robust validation strategy for my time series model:
Model type: ARIMA/Prophet/ML/Deep Learning (we used SARIMAX with exogenous regressors)
Dataset: Daily synthetic retail sales; 730 rows from 2022-01-01 to 2024-12-31
Forecast horizon: 14 days
Business requirements: short-term accuracy, weekly update cadence
Validation approach:
1. Time series split (no random shuffling)
2. Expanding window vs sliding window analysis
3. Multiple forecast origins testing
4. Seasonal validation considerations
5. Performance metrics selection:
- Scale-dependent: MAE, MSE, RMSE
- Percentage errors: MAPE, sMAPE
- Scaled errors: MASE
- Distributional accuracy: CRPS
Provide Python implementation for:
- Cross-validation splitters
- Metrics calculation functions
- Performance comparison across validation folds
- Statistical significance testing for model comparison
"""
wf_advice = ask_llm(walk_forward_prompt)
print(wf_advice or "(LLM call skipped)")
一旦运行这个单元格,LLM的响应就会直接出现在你的笔记本中,通常是简短的指南或代码片段,你可以复制、调整和测试。
这是一个简单的工作流程,但出乎意料地强大:你不再在文档和实验之间切换上下文,而是直接把模型循环到笔记本里。
你可以用之前的提示重复同样的模式,比如更换综合模型诊断提示,让大型语言模型解释残差或建议预测改进。
4. 最佳实践与高级技巧
4.1 提示优化策略
迭代提示的精炼:
从基础提示开始,逐步增加复杂度,不要一开始就追求完美。 测试不同的提示结构(角色扮演与直接指导等) 验证提示在不同数据集中的效果 使用少数样本学习并结合相关示例 始终添加领域知识和业务背景!
关于代币效率(如果成本是考虑因素):
尽量在信息完整性和代币使用之间保持平衡 使用基于补丁的方法来减少输入大小 为重复模式实现提示缓存 请与团队一起权衡准确性与计算成本之间的权衡
别忘了多做诊断,确保结果可靠,并随着数据和业务问题的变化不断完善提示。记住,这是一个反复练习的过程,而不是一开始就追求完美。
感谢阅读!
引用
MingyuJ666/带大型语言模型的时间序列预测:[KDD Explore'24]使用大型语言模型的时间序列预测:理解与增强模型能力
用于预测分析和时间序列预测的大型语言模型
更智能的时间序列预测,更少努力
通过基于补丁的提示和分解,用LLM预测时间序列
时间序列中的大型语言模型:人工智能中数据分析的转型
kdd.org/exploration_files/p109-Time_Series_Forecasting_with_LLMs.pdf


雷达卡





京公网安备 11010802022788号







