移动平均法是运用过去时间序列的数据进行统计分析去推测事物的发展趋势,适用于历史序列的基本趋势变化不大且序列中随机变动成分较多时使用,只适合做近期预测。移动平均法有简单移动平均法,加权移动平均法,趋势移动平均法等。
理论计算
假设观测的数据序列为,取值移动平均值的项数m<n。一次移动平均值计算的公式为:
进一步化简可得:
一次移动平均值法建立的预测模型:
预测的标准误差计算公式:
用最近m期序列值的平均值作为预测结果,其中移动平均的项数m的一步取值为:
当预测目标的基本趋势与某一线型模型相吻合时,常用二次移动平均法。
二次移动平均值计算公式为:
但序列同时存在线型趋势与周期波动时,可以使用趋势移动平均法建立预测模型:
其中:
一次移动平均法GUI界面如下:
加载数据——输入原始数据起始年份、原始数据最终年份、步长数、预测数据个数、x轴坐标名称、y轴坐标名称——点击开始计算即可出现结果,同时会在当前文件夹下生成预测数据的excel文件“MSE.xlsx”,“结果.xlsx”和预测结果.jpg。需要一次移动平均法GUI界面完整GUI程序,可以进行赞赏后截图(10元及以上),进行联系,或者在微信公众号“云龙派”内回复截图,几小时内会回复。界面编程不易,还请见谅!
界面举例计算:
数据为内蒙古2009-2017年货品运输数量
1、点击加载数据,选择数据excel文件
2、输入参数
3、点击开始计算
一次移动平均法GUI主要程序如下:
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global data12
data = data12(:,2)';
data2 = data;
year_str= str2num(get(handles.edit1,'string'));
year_end = str2num(get(handles.edit2,'string'));
m = str2num(get(handles.edit3,'string'));
num = str2num(get(handles.edit4,'string'));
str1 = get(handles.edit5,'string');
str2 = get(handles.edit6,'string');
data1 = [];
M = [];
str_1 = [];
for k=1:num
Len=length(data);
for i=1:length(m)
for j=1:Len-m(i)+1
Yt{i}(j)=sum(data(j:j+m(i)-1))/m(i); %求y的预测值
end
MSE(i)=1/(Len-m(i))*sum((data(m(i)+1:Len)-Yt{i}(1:Len-m(i))).^2); %求MSE
end
[ans1,p]=min(MSE);
p=m(p);
year=2017;
year=year+k;
data=cat(2,data,[Yt{p}(Len-m(p)+1)]);
str =[num2str(year) '年 ' '预测数据为:' num2str(Yt{p}(Len-m(p)+1)) ' 最优的MSE为:' num2str(ans1) ' 移动项数为:' num2str(p)];
str_1 = strcat(str_1,[newline str]) ;%newline 等效于 char(10) 或 sprintf('\n')。使用 newline 将换行符与字符向量或字符串串联,或在换行符处拆分文本。
data1 = [data1;year Yt{p}(Len-m(p)+1) p];
M = [M;MSE];
end
set(handles.edit7,'string',str_1);
figure;
plot(year_str:year_end,data2,'--o','LineWidth',1.5);
hold on;
plot(data1(:,1),data1(:,2),'x','LineWidth',1.5);
legend('真实数据','预测数据','location','northwest');
xlabel(str1);
ylabel(str2);
saveas(gcf,'预测结果.jpg');%保存生成的图片
close(gcf);
axes(handles.axes1);
plot(year_str:year_end,data2,'--o','LineWidth',1.5);
hold on;
plot(data1(:,1),data1(:,2),'x','LineWidth',1.5);
legend('真实数据','预测数据','location','northwest');
xlabel(str1);
ylabel(str2);
Year = [year_str: data1(end,1)];
Data = [data2 data1(:,2)'];
DATA = [Year;Data];
xlswrite('结果.xlsx',DATA);
M = [year_end+1:year_end+num;M'];
xlswrite('MSE.xlsx',M);
set(handles.uitable1,'data',DATA );
set(handles.uitable2,'data',M );
二次移动平均法GUI界面如下:
加载数据——输入原始数据起始年份、原始数据最终年份、步长数、预测数据个数、x轴坐标名称、y轴坐标名称——点击开始计算即可出现结果,同时会在当前文件夹下生成预测数据的excel文件“Result.xlsx”和预测1.jpg和预测2.jpg。需要二次移动平均法GUI界面完整GUI程序,可以进行赞赏后截图(10元及以上),进行联系,或者在微信公众号“云龙派”内回复截图,几小时内会回复。界面编程不易,还请见谅!
界面举例计算:
数据同样使用内蒙古2009-2017年货品运输数量
1、点击加载数据,选择数据excel文件
2、输入参数
3、点击开始计算
二次移动平均法GUI主要程序如下:
function varargout = erciyidong(varargin)
% ERCIYIDONG MATLAB code for erciyidong.fig
% ERCIYIDONG, by itself, creates a new ERCIYIDONG or raises the existing
% singleton*.
%
% H = ERCIYIDONG returns the handle to a new ERCIYIDONG or the handle to
% the existing singleton*.
%
% ERCIYIDONG('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ERCIYIDONG.M with the given input arguments.
%
% ERCIYIDONG('Property','Value',...) creates a new ERCIYIDONG or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before erciyidong_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to erciyidong_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help erciyidong
% Last Modified by GUIDE v2.5 21-Feb-2022 15:08:04
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @erciyidong_OpeningFcn, ...
'gui_OutputFcn', @erciyidong_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before erciyidong is made visible.
function erciyidong_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to erciyidong (see VARARGIN)
% Choose default command line output for erciyidong
handles.output = hObject;
movegui(gcf,'center');
%关闭窗口的名字 修改为其他名字
set(gcf,'NumberTitle','off','Name','二次移动平均法计算系统');
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes erciyidong wait for user response (see UIRESUME)
% uiwait(handles.figure1);
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global Data
n0 = str2num(get(handles.edit12,'string'));
n1 = str2num(get(handles.edit13,'string'));
N_list = str2num(get(handles.edit14,'string'));
num = str2num(get(handles.edit15,'string'));
str1 = get(handles.edit16,'string');
str2 = get(handles.edit17,'string');
year = Data(1:end-1,1);
data = Data(1:end-1,2);
data1 = [];
d_2007= Data(end,2);
for n=1:length(N_list)
figure;
plot(year,data,'--o','LineWidth',1.5)
hold on
plot(Data(end,1),d_2007,'s','LineWidth',1.5)
xlabel(str1)
ylabel(str2);
hold on
N=N_list(n); %设定步长数
for t=N:length(year)
M1(t)=sum(data(t-N+1:t))/N;%一次移动平均值
end
for t=(2*N-1):length(M1)
M2(t)=sum(M1(t-N+1:t))/N;%二次移动平均值
end
T=1:num; %预预测时间长度x
a=2*M1(end)-M2(end);
b=2*(M1(end)-M2(end))/(N-1);
y_p=a+b*T;
T_p=year(end)+T;
plot(T_p,y_p,'*','LineWidth',1.5)
data1 = [ data1; y_p];
hold on
T=1; %预预测时间长度x
a=2*M1(2*N-1:end-1)-M2(2*N-1:end-1);
b=2*(M1(2*N-1:end-1)-M2(2*N-1:end-1))/(N-1);
y_p2=a+b*T;
plot(year((2*N-1:end-1)+1),y_p2,'--x','LineWidth',1.5)
hold on
legend('真实数据','测试的真实数据','预测数据','拟合数据','location','northwest')
title_str=['移动平均值预测法',' 步长为:',num2str(N)];
title(title_str)
saveas(gcf,strcat(strcat('预测',num2str(n)),'.jpg'));%保存生成的图片
close(gcf);
end
data2 = [T_p; data1];
set(handles.uitable2,'data',data2);
xlswrite('Result.xlsx',data2);
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.edit12,'string','');
set(handles.edit13,'string','');
set(handles.edit14,'string','');
set(handles.edit15,'string','');
set(handles.edit16,'string','');
set(handles.edit17,'string','');
tableData = [];
set(handles.uitable2,'data',tableData);
try
delete(allchild(handles.axes4));
delete(allchild(handles.axes5));
end
作 者 | 郭志龙
编 辑 | 郭志龙
校 对 | 郭志龙
本文内容来源于网络,仅供参考学习,如内容、图片有任何版权问题,请联系处理,24小时内删除。
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站不拥有所有权,不承担相关法律责任。如发现有侵权/违规的内容, 联系QQ15101117,本站将立刻清除。