Time series prediction | MATLAB implements PSO-LSSVM particle swarm algorithm optimization least squares support vector machine time series prediction future

Time series prediction | MATLAB implements PSO-LSSVM particle swarm algorithm optimization least squares support vector machine time series prediction future

predictive effect

insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here
insert image description here

basic introduction

1. Matlab implements PSO-LSSVM time series to predict the future (particle swarm optimization least squares support vector machine, optimize gam and sig of RBF kernel function); 2. The
operating environment is Matlab2018 and above, data is the data set, and univariate time series prediction , just run the main program PSO_LSSVMTSF, and the rest are function files, no need to run;
3. Recursively predict future data, you can control the number of predicted future sizes, suitable for cyclical and periodic data prediction;
4. Command window output R2, MAE, MAPE , MBE, MSE and other evaluation indicators.

Model description

There is no definitive or general consensus method for the LSSVM parameter optimization problem. Due to the robustness and versatility of intelligent algorithms in the selection and determination of prediction model parameters, genetic algorithms, fruit fly optimization algorithms, firefly algorithms, particle swarm optimization algorithms (PSO), and grid search algorithms are mainly used in the optimization process of prediction model parameters. , neural network and other intelligent algorithms. The particle swarm optimization algorithm constantly adjusts the relationship between itself and the optimal position of the population, and has stronger optimization capabilities. Therefore, in order to further obtain reliable model parameters, the particle swarm optimization algorithm can be used for trial verification.

9

programming

  • Complete program and data download methods Private message blogger replies: MATLAB implements PSO-LSSVM particle swarm optimization algorithm to optimize least squares support vector machine time series to predict the future .
%%  参数设置
pop = 5;              % 种群数目
Max_iter = 50;         % 迭代次数
dim = 2;               % 优化参数个数
lb = [10,   10];       % 下限
ub = [1000, 1000];       % 上限

%% 优化函数
fobj = @(x)fitnessfunclssvm(x, p_train, t_train);

%% 优化
[Best_pos, Best_score, curve] = PSO(pop, Max_iter, lb, ub, dim, fobj);

%% LSSVM参数设置
type       = 'f';                % 模型类型 回归
kernel     = 'RBF_kernel';       % RBF 核函数
proprecess = 'preprocess';       % 是否归一化

%% 建立模型
gam = Best_score(1);  
sig = Best_score(2);
model = initlssvm(p_train, t_train, type, gam, sig, kernel, proprecess);

%% 训练模型
model = trainlssvm(model);

%% 模型预测
t_sim1 = simlssvm(model, p_train);
t_sim2 = simlssvm(model, p_test);

%%  数据反归一化
T_sim1 = mapminmax('reverse', t_sim1, ps_output);
T_sim2 = mapminmax('reverse', t_sim2, ps_output);
%% 定义粒子群算法参数
% N 种群 T 迭代次数 
%% 随机初始化种群
D=dim;                   %粒子维数
c1=1.5;                 %学习因子1
c2=1.5;                 %学习因子2
w=0.8;                  %惯性权重

Xmax=ub;                %位置最大值
Xmin=lb;               %位置最小值
Vmax=ub;                %速度最大值
Vmin=lb;               %速度最小值
%%
%%%%%%%%%%%%%%%%初始化种群个体(限定位置和速度)%%%%%%%%%%%%%%%%

x=rand(N,D).*(Xmax-Xmin)+Xmin;
v=rand(N,D).*(Vmax-Vmin)+Vmin;
%%%%%%%%%%%%%%%%%%初始化个体最优位置和最优值%%%%%%%%%%%%%%%%%%%
p=x;
pbest=ones(N,1);
for i=1:N
    pbest(i)=fobj(x(i,:)); 
end
%%%%%%%%%%%%%%%%%%%初始化全局最优位置和最优值%%%%%%%%%%%%%%%%%%
g=ones(1,D);
gbest=inf;
for i=1:N
    if(pbest(i)<gbest)
        g=p(i,:);
        gbest=pbest(i);
    end
end
%%%%%%%%%%%按照公式依次迭代直到满足精度或者迭代次数%%%%%%%%%%%%%
for i=1:T
    i
    for j=1:N
        %%%%%%%%%%%%%%更新个体最优位置和最优值%%%%%%%%%%%%%%%%%
        if (fobj(x(j,:))) <pbest(j)
            p(j,:)=x(j,:);
            pbest(j)=fobj(x(j,:)); 
        end
        %%%%%%%%%%%%%%%%更新全局最优位置和最优值%%%%%%%%%%%%%%%
        if(pbest(j)<gbest)
            g=p(j,:);
            gbest=pbest(j);
        end
        %%%%%%%%%%%%%%%%%跟新位置和速度值%%%%%%%%%%%%%%%%%%%%%
        v(j,:)=w*v(j,:)+c1*rand*(p(j,:)-x(j,:))...
            +c2*rand*(g-x(j,:));
        x(j,:)=x(j,:)+v(j,:);
        %%%%%%%%%%%%%%%%%%%%边界条件处理%%%%%%%%%%%%%%%%%%%%%%
        if length(Vmax)==1
            for ii=1:D
                if (v(j,ii)>Vmax)  |  (v(j,ii)< Vmin)
                    v(j,ii)=rand * (Vmax-Vmin)+Vmin;
                end
                if (x(j,ii)>Xmax)  |  (x(j,ii)< Xmin)
                    x(j,ii)=rand * (Xmax-Xmin)+Xmin;
                end
            end           
        else
            for ii=1:D
                if (v(j,ii)>Vmax(ii))  |  (v(j,ii)< Vmin(ii))
                    v(j,ii)=rand * (Vmax(ii)-Vmin(ii))+Vmin(ii);
                end
                if (x(j,ii)>Xmax(ii))  |  (x(j,ii)< Xmin(ii))
                    x(j,ii)=rand * (Xmax(ii)-Xmin(ii))+Xmin(ii);
                end
            end
        end
            
    end
    %%%%%%%%%%%%%%%%%%%%记录历代全局最优值%%%%%%%%%%%%%%%%%%%%%
   Convergence_curve(i)=gbest;%记录训练集的适应度值

References

[1] https://blog.csdn.net/article/details/126072792?spm=1001.2014.3001.5502
[2] https://blog.csdn.net/article/details/126044265?spm=1001.2014.3001.5502

Guess you like

Origin blog.csdn.net/kjm13182345320/article/details/132680009