c++ 時間序列工具包
When it comes to time series forecasting, I’m a great believer that the simpler the model, the better.
關于時間序列預測,我堅信模型越簡單越好。
However, not all time series are created equal. Some time series have a strongly defined trend — we often see this with economic data, for instance:
但是,并非所有時間序列都是相同的。 某些時間序列具有明確定義的趨勢-例如,我們經常在經濟數據中看到這一趨勢:
Others show a more stationary-like pattern — e.g. monthly air passenger numbers:
其他人則表現出更平穩的模式,例如每月的航空旅客人數:

The choice of time series model will depend highly on the type of time series one is working with. Here are some of the most useful time series models I’ve encountered.
時間序列模型的選擇將在很大程度上取決于正在使用的時間序列的類型。 這是我遇到的一些最有用的時間序列模型。
1. ARIMA (1. ARIMA)
In my experience, ARIMA tends to be most useful when modelling time series with a strong trend. The model is also adept at modelling seasonality patterns.
以我的經驗,當對具有強烈趨勢的時間序列進行建模時,ARIMA往往最有用。 該模型還擅長對季節性模式進行建模。
Let’s take an example.
讓我們舉個例子。
Suppose we wish to model monthly air passenger numbers over a period of years. The original data is sourced from San Francisco Open Data.
假設我們希望對幾年內的每月航空旅客數量進行建模。 原始數據來自San Francisco Open Data 。
Such a time series will have a seasonal component (holiday seasons tend to have higher passenger numbers, for instance) as well as evidence of a trend as indicated when the series is decomposed as below.
這樣的時間序列將具有季節性成分(例如,假日季節往往會有更高的乘客人數),以及當序列分解如下時所指示的趨勢的證據。

The purpose of using an ARIMA model is to capture the trend as well as account for the seasonality inherent in the time series.
使用ARIMA模型的目的是捕獲趨勢并考慮時間序列固有的季節性。
To do this, one can use the auto.arima function in R, which can select the best fit p, d, q coordinates for the model as well as the appropriate seasonal component.
為此,可以使用R中的auto.arima函數,該函數可以為模型選擇最佳擬合的p,d,q坐標以及適當的季節分量。
For the above example, the model that performed best in terms of the lowest BIC was as follows:
對于上面的示例,就最低BIC而言表現最佳的模型如下:
Series: passengernumbers
ARIMA(1,0,0)(0,1,1)[12]Coefficients:
ar1 sma1
0.7794 -0.5001
s.e. 0.0609 0.0840sigma^2 estimated as 585834: log likelihood=-831.27
AIC=1668.54 AICc=1668.78 BIC=1676.44
Here is a visual of the forecasts.
這是預測的視覺效果。

We can see that ARIMA is adequately forecasting the seasonal pattern in the series. In terms of the model performance, the RMSE (root mean squared error) and MFE (mean forecast error) were as follows:
我們可以看到ARIMA可以充分預測該系列的季節性模式。 在模型性能方面,RMSE(均方根誤差)和MFE(平均預測誤差)如下:
RMSE: 698
RMSE: 698
MFE: -115
MFE: -115
Given a mean of 8,799 passengers per month across the validation set, the errors recorded were quite small in comparison to the average — indicating that the model is performing well in forecasting air passenger numbers.
假設整個驗證集中平均每月有8799名乘客,則記錄的誤差與平均值相比很小,這表明該模型在預測航空乘客人數方面表現良好。
2.先知 (2. Prophet)
Let’s take a look at the air passenger example once again, but this time using Facebook’s Prophet. Prophet is a time series tool that allows for forecasting bsaed on an additive model, and works especially well with data that has strong seasonal trends.
讓我們再來看一次航空乘客示例,但這一次使用Facebook的Prophet 。 Prophet是一個時間序列工具,可用于根據加性模型進行預測,尤其適用于季節性趨勢強烈的數據。
The air passenger dataset appears to fit the bill, so let’s see how the model would perform compared to ARIMA.
航空乘客數據集似乎符合要求,因此讓我們看看與ARIMA相比該模型的性能如何。
In this example, Prophet can be used to identify the long-term trend for air passenger numbers, as well as seasonal fluctuations throughout the year:
在此示例中,可以使用先知來確定航空客運量的長期趨勢以及全年的季節性波動:

prophet_basic = Prophet()
prophet_basic.fit(train_dataset)
A standard Prophet model can be fit to pick up the trend and seasonal components automatically, although these can also be configured manually by the user.
盡管可以由用戶手動配置,但標準的Prophet模型可以適合自動獲取趨勢和季節成分。
One particularly useful component of Prophet is the inclusion of changepoints, or significant structural breaks in a time series.
先知的一個特別有用的組成部分是包含變更點 ,即時間序列中的重大結構中斷。

