matlab动态神经网络进行时间序列预测分析

matlab动态神经网络进行时间序列预测分析

时间序列预测问题分类

  • 有y,无x,即 y(t)=f(y(t1),y(t2),...) y ( t ) = f ( y ( t − 1 ) , y ( t − 2 ) , . . . ) (NAR)
  • 有x,有y,即 y(t)=f(x(t1),x(t2),...,y(t1),y(t2),...) y ( t ) = f ( x ( t − 1 ) , x ( t − 2 ) , . . . , y ( t − 1 ) , y ( t − 2 ) , . . . ) (NARX)
  • 有x,无y,即 y(t)=f(x(t1),x(t2),... y ( t ) = f ( x ( t − 1 ) , x ( t − 2 ) , . . . (比较少见)

matlab中的动态神经网络

  • matlab中有narnet、narxnet等函数可以用于时间序列的预测分析,下面主要对narnet尽心说明
  • narnet需要设置当前值依赖过去值的个数k,即依赖多少个过去的值,同时还需要设置隐含层节点的个数,搭建之后就可以进行构建模型了
  • 预测的时候,需要首先给出预测初始时刻的前k个值,因为当前值是由前k个值决定的,将此作为输入,就可以依次给出后面的预测值。

代码

%%
clc,clear,close all
t = -5:.01:5;
pressure = sin(10*t)';
figure
plot( pressure )
%% 训练集与测试集的个数
num_all_data = length( pressure );
% 前75%的数据作为训练数据
num_train = floor( num_all_data * 0.75 );
% 后25%的数据作为测试数据
num_test = num_all_data - num_train;
% 转化为narnet需要的序列数据
y_train_nn = num2cell( pressure(1:num_train) )';
y_test_nn = num2cell( pressure(1+num_train:end))';
%% 延迟,即当前值依赖于过去的多少个值
feedback_delays = 1:10;
% 隐含层节点的个数
num_hd_neuron = 10;
% narnet构建
net = narnet(feedback_delays, num_hd_neuron);
[Xs,Xi,Ai,Ts] = preparets(net,{},{}, y_train_nn);
net = train(net,Xs,Ts,Xi,Ai);
view(net)
Y = net(Xs,Xi);
perf = perform(net,Ts,Y);
fprintf( 'neural network: mse on training set : %.6f\n', perf );
%% 神经网络进行进行预测
yini = y_train_nn(end-max(feedback_delays)+1:end);
[Xs,Xi,Ai] = preparets(net,{},{},[yini y_test_nn]);
y_pred_nn = net(Xs,Xi,Ai)';
y_pred_nn = cell2mat( y_pred_nn );
y_test_nn = cell2mat( y_test_nn )';

%% 画图,计算mse
figure
title('NARNET预测')
hold on
plot( y_test_nn, 'r', 'linewidth', 2 );
plot( y_pred_nn, 'b--', 'linewidth', 2 );
legend({ '真实值', '神经网络预测值'})
nn_per_error = mean(abs(y_pred_nn-y_test_nn) ./ y_test_nn);
nn_mse_error = mean( (y_pred_nn - y_test_nn).^2 );
fprintf('nn model: relative error on test set: %.6f\n', nn_per_error);
fprintf('nn model: mse on test set: %.6f\n', nn_mse_error);

结果

matlab动态神经网络进行时间序列预测分析_第1张图片

matlab动态神经网络进行时间序列预测分析_第2张图片

参考链接

  • http://www.ilovematlab.cn/thread-113431-1-1.html
  • http://www.ilovematlab.cn/thread-132940-1-1.html

你可能感兴趣的:(动态神经网络,时间序列,matlab相关,深度学习,机器学习)