You need to enable JavaScript to run this app. 基于Keras搭建股票收益模拟GAN,训练仅输出损失,如何获取生成的模拟收益
优惠活动
大模型
产品
解决方案
定价
更多

基于Keras搭建股票收益模拟GAN,训练仅输出损失,如何获取生成的模拟收益

代码调整全流程

先修正现有代码的核心逻辑缺陷

  • 修正训练样本的尺度匹配问题:当前代码定义了归一化后的ret_scaled,但实际传给样本构造函数的是原始收益率ret,需要改成传入归一化序列,确保训练样本和生成器输出的tanh(-1~1)尺度匹配
  • 补全全量数据集遍历逻辑:当前训练循环每个epoch只抽1个batch训练,训练强度严重不足,需要在每个epoch内层加循环遍历全部批次,覆盖所有训练样本
  • 修正返回值逻辑:当前train函数训练结束后只返回损失日志,没有保留训练完成的生成器对象,导致训练完就丢失模型权重,无法生成模拟数据

具体代码改动点

1. 调整样本构造的入参

# 原代码行:samples = create_samples(ret)
# 修改为传入归一化后的收益率序列
samples = create_samples(ret_scaled)

2. 修改train函数的训练循环,增加全量遍历

将原train函数内的单层epoch循环改成双层循环,每个epoch遍历所有批次:

for epoch in range(epochs):
    # 每个epoch遍历全部训练批次
    for _ in range(int(batch_count)):
        noise= np.random.normal(0,1,[batch_size,100])
        gen_imgs = generator.predict(noise)
        y_fake = np.zeros(batch_size)
        
        idx = np.random.randint(0, X_train.shape[0], batch_size)
        image_batch = X_train[idx]
        y_real = np.ones(batch_size)
        
        discriminator.trainable=True
        d_loss_real = discriminator.train_on_batch(image_batch, y_real)
        d_loss_fake = discriminator.train_on_batch(gen_imgs, y_fake)
        d_loss = 0.5 * np.add(d_loss_real, d_loss_fake) 
        
        noise= np.random.normal(0,1,[batch_size,100])
        y_gen = np.ones(batch_size)
        discriminator.trainable=False
        g_loss = gan.train_on_batch(noise, y_gen)
    # 每轮epoch训练完成后记录损失
    d_loss_logs_r.append([epoch,d_loss_real])
    d_loss_logs_f.append([epoch,d_loss_fake])
    g_loss_logs.append([epoch,g_loss])

3. 调整train函数的返回值

将原函数末尾的返回值改成携带训练好的生成器对象:

# 原代码行:return d_loss_logs_r_a, d_loss_logs_f_a, g_loss_logs_a
# 修改为:
return generator, d_loss_logs_r_a, d_loss_logs_f_a, g_loss_logs_a

增加模拟收益率序列生成逻辑

生成器输出的是归一化尺度的数值,需要通过之前定义的MinMaxScaler做逆变换,还原成真实的收益率尺度,才能替代蒙特卡洛的模拟结果。
新增专门的序列生成函数:

def generate_simulated_returns(trained_generator, fitted_scaler, sim_path_num=1000):
    # 生成和训练维度一致的100维高斯噪声作为生成器输入
    input_noise = np.random.normal(0, 1, (sim_path_num, 100))
    # 生成器输出形状为 (sim_path_num, 25),每个样本对应25步的收益窗口
    scaled_return = trained_generator.predict(input_noise)
    # 逆缩放还原到真实收益率的数值尺度
    real_scale_return = fitted_scaler.inverse_transform(scaled_return)
    # 展平为连续的收益率序列,可直接用于VaR计算、路径模拟、衍生品定价等原本依赖蒙特卡洛的场景
    continuous_return = real_scale_return.flatten()
    return continuous_return

最终调用示例

# 启动训练,epochs建议设置为1000-5000轮,batch_size根据内存调整为32/64
trained_gen, loss_r, loss_f, loss_g = train(epochs=2000, batch_size=64)
# 生成模拟收益率,sim_path_num对应需要的模拟样本量,和传统蒙特卡洛的抽样数对齐
simulated_returns = generate_simulated_returns(
    trained_generator=trained_gen,
    fitted_scaler=scaler,
    sim_path_num=5000
)

效果校验

生成完成后可以直接对比三组数据的分布特征:

  • 原始GM股票的真实对数收益率
  • 传统蒙特卡洛模拟的收益率
  • GAN生成的收益率
    如果三者的均值、波动率、偏度、峰度、极值区间接近,即可直接替换蒙特卡洛的模拟结果使用。

内容的提问来源于stack exchange,提问作者Juan Trevisiol

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.20 20:36:06