Matlab数学建模实战应用:案例1 - 股票价格预测

目录

前言

一、问题分析

二、模型选择

三、Matlab代码实现

完整代码示例

四、模型改进

[1. 引入更多特征](#1. 引入更多特征)

[2. 使用更复杂的模型](#2. 使用更复杂的模型)

[3. 模型参数优化](#3. 模型参数优化)

五、实例总结

总结


前言

股票价格预测是金融工程中的重要问题,利用数学建模可以帮助分析和预测股票价格的波动趋势,帮助投资者做出更明智的决策。本文将详细介绍股票价格预测的步骤,包括问题分析、模型选择、Matlab代码实现、模型验证和模型改进。

一、问题分析
  1. 股票价格的波动性
    • 股票价格具有高度的波动性,由多种因素(例如市场需求、公司业绩、经济形势等)共同作用导致。
  2. 影响因素
    • 常见的影响股票价格的因素包括:公司财务报表、行业发展、国家政策、国际经济环境、投资者情绪等。
  3. 预测目标
    • 短期预测:对未来几天或几周的股票价格进行预测,主要为日交易策略提供依据。
    • 中长期预测:对未来几个月或几年内的价格趋势进行预测,辅助长期投资决策。
二、模型选择
  1. 线性回归模型

    • 假设股票价格与某些因子(如技术指标)之间的关系是线性的。线性回归模型简单易用,但对复杂的股票价格波动可能无能为力。
  2. 时间序列模型

    • 自回归移动平均模型(ARMA)、自回归积分滑动平均模型(ARIMA)等,用于处理具有时间依赖性的序列数据。
  3. 机器学习模型

    • 支持向量机(SVM)、LSTM神经网络等,能够捕捉股票价格的非线性关系和复杂的波动模式。

本次实例将选择时间序列模型(ARIMA)进行股票价格预测。

三、Matlab代码实现

以下是一个使用ARIMA模型进行股票价格预测的完整代码示例。

  1. 导入数据
    • 我们假设股票数据(包含日期和收盘价)存储在stock_data.csv文件中。
Matlab 复制代码
    % 读取股票数据
    data = readtable('stock_data.csv');
    dates = data.Date;
    prices = data.Close;

    % 将日期转换为 MATLAB 日期格式
    dates = datetime(dates, 'InputFormat', 'yyyy-MM-dd');
    
    % 绘制收盘价时序图
    figure;
    plot(dates, prices);
    title('Stock Closing Prices');
    xlabel('Date');
    ylabel('Closing Price');
    grid on;
  1. 数据预处理
    • 检查和处理缺失值,并拆分数据集为训练集和测试集。
Matlab 复制代码
    % 检查缺失值
    if any(ismissing(prices))
        disp('存在缺失值,将其移除');
        data = rmmissing(data);
        dates = data.Date;
        prices = data.Close;
    end

    % 拆分数据集为训练集和测试集(70% 训练,30% 测试)
    n = length(prices);
    train_size = round(0.7 * n);
    train_prices = prices(1:train_size);
    test_prices = prices(train_size + 1:end);
    test_dates = dates(train_size + 1:end);
  1. 模型训练
    • 使用训练集数据训练ARIMA模型。
Matlab 复制代码
    % 训练 ARIMA 模型
    model = arima('Constant', 0, 'D', 1, 'Seasonality', 12, 'MALags', 1, 'SMALags', 12);
    arima_model = estimate(model, train_prices);
  1. 模型预测和验证
    • 使用训练好的模型进行预测,并与测试集数据进行比较。
Matlab 复制代码
    % 预测未来价格
    [forecast_prices, ~, forecast_CI] = forecast(arima_model, length(test_prices), 'Y0', train_prices);

    % 绘制预测结果
    figure;
    hold on;
    plot(test_dates, test_prices, 'b', 'DisplayName', 'Actual Prices');
    plot(test_dates, forecast_prices, 'r', 'DisplayName', 'Forecasted Prices');
    plot(test_dates, forecast_CI(:, 1), 'k--', 'DisplayName', '95% CI Lower');
    plot(test_dates, forecast_CI(:, 2), 'k--', 'DisplayName', '95% CI Upper');
    title('Stock Price Prediction Using ARIMA');
    xlabel('Date');
    ylabel('Price');
    legend('show');
    grid on;
    hold off;
  1. 计算预测误差
    • 计算模型预测的均方误差(MSE)和平均绝对误差(MAE)等。
Matlab 复制代码
    % 计算 MAE 和 MSE
    MAE = mean(abs(forecast_prices - test_prices));
    MSE = mean((forecast_prices - test_prices).^2);
    
    disp(['Mean Absolute Error: ', num2str(MAE)]);
    disp(['Mean Squared Error: ', num2str(MSE)]);
完整代码示例
Matlab 复制代码
% 读取股票数据
data = readtable('stock_data.csv');
dates = data.Date;
prices = data.Close;

% 将日期转换为 MATLAB 日期格式
dates = datetime(dates, 'InputFormat', 'yyyy-MM-dd');

% 绘制收盘价时序图
figure;
plot(dates, prices);
title('Stock Closing Prices');
xlabel('Date');
ylabel('Closing Price');
grid on;

% 检查缺失值
if any(ismissing(prices))
    disp('存在缺失值,将其移除');
    data = rmmissing(data);
    dates = data.Date;
    prices = data.Close;
end

% 拆分数据集为训练集和测试集(70% 训练,30% 测试)
n = length(prices);
train_size = round(0.7 * n);
train_prices = prices(1:train_size);
test_prices = prices(train_size + 1:end);
test_dates = dates(train_size + 1:end);

% 训练 ARIMA 模型
model = arima('Constant', 0, 'D', 1, 'Seasonality', 12, 'MALags', 1, 'SMALags', 12);
arima_model = estimate(model, train_prices);

% 预测未来价格
[forecast_prices, ~, forecast_CI] = forecast(arima_model, length(test_prices), 'Y0', train_prices);

% 绘制预测结果
figure;
hold on;
plot(test_dates, test_prices, 'b', 'DisplayName', 'Actual Prices');
plot(test_dates, forecast_prices, 'r', 'DisplayName', 'Forecasted Prices');
plot(test_dates, forecast_CI(:, 1), 'k--', 'DisplayName', '95% CI Lower');
plot(test_dates, forecast_CI(:, 2), 'k--', 'DisplayName', '95% CI Upper');
title('Stock Price Prediction Using ARIMA');
xlabel('Date');
ylabel('Price');
legend('show');
grid on;
hold off;

% 计算 MAE 和 MSE
MAE = mean(abs(forecast_prices - test_prices));
MSE = mean((forecast_prices - test_prices).^2);

disp(['Mean Absolute Error: ', num2str(MAE)]);
disp(['Mean Squared Error: ', num2str(MSE)]);
四、模型改进

在初步模型的基础上,我们可以通过引入更多特征、使用更复杂的模型和优化模型参数来进一步改进股票价格预测模型,提高预测的精度。

1. 引入更多特征

除了使用历史价格数据,我们还可以引入一些技术指标和宏观经济指标作为特征输入到模型中。这些额外特征可以提供更全面的信息,有助于提高模型的预测能力。

  1. 技术指标
    • 移动平均线(MA)、指数平滑移动平均线(EMA)、相对强弱指数(RSI)等。
Matlab 复制代码
    % 计算技术指标
    ma = movmean(prices, 10);  % 10 日移动平均线
    rsi = rsindex(prices, 14); % 14 日相对强弱指数
    
    % 合并特征
    features = [prices, ma, rsi];
  1. 宏观经济指标
    • 例如利率、通货膨胀率、GDP 增长率等。
Matlab 复制代码
    % 假设我们有宏观经济数据(已经加载到变量 macro_data 中)
    % 合并特征
    features = [prices, macro_data];
2. 使用更复杂的模型

简单的时间序列模型(如 ARIMA)可能无法捕捉股票价格的复杂波动模式。我们可以考虑使用更复杂的模型,如 GARCH 模型和 LSTM 神经网络。

  1. GARCH 模型
    • 用于建模金融时间序列的波动率。
Matlab 复制代码
    % 定义 GARCH 模型
    model = garch(1, 1);
    
    % 估计模型参数
    garch_model = estimate(model, train_prices);
    
    % 预测未来价格波动率
    [v, ~] = forecast(garch_model, length(test_prices), 'Y0', train_prices);
  1. LSTM 神经网络
    • 强大的深度学习模型,可以捕捉时间序列的长短期依赖关系。
Matlab 复制代码
    % 定义 LSTM 神经网络
    layers = [
        sequenceInputLayer(1)
        lstmLayer(100, 'OutputMode', 'sequence')
        fullyConnectedLayer(1)
        regressionLayer];
    
    % 设置训练选项
    options = trainingOptions('adam', ...
        'MaxEpochs', 250, ...
        'GradientThreshold', 1, ...
        'InitialLearnRate', 0.005, ...
        'LearnRateSchedule', 'piecewise', ...
        'LearnRateDropFactor', 0.2, ...
        'LearnRateDropPeriod', 125, ...
        'Verbose', 0, ...
        'Plots', 'training-progress');
    
    % 训练 LSTM 网络
    train_prices_sequence = reshape(train_prices, [numel(train_prices), 1, 1]);
    lstm_model = trainNetwork(train_prices_sequence, train_prices_sequence, layers, options);
    
    % 预测未来价格
    test_prices_sequence = reshape(test_prices, [numel(test_prices), 1, 1]);
    forecast_prices = predict(lstm_model, test_prices_sequence);
3. 模型参数优化

通过使用交叉验证、网格搜索等方法对模型参数进行优化,以找到最佳的参数组合。

  1. 交叉验证
    • 交叉验证用于评估模型的表现,并选择最佳模型参数。
Matlab 复制代码
    % 使用交叉验证选择最佳 ARIMA 模型参数
    best_model = [];
    best_mse = Inf;
    for p = 0:5
        for q = 0:5
            for d = 0:2
                try
                    model = arima('Constant', 0, 'ARLags', p, 'D', d, 'MALags', q);
                    arima_model = estimate(model, train_prices);
                    forecast_prices = forecast(arima_model, length(test_prices), 'Y0', train_prices);
                    mse = mean((forecast_prices - test_prices).^2);
                    if mse < best_mse
                        best_mse = mse;
                        best_model = arima_model;
                    end
                end
            end
        end
    end
  1. 网格搜索
    • 网格搜索通过在参数空间中进行穷举搜索,找到最佳参数组合。
Matlab 复制代码
    % 定义参数空间
    paramGrid = struct('NumHiddenUnits', [50, 100], 'InitialLearnRate', [0.001, 0.005]);
    
    % 初始化最优参数和最小误差
    bestParams = [];
    bestMSE = Inf;
    
    % 网格搜索参数
    for hiddenUnits = paramGrid.NumHiddenUnits
        for learnRate = paramGrid.InitialLearnRate
            % 设置 LSTM 网络和训练选项
            layers = [
                sequenceInputLayer(1)
                lstmLayer(hiddenUnits, 'OutputMode', 'sequence')
                fullyConnectedLayer(1)
                regressionLayer];
            
            options = trainingOptions('adam', ...
                'MaxEpochs', 250, ...
                'GradientThreshold', 1, ...
                'InitialLearnRate', learnRate, ...
                'LearnRateSchedule', 'piecewise', ...
                'LearnRateDropFactor', 0.2, ...
                'LearnRateDropPeriod', 125, ...
                'Verbose', 0);
            
            % 训练 LSTM 网络
            lstm_model = trainNetwork(train_prices_sequence, train_prices_sequence, layers, options);
            
            % 预测未来价格
            forecast_prices = predict(lstm_model, test_prices_sequence);
            mse = mean((forecast_prices - test_prices).^2);
            
            % 更新最优参数
            if mse < bestMSE
                bestMSE = mse;
                bestParams = struct('NumHiddenUnits', hiddenUnits, 'InitialLearnRate', learnRate);
            end
        end
    end

以下是改进模型的方法及其示例总结:

方法 说明 示例代码
引入更多特征 使用技术指标和宏观经济指标作为额外特征输入 [prices, ma, rsi, macro_data]
使用更复杂的模型 尝试使用更复杂的时间序列模型(如GARCH)和深度学习模型(如LSTM) garch, lstmLayer
模型参数优化 使用交叉验证和网格搜索找到最佳模型参数组合 for p = 0:5, q = 0:5, ...
五、实例总结

通过上述步骤,我们展示了如何使用 ARIMA 模型进行股票价格预测的全过程。接着,进一步改进模型,包括引入更多特征、使用更复杂的模型和优化参数的方法。以下是总结:

步骤 说明 示例代码
问题分析 分析股票价格的波动性及其影响因素 -
模型选择 选择合适的预测模型(如ARIMA、机器学习模型等) -
数据导入 从CSV文件中导入股票数据 data = readtable('stock_data.csv');
数据预处理 检查和处理缺失值,拆分训练集和测试集 train_prices = prices(1:train_size);
模型训练 使用训练集数据训练ARIMA模型 model = arima(...);
模型预测和验证 使用模型进行预测,并与测试集数据进行比较 [forecast_prices, ~, forecast_CI] = ...
模型改进 引入更多特征、使用更复杂的模型、优化模型参数 garch, lstmLayer, cross-validation

总结

本文详细介绍了股票价格预测的步骤,包括问题分析、模型选择、Matlab代码实现、模型验证和模型改进。通过实际案例,展示了如何使用 ARIMA 模型进行股票价格预测,并详细解释了如何通过引入更多特征、使用更复杂的模型和优化参数来改进预测模型。

相关推荐
isyangli_blog12 小时前
OpenDayLight (Carbon 版本) 启动与组件安装
开发语言·php
vb20081113 小时前
FastAPI APIRouter
开发语言·python
Benszen13 小时前
KVM虚拟化解决方案
开发语言·perl
会编程的土豆13 小时前
Go 语言反射(Reflection)详解
开发语言·后端·golang
東雪木13 小时前
多线程与并发编程 专属复习笔记
java·开发语言·笔记·java面试
杨充13 小时前
1.3 浮点型数据设计灵魂
开发语言·python·算法
噜噜噜阿鲁~13 小时前
python学习笔记 | 11.3、面向对象高级编程-多重继承
java·开发语言
basketball61613 小时前
Go 语言从入门到进阶:4. 数组和MAP使用方法总结
开发语言·后端·golang
春生野草14 小时前
反射、Tomcat执行
java·开发语言
雪的季节15 小时前
企业级 Qt 全功能项目
开发语言·数据库·qt