楼主: Jia853042915
679 4

[问答] 急求GARCH-LSTM模型预测汇率/股价的python代码!!(60币)) [推广有奖]

  • 0关注
  • 0粉丝

小学生

0%

还不是VIP/贵宾

-

威望
0
论坛币
923 个
通用积分
0.0081
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
23 点
帖子
2
精华
0
在线时间
6 小时
注册时间
2024-2-5
最后登录
2024-11-20

楼主
Jia853042915 在职认证  发表于 2024-2-5 16:07:39 |AI写论文
60论坛币
原汇率序列非平稳,经一阶差分后为平稳白噪声序列,想构建GARCH模型,并将波动率和其他宏观指标作为LSTM的输入变量,预测一个月后的汇率。急求相关的python代码!

关键词:python GARCH 模型预测 ARCH RCH

沙发
Killua609 发表于 2024-2-8 09:36:28
把数据做导入后,即可跑代码出来

藤椅
Jia853042915 在职认证  发表于 2024-2-18 09:17:45
Killua609 发表于 2024-2-8 09:36
把数据做导入后,即可跑代码出来
请问您有相关的代码吗

板凳
ewrewrlll 学生认证  发表于 2024-3-10 16:25:22
import numpy as np
import pandas as pd
from arch import arch_model
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import LSTM, Dense

# 读取数据
data = pd.read_csv('exchange_rate.csv')  # 替换为自己的数据文件路径
exchange_rate = data['exchange_rate'].values

# 进行一阶差分
diff_exchange_rate = np.diff(exchange_rate)

# 构建GARCH模型
model = arch_model(diff_exchange_rate, vol='Garch', p=1, q=1)
model_fit = model.fit()

# 预测波动率
forecast_volatility = model_fit.forecast(horizon=30)

# 准备LSTM模型的输入数据
X = np.concatenate((forecast_volatility.variance[-1, :], data['macro_indicator'].values[:-30]), axis=1)
y = exchange_rate[30:]

# 归一化输入数据
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)

# 划分训练集和测试集
train_size = int(len(X_scaled) * 0.8)
X_train, X_test = X_scaled[:train_size], X_scaled[train_size:]
y_train, y_test = y[:train_size], y[train_size:]

# 调整输入数据形状
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))

# 构建LSTM模型
model = Sequential()
model.add(LSTM(50, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')

# 训练LSTM模型
model.fit(X_train, y_train, epochs=100, batch_size=32, verbose=0)

# 在测试集上进行预测
y_pred = model.predict(X_test)

# 还原预测结果的尺度
y_pred = scaler.inverse_transform(y_pred)

# 打印预测结果
print(y_pred)

报纸
ewrewrlll 学生认证  发表于 2024-3-10 16:25:31
import numpy as np
import pandas as pd
from arch import arch_model
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import LSTM, Dense

# 读取数据
data = pd.read_csv('exchange_rate.csv')  # 替换为自己的数据文件路径
exchange_rate = data['exchange_rate'].values

# 进行一阶差分
diff_exchange_rate = np.diff(exchange_rate)

# 构建GARCH模型
model = arch_model(diff_exchange_rate, vol='Garch', p=1, q=1)
model_fit = model.fit()

# 预测波动率
forecast_volatility = model_fit.forecast(horizon=30)

# 准备LSTM模型的输入数据
X = np.concatenate((forecast_volatility.variance[-1, :], data['macro_indicator'].values[:-30]), axis=1)
y = exchange_rate[30:]

# 归一化输入数据
scaler = MinMaxScaler()
X_scaled = scaler.fit_transform(X)

# 划分训练集和测试集
train_size = int(len(X_scaled) * 0.8)
X_train, X_test = X_scaled[:train_size], X_scaled[train_size:]
y_train, y_test = y[:train_size], y[train_size:]

# 调整输入数据形状
X_train = np.reshape(X_train, (X_train.shape[0], 1, X_train.shape[1]))
X_test = np.reshape(X_test, (X_test.shape[0], 1, X_test.shape[1]))

# 构建LSTM模型
model = Sequential()
model.add(LSTM(50, input_shape=(X_train.shape[1], X_train.shape[2])))
model.add(Dense(1))
model.compile(loss='mean_squared_error', optimizer='adam')

# 训练LSTM模型
model.fit(X_train, y_train, epochs=100, batch_size=32, verbose=0)

# 在测试集上进行预测
y_pred = model.predict(X_test)

# 还原预测结果的尺度
y_pred = scaler.inverse_transform(y_pred)

# 打印预测结果
print(y_pred)

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-11 01:52