本文的目的是演示人工神經網絡(Artificial Neural Network ,ANN)和長短期記憶循環神經網絡(Long Short-Term Memory Recurrent Neural Network ,LSTM RNN)工作過程,使您能夠在現實生活中使用它們,並對時間序列數據建立最簡單的ANN和LSTM循環神經網絡。
人工神經網絡(Artificial Neural Network ,ANN)
https://en.wikipedia.org/wiki/Artificial_neural_network
長短期記憶循環神經網絡(Long Short-Term Memory Recurrent Neural Network ,LSTM RNN)
https://en.wikipedia.org/wiki/Long_short-term_memory
數據
CBOE(Chicago Board Options Exchange,芝加哥期權交易所)波動性指數是用來衡量標準普爾500指數期權的一種常用隱含波動率,以其代號VIX(Volatility Index,也稱“恐懼指數”)而聞名。
CBOE(Chicago Board Options Exchange,芝加哥期權交易所)波動性指數
https://en.wikipedia.org/wiki/VIX
芝加哥期權交易所CBOE實時計算出VIX指數後,將其推出。
芝加哥期權交易所
https://en.wikipedia.org/wiki/Chicago_Board_Options_Exchange
可以從這裡(https://ca.finance.yahoo.com/quote/%5Evix/history?ltr=1)下載波動性標準普爾500數據集,時間範圍是:2011年2月11日至2019年2月11日。我的目標是採用ANN和LSTM來預測波動性標準普爾500時間序列。
首先,我們需要導入以下庫:
import pandas as pd
import numpy as np
%matplotlib inline
import matplotlib.pyplot as plt
from sklearn.preprocessing import MinMaxScaler
from sklearn.metrics import r2_score
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping
from keras.optimizers import Adam
from keras.layers import LSTM
並將數據加載到Pandas 的dataframe中。
df = pd.read_csv("vix_2011_2019.csv")
我們可以快速瀏覽前幾行。
print(df.head())
刪除不需要的列,然後將“日期”列轉換為時間數據類型,並將“日期”列設置為索引。
df.drop(['Open', 'High', 'Low', 'Close', 'Volume'], axis=1, inplace=True)
df['Date'] = pd.to_datetime(df['Date'])
df = df.set_index(['Date'], drop=True)
df.head(10)
接下來,我們繪製一個時間序列線圖。
plt.figure(figsize=(10, 6))
df['Adj Close'].plot();
可以看出,“Adj close”數據相當不穩定,既沒有上升趨勢,也沒有下降趨勢。
按日期“2018–01–01”將數據拆分為訓練集和測試集,即在此日期之前的數據是訓練數據,此之後的數據是測試數據,我們再次將其可視化。
split_date = pd.Timestamp('2018-01-01')
df = df['Adj Close']
train = df.loc[:split_date]
test = df.loc[split_date:]
plt.figure(figsize=(10, 6))
ax = train.plot()
test.plot(ax=ax)
plt.legend(['train', 'test']);
我們將訓練和測試數據縮放為[-1,1]。
scaler = MinMaxScaler(feature_range=(-1, 1))
train_sc = scaler.fit_transform(train)
test_sc = scaler.transform(test)
獲取訓練和測試數據。
X_train = train_sc[:-1]
y_train = train_sc[1:]
X_test = test_sc[:-1]
y_test = test_sc[1:]
1.簡單人工神經網絡
我們創建一個序列模型。
通過.add()方法添加層。
將“input_dim”參數傳遞到第一層。
激活函數為線性整流函數Relu(Rectified Linear Unit,也稱校正線性單位)。
通過compile方法完成學習過程的配置。
損失函數是mean_squared_error,優化器是Adam。
當監測到loss停止改進時,結束訓練。
patience =2,表示經過數個週期結果依舊沒有改進,此時可以結束訓練。
人工神經網絡的訓練時間為100個週期,每次用1個樣本進行訓練。
nn_model = Sequential()
nn_model.add(Dense(12, input_dim=1, activation='relu'))
nn_model.add(Dense(1))
nn_model.compile(loss='mean_squared_error', optimizer='adam')
early_stop = EarlyStopping(monitor='loss', patience=2, verbose=1)
history = nn_model.fit(X_train, y_train, epochs=100, batch_size=1, verbose=1, callbacks=[early_stop], shuffle=False)
我不會把整個輸出結果打印出來,它早在第19個週期就停了下來。
<code>y_pred_test_nn = nn_model.predict(X_test)
y_train_pred_nn = nn_model.predict(X_train)
print("The R2 score on the Train set is:\\t{:0.3f}".format(r2_score(y_train, y_train_pred_nn)))
print("The R2 score on the Test set is:\\t{:0.3f}".format(r2_score(y_test, y_pred_test_nn)))/<code>
2.LSTM
LSTM網絡的構建和模型編譯和人工神經網絡相似。
LSTM有一個可見層,它有1個輸入。
隱藏層有7個LSTM神經元。
輸出層進行單值預測。
LSTM神經元使用Relu函數進行激活。
LSTM的訓練時間為100個週期,每次用1個樣本進行訓練。
lstm_model = Sequential()
lstm_model.add(LSTM(7, input_shape=(1, X_train_lmse.shape[1]), activation='relu', kernel_initializer='lecun_uniform', return_sequences=False))
lstm_model.add(Dense(1))
lstm_model.compile(loss='mean_squared_error', optimizer='adam')
early_stop = EarlyStopping(monitor='loss', patience=2, verbose=1)
history_lstm_model = lstm_model.fit(X_train_lmse, y_train, epochs=100, batch_size=1, verbose=1, shuffle=False, callbacks=[early_stop])
訓練早在第10個週期就停了下來。
y_pred_test_lstm = lstm_model.predict(X_test_lmse)
y_train_pred_lstm = lstm_model.predict(X_train_lmse)
print("The R2 score on the Train set is:t{:0.3f}".format(r2_score(y_train, y_train_pred_lstm)))
print("The R2 score on the Test set is:t{:0.3f}".format(r2_score(y_test, y_pred_test_lstm)))
訓練和測試R^2均優於人工神經網絡模型。
3.比較模型
我們比較了兩種模型的測試MSE
nn_test_mse = nn_model.evaluate(X_test, y_test, batch_size=1)
lstm_test_mse = lstm_model.evaluate(X_test_lmse, y_test, batch_size=1)
print('NN: %f'%nn_test_mse)
print('LSTM: %f'%lstm_test_mse)
4.進行預測
nn_y_pred_test = nn_model.predict(X_test)
lstm_y_pred_test = lstm_model.predict(X_test_lmse)
plt.figure(figsize=(10, 6))
plt.plot(y_test, label='True')
plt.plot(y_pred_test_nn, label='NN')
plt.title("NN's Prediction")
plt.xlabel('Observation')
plt.ylabel('Adj Close Scaled')
plt.legend()
plt.show();
plt.figure(figsize=(10, 6))
plt.plot(y_test, label='True')
plt.plot(y_pred_test_lstm, label='LSTM')
plt.title("LSTM's Prediction")
plt.xlabel('Observation')
plt.ylabel('Adj Close scaled')
plt.legend()
plt.show();
就是這樣!在這篇文章中,我們發現瞭如何採用python語言基於Keras深度學習網絡框架,開發用於時間序列預測的人工神經網絡和LSTM循環神經網絡,以及如何利用它們更好地預測時間序列數據。
閱讀更多 風信子編程 的文章