Matlab Simulation of Data Prediction Algorithm of BP Neural Network Based on PSO Particle Swarm Optimization

Table of contents

1. Theoretical basis

1.1. Implementation steps

1.2. Mathematical formula

2. Core program

3. Simulation conclusion


1. Theoretical basis

        BP neural network is a commonly used artificial neural network model, which can learn to adjust the connection weights between neurons, so as to realize the modeling and prediction of complex nonlinear problems. However, BP neural network has problems such as easy to fall into local minimum and slow training speed in practical application. For these problems, the particle swarm optimization (PSO) algorithm can be used to optimize the BP neural network, thereby improving its performance and prediction accuracy. This article will introduce in detail the data prediction algorithm of BP neural network based on PSO particle swarm optimization, including implementation steps, mathematical formulas and application cases.

1.1. Implementation steps

The data prediction algorithm of BP neural network based on PSO particle swarm optimization is mainly divided into the following steps:

       Data preprocessing: Preprocessing the original data, including data cleaning, normalization, etc., to improve the training effect of the neural network.

      Neural network model design: Design the structure and parameters of the BP neural network, including the number of neurons in the input layer, hidden layer, and output layer, and connection weights.

       Initialize the particle swarm: To optimize the BP neural network using the PSO algorithm, a group of particle swarms needs to be initialized, where each particle represents a group of connection weights.

       Calculation of fitness function: According to the connection weight of each particle, the prediction error of the neural network is calculated as the fitness function.

      Update particle position and velocity: update the particle's position and velocity according to the current position and velocity of the particle, as well as the global optimal position and the individual optimal position.

       Update connection weights: Calculate new connection weights based on the updated particle positions, and update the parameters of the neural network.

        Determine the stopping condition: Repeat the above steps until the preset stopping condition is reached, for example, the prediction error reaches a certain accuracy or the maximum number of iterations is reached.

1.2. Mathematical formula

The mathematical formula of BP neural network based on PSO particle swarm optimization is as follows: The
forward propagation formula of BP neural network can be expressed as:

$$ y_k = f\left(\sum_{j=1}^m w_{kj}x_j+b_k\right) $$

       Among them, y_k represents the output value of the kth neuron of the output layer, f represents the activation function, w_{kj} represents the weight between the jth neuron of the input layer and the kth neuron of the output layer, and x_j represents The input value of the jth neuron of the input layer, b_k represents the bias value of the kth neuron of the output layer.

The backpropagation formula of BP neural network can be expressed as:

$$ \Delta w_{kj}=-\eta\frac{\partial E}{\partial w_{kj}} $$

Among them, \eta represents the learning rate, and E represents the prediction error.
The position update formula of the PSO algorithm can be expressed as:

$$ x_i(t+1)=x_i(t)+v_i(t+1) $$

Among them, x_i(t) represents the position of particle i at time t, and v_i(t+1) represents the velocity of particle i at time t+1.

The speed update formula of the PSO algorithm can be expressed as:

$$ v_i(t+1)=wv_i(t)+c_1r_1(p_i-x_i(t))+c_2r_2(g-x_i(t)) $$

       Among them, w represents inertial weight, c_1 and c_2 represent individual and global learning factors respectively, r_1 and r_2 represent random numbers respectively, p_i represents the historical optimal position of particle i, and g represents the global optimal position.
       The fitness function is used to evaluate the performance of each particle and can be expressed as the sum of squared prediction errors:

$$ F=\sum_{i=1}^n(y_i-\hat{y}_i)^2 $$

Among them, y_i represents the actual value, \hat{y}_i represents the predicted value, and n represents the number of data samples.

      The data prediction algorithm of BP neural network based on PSO particle swarm optimization has been widely used in many fields, such as finance, meteorology, transportation and so on. Taking the stock price prediction as an example, the specific application steps of the algorithm are introduced below.

       Data preprocessing: collect historical stock price data, perform data cleaning and normalization processing.

       Neural network model design: design the structure and parameters of the BP neural network, for example, the input layer is 5 neurons, the hidden layer is 10 neurons, and the output layer is 1 neuron, using the Sigmoid activation function, the learning rate is 0.01, and the maximum The number of iterations is 1000 times.

Initialize the particle swarm: initialize 100 particles, each particle represents a set of connection weights, and randomly generate the initial value of the weights.

Calculate the fitness function: according to the connection weight of each particle, construct the BP neural network, calculate the prediction error, and use it as the fitness function.

Update particle position and velocity: update the particle's position and velocity according to the current position and velocity of the particle, as well as the global optimal position and the individual optimal position.

