Average variance portfolio optimization using machine learning-based stock price prediction case of renewable energy investments

: With the progress of time series prediction, several recent developments in machine learning have shown that the integration of prediction methods into portfolio selection is a great opportunity to structure investment decisions in the renewable energy industry. In this paper, we propose a novel approach to portfolio formation strategy based on a hybrid machine learning model that combines a convolutional neural network (CNN) and long-term bidirectional memory (BiLSTM) with robust input characteristics obtained from Huber's location for stock prediction and the mean-variance (MV) Markowitz model for optimal portfolio construction. Specifically, this study first applies a prediction method for stock pre-selection to ensure high-quality stock inflows for portfolio formation. Then, the predicted results are integrated into the MV model. To comprehensively demonstrate the superiority of the proposed model, we used two portfolio models, the MV model and the equal-weighted (1/N) portfolio model, with LSTM, BiLSTM and CNN-BiLSTM, and used them as references. Between January 2016 and December 2021, historical data from the Stock Exchange of Thailand 50 Index (SET50) was collected for the study. Experience shows that integrating stock pre-selection can improve VM performance, and the results of the proposed method show that they outperform comparison models in terms of Sharpe ratio, average return and risk .


Introduction
In recent years, there has been a reluctance on the part of investors to invest in renewable energy technologies. Therefore, the most important prerequisite for assessing investment risks and creating a better situation to attract the tendency of investors to invest in this area is the optimization of portfolio selection to structure investment decisions in this area.
Portfolio optimization is one of the most interesting issues, the MV model relies on historical data to generate the optimal portfolio and can only display the optimal portfolio as far as data entry is concerned. Therefore, a number of researchers have applied machine learning to predict return and volatility in the future (Henrique et al. 2019). Investors in the financial market need to evaluate a variety of factors and perspectives to maximize their investment income (Rahiminezhad Galankashi et al. 2020). In this regard, including stock price prediction methods in portfolio optimization would be beneficial and cost-effective for investors (Kolm et al. 2014). Financial time series forecasting has long been a challenging area of study, as financial market fluctuations are inherently volatile, complex and dynamic (Paiva et al. 2019). However, several related studies claim that there is a pattern of asset price movement in financial time series data and that this pattern can be used to predict financial time series data to some extent (Wan et al. 2020; Wang et al. 2020).
Portfolio management is an analytical process of selecting and allocating a group of investment assets in which the allocated portion of the investment is constantly modified to optimize expected return and risk tolerance (Markowitz 1952). Markowitz's mean variance (MV) model, first developed in 1952, is the foundation of portfolio theory, which is widely used and recognized in portfolio management (Sharpe and Markowitz 1989).
However, based on the classical MV model, there are two main problems of practical application. The first is that the MV relies on the expected return and risk of asset inflows to produce optimal portfolios for each level of expected return and risk (Beheshti et al) to put into the optimization process, the MV model can achieve improved returns (Mitra Thakur et al 2018). Another problem is that many high-risk assets often return a large number of small-scale weightings into the optimal portfolio, making them difficult to implement, especially for individual investors (Ben Salah et al. 2018; Ortiz et al. 2021;Huang et al. 2021).
The main objective of this study is to develop a portfolio formation approach for individual investors in which a hybrid machine learning model that combines a convolutional neural network and bidirectional short-term memory with robust input characteristics (R-CNN-BiLSTM) is applied to predict future closing stock prices before using the MV model to build the optimal portfolio. In this regard, this study proposes a new approach to portfolio formation that combines R-CNN-BiLSTM and MV (R-CNN-BiLSTM+MV1). The article is organized as follows. In Section 2, reviews of some existing studies are discussed regarding inventory forecasting and portfolio optimization, as well as empirical work that uses traditional statistics and machine learning methods to solve problems related to inventory forecasting and selection. Section 3 briefly explains the underlying knowledge used in this study. Section 4 presents the detailed experimental process. Section 5 reports the experimental results. Finally, Section 6 discusses the main findings of the work, theoretical implementations, and limitations. Markowitz (1952) proposed the mean-variance (MV) model and was awarded the Noble Prize in Economics in 1990. The MV model used mean and variance, which are calculated from historical asset prices to quantify the expected return and risk of the generated portfolio. The MV model assumes that the investor wants to either maximize the expected return for a given level of risk or minimize the risk for a given return (Kolm et al. 2014). However, in this study we only show optimization with minimal variance. The MV model is described as follows:

Mean-variance optimization
Minimize: Subject to: ≥ 0 (4) where N is the total number of assets, which indicates the dimensionality of optimization in the portfolio. wi is the weight of each i asset in the portfolio to be optimized; 2 represents the variance of the portfolio which generally refers to the risk of the portfolio; Cij is the covariance of the return between assets i and j; is the expected or target performance; and Ei is the average return on an individual asset i.

CNN
A convolutional neural network (CNN) is a kind of deep learning model for processing grid model data, such as image processing and natural language processing. CNN can be applied to predict time series data (Sadouk 2019). CNN can significantly improve the quality of learning models by reducing the number of parameters. CNN is mainly composed of three types of layers: a convolution layer, a clustering layer, and a fully connected layer (Albawi et al. 2017). The first two layers, the convolution layer and the grouping layer, perform feature extraction, while the last layer, the fully connected layer, directs the extracted features to the output (Miloševi ć and Rackovi ć 2019).

LSTM
Long-term short-term memory (LSTM) was proposed by Hochreiter and Schmidhuber (Hochreiter and Schmidhuber 1997). The model is a class of RNN but has a memory function, which allows LSTM to recycle data over a long period of time compared to RNN (Fischer and Krauss 2018  (6) Ot = (w σ 0 [ht-1, xt] + b0) (7) Ct= tanh(wc[ht-1,xt] + bc) (8) Output gate : ct=ft ct-1 + it ct (9) ht = Ot tanh(ct) (10) where ft it and Ot denote respectively the forgotten door, the entrance door and the exit door; w is the weight of the matrix; bt, bi and bo indicate the polarization of the forgetting door, the entrance door and the exit door, respectively; means the sigmoid function; xt and ht designate respectively the input and output of current at time t; This is the value of the entrance gate at time t; and the hyperbolic function (tangent) is represented by tanh .

BiLSTM
Long-term bidirectional memory (BiLSTM) is an improved version of LSTM with the ability to access the front and rear directions of the input feature (Dong et al. 2014). The main difference between BiLSTM and LSTM is that it uses two hidden layers. BiLSTM was found to be better than LSTM in terms of predicting time series data (Siami-Namini et al. 2019). The output of the hidden layer of BiLSTM has the activation function both forward and backward. The BiLSTM equations (Yang and Wang 2022) are described as follows: ℎ ⃑⃑⃑ = σ( ℎ → + ℎ ⃑ ⃑ ℎ ⃑ ⃑ ℎ −1 ⃑⃑⃑⃑⃑⃑⃑⃑ + ℎ ⃑ ⃑ ) (11) ℎ ⃖⃑⃑⃑ = σ( ℎ ← + ℎ ⃖⃑ ⃑ ℎ ⃖⃑ ⃑ ℎ −1 ⃖⃑⃑⃑⃑⃑⃑⃑⃑ + ℎ ⃖⃑ ⃑ ) (12) = ℎ → ℎ ⃑ + ℎ ⃖⃑ ⃑ ℎ ⃑ + (13) where represents the function for activating the model. W is the weight of the matrix; Wxh is the weight of the input (x) in the hidden layer (h); Ht indicates the entrance of the hidden layer; and bx denotes the polarization of the respective gates (x). The output is carried out by updating forward and backward structures.ℎ ⃑⃑⃑ ℎ ⃖⃑⃑⃑

Robust statistics
In real-world applications, data collection often includes atypical observations that deviate from the majority or mass of data, in which these observations are referred to as outliers, especially in financial time series data. For example, for stock prices, outliers deviate from the general trend in the data. Therefore, it is very difficult to predict future stock prices.
To overcome these limitations, this study uses a robust statistical theory (Maronna et al. 2019) to estimate the appropriate dataset to use in the machine learning training process.

The Classic Robust Location Estimator
The mean and median of the sample are considered location estimators of the data distribution. The main difference is that the sample mean is not robust to extreme outliers. For example, the closing price of a certain stock in a week falls immediately for unexpected reasons. For example, provided that the historical closing prices of a certain stock are {243, 190, 150, 80, 56, 28, 142}, in this case, the sample average is 125, which is not considered a good location estimator for these observations. Then the median of the sample is 150, which is a robust data location estimator. However, if the distribution of data is considered approximately normal, then the sample mean would be considered a better estimator than the sample median. The robust location estimator is a combination of these two classical estimators. To put it precisely, when there are extreme outliers in observations, the robust estimator approaches the median of the sample. On the other hand, the estimated location is close to the sample average.

Huber Location Estimator Tool
Huber (1964) proposed a good combination of mean and median, called a robust location estimator or M estimator of a location, which can be described as follows: where is a robust location estimator of the observation; xi is the observation variable I; represents the error function.
Robust location is a parameter that minimizes function to ensure that the parameter provides the minimum error between the location estimator and all observations. Several methods have been proposed (Maronna et al. 2006) to find the local minimum of the function such as the maximum likelihood estimator (MLE). In this paper, we use a numerical method, the Newton-Raphson method, to find the robust location estimator. The Newton-Raphson method is an iterative method for solving nonlinear equations. To solve the equation h(y) = 0, h is set to be linearized at each iteration. In a location M-estimator, it is necessary to solve the equation Where is the value of the location estimator at iteration m. x is defined by the following function with respect to a given positive constant of k as follows: if is bounded, its derivative tends to be infinitely zero (Hampel et al. 2011). If Ѱ k tends to infinity, then is the mean.
On the other hand, if Ѱ k tends to zero, then the acts as the median. In this article, we use Ѱ k = 1.435 in our proposed method. This value is similar to that used by Fox and Weisberg (2019).

Data preparation
One of the biggest challenges in forecasting inventories is capturing the financial data schema of the time series between the past and the future (Wang et al. 2020

R-CNN-BiLSTM Architecture
This study proposes a hybrid R-CNN-BiLSTM model to improve prediction accuracy. The proposed model consists of three parts: data transformation, feature extraction, and price forecasting. First, the data transformation component converts the closing prices of stocks into the robust domain, which is the non-noisy version of the data. In this study, direct stock closing price data is not suitable for machine learning training due to the high standard deviations. Therefore, we need to transform data to make it more relevant to the training process. The closing prices of the shares are divided into a small time series of 4 days, called latency. The latency times overlap 1 day with each other. The Huber location estimator of each latency is calculated using equations (14) and (15). Secondly, feature extraction is performed using a CNN network. CNN has the ability to identify important factors in the data, called "characteristics." The purpose of this step is to preserve historical data in time series data and feed it into BiLSTM. Therefore, input data is converted by performing convolution operations on the time series data time steps using a sequence aliasing layer. In the next step, the two-dimensional convolutional layer is used to extract characteristics from the data. The filtering size of the first convolutional layer is 3 × 3 and the stride parameter is set to {a = 1, b = 1}, 1}, where a is a vertical step size and b is a horizontal step size.
The first convolutional layer is followed by Batch Normalize (BN), which normalizes a mini batch of data across all data points to speed up the CNN formation process. In our model, the exponential linear unit (ELU) is used as the activation function. This function performs the identity operation on positive inputs and exponential nonlinearity on negative inputs. The filtering size of the second and third convolutional layers increases to 5*5 and 7*7, respectively. The next layer is the grouping layer. It subsamples by dividing the data into sub-regions and then calculating the maximum for each region.
The sequence structure of the input is restored using the sequence unfold layer. Finally, the spatial dimensions of the data are reduced with the flattened layer. An overview of the proposed framework for the CNN model is shown in Figure 1.

Training and testing process
One of the most important factors that determine the success of machine learning is the process of training and testing.
In this study, we divided the closing price of each chosen stock into training and test sets according to the ratio of 80:20. Therefore, the first 1201 days of data are used in the training process and the last 262 days are used as a test set.

Hyperparameter parameter
The training data set is transmitted to the proposed training model. In this step, the different hyperparameters of the neural network are specified. These include the number of hidden layers, the number of eras, and the size of batch entries. Finding optimal hyperparameters remains a major challenge in the field of deep learning. In this study, hyperparameters are defined manually by trial and error with the selection of the best parameters of the experiment. The following is a detailed description of hyperparameters and their value parameters. 1. Number of eras: An era is a complete training cycle. In our experiments, we set the number of eras at 100 and carried out our training. After training, we found that all workouts stop at a maximum of 100 to 120 eras. Therefore, 100 is selected as the value for this hyperparameter.
2. The number of hidden layers: This is the number of layers between the input and output layers. For the CNN network, we set the number of hidden convolutional layers to 100, 100 and 50. In the BiLSTM network, we have set these numbers to 128 and 16. 3. Learning rate: This value is defined for the precise convergence of the model in the prediction. In our experiment, we set an apprenticeship rate at 0.0001. Many researchers recommend using a learning value below 0.01 (Hastie et al. 2017). 4. Optimizer: This is the optimization function used to get the best results. In our work, we use the Adam optimizer, as it works well for LSTM-based networks. 5. Loss function: The mean square error (RMS) was used as the loss function. Our implementation was written using MATLAB with GPU compute.

Selection of actions
Once all stock prices are successfully predicted, high-quality stocks are selected to perform the optimization process one by one by ranking them in descending order based on expected (average) return. The expected share price is used to calculate the return on the stock using equation (17).
where is the return of the stock ̂ at time t, while is the expected share price at time t and is the expected share price at time ̂ t-1̂ .
As a result, we select the top number (N) of stocks with a higher potential return in ranking order. Only the selected securities are qualified to build the portfolio in the next step. The MV model is used in this process to build the optimal portfolio with different proportions of asset allocation depending on the qualified stocks. The optimization process is carried out using the MS Excel solver in which the minimum variance is defined as the objective function and the weight of each asset is adjusted using the Excel solver. Therefore, each of the optimal portfolios with the lowest variance is found and used for analysis.

Experimental results
This section first presents the prediction performance of the LSTM, BiLSTM, CNNBiLSTM and R-CNN-BiLSTM models. In the following, this study constructs different portfolio sizes using the classic MV model to compare the prediction result of different machine learning models without transaction fees.

Machine Learning Metrics
In this section, the predictive accuracy of machine learning models is evaluated by three criteria, mean absolute error (MAE), mean square error (MSE), and mean absolute error in percentage (SMAPE), as they are widely used as performance measures (Jierula et al. 2021; Singh et al. 2021). These measures are described as follows: (20) where refers to the predicted price, represents the actual value, and ̂n indicates the total number of shares used in the experiment.

Prediction performance
Tables 2 and 3 present the results of each model that was applied in accordance with the performance measures used. From both tables, it is clear that the R-CNN-BiLSTM model provides most of the best results compared to the other models in the experiment. However, there are still a few exceptions in which some comparison models work better. For example, the prediction error of BTS, LH, PTTGC and SAWAD shares in terms of BiLSTM's MAE, MSE and SMAPE is lower than that of R-CNN-BiLSTM. Another example is that the EAWs, MSEs and SMAPE in the SCC stock that have been predicted using CNN-LSTM are smaller than R-CNN-BiLSTM. In conclusion, most of the R-CNN-BiLSTM results outperform the LSTM, BiLSTM and CNN-BiLSTM models for the inventory forecasting process in terms of EAW, MSE and SMAPE. Specifically, 14 stocks, HMPRO, BJC, CPALL, CPN, DELTA, EA, GLOBAL, IVL, KCE, KTC, MINT, MTC, PTT and PTTEP, which were predicted using R-CNN-BiLSTM, scored best on all three measures, followed by BiLSTM and CNN-BiLSTM. What's more, a unique traditional machine learning model, LSTM, is the worst, with several predictive errors in this experiment. Specifically, only the original RATCH is the most efficient in terms of MAE and SMAPE for the LSTM model. It can be seen that the proposed model, R-CNN-BiLSTM, which uses robust entry characteristics instead of the direct closing price of stocks in the machine learning training process, achieves a majority of better results than machine learning models that use the direct closing price of stocks. Seize.

Portfolio metrics
In this section, the performance of different optimal portfolios is measured and compared using three criteria, the Sharpe ratio, the average return and the risk of the portfolio. These metrics are widely used to evaluate and compare the performance of equity portfolios (Lefebvre et al. 2020;Sikalo et al. 2022;Mba et al. 2022). Another measure of the portfolio is the Sharpe ratio, which can be described as follows: where is the expected (average) return or average return of the portfolio; is the standard deviation or risk of the portfolio; and refers to risk-free assets. In this study, we use a risk-free asset rate of 0.022, based on the Thai 10-year Treasury rate.

Performance of portfolios of different sizes
Numerous studies have shown that owning too many different stocks makes them difficult to control and manage, especially for individual investors. Several studies related to portfolio optimization consider building a portfolio with less than 10 stocks (Almahdi and Yang 2017). Paiva et al. (2019) found that a portfolio with an average of seven stocks outperforms other portfolios with a different number of stocks. Wang et al. (2020) showed that the optimal portfolio with ten stocks performs better than a portfolio with other numbers of stocks. Chen et al. (2021) argued that having seven stocks in a portfolio is the most appropriate number for portfolio formation. As a result, this study decides to build portfolios corresponding to the number of shares N = 5, 6, 7, 8, 9 and 10 and to comprehensively evaluate the performance of the proposed models. The annualized average return, annualized standard deviation and annualized Sharpe ratio are used as indicators. Figure 4 shows annualized returns for different portfolio sizes N = 5, 6, 7, 8, 9 and 10 are presented in three sub-graphs in which the y-axis of each sub-graph shows the amount of the average return, the standard deviation and the Sharpe ratio calculated annually, while the X axis of each sub-graph represents the different models. which are formed by a different number of actions. It can be clearly seen in Figure 4 that when the number of N = 5, R-CNN-BiLSTM + MV is the best performer in terms of average yield, standard deviation and Sharp ratio. Specifically, when N = 5, R-CNN-BiLSTM+MV has the highest average efficiency of 0.47, followed by 0.46 for R-CNN-BiLSTM+1/N, 0.43 for CNNBiLSTM+MV, 0.42 for BiLSTM+MV, 0.38 for LSTM+MV, 0.37 for CNN+BiLSTM+1/N, 0.36 for LSTM+1/N, 0.34 for BiLSTM+1/N, 0.07 for Random+MV and 0.02 for Random+1/N. In addition, when N = 9 and 10, R-CNN-BiLSTM+MV outperforms other models in terms of standard deviation. Specifically, R-CNN-iLSTM+MV has the lowest standard deviation of 0.07 for N = 9 and 10. Regarding the measurement of the Sharpe ratio, when N = 5, 8, R-CNN-BiLSTM+MV provides the best Sharpe ratios of 2.62 and 1.99, respectively. However, R-CNN-BiLSTM+1/N also has the same Sharpe ratio as R-CNN-BiLSTM+MV when N = 8. In summary, a clear advantage of R-CNN-BiLSTM+MV is found with a portfolio size N = 5 in which all three indicators outperform the other models except for the risk of Random+MV and Random+1/N; however, these two models have low expected returns of 0.07 and 0.02, respectively, but it is still reasonable to consider R-CNN-BiLSTM+MV superior. In addition, most models tend to perform better in terms of annualized expected return, annualized Sharpe ratio, and standard deviation or annualized risk when the number of stocks in the portfolio is five.

Discussion
The article aims to expand the existing literature on portfolio optimization with stock selection. The proposed prediction model is developed based on the use of robust statistical theory and the CNN-BiLSTM machine learning model to advance the MV model, which integrates the benefits of machine learning into stock selection. This study has several results.
First, this article compares the predictive performance of LSTM, BiLSTM, CNN BiLSTM and inventory forecasting. The experimental results show that BiLSTM is superior to other models, indicating that it is more suitable for predicting financial time series than the other machine models applied in this experiment, confirming the study by Wang et al. (2020) showing that this traditional LSTM was superior in terms of prediction performance. Second, this study improves the predictive accuracy of CNN-BiLSTM by transforming the closing price of shares into a robust entry feature that can effectively reduce prediction error before the model predicts the future price. After comparing the results of the R-CNN-BiLSTM to LSTM, BiLSTM and CNN-BiLSTM, it was discovered that the robust input is appropriate to be used as an input feature for the machine learning training process to capture time series financial data that can overcome other comparison models when the direct closing price of shares is used as an input characteristic.
Finally, the result of the prediction process is integrated into the stock selection for portfolio optimization; Stocks with higher returns calculated from predicted prices are chosen to build the optimal portfolio. The experimental results show that holding five shares is appropriate and realistic for individual investors, which is different from the results of

Conclusion
This study enriches theoretical research on stock price forecasting and portfolio optimization. This paper uses four prediction models, which can capture financial time series data to ensure high-quality assets before starting portfolio optimization. Specifically, LSTM, BiLSTM, CNN-BiLSTM and R-CNN-BiLSTM are adopted to predict the daily future closing price of the stock and compare R-CNNBiLSTM's forecast results with LSTM, BiLSTM and CNN-BiLSTM to show the predictability of R-NNBiLSTM using robust data instead of the direct closing price of the shares to more accurately predict financial time series data.
While this study provides useful information, there are some limitations. First, we only use stock data in Thailand. Due to economic differences between countries, this method may not be suitable for stock markets in other countries. Second, several external factors have an impact on the financial market and can be added as input indicators to improve the method, such as the COVID-19 crisis, interest rates and politics. Third, this study does not consider temporal complexity as a constraint for comparing results. Finally, since this study manually defines hyperparameters based on trial and error, the application of hyperparameter optimization algorithms can provide better hyperparameters. In future research, temporal complexity should be considered to further demonstrate the applicability of the proposed method.