SARIMA(季节性自回归移动平均)模型是一种常用的时间序列预测方法,适用于具有季节性特征的数据。
import pandas as pdfrom statsmodels.tsa.statespace.sarimax import SARIMAX# 将data转换为时间序列数据data['date'] = pd.to_datetime(data['date'], format='%Y%m%d')data.set_index('date', inplace=True)# 拟合SARIMA模型model = SARIMAX(data['close'], order=(1, 0, 0), seasonal_order=(1, 1, 1, 7))result = model.fit()# 预测未来3天的股票价格forecast = result.get_forecast(steps=3)predicted_prices_sarima = forecast.predicted_mean
简单移动平均线是一种基本的平滑方法,通过计算时间窗口内价格的平均值来预测未来价格。
# 计算简单移动平均线window = 5data['sma'] = data['close'].rolling(window=window).mean()# 预测未来3天的股票价格predicted_prices_sma = data['sma'].iloc[-1] * (1 + (data['close'].iloc[-1] - data['sma'].iloc[-1]) / data['sma'].iloc[-1])
指数加权移动平均线给予近期价格更高的权重,对短期趋势变化更敏感。
# 计算指数加权移动平均线data['ewm'] = data['close'].ewm(span=window, adjust=False).mean()# 预测未来3天的股票价格predicted_prices_ewm = data['ewm'].iloc[-1] * (1 + (data['close'].iloc[-1] - data['ewm'].iloc[-1]) / data['ewm'].iloc[-1])
Bollinger带通过计算价格的标准差来确定价格的波动范围,可以用于判断价格的超买和超卖区域。
# 计算Bollinger带data['middle_band'] = data['close'].rolling(window=window).mean()data['std'] = data['close'].rolling(window=window).std()data['upper_band'] = data['middle_band'] + 2 * data['std']data['lower_band'] = data['middle_band'] - 2 * data['std']# 预测未来3天的股票价格predicted_prices_bollinger = data['middle_band'].iloc[-1]
相对强弱指标通过比较一段时间内的平均涨幅和平均跌幅,来判断价格的超买和超卖情况。
# 计算相对强弱指标(RSI)delta = data['close'].diff()up, down = delta.copy(), delta.copy()up[up 0] = 0roll_up = up.rolling(window=window).mean()roll_down = abs(down.rolling(window=window).mean())rs = roll_up / roll_downrsi = 100 - (100 / (1 + rs))# 预测未来3天的股票价格predicted_prices_rsi = data['close'].iloc[-1] * (1 + (rsi.iloc[-1] - 50) / 50)
随机指标通过比较一段时间内的最高价和最低价与收盘价的关系,来判断价格的超买和超卖情况。
# 计算KDJ指标data['lowest_low'] = data['low'].rolling(window=window).min()data['highest_high'] = data['high'].rolling(window=window).max()rsv = (data['close'] - data['lowest_low']) / (data['highest_high'] - data['lowest_low'])k = rsv.rolling(window=3).mean() * 100d = k.rolling(window=3).mean()j = 3 * k - 2 * d# 预测未来3天的股票价格predicted_prices_kdj = data['close'].iloc[-1] * (1 + (j.iloc[-1] - 50) / 50)
使用线性回归模型来拟合股票价格与时间的关系,然后预测未来的价格。
from sklearn.linear_model import LinearRegression# 创建线性回归模型并拟合数据X = data.reset_index().index.values.reshape(-1, 1)y = data['close'].valuesmodel = LinearRegression()model.fit(X, y)# 预测未来3天的股票价格predicted_prices_linear = model.predict([[len(data) + i] for i in range(1, 4)])
使用随机森林回归模型来拟合股票价格与其他特征(如交易量、开盘价等)的关系,然后预测未来的价格。
from sklearn.ensemble import RandomForestRegressor# 创建随机森林回归模型并拟合数据X = data[['volume', 'open', 'high', 'low']].valuesy = data['close'].valuesmodel = RandomForestRegressor(n_estimators=100, random_state=0)model.fit(X, y)# 预测未来3天的股票价格future_data = [[data['volume'].iloc[-1], data['open'].iloc[-1], data['high'].iloc[-1], data['low'].iloc[-1]]]predicted_prices_rf = model.predict(future_data)
使用支持向量回归模型来拟合股票价格与其他特征的非线性关系,然后预测未来的价格。
from sklearn.svm import SVRfrom sklearn.preprocessing import StandardScaler# 特征缩放scaler = StandardScaler()X_scaled = scaler.fit_transform(data[['volume', 'open', 'high', 'low']].values)y_scaled = scaler.fit_transform(data['close'].values.reshape(-1, 1))# 创建支持向量回归模型并拟合数据model = SVR(kernel='rbf')model.fit(X_scaled, y_scaled.flatten())# 预测未来3天的股票价格future_data_scaled = scaler.transform([[data['volume'].iloc[-1], data['open'].iloc[-1], data['high'].iloc[-1], data['low'].iloc[-1]]])predicted_prices_svr = scaler.inverse_transform(model.predict(future_data_scaled))
使用自回归移动平均法模型来拟合股票价格的时间序列数据,并预测未来的价格。
from statsmodels.tsa.arima.model import ARIMA# 拟合ARIMA模型model = ARIMA(data['close'], order=(2, 0, 1))result = model.fit()# 预测未来3天的股票价格forecast = result.get_forecast(steps=3)predicted_prices_arma = forecast.predicted_mean
使用LSTM神经网络模型来学习股票价格的长期依赖关系,并预测未来的价格。
import numpy as npfrom keras.models import Sequentialfrom keras.layers import LSTM, Densefrom sklearn.preprocessing import MinMaxScaler# 特征缩放scaler = MinMaxScaler()scaled_data = scaler.fit_transform(data['close'].values.reshape(-1, 1))# 构建训练集和测试集train_data = scaled_data[:-3]test_data = scaled_data[-3:]# 创建LSTM模型model = Sequential()model.add(LSTM(units=50, return_sequences=True, input_shape=(None, 1)))model.add(LSTM(units=50))model.add(Dense(units=1))model.compile(optimizer='adam', loss='mean_squared_error')# 将训练数据转换为适合LSTM模型的输入格式X_train = []y_train = []for i in range(5, len(train_data)): X_train.append(train_data[i-5:i, 0]) y_train.append(train_data[i, 0])X_train, y_train = np.array(X_train), np.array(y_train)X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))# 训练LSTM模型model.fit(X_train, y_train, epochs=100, batch_size=32)# 将测试数据转换为适合LSTM模型的输入格式inputs = scaled_data[-5:].reshape(1, -1)X_test = []for i in range(3): X_test.append(inputs[:, i:i+5])X_test = np.array(X_test)X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))# 预测未来3天的股票价格predicted_prices_lstm = model.predict(X_test)predicted_prices_lstm = scaler.inverse_transform(predicted_prices_lstm).flatten()最后,计算各种预测价格的均值,并给出建议:
avg_predicted_price = np.mean([ predicted_prices_sarima[-1], predicted_prices_sma, predicted_prices_ewm, predicted_prices_bollinger, predicted_prices_rsi, predicted_prices_kdj, predicted_prices_linear[-1], predicted_prices_rf[-1], predicted_prices_svr[-1], predicted_prices_arma[-1], predicted_prices_lstm[-
[color=inherit !important]civilpy:Python数据分析及可视化实例目录