https://zhuanlan.zhihu.com/p/425224434
時間序列
- 參考網址 (資源整合網址)
https://zhuanlan.zhihu.com/p/396504683
https://zhuanlan.zhihu.com/p/385094015
https://analyticsindiamag.com/top-10-python-tools-for-time-series-analysis/
https://machinelearningmastery.com/time-series-forecasting-methods-in-python-cheat-sheet/
https://github.com/MaxBenChrist/awesome_time_series_in_python
包列表
package name | 標簽 | bref introduction |
---|---|---|
Arrow | 時間格式修改 | 方便修改時間表示形式 |
Featuretools | 自動特征工程 | 固定特征模板,不一定有業務意義 |
TSFRESH | 自動特征工程 | 自動抽取時間序列特征 |
PyFlux | 模型,傳統模型 | 傳統的時間序列模型 |
TimeSynth | 模擬數據 | 用於模擬時間序列的生成 |
Sktime | 模型,機器學習 | 類sklearn調佣方式的時間序列處理器,模型偏機器學習 |
Darts | 模型,傳統模型 | 傳統模型到深度學習模型都有, |
Orbit | 模型,貝葉斯 | 貝葉斯模型 ,Uber出品 |
AutoTS | 模型,深度學習,AutoML | 自動化 測試多種模型 並給出預測結果,包括深度學習 |
Prophet | 模型,季節性特征 | facebook的開源時間序列處理,適合有季節性 的數據 |
AtsPy | 模型,深度學習,傳統模型 | 自動化實現多個模型,傳統及深度學習 |
kats | 模型 | 傳統模型及facebook自己的模型,深度學習暫時少 |
gluon-ts | 模型,深度學習 | 亞馬遜 的包,基於MXNET ,深度學習模型多 |
AutoGluon | 模型,AutoML,深度學習 | 亞馬遜的包,基於MXNET,AutoML,文本、圖像、表格數據都可以。 |
GENDIS | 模型,shaplet | shaplet構建距離特征,進行分類 |
Flow Forecast | 模型,深度學習 | 深度學習為主,pytorch框架,都是常見STOA模型 |
pandas-ta | quant,特征工程 | 技術指標 計算,基於ta-lib搭建 |
PyTorch_Forecasting | 模型,深度學習 | 將時間序列的STOA模型基於pytorch實現 |
statsmodels | 模型,傳統模型 | 基於scipy,傳統的時間序列模型,模型很全 |
stumpy | 特征工程 | 用於構造時間序列的特征,能夠為某一個特定的時間序列構造特征向量。 |
TA-Lib | quant,特征工程 | 技術指標 計算 |
ta | quant,特征工程 | 技術指標 計算 |
tslearn | 模型,傳統模型 | 傳統的模型,類似sklearn的處理方法。 |
tsmoothie | 數據預處理,模型 | 用於對時間序列進行平滑,去除異常點。 |
tsmoothie
Description
用於對時間序列進行平滑,去除異常點。
A python library for time-series smoothing and outlier detection in a vectorized way.
數據預處理目的:
- Time Series Smoothing for better Clustering
- Time Series Smoothing for better Forecasting
- Real-Time Time Series Anomaly Detection
- Extreme Event Time Series Preprocessing
- Time Series Bootstrap in the age of Deep Learning
installation
pip install --upgrade tsmoothie
document
https://github.com/cerlymarco/tsmoothie
tslearn
Description
傳統的模型,類似sklearn的處理方法。
功能包括:
|data|processing|clustering|classification|regression|metrics| |---|---|---|---|---|---| |UCR Datasets|Scaling|TimeSeriesKMeans|KNN Classifier|KNN Regressor|Dynamic Time Warping| |Generators|Piecewise|KShape|TimeSeriesSVC|TimeSeriesSVR|Global Alignment Kernel| |Conversion(1, 2)| |KernelKmeans|LearningShapelets|MLP|Barycenters| | | | |Early Classification| |Matrix Profile|
installation
There are different alternatives to install tslearn:
- PyPi:
python -m pip install tslearn<br />
- Conda:
conda install -c conda-forge tslearn<br />
- Git:
python -m pip install https://github.com/tslearn-team/tslearn/archive/main.zip
document
https://github.com/tslearn-team/tslearn
ta
Description
用於技術指標的計算。
- 可以計算的技術指標
Volume
- Money Flow Index (MFI)
- Accumulation/Distribution Index (ADI)
- On-Balance Volume (OBV)
- Chaikin Money Flow (CMF)
- Force Index (FI)
- Ease of Movement (EoM, EMV)
- Volume-price Trend (VPT)
- Negative Volume Index (NVI)
- Volume Weighted Average Price (VWAP)
Volatility
- Average True Range (ATR)
- Bollinger Bands (BB)
- Keltner Channel (KC)
- Donchian Channel (DC)
- Ulcer Index (UI)
Trend
- Simple Moving Average (SMA)
- Exponential Moving Average (EMA)
- Weighted Moving Average (WMA)
- Moving Average Convergence Divergence (MACD)
- Average Directional Movement Index (ADX)
- Vortex Indicator (VI)
- Trix (TRIX)
- Mass Index (MI)
- Commodity Channel Index (CCI)
- Detrended Price Oscillator (DPO)
- KST Oscillator (KST)
- Ichimoku Kinkō Hyō (Ichimoku)
- Parabolic Stop And Reverse (Parabolic SAR)
- Schaff Trend Cycle (STC)
Momentum
- Relative Strength Index (RSI)
- Stochastic RSI (SRSI)
- True strength index (TSI)
- Ultimate Oscillator (UO)
- Stochastic Oscillator (SR)
- Williams %R (WR)
- Awesome Oscillator (AO)
- Kaufman's Adaptive Moving Average (KAMA)
- Rate of Change (ROC)
- Percentage Price Oscillator (PPO)
- Percentage Volume Oscillator (PVO)
Others
- Daily Return (DR)
- Daily Log Return (DLR)
- Cumulative Return (CR)
installation
pip install --upgrade ta
document
https://github.com/bukosabino/ta
https://technical-analysis-library-in-python.readthedocs.io/en/latest/
TA-Lib
Description
用於計算金融時間序列的各項指標。
from talib import abstract
# directly
SMA = abstract.SMA
# or by name
SMA = abstract.Function('sma')
installation
安裝問題參考:
https://github.com/mrjbq7/ta-lib
You can install from PyPI:
$ pip install TA-Lib
Or checkout the sources and run `setup.py` yourself:
$ python [setup.py](http://setup.py) install
It also appears possible to install via [Conda Forge](https://anaconda.org/conda-forge/ta-lib):
$ conda install -c conda-forge ta-lib
document
https://github.com/mrjbq7/ta-lib
stumpy
Description
用於構造時間序列的特征,能夠為某一個特定的時間序列構造特征向量。
特征向量含義:https://stumpy.readthedocs.io/en/latest/Tutorial_The_Matrix_Profile.html
installation
python -m pip install stumpy
conda install -c conda-forge stumpy
document
https://github.com/TDAmeritrade/stumpy
https://stumpy.readthedocs.io/en/latest/Tutorial_The_Matrix_Profile.html
statsmodels
Description
基於scipy,傳統的時間序列模型,模型很全
Contains a submodule for classical time series models and hypothesis tests
installation
https://www.statsmodels.org/dev/install.html
pip install statsmodels
conda install -c conda-forge statsmodels
document
https://www.statsmodels.org/devel/
https://github.com/statsmodels/statsmodels#documentation
PyTorch_Forecasting
Description
將時間序列的STOA模型基於pytorch實現
PyTorch Forecasting is a PyTorch-based package for forecasting time series with state-of-the-art network architectures. It provides a high-level API for training networks on pandas data frames and leverages PyTorch Lightning for scalable training on (multiple) GPUs, CPUs and for automatic logging.
適用模型:
https://github.com/jdb78/pytorch-forecasting#available-models
|Name|Covariates|Multiple targets|Regression|Classification|Probabilistic|Uncertainty|Interactions between series|Flexible history length|Cold-start|Required computational resources (1-5, 5=most)| |---|---|---|---|---|---|---|---|---|---|---| |RecurrentNetwork|x|x|x| | | | |x| |2| |DecoderMLP|x|x|x|x| |x| |x|x|1| |NBeats| | |x| | | | | | |1| |DeepAR|x|x|x| |x|x| |x| |3| |TemporalFusionTransformer|x|x|x|x| |x| |x|x|4|
installation
pip install pytorch-forecasting
# 或者
conda install pytorch-forecasting pytorch -c pytorch>=1.7 -c conda-forge
document
https://github.com/jdb78/pytorch-forecasting
https://pytorch-forecasting.readthedocs.io/en/latest/
pandas-ta
Description
基於TA Lib 的封裝,金融時間序列的技術指標,比如macd等。
An easy to use Python 3 Pandas Extension with 130+ Technical Analysis Indicators
installation
pip install pandas_ta
或者最新版
pip install -U git+https://github.com/twopirllc/pandas-ta
document
https://github.com/twopirllc/pandas-ta
指標說明:
https://github.com/twopirllc/pandas-ta#indicators-by-category
Flow Forecast
Description
基本都是深度學習模型,基於pytorch
Flow Forecast is a deep learning for time series forecasting , classification , and anomaly detection framework built in PyTorch
Models currently supported
- Vanilla LSTM : A basic LSTM that is suitable for multivariate time series forecasting and transfer learning.
- Full transformer : The full original transformer with all 8 encoder and decoder blocks. Requires passing the target in at inference.
- Simple Multi-Head Attention : A simple multi-head attention block and linear embedding layers. Suitable for transfer learning.
- Transformer with a linear decoder: A transformer with n-encoder blocks (this is tunable) and a linear decoder.
- DA-RNN: A well rounded model with which utilizes a LSTM + attention.
- Enhancing the Locality and Breaking the Memory Bottleneck of Transformer on Time Series Forecasting:
- Transformer XL:
- Informer: Beyond Efficient Transformer for Long Sequence Time-Series Forecasting
- DeepAR
installation
pip install flood-forecast
document
https://github.com/AIStream-Peelout/flow-forecast
https://flow-forecast.atlassian.net/wiki/spaces/FF/overview
https://flow-forecast.atlassian.net/wiki/spaces/FF/pages/364019713/Training+Models
AutoGluon
Description
AutoML的包,用於自動化的學習,包括文本、圖像、表格數據;
示例
from autogluon.tabular import TabularDataset, TabularPredictor
train_data = TabularDataset('https://autogluon.s3.amazonaws.com/datasets/Inc/train.csv')
test_data = TabularDataset('https://autogluon.s3.amazonaws.com/datasets/Inc/test.csv')
predictor = TabularPredictor(label='class').fit(train_data, time_limit=120) # Fit models for 120s
leaderboard = predictor.leaderboard(test_data)
installation
# First install package from terminal:
python3 -m pip install -U pip
python3 -m pip install -U setuptools wheel
python3 -m pip install -U "mxnet<2.0.0"
python3 -m pip install autogluon # autogluon==0.3.1
document
https://github.com/awslabs/autogluon
https://auto.gluon.ai/stable/tutorials/tabular_prediction/tabular-quickstart.html
GENDIS
Description
能夠自動識別出shaplet(時間序列的子序列),並將與各個序列的距離作為特征,根據特征進行序列的分類處理。
installation
https://github.com/IBCNServices/GENDIS#installation
document
https://github.com/IBCNServices/GENDIS
gluon-ts
Description
有較多深度學習模型 的模塊,亞馬遜的包,基於MXNET。
模型參考:https://github.com/awslabs/gluon-ts/tree/master/src/gluonts/model
installation
pip install --upgrade mxnet~=1.7 gluonts
document
https://github.com/awslabs/gluon-ts
https://github.com/awslabs/gluon-ts/tree/master/src/gluonts
kats
Description
facebook輕量級,可以工業使用的框架,支持模型SARIMA,Prophet,Holt-Winters
支持模型:https://github.com/facebookresearch/Kats/tree/main/kats/models
Kats is a toolkit to analyze time series data, a lightweight , easy-to-use, and generalizable framework to perform time series analysis. Time series analysis is an essential component of Data Science and Engineering work at industry, from understanding the key statistics and characteristics, detecting regressions and anomalies, to forecasting future trends. Kats aims to provide the one-stop shop for time series analysis , including detection, forecasting, feature extraction/embedding , multivariate analysis, etc.
Important links
- Homepage: https://facebookresearch.github.io/Kats/
- Kats Python package: https://pypi.org/project/kats/0.1.0/
- Facebook Engineering Blog Post: https://engineering.fb.com/2021/06/21/open-source/kats/
- Source code repository: https://github.com/facebookresearch/kats
- Contributing: https://github.com/facebookresearch/Kats/blob/master/CONTRIBUTING.md
- Tutorials: https://github.com/facebookresearch/Kats/tree/master/tutorials
installation
pip install --upgrade pip
pip install kats
If you need only a small subset of Kats, you can install a minimal version of Kats with
MINIMAL_KATS=1 pip install kats
document
AtsPy
Description
自動化實現多個模型。
Easily develop state of the art time series models to forecast univariate data series. Simply load your data and select which models you want to test. This is the largest repository of automated structural and machine learning time series models . Please get in contact if you want to contribute a model. This is a fledgling project, all advice appreciated.
Automated Models
ARIMA
- Automated ARIMA ModellingProphet
- Modeling Multiple Seasonality With Linear or Non-linear GrowthHWAAS
- Exponential Smoothing With Additive Trend and Additive SeasonalityHWAMS
- Exponential Smoothing with Additive Trend and Multiplicative SeasonalityNBEATS
- Neural basis expansion analysis (now fixed at 20 Epochs)Gluonts
- RNN-based Model (now fixed at 20 Epochs)TATS
- Seasonal and Trend no Box CoxTBAT
- Trend and Box CoxTBATS1
- Trend, Seasonal (one), and Box CoxTBATP1
- TBATS1 but Seasonal Inference is Hardcoded by PeriodicityTBATS2
- TBATS1 With Two Seasonal Periods
installation
pip install atspy
document
https://github.com/firmai/atspy
Prophet
Description
facebook的開源時間序列處理,適用於有季節性 的數據。
It works best with time series that have strong seasonal effects and several seasons of historical data. (github文檔說明)
Prophet是Facebook研究團隊開發的知名時間序列軟件包,於2017年首次發布,適用於具有 強烈季節性影響 的數據和 多個季節 的歷史數據。它具有高度的用戶友好性和可定制性,只需進行最少的設置。
# Loading the library
import pandas as pd
import matplotlib.pyplot as plt
from fbprophet import Prophet
# Loading the data from the repo:
df = pd.read_csv("https://raw.githubusercontent.com/facebook/prophet/master/examples/example_wp_log_peyton_manning.csv")
# Fitting the model
model = Prophet()
model.fit(df) #fit the model.
# Predict
future = model.make_future_dataframe(periods=730) # predicting for ~ 2 years
forecast = model.predict(future) # Predict future
# Plot results
fig1 = model.plot(forecast) # Plot the fit to past data and future forcast.
fig2 = model.plot_components(forecast) # Plot breakdown of components.
plt.show()
forecast # Displaying various results in table format.
installation
pip install prophet
document
https://github.com/facebook/prophet
https://machinelearningmastery.com/time-series-forecasting-with-prophet-in-python/
https://facebook.github.io/prophet/
https://pypi.org/project/prophet/
AutoT S
Description
AutoTS 是一個自動化的時間序列預測庫,可以使用簡單的代碼訓練多個時間序列模型,此庫的一些最佳功能包括:
- 利用遺傳規划 優化方法尋找最優時間序列預測模型。
- 提供置信區間 預測值的下限和上限。
- 它訓練各種各樣的模型 ,如統計的,機器學習以及深度學習模型
- 它還可以執行最佳模型的自動集成
- 它還可以通過學習最優NaN插補和異常值去除 來處理混亂的數據
- 它可以運行單變量和多變量 時間序列
# also: _hourly, _daily, _weekly, or _yearly
from autots.datasets import load_monthly
df_long = load_monthly(long=True)
from autots import AutoTS
model = AutoTS(
forecast_length=3,
frequency='infer',
ensemble='simple',
max_generations=5,
num_validations=2,
)
model = model.fit(df_long, date_col='datetime', value_col='value', id_col='series_id')
# Print the name of the best model
print(model)
installation
pip install autots
document
https://winedarksea.github.io/AutoTS/build/html/source/tutorial.html
https://github.com/winedarksea/AutoTS
Orbit
Description
Uber開發的時間序列預測包,使用貝葉斯方法,比較特別
目前支持模型
Currently, it supports concrete implementations for the following models : - Exponential Smoothing (ETS) - Local Global Trend (LGT) - Damped Local Trend (DLT) - Kernel Time-based Regression (KTR)
支持優化方法
It also supports the following sampling/optimization methods for model estimation/inferences:
Markov-Chain Monte Carlo (MCMC) as a full sampling method Maximum a Posteriori (MAP) as a point estimate method Variational Inference (VI) as a hybrid-sampling method on approximate distribution
installation
pip install orbit-ml
document
For details, check out our documentation and tutorials:
- HTML (stable): https://orbit-ml.readthedocs.io/en/stable/
- HTML (latest): https://orbit-ml.readthedocs.io/en/latest/
Darts
Description
傳統模型到深度學習模型都有。支持多元時間序列。
支持的模型類型:
|Model|Univariate|Multivariate|Probabilistic|Multiple-series training|Past-observed covariates support|Future-known covariates support|Reference| |---|---|---|---|---|---|---|---| |ARIMA
|x| |x| | | | | |VARIMA
|x|x| | | | | | |AutoARIMA
|x| | | | | | | |ExponentialSmoothing
|x| |x| | | | | |Theta
and FourTheta
|x| | | | | |Theta & 4 Theta| |Prophet
|x| |x| | |x|Prophet repo| |FFT
(Fast Fourier Transform)|x| | | | | | | |RegressionModel
(incl RandomForest
, LinearRegressionModel
and LightGBMModel
)|x|x| |x|x|x| | |RNNModel
(incl. LSTM and GRU); equivalent to DeepAR in its probabilistic version|x|x|x|x| |x|DeepAR paper| |BlockRNNModel
(incl. LSTM and GRU)|x|x| |x|x| | | |NBEATSModel
|x|x| |x|x| |N-BEATS paper| |TCNModel
|x|x|x|x|x| |TCN paper, DeepTCN paper, blog post| |TransformerModel
|x|x| |x|x| | | |Naive Baselines|x| | | | | | |
項目features
Forecasting Models: A large collection of forecasting models; from statistical models (such as ARIMA) to deep learning models (such as N-BEATS). See table of models below.
Data processing: Tools to easily apply (and revert) common transformations on time series data (scaling, boxcox, …)
Metrics: A variety of metrics for evaluating time series' goodness of fit; from R2-scores to Mean Absolute Scaled Error.
Backtesting: Utilities for simulating historical forecasts, using moving time windows.
Regression Models: Possibility to predict a time series from lagged versions of itself and of some external covariate series, using arbitrary regression models (e.g. scikit-learn models).
Multiple series training: All neural networks, as well as RegressionModel
s (incl. LinearRegressionModel
and RandomForest
) support being trained on multiple series.
Past and Future Covariates support: Some models support past-observed and/or future-known covariate time series as inputs for producing forecasts.
Multivariate Support: Tools to create, manipulate and forecast multivariate time series.
Probabilistic Support: TimeSeries
objects can (optionally) represent stochastic time series; this can for instance be used to get confidence intervals.
Filtering Models: Darts offers three filtering models: KalmanFilter
, GaussianProcessFilter
, and MovingAverage
, which allow to filter time series, and in some cases obtain probabilistic inferences of the underlying states/values.
installation
pip install darts
document
https://analyticsindiamag.com/hands-on-guide-to-darts-a-python-tool-for-time-series-forecasting/
https://github.com/unit8co/darts
Sktime
Description
類sklearn的時間序列處理包
About: Sktime is a unified python framework that provides API for machine learning with time series data. The framework also provides scikit-learn compatible tools to build, tune and validate time series models for multiple learning problems, including time series classification, time series regression and forecasting.
from sktime.datasets import load_airline
from sktime.forecasting.base import ForecastingHorizon
from sktime.forecasting.model_selection import temporal_train_test_split
from sktime.forecasting.theta import ThetaForecaster
from sktime.performance_metrics.forecasting import mean_absolute_percentage_error
y = load_airline()
y_train, y_test = temporal_train_test_split(y)
fh = ForecastingHorizon(y_test.index, is_relative=False)
forecaster = ThetaForecaster(sp=12) # monthly seasonal periodicity
forecaster.fit(y_train)
y_pred = forecaster.predict(fh)
mean_absolute_percentage_error(y_test, y_pred)
>>> 0.08661467738190656
installation
pip
pip install sktime
安裝額外依賴
pip install sktime[all_extras]
conda安裝
conda install -c conda-forge sktime
安裝依賴
conda install -c conda-forge sktime-all-extras
document
https://www.sktime.org/en/latest/api_reference.html
https://github.com/alan-turing-institute/sktime
TimeSynth
Description
可以用於生成時間序列的模擬數據
Signal Types
- Harmonic functions(sin, cos or custom functions )
- Gaussian processes with different kernels
- Constant
- Squared exponential
- Exponential
- Rational quadratic
- Linear
- Matern
- Periodic
- Pseudoperiodic signals
- Autoregressive(p) process
- Continuous autoregressive process (CAR)
- Nonlinear Autoregressive Moving Average model (NARMA)
installation
git clone https://github.com/TimeSynth/TimeSynth.git
cd TimeSynth
python setup.py install
document
https://github.com/TimeSynth/TimeSynth
PyFlux
Description
提供傳統的時間序列方法
About: PyFlux is an open source library for time series analysis and prediction. In this library, users can choose from a flexible range of modelling and inference options , and use the output for forecasting and retrospection. The library allows for a probabilistic approach to time series modelling. The latest release version of PyFlux is available on PyPi. Python 2.7 and Python 3.5 are supported, but development occurs primarily on 3.5.
- ARIMA models
- ARIMAX models
- Dynamic Autoregression models
- Dynamic Paired Comparison models
- GARCH models
- Beta-t-EGARCH models
- EGARCH-in-mean models
- EGARCH-in-mean regression models
- Long Memory EGARCH models
- Skew-t-EGARCH models
- Skew-t-EGARCH-in-mean models
- GAS models
- GASX models
- GAS State Space models
- Gaussian State Space models
- Non-Gaussian State Space models
- VAR models
installation
pip install pyflux
document
https://github.com/RJT1990/pyflux
TSFRESH
Description
About: TSFRESH or Time Series Feature extraction based on scalable hypothesis tests is a Python package with various feature extraction methods and a robust feature selection algorithm. The package automatically calculates a large number of time series characteristics and contains methods to evaluate the explaining power and importance of such characteristics for regression or classification tasks. Advantages include: It is compatible with sklearn, pandas and numpy It allows anyone to easily add their favorite features * It both runs on the local machine or even on a cluster
installation
pip install tsfresh
docker pull nbraun/tsfresh
document
https://github.com/blue-yonder/tsfresh
Featuretools
Description
時間序列相關的自動化特征工程。
About: Featuretools is an open source Python library for automated feature engineering. The framework excels at transforming temporal and relational datasets into feature matrices for machine learning. Featuretools references generated features through the feature name. In order to make features easier to understand, Featuretools offers two additional tools, featuretools.graph_feature() and featuretools.describe_feature(), to help explain what a feature is and the steps Featuretools took to generate it.
installation
pip install featuretools
https://featuretools.alteryx.com/en/stable/install.html
document
https://featuretools.alteryx.com/en/stable/getting_started/getting_started_index.html
https://github.com/alteryx/featuretools
Arrow
Description
Arrow is a Python library that offers a sensible and human-friendly approach to creating, manipulating, formatting and converting dates, times and timestamps
用於時間的格式轉換,可以轉換為便於人閱讀的格式。
installation
pip install -U arrow
document
https://github.com/arrow-py/arrow
>>> utc = utc.shift(hours=-1)
>>> utc
<Arrow [2013-05-11T20:23:58.970460+00:00]>
>>> local = utc.to('US/Pacific')
>>> local
<Arrow [2013-05-11T13:23:58.970460-07:00]>
>>> local.timestamp()
1368303838.970460
>>> local.format()
'2013-05-11 13:23:58 -07:00'
>>> local.format('YYYY-MM-DD HH:mm:ss ZZ')
'2013-05-11 13:23:58 -07:00'
>>> local.humanize()
'an hour ago'
>>> local.humanize(locale='ko-kr')
'한시간 전'
ithub: https://github.com/winedarksea/AutoTS
pip install --upgrade pip pip install kats