Update connection weights: Calculate new connection weights based on the updated particle positions, and update the parameters of the neural network.

Judging the stop condition: Repeat the above steps until the preset stop condition is reached.

Through experimental verification, the data prediction algorithm based on PSO particle swarm optimization BP neural network can significantly improve the accuracy and precision of stock price prediction, and provide investors with more reliable decision-making basis.

        Summary: This article introduces in detail the data prediction algorithm of BP neural network based on PSO particle swarm optimization, including implementation steps, mathematical formulas and application cases. This algorithm can effectively overcome the local minimum problem of BP neural network, improve the prediction accuracy and performance of neural network, and has broad application prospects.

2. Core program



%节点个数
inputnum=2;
hiddennum=5;
outputnum=1;

%训练数据和预测数据
input_train=input(1:1900,:)';
input_test=input(1901:2000,:)';
output_train=output(1:1900)';
output_test=output(1901:2000)';

%选连样本输入输出数据归一化
[inputn,inputps]=mapminmax(input_train);
[outputn,outputps]=mapminmax(output_train);

%构建网络
net=newff(inputn,outputn,hiddennum);

% 参数初始化
%粒子群算法中的两个参数
c1 = 1.49445;
c2 = 1.49445;

maxgen=100;   % 进化次数  
sizepop=30;   %种群规模

Vmax=1;
Vmin=-1;
popmax=5;
popmin=-5;

for i=1:sizepop
    pop(i,:)=5*rands(1,21);
    V(i,:)=rands(1,21);
    fitness(i)=fun(pop(i,:),inputnum,hiddennum,outputnum,net,inputn,outputn);
end


% 个体极值和群体极值
[bestfitness bestindex]=min(fitness);
zbest=pop(bestindex,:);   %全局最佳
gbest=pop;    %个体最佳
fitnessgbest=fitness;   %个体最佳适应度值
fitnesszbest=bestfitness;   %全局最佳适应度值

%% 迭代寻优
for i=1:maxgen
    i 
    
    for j=1:sizepop
        
        %速度更新
        V(j,:) = V(j,:) + c1*rand*(gbest(j,:) - pop(j,:)) + c2*rand*(zbest - pop(j,:));
        V(j,find(V(j,:)>Vmax))=Vmax;
        V(j,find(V(j,:)<Vmin))=Vmin;
        
        %种群更新
        pop(j,:)=pop(j,:)+0.2*V(j,:);
        pop(j,find(pop(j,:)>popmax))=popmax;
        pop(j,find(pop(j,:)<popmin))=popmin;
        
        %自适应变异
        pos=unidrnd(21);
        if rand>0.95
            pop(j,pos)=5*rands(1,1);
        end
      
        %适应度值
        fitness(j)=fun(pop(j,:),inputnum,hiddennum,outputnum,net,inputn,outputn);
    end
    
    for j=1:sizepop
    %个体最优更新
    if fitness(j) < fitnessgbest(j)
        gbest(j,:) = pop(j,:);
        fitnessgbest(j) = fitness(j);
    end
    
    %群体最优更新 
    if fitness(j) < fitnesszbest
        zbest = pop(j,:);
        fitnesszbest = fitness(j);
    end
    
    end
    
    yy(i)=fitnesszbest;    
        
end

%% 结果分析
plot(yy)
title(['适应度曲线  ' '终止代数=' num2str(maxgen)]);
xlabel('进化代数');ylabel('适应度');

x=zbest;
%% 把最优初始阀值权值赋予网络预测
% %用遗传算法优化的BP网络进行值预测
w1=x(1:inputnum*hiddennum);
B1=x(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
w2=x(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
B2=x(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum);

net.iw{1,1}=reshape(w1,hiddennum,inputnum);
net.lw{2,1}=reshape(w2,outputnum,hiddennum);
net.b{1}=reshape(B1,hiddennum,1);
net.b{2}=B2;

%% BP网络训练
%网络进化参数
net.trainParam.epochs=100;
net.trainParam.lr=0.1;
%net.trainParam.goal=0.00001;

%网络训练
[net,per2]=train(net,inputn,outputn);

%% BP网络预测
%数据归一化
inputn_test=mapminmax('apply',input_test,inputps);
an=sim(net,inputn_test);
test_simu=mapminmax('reverse',an,outputps);
error=test_simu-output_test;
up2152

3. Simulation conclusion

 

Guess you like

Origin blog.csdn.net/ccsss22/article/details/131484773