ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【时间序列预测】基于matlab最小均方(LMS)算法时间序列预测【含Matlab源码 1335期】

2021-09-25 16:03:25  阅读:239  来源: 互联网

标签:end 1335 Tr Ts 源码 steps time 序列 MSE


一、最小均方(LMS)算法简介

理论知识参考:最小均方算法(LMS)

二、部分源代码

%% Mackey Glass Time Series Prediction Using Least Mean Square (LMS)
% Author: 紫极神光
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 
time_steps=2;
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

% For testing
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

eta=5e-3;       % Learning rate
M=1;            % Order of LMS filter


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
        U(end)=Yr(t);           % Input (past/current samples)
    end
 
  
    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
    U(1:end-1)=U(2:end);    % Shifting of tap window

    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
     
    
    
    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)
plot(Tr(2*M:end),Yr(Tr(2*M:end)));      % Actual values of mackey glass series
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.

标签:end,1335,Tr,Ts,源码,steps,time,序列,MSE
来源: https://www.cnblogs.com/Q912100926/p/15334580.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有