Through trial and error, 4 changepoints were shown to minimise the MFE and RMSE:
通過反復試驗,顯示了4個更改點以最大程度地減少MFE和RMSE:
pro_change= Prophet(n_changepoints=4)
forecast = pro_change.fit(train_dataset).predict(future)
fig= pro_change.plot(forecast);
a = add_changepoints_to_plot(fig.gca(), pro_change, forecast)
The RMSE and MAE can now be calculated as follows:
現在可以按以下方式計算RMSE和MAE:
>>> from sklearn.metrics import mean_squared_error
>>> from math import sqrt
>>> mse = mean_squared_error(passenger_test, yhat14)
>>> rmse = sqrt(mse)
>>> print('RMSE: %f' % rmse)RMSE: 524.263928>>> forecast_error = (passenger_test-yhat14)
>>> forecast_error
>>> mean_forecast_error = np.mean(forecast_error)
>>> mean_forecast_error71.58326743881493
The RMSE and MFE for Prophet are both lower than that obtained using ARIMA, suggesting that the model has performed better in forecasting monthly air passenger numbers.
先知的RMSE和MFE均低于使用ARIMA獲得的值,這表明該模型在預測每月航空乘客人數方面表現更好。
3. TensorFlow概率 (3. TensorFlow Probability)
In the aftermath of COVID-19, many time series forecasts have proven to be erroneous as they have been made with the wrong set of assumptions.
在COVID-19之后,許多時間序列的預測被證明是錯誤的,因為它們是用錯誤的假設集做出的。
Increasingly, it is coming to be recognised that time series models which can produce a range of forecasts can be more practically applied, as they allow for a “scenario analysis” of what might happen in the future.
人們越來越認識到,可以產生一系列預測的時間序列模型可以更實際地應用,因為它們可以對未來可能發生的情況進行“情景分析”。
As an example, an ARIMA model built using the air passenger data as above could not have possibly forecasted the sharp drop in passenger numbers that came about as a result of COVID-19.
例如,使用上述航空旅客數據構建的ARIMA模型可能無法預測由于COVID-19而導致的旅客人數急劇下降。
However, using more recent air passenger data, let’s see how a model built using TensorFlow Probability would have performed:
但是,使用最近的航空乘客數據,讓我們看看使用TensorFlow Probability構建的模型將如何執行:

While the model would not have forecasted the sharp drop that ultimately came to pass, we do see that the model is forecasting a drop in passenger numbers to below 150,000. Use of this model can allow for more of a “what-if” series of forecasts — e.g. an airline could forecast monthly passenger numbers for a particular airport and note that passenger numbers could be significantly lower than usual — which could inform the company in terms of managing resources such as fleet utilisation, for instance.
盡管該模型無法預測最終會發生的急劇下降,但我們確實看到該模型預測的乘客人數將下降到150,000以下。 使用此模型可以進行更多的“假設分析”系列預測-例如,航空公司可以預測特定機場的每月乘客人數,并請注意,乘客人數可能大大低于平時-這可以向公司傳達例如,管理資源,例如車隊利用。
Specifically, TensorFlow Probability makes forecasts using the assumption of a posterior distribution — which is comprised of a prior distribution (prior data) and the likelihood function.
具體來說,TensorFlow概率使用后驗分布的假設進行預測,該后驗分布由先驗分布(先驗數據)和似然函數組成。

For reference, the example illustrated here uses the template from the Structural Time Series modeling in TensorFlow Probability tutorial, of which the original authors (Copyright 2019 The TensorFlow Authors) have made available under the Apache 2.0 license.
作為參考,此處顯示的示例使用TensorFlow概率教程中的結構時間序列建模中的模板,該原始模板的作者(Copyright 2019 The TensorFlow Authors)已獲得Apache 2.0許可。
結論 (Conclusion)
Time series analysis is about making reliable forecasts using models suited to the data in question. For data with defined trend and seasonal components, it has been my experience that these models work quite well.
時間序列分析是關于使用適用于相關數據的模型進行可靠的預測。 對于具有定義的趨勢和季節性成分的數據,根據我的經驗,這些模型非常有效。
Hope you found the above article of use, and feel free to leave any questions or feedback in the comments section.
希望您找到了上面的使用文章,并隨時在評論部分中留下任何問題或反饋。
Disclaimer: This article is written on an “as is” basis and without warranty. It was written with the intention of providing an overview of data science concepts, and should not be interpreted as professional advice in any way.
免責聲明:本文按“原樣”撰寫,不作任何擔保。 它旨在提供數據科學概念的概述,并且不應以任何方式解釋為專業建議。
翻譯自: https://towardsdatascience.com/my-time-series-toolkit-4aa841d08325
c++ 時間序列工具包
本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。 如若轉載,請注明出處:http://www.pswp.cn/news/391159.shtml 繁體地址,請注明出處:http://hk.pswp.cn/news/391159.shtml 英文地址,請注明出處:http://en.pswp.cn/news/391159.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!