【时间序列预测】基于matlab LMS麦基玻璃时间序列预测【含Matlab源码 1443期】

·  阅读 177

一、获取代码方式

获取代码方式1: 通过订阅紫极神光博客付费专栏,凭支付凭证,私信博主,可获得此代码。

获取代码方式2: 通过紫极神光博客主页开通CSDN会员,凭支付凭证,私信博主,可获得此代码。

获取代码方式3: 完整代码已上传我的资源:【时间序列预测】基于matlab LMS麦基玻璃时间序列预测【含Matlab源码 1443期】

备注:开通CSDN会员,仅只能免费获得1份代码(有效期为开通日起,三天内有效); 订阅紫极神光博客付费专栏,可免费获得2份代码(有效期为订阅日起,三天内有效);

二、部分源代码

%% Mackey Glass Time Series Prediction Using Least Mean Square (LMS)
%  
clc
clear all
close all

%% Loading Time series data
% I generated a series y(t) for t = 0,1, . . . ,3000, using
% mackey glass series equation with the following configurations:
% b = 0.1, a = 0.2, Tau = 20, and the initial conditions y(t - Tau) = 0.
load Dataset\Data.mat 
 
teacher_forcing=1; % recurrent ARMA modelling with forced desired input after defined time steps 
%% Training and Testing datasets
% For training
Tr=Data(100:2500,1);    % Selecting a Interval of series data t = 100~2500
 
Ts=Data(2500:3000,1);   % Selecting a Interval of series data t = 2500~3000
Ys(Ts)=Data(Ts,2);      % Selecting a chuck of series data y(t)

%% LMS Parameters

 
U=zeros(M+1,1); % Initial values of taps
W=zeros(M+1,1); % Initial weight of LMS

MSE=[];         % Initial mean squared error (MSE)

%% Learning weights of LMS (Training)
tic % start
for t=Tr(1):Tr(end)-time_steps
    U(1:end-1)=U(2:end);    % Shifting of tap window
    
    if (teacher_forcing==1)
        if rem(t,time_steps)==0 || (t==Tr(1))
            U(end)=Yr(t);           % Input (past/current samples)
        else
            U(end)=Yp(t-1);          % Input (past/current samples)          
        end
    else
       
    end
 
    Yp(t)=W'*U;                         % Predicted output
    e(t)=Yr(t+time_steps)-Yp(t);        % Error in predicted output

    W=W+eta*e(t)*U;     % Weight update rule of LMS
    
    
end
training_time=toc; % total time including training and calculation of MSE

%% Prediction of a next outcome of series using previous samples (Testing)
tic % start
U=U*0;  % Reinitialization of taps (optional)
for t=Ts(1):Ts(end)-time_steps+1
    

    if (teacher_forcing==1)
        if rem(t,time_steps)==0 || (t==Ts(1))
            U(end)=Ys(t);           % Input (past/current samples)
        else
            U(end)=Yp(t-1);          % Input (past/current samples)          
        end
    else
        U(end)=Ys(t);           % Input (past/current samples)
    end
    
    
    Yp(t)=W'*U;             % Calculating output (future value)
    e(t)=Ys(t+time_steps-1)-Yp(t);        % Error in predicted output

    E(t)=e(t).^2;   % Current mean squared error (MSE)
end
testing_time=toc; % total time including testing and calculation of MSE

%% Results
figure(1)
plot(Tr,10*log10(E(Tr)));   % MSE curve
hold on
plot(Ts(1:end-time_steps+1),10*log10(E(Ts(1:end-time_steps+1))),'r');   % MSE curve
grid minor

title('Cost Function');
xlabel('Iterations (samples)');
ylabel('Mean Squared Error (MSE)');
legend('Training Phase','Test Phase');

figure(2)
 
hold on
plot(Tr(2*M:end),Yp(Tr(2*M:end))','r')   % Predicted values during training
plot(Ts,Ys(Ts),'--b');        % Actual unseen data
plot(Ts(1:end-time_steps+1),Yp(Ts(1:end-time_steps+1))','--r');  % Predicted values of mackey glass series (testing)
xlabel('Time: t');
ylabel('Output: Y(t)');
title('Mackey Glass Time Series Prediction Using Least Mean Square (LMS)')
ylim([min(Ys)-0.5, max(Ys)+0.5])
legend('Training Phase (desired)','Training Phase (predicted)','Test Phase (desired)','Test Phase (predicted)');

mitr=10*log10(mean(E(Tr)));  % Minimum MSE of training
mits=10*log10(mean(E(Ts(1:end-time_steps+1))));  % Minimum MSE of testing

display(sprintf('Total training time is %.5f, \nTotal testing time is %.5f \nMSE value during training %.3f (dB),\nMSE value during testing %.3f (dB)', ...
training_time,testing_time,mitr,mits));

复制代码

三、运行结果

在这里插入图片描述 在这里插入图片描述

四、matlab版本及参考文献

1 matlab版本 2014a

2 参考文献 [1] 包子阳,余继周,杨杉.智能优化算法及其MATLAB实例(第2版)[M].电子工业出版社,2016. [2]张岩,吴水根.MATLAB优化算法源代码[M].清华大学出版社,2017. [3]周品.MATLAB 神经网络设计与应用[M].清华大学出版社,2013. [4]陈明.MATLAB神经网络原理与实例精解[M].清华大学出版社,2013. [5]方清城.MATLAB R2016a神经网络设计与应用28个案例分析[M].清华大学出版社,2018.

分类:
人工智能
标签:
收藏成功!
已添加到「」, 点击更改