1 概要介绍
深度学习方法在金融领域的应用受到了投资者和研究人员的广泛关注。本研究提出了一种新颖的深度学习框架,将小波变换(WT)、堆叠自编码器(SAEs)和长短期记忆(LSTM)相结合,用于股票价格预测。首次引入了层级提取深度特征的SAEs用于股票价格预测。该深度学习框架由三个阶段组成。首先,利用WT对股票价格时间序列进行分解以消除噪音。其次,应用SAEs生成用于预测股票价格的深层高级特征。最后,将高级去噪特征输入LSTM以预测下一天的收盘价。选择了六个市场指数及其相应的指数期货来检验所提出模型的性能。结果表明,所提出的模型在预测准确性和盈利能力方面优于其他类似模型。
2 关键代码
result_dict = dict()
EPOCHS = 500 #0
past_history = 4
for index in INDEX_SHEET_NAME:
print(f"Start {index} part!")
result_dict[index] = dict()
data_dir = f'../data/processed/wsae/{index}'
if not os.path.exists(data_dir):
raw = load_data(sheet_name=index)
generate_features(raw, index)
train_lst = os.listdir(data_dir)
train_lst.sort()
for name in train_lst:
x_train = np.load(data_dir + f'/{name}/X_train.npy')
y_train = np.load(data_dir + f'/{name}/Y_train.npy')
x_val = np.load(data_dir + f'/{name}/X_val.npy')
y_val = np.load(data_dir + f'/{name}/Y_val.npy')
x_test = np.load(data_dir + f'/{name}/X_test.npy')
y_test = np.load(data_dir + f'/{name}/Y_test.npy')
train_data, val_data, test_data = generate_train_val_data(
x_train, y_train, x_val, y_val, x_test, y_test,
past_history=4, batch_size=60
)
model_save_dir = f'../models/{index}/{name}'
if not os.path.exists(model_save_dir):
os.makedirs(model_save_dir)
if not os.path.exists(model_save_dir+'/wase-lstm_config.json') \
or not os.path.exists(model_save_dir+'/wase-lstm_weights.h5'):
print("No existing model, start to train!")
lstm = build_lstm_model(inputs_shape=[4, 10],
layers=5,
units=[64, 64, 64, 64, 64],
learning_rate=0.05)
lstm.fit(train_data,
epochs=EPOCHS,
steps_per_epoch=(y_train.shape[0] // 60),
validation_data=val_data,
validation_steps=1,
verbose=0)
json_config = lstm.to_json()
with open(model_save_dir+'/wase-lstm_config.json', 'w') as json_file:
json_file.write(json_config)
# Save weights to disk
lstm.save_weights(model_save_dir+'/wase-lstm_weights.h5')
print("Model Saved!")
else:
with open(model_save_dir+'/wase-lstm_config.json') as json_file:
json_config = json_file.read()
lstm = tf.keras.models.model_from_json(json_config)
lstm.compile(loss='mse',
optimizer='Adam',
metrics=[
tf.keras.metrics.MeanAbsolutePercentageError(),
MeanAbsolutePercentageError(),
LinearCorrelation(),
TheilU()],
lr=0.05
)
lstm.load_weights(model_save_dir+'/wase-lstm_weights.h5')
print("Model Loaded!")
result_dict[index][name] = lstm.evaluate(test_data, steps=1)
tf.keras.backend.clear_session()
print(f">>>>{index} {name} done!<<<<")
try:
with open(f'./wsae-lstm/{index}_train_result.pickle', 'wb') as handle:
pickle.dump(result_dict[index], handle, protocol=pickle.HIGHEST_PROTOCOL)
except:
print('fail to save!')
with open(f'./wsae-lstm/train_result.pickle', 'wb') as handle:
pickle.dump(result_dict, handle, protocol=pickle.HIGHEST_PROTOCOL)
3 结果展示