Application Of Particle Swarm Optimization and Improved PSO-BP Algorithm in Computer Forecasting Model

Given the shortcomings of the traditional BP (Back Propagation) neural network in dealing with medium and long-term stock forecasting, this paper presents a combination forecasting model of BP network optimized by PSO (Particle Swarm Optimization). Firstly, the ARIMA (Autoregressive Integrated Moving Average) model was used to predict the stock's closing price, which was used as part of the BP neural network input. On this basis, the BP network was optimized by particle swarm optimization algorithm, and the optimal threshold and weight were given to the network for further prediction, which improved the generalization ability and prediction accuracy of the whole network. The results show that, compared with BP neural network, LSTM (Long Short-Term Memory) and ARIMA model, the PSO-BP network model has a significant improvement in forecasting accuracy, which verifies the effectiveness of the combination forecasting method in dealing with the problem of medium and long-term stock price forecasting.


Introduction
The price forecast of the stock market has been deeply concerned by researchers. Due to its inherently high noise, non-stationary characteristics, and the impact of large fluctuations in the market, the whole forecasting process is very complicated. Therefore, how to effectively predict the stock market is still a problem worthy of discussion. The traditional stock forecasting methods include grey forecasting, time series forecasting model, artificial intelligence algorithms such as support vector machine, BP neural network, recurrent neural network, and long-term memory network.
In the existing research of stock time series prediction, Changpin Cheng constructed the ARIMA-SVM (Support Vector Machine) prediction model based on time series [1] and used ARIMA model to capture the linear law of stock closing price. The combined model has a good prediction effect in the short term. The time series is outstanding, it does not consider the influence of external factors, requires the market to follow the characteristics of continuous and gradual, so there are often apparent errors in the process of mid-and long-term stock price forecasting. In the experimental process, the prediction effect of SVM is significantly affected by its kernel function, which affects the accuracy of the results, so the research is not easy to expand.  [2] for short-and medium-term prediction, and the prediction results were better than models such as CNN (Convolutional Neural Networks) and RNN. However, many parameters need to be set manually in the LSTM network structure. The different parameters directly determine the training effect of the network, thus affecting the prediction accuracy of the model. Therefore, its limitations sometimes make the model structure poor, resulting in a resulting error in the actual process.
Based on Wanwei High-tech stock data, Aiping Wang had made a prediction and analysis of the short-term trend of the stock through the BP neural network [3] and achieved good results. The shortterm prediction accuracy can reach up to 80%. However, in the past stock forecast, the short-term forecast has occupied the majority. Many models show good performance in short-term forecasting but poor performance in mid-to-long-term forecasting. With its powerful nonlinear mapping function, BP neural network is conducive to solving the complex problems of internal mechanisms such as stocks. It can fit the market characteristics of stocks in the prediction, so it also has a good performance in the medium and long-term stock price prediction. As a local search method, BP neural network is easy to fall into the local minimum. Simultaneously, it is faced with the shortcomings of a large number of samples and weak generalization ability in dealing with the medium and long-term complex stock price prediction, which affects the training results.
Therefore, this paper used the improved PSO-BP neural network algorithm for medium and longterm stock price prediction because of the complex problem of stock price prediction. This method used the particle swarm optimization algorithm's good global search ability to improve the parameter adjustment model and improve the overall search efficiency. The model can make up for the lack of single BP neural network structure selection and weak generalization ability and improve the prediction accuracy of the model as a whole.

ARIMA Time series model
Standard stationary time series models include autoregressive AR, moving average Ma, ARMA, etc. [4]. However, in dealing with practical problems, many time series are not approximate to stationary time series so that the original non-stationary time series can be transformed into new stationary time series by some processing such as difference and logarithm. Then ARIMA model can be used for modeling. An ARIMA (p, d, q): p represents the autoregressive order; d represents the number of differential processing for nonstationary time series with long-term trends, seasonal variations, and cyclic variations; q represents the order of moving average. In this paper, the ARIMA model (autoregressive moving average model) was used to predict and analyze the data, then part of the prediction results was used as the training input of the BP neural network.

BP Neural networks
BP network is a multilayer feedforward neural network [5] , which has strong mapping ability and can realize any nonlinear mapping from input to output. It is divided into the input layer, hidden layer, and output layer. The hidden layer can have one or more layers, and there is no interconnection between the same layer. The input data is transmitted from the input node to each hidden layer node and then to the output node. The output of each layer node only affects the output of the next layer node. The commonly used activation functions among nodes are sigmoid, tanh, etc.

Particle swarm optimization algorithm
PSO is a random search algorithm that simulates the foraging behavior of birds. It has high convergence speed, strong global search ability in dealing with nonlinear problems, and convergence to the optimal global solution with high probability. Suppose that the particle swarm composed of n particles B = (B1, , B3, …, Bn), Where the position of the ith particle Bi can be expressed as Xi = (Xi1, Xi2, …, Xid); its "flying" speed is recorded as Vi = (Vi1, Vi2, Vid), Then the position and velocity of the particle in the t+1 iteration can be updated as: In the formula, t is the number of iterations, is the inertia factor, c1 and c2 are the learning factors, r1 and r2 are the uniform random numbers in the range of [0,1]. Besides, and are the optimal positions searched by the ith particle and the whole particle swarm from the initial to the current number of iterations, respectively, recorded as individual extremum and global extremum.
In particle swarm optimization, increasing (decreasing) the value of inertia weight can improve the global (local) search ability of the algorithm, and reasonably designing inertia weight can effectively avoid falling into local optimum to achieve the purpose of the efficient search.

PSO Optimization BP network
Firstly, the activation function of the BP network is set as a sigmoid function. Then, the PSO algorithm is used to compare the fitness of each particle to find out the optimal global position so that the total mean square error of P samples in the system can be minimized: Among them: P is the number of samples, L is the number of output neurons, and are the ideal (actual) output values of the p-th sample at the k-th node. The specific implementation steps of the PSO optimization algorithm are as follows: I. Initialize the neural network, including network structure, transfer function, target vector, etc. II. Initialize the particle swarm according to the network size, including the network swarm size N, the initial position Xi, and the velocity Vi of the particle, and use the connection weights and thresholds of the neural network as the position vector of the particle. At the same time, set the error limit of the fitness value, the maximum allowable number of iterations, and the maximum speed of particle movement. III. Evaluate each particle's fitness value and store the position and fitness value of the particle in the extreme individual value of the particle. Compare each particle's current fitness and select the global optimal value Gbest from the optimal local value of each particle.
Ⅳ. Use the above formulas (1) and (2) to update each particle's position and velocity and continue to iterate.
Ⅴ. Compare the fitness value of each particle with the best position of the particle. If it is similar, use the current value as the best position of the particle, compare all Pbest and Gbest, and update Gbest.
Ⅵ. If the system fitness error reaches the set error limit or exceeds the maximum allowable number of iterations, the iteration stops, the historical global optimal position is output, and the optimal initial threshold and weight are assigned to the network prediction. Otherwise, update the position and velocity of each particle and continue the above iteration.

Improved Neural Network Combination Prediction Model
In recent years, particle swarm optimization BP neural network has been widely used in energy machinery. In their research, Jifeng Cui had used PSO Optimized BP neural network [4] for power data prediction, which verified the effectiveness of the combined prediction method in medium and longterm prediction. In this paper, we selected the data of growing pharmaceutical stocks affected by market volatility and used a particle swarm optimization algorithm to optimize BP neural network to predict the long-term trend of stock closing price to provide some reference for medium and long-term investors.
Step1 this experiment selected 460 historical trading days data from March 2019 to January 2021, including the opening price, closing price, highest price, and lowest price. First, the 460-day trading data was divided into three groups, the first 200 groups were used for time series prediction, the second 200 groups were used for BP neural network training, and the remaining 60 groups were used to test the performance of the neural network. As the stock closing price is a variable, the prediction is based on its historical data. There is a specific functional relationship between the following value and the previous value in the time series. Therefore, this paper used the ARIMA model for the rolling forecast method, selected 200 closing price data from March 19 to January 20, and forecast the closing price 40 days after each forecast. Repeated the above operation five times to get the closing price of 200 days after the initial prediction, which was used for part of the training input of the network. Next, forecast the closing price for the remaining 60 days.
Step2 The predicted value of the time series was used as the input of the BP neural network, and the corresponding actual value was matched as the training output of the network. The input layer of the BP neural network has four layers: the opening price, the highest price, the lowest price, and the closing price of the time series prediction. The output layer is the closing price of the stock on the day. The number of hidden layer nodes is 4, and the transfer function between nodes is set as a Sigmoid type function.
The input of training set: 200 sets of the historical opening price, high price, low price, and closing price data predicted by time series Training set output: the closing price of 200 trading days after the forecast Test set input: 60 groups of the historical opening price, high price, low price, closing price data obtained from time series prediction Test set output: closing price of 60 trading days after forecast Step3 Used particle swarm optimization algorithm to optimize the BP neural network, obtained the optimal global value by searching, assigned the optimal threshold and weight to the network prediction, and conducted BP network training and learning according to the given samples. Finally, it forecast the closing price of the stock in the last 60 days. The whole network prediction process is shown in Figure 1.

Comparative analysis of experimental results
While establishing the PSO-BP combination model for forecasting, this paper also used the moving average autoregressive model (ARIMA), LSTM, and single BP neural network model for comparative experiments to predict the closing price of the next 60 trading days.
First, the improved PSO-BP algorithm and BP neural network were used to compare the predicted closing price with the real value to verify the effectiveness of the prediction method proposed in this study. The comparison results are shown in Figure2. It can be seen from the figure that the predicted value curve of the PSO-BP model is closer to the real value curve of the stock price than that of the single BP network model. However, because the two prediction models have the same unit structure, they are very close to the actual value, and the gap is not apparent.

Model evaluation criteria
In order to further verify the prediction performance of the combined model, this paper selected RMSE (Root Mean Square Error), MAE (Mean Absolute Error), MAPE (Mean Absolute Percentage Error), and R 2 (Goodness of Fit) as evaluation indicators to quantitatively analyze the model prediction results. The smaller the value of RMSE and MAE, the closer the percentage of MAPE is to 0, the smaller the deviation between the predicted results and the real values, the higher the accuracy of the results; the closer the determination coefficient R 2 is to 1, the higher the goodness of fit, the better the prediction effect of the model. The specific formula is defined as follows: In the formula, n is the number of experimental predictions, is the predicted value of the model, is the actual real value, and is the average value of the real value. The error comparison results between the improved PSO-BP algorithm and BP neural network, LSTM, and ARIMA prediction models are shown in Table 1. The results show that the PSO-BP neural network combination model's prediction error is lower than other prediction models under RMSE, MAE, and MAPE evaluation standards, and the determination coefficient R 2 reaches 0.9569, which is closer to 1 than other prediction models. Under the MAPE index, the PSO-BP model is 46% lower than the ARIMA model, 40% lower than the LSTM model, and 28% lower than the single BP neural network model. The prediction accuracy of the model is significantly improved. Therefore, the prediction result of the BP neural network model optimized by PSO is better than other models.