1 Introduction

Portfolio management is one of the major problems in today’s competitive financial environment [4, 16]. Due to stock market volatility as well as diverse factors related to human, political, and economic behavior, creating and managing portfolios successfully becomes an increasingly challenging practice. Since the precursor works of Markowitz [13, 14] referring to portfolio selection and modern portfolio theory, many researchers have been striving to find an ideal solution to create an efficient asset portfolio that maximizes return and minimizes risks.

To cope with market instability, researchers use different methods such as modeling the correlation between asset pricing, listening to human interactions in specialized forums, clustering assets according to their performance and, analyzing machine learning predictions [2, 11]. Among the portfolio allocation methods, Tactical Asset Allocation (TAA) is an asset management investment method that consists of readjusting the proportions of each category of assets, based on a signal that indicates which asset class will perform best in the upcoming period [1]. Particularly, systematic TAA strategies involve a quantitative, data-driven investment model to identify trends and perform forecasts to aid portfolio management.

One aspect that can directly influence the quality of a portfolio is the number of assets considered for analysis. Human analysts tend to focus on a pre-defined group of assets, limiting choices, and, consequently, the possibility of better results. While most automated portfolio management systems may exhibit prohibitive computational costs associated with optimization procedures when dealing with a large set of assets [18].

In this paper, we propose the Stock Network Portfolio Allocation (SNPA) algorithm for the automatic recommendation of a stock portfolio, aiming to maximize profit and minimize risk. The motivation for this study is to efficiently cover a large number of assets by modeling them as a complex network. In this network, each node represents an asset, and edges are established according to the correlation between their returns. Portfolio selection is then made through a random walk on the network, selecting, in the end, the most visited assets. Investment simulations have shown a return higher than the benchmarks: the Ibovespa, classic Markowitz’s mean-variance portfolio (MV), Mean Absolute Deviation (MAD) portfolio, Conditional Value at Risk (CVaR) portfolio, and Hierarchical Risk Parity (HRP).

The remainder of this paper is organized as follows. In Sect. 2, we present a literature review on some topics related to this work as methods for tactical asset allocation, stock correlation analyzes, and stock networks. In Sect. 3, we detail the SNPA algorithm. In Sect. 4, we conduct some analysis with the hyperparameters of the model. Section 5 we describe the portfolio strategies found in the literature and used in the present work to compare with the proposed algorithm. The performance metrics used in the comparisons are also presented. We present and discuss the result of experiments in Sect. 6. Finally, Sect. 7 concludes the paper.

2 Related Works

Network analysis is commonly used to describe the characteristics or behavior of complex networks. Some research has been conducted to model the stock market using networks [15], where the stock market is presented through a minimal spanning tree (MST) obtained from the matrix of correlation coefficients calculated between all pairs of stocks in a portfolio, considering stock returns. MST-type networks have also been implemented to demonstrate the impact of the new coronavirus pandemic on the financial market [26].

George and Changat [7] emphasize that stock network analysis can play an important role in the stock market study. They created a network of stocks with a set of data from the period of one year, their objective was to identify the structural properties of the market. His work was able to reveal important inputs for decision making, such as, for example, creating a portfolio of more relevant sectors.

Ben-Jacob et al. [20] analyzed stock index correlations, nested stock correlations, and correlations after subtraction of the index return from the stocks’ returns, they showed that the behavior of the stock market could not be understood and predicted only based on individual stocks; rather, system-level analysis methods need to be devised to analyze the stock market as a whole. Kim and Jeong [8] proposed improved methods to identify stock groups using the correlation matrix of stock price changes, they identified the multiple groups of stocks from the empirical correlation matrix of stock price changes in the New York Stock Exchange.

Chen et al. [5] discuss the stock network construction problem under simultaneous consideration of linear and nonlinear relations between stocks. The results showed that the proposed multi-layer network better balances the relation between prediction accuracy and the number of predictable nodes. Hong et al. [10] consider cross-correlations among stock prices in the Korean stock market. They used the daily Korean stock market prices of KOSPI200 for four years from Jan. 2000 to Dec. 2004. They observed the behavior of the stock network specifying a threshold for the correlation between the nodes of that network, so a scale-free network was obtained by the cross-correlation coefficient.

3 The Stock Network Portfolio Allocation Algorithm

Prior to detail the Stock Network Portfolio Allocation (SNPA) algorithm, we shall present some concepts about asset return, risk, portfolio return, and weighted stock network.

Measuring the asset return is the way to determine its performance over time. Let \(R_{t,i}\) be the observed return of the asset i at time \(t \in \{1, 2, \dots , \tau \}\) considering historical data over the time horizon \(\tau \) . Here, t can represent a day, a week, a month, a quarter or a year, according to the study interval.

From time \( t-1 \) to time t, the returns from a set of N stocks accessible to investors is denoted as \( \mathbf{R} _t = [R_ {t, 1}, \dots , R_ { t, i}, \dots , R_ {t, N}] \). The return \( R_{t,i} \) for the i-th asset is calculated by Eq. (1).

$$\begin{aligned} R_ {t, i} = \frac{pr_ {t, i} - pr_ {t-1, i}}{pr_ {t-1, i}} \end{aligned}$$
(1)

where \( pr_{t,i} \) and \( pr_{t-1,i} \) represent the closing prices of the ith asset at times t and \( t-1 \), respectively.

The cumulative wealth of a given asset i during the analyzed period t is denoted as \(cw_{t,i}\), and can be calculated according Eq. (2).

$$\begin{aligned} cw_{t,i} = \prod ^\tau _{t=1} { (1 + R_{t,i}) - 1} \end{aligned}$$
(2)

The standard deviation, \(\sigma _i\), and the variance, \(\sigma _i^2\), of the return for asset i, can be used to measure the risk of a given asset. Let \(\mu _i = \frac{1}{\tau }\sum _{i=1}^{\tau }R_{t,i}\) be the average of returns for asset i over time horizon \(\tau \), the variance of the returns for the asset i is given by Eq. (3)

$$\begin{aligned} \sigma _i^2 = \frac{1}{\tau }\sum _{i=1}^{\tau }(R_{t,i} - \mu _i)^2 \end{aligned}$$
(3)

We denote \( \boldsymbol{\omega }_t = [\omega _{t, 1}, \dots , \omega _{t, i}, ..., \omega _{t, N}] \) as the weights from set of N stocks of the portfolio at time t, such that \( \sum \boldsymbol{\omega }_t = 1 \); \( \omega _{t, i} \) represents the allocation percentage of the i-th asset in the portfolio. Having obtained the set of returns and the set of weights of the assets in the portfolio, we can calculate the return of the portfolio in time t, noted as \( r_t \) through Eq. (4).

$$\begin{aligned} r_t=\sum \mathbf{R} _t \boldsymbol{\omega }_t \end{aligned}$$
(4)

A weighted stock network is denoted as \(G=(V, E, Z, W)\), where V is the set with n nodes, E is the set of m edges, Z is the set of z nodes weights and W is the set of w edges weights. The network G is obtained through a correlation matrix \(\mathbf{M} _t\), as shown in Eq. (5), and obtained by Eq. (6).

$$\begin{aligned} \mathbf {M}_t=\left[ \begin{array}{lllll} c_{11} &{} c_{12} &{} c_{13} &{} \cdots &{} c_{1N} \\ c_{21} &{} c_{22} &{} c_{23} &{} \cdots &{} c_{2N} \\ c_{31} &{} c_{32} &{} c_{33} &{} \cdots &{} c_{3N} \\ \vdots &{} \vdots &{} \vdots &{} \ddots &{} \vdots \\ c_{N1} &{} c_{N2} &{} c_{N3} &{} \cdots &{} c_{NN} \end{array} \right] \end{aligned}$$
(5)
$$\begin{aligned} c_{ij} = \frac{{} \sum _{t=1}^{\tau } (R_{t,i} - \mu _i)(R_{t,j} - \mu _j)}{\sqrt{ \sum _{t=1}^{\tau } (R_{t,i} - \mu _i)^2 \sum _{t=1}^{\tau } (R_{t,j} - \mu _j)^2}} \end{aligned}$$
(6)

To suggest a portfolio for the following month, the algorithm represents the set of assets as an undirected complex network. The most visited assets compose the portfolio after a random walk takes place. Therefore, the proposed approach can be addressed into two parts, building the network from a set of assets and traversing the network to find out the portfolio.

Given a set of assets \(A = \{1,\dots , N\}\), the network is built as follows:

  1. 1.

    each asset \(i \in A\) is abstracted to a node \(v_i\).

  2. 2.

    given the correlation between assets i and j, \(c_{i,j}\) (Eq. (6)), each pair of nodes \(v_i\) and \(v_j\) is connected if \(c_{ij} < \lambda _n\) or \(c_{ij} > \lambda _p\).

In item 2, \(\lambda _n\) and \(\lambda _p\) are thresholds on the value of the correlation of the returns. The rationale of those parameters is to avoid values of correlations close to zero, keeping only significant values of correlation between returns. Notice that their values directly interfere with the structure of the network, consequently, they need to be chosen considering the algorithm performance and restraining to values that generate connected networks.

SNPA uses the asset cumulative wealth, Eq. (5), of the analyzed period as a node weight. So node \(v_i\) has as an associated weight, \(z_i\), its cumulative wealth \(cw_{t,i}\). In this work, the historical series of the last five years prior to the date on which the portfolio is to be generated is used. Let \(\varGamma _i\) be the set of neighbors of vertex \(v_i\). The way the network is built, for each node \(v_{i}\), \(\varGamma _i\) may have nodes whose correlation to \(v_i\) is positive or negative. Supposing \(v_j \in \varGamma _i\), the probability of the particle to visit \(v_j\), noted by \(P_{ij}\), can be calculated by the softmax function as in Eq. (7). The reason to consider the softmax function is that \(cw_{t,j}\) may be negative.

$$\begin{aligned} P_{ij} = \frac{e^{cw_{t,i}}}{{\sum _{v_j \in \varGamma _i} e^{cw_{t,j}}}} \end{aligned}$$
(7)

Figure 1(a) shows the paths that can be taken by the particle starting from node \(v_1\) and the cumulative wealth \(cw_{i,j}\) for each asset represented in node \(v_j\). Figure 1(b) shows the transition probabilities from node \(v_1\) to each of its neighbors \(v_2\), \(v_3\) and \(v_4\) as given by Eq. (7).

Fig. 1.
figure 1

Example of selecting a neighbor using cumulative wealth probability

Algorithm 1 presents the details of the proposed method. The algorithm has as inputs: the set of stocks A, the number of iterations k, the thresholds on the correlation values \(\lambda _p\), and \(\lambda _n\). The number of stocks in the portfolio, q, can be either given as an input and in this way, the algorithm will search for a portfolio with q stocks. Or, if it is not given, the algorithm outputs the portfolio with the highest return considering any possible value of q. The most costly part of the algorithm is to calculate the transition probabilities, which requires to access every edge of the graph.

figure a

Once all the probabilities have been defined, a random walk takes place. The walk proceeds in the network by following the nodes according to the transition probabilities, while the algorithm keeps track of the number of visits each node had. In the end, the stocks represented by the most visited nodes are chosen to compose the portfolio, and the weight of each asset corresponds to the proportion of the total number of visits. The selected stocks tend to have a high return and be highly correlated to each other (positively and negatively). At the same time, the randomness of the algorithm allows us to consider different but also profitable portfolios.

4 Hyperparameter Analysis

To define the hyperparameters values of the algorithm, experiments were carried out with available stocks from IBrX100 index (approximately 100 stocks). Ten experiments were performed to mitigate the effect of randomization. The experiments consider build a portfolio for Jan. 2020 using stock data of the period from Jan. 2019 to Dec. 2019. The datasets used in this work were built with information from the Yahoo Finance [https://finance.yahoo.com/lookup].

First, consider analysing how each parameter relates to each other and to portfolio return. For this analysis, experiments to find a portfolio have been conducted considering several parameters settings. The number of iterations k varied in the range \(\{50, 100, \dots , 1000\}\), threshold values \(\lambda _p\) and \(\lambda _n\) were evaluated in \(\{0.1, 0.2, \dots , 1\}\). The number of assets in the portfolio, q, was given by the algorithm along with the best portfolio return. To each parameter setting, risk has been calculated as the standard deviation of returns among ten runs. Node average degree in the stock network have also been considered for analysis.

Figure 2 shows some pairwise correlations, considering hyperparameters, return, risk and node average degree. It is possible to observe a strong positive correlation between risk and return (0.63), i.e., the higher the return, the higher the risk. Also, a strong negative correlation between \(\lambda _p\) and node average degree in the network, because as the value of \(\lambda _p\) as increases, less nodes are connected. It is also interesting to note the positive correlation between \(\lambda _n\) and risk and return, the greater the value of \(\lambda _n\) the greater the value of the return, although the risk also increases, the increase in risk is less than the increase in return.

Fig. 2.
figure 2

Hyperparameters correlations

Table 1 displays a summary of the main statistical measures obtained through the experiments. The most relevant are the average return and standard deviation, 0.164 ± 0.106. Another relevant information is about the hyperparameter q, although the range was between 2 and 29, most portfolios were composed of 2 assets, as can be seen in the measurement of the median.

Table 1. Statistical summary

With the data from the experiments it is possible to identify the best values for hyperparameters. Figure 3 presents the mean return against the standard deviation of the returns for each value of k and varying \(\lambda _p\) and \(\lambda _n\) in the set \(\{0.1, 0.2, \dots , 1\}\). Each result is the average of 10 realizations.

For k = 950 we have a value with less risk and for k = 350 portfolios with higher mean return. It is a trade-off matter between risk and return. Whereas we aim to maximize the return and the difference in risk is small between the two values, the value of 350 was chosen for the hyperparameter k.

Having defined the value of k, we can obtain the values of \(\lambda _p\) and \(\lambda _n\). Figure 4 shows the portfolio average returns for \(k = 350\), considering different combination of \(\lambda _p\) and \(\lambda _n\). In the figure, only those return values that are within the range \(0.164\,\pm \,0.106\) are shown, in order to stay close to average returns and avoid outliers.

Fig. 3.
figure 3

Estimated mean return and standard deviation for different values of k.

Fig. 4.
figure 4

Estimated portfolio return for when varying \(\lambda _p\) and \(\lambda _n\) and \(k = 350\).

As can be seen in Fig. 4, the best result obtained with the experiment is the mean return for \( \lambda _p\) = 0.9 and \( \lambda _n\) = −0.3. These are the values of the hyperparameters that will be used in Sect. 6 about experimental result. Hyperparameters \(\lambda _p \) and \(\lambda _n\) are very relevant in the decision process of the algorithm, by defining these hyperparameters, the number of paths in the network and the interconnection between nodes are defined. Setting hyperparameters at random can result in an ineffective portfolio at the end process.

5 Methodology

In this section, we describe the main characteristics of the benchmarks used in the present work, which will be compared with the proposed algorithm: Markowitz’s classic mean-variance (MV) [14] portfolio; MAD portfolio (Mean Absolute Deviation) [9]; CVaR portfolio (Conditional Value-at-Risk) [19] and Hierarchical Risk Parity (HRP) portfolio [17]. The performance metrics methodologies that are used to compare these portfolio strategies with the proposed algorithm are also presented.

5.1 Portfolio Allocation Methods

Markowitz’s Classic Mean-Variance (MV): Markowitz has developed a mathematical model showing a reduction in the volatility of a portfolio through the combination of investments [13, 14]. He assumed that portfolios could be entirely characterized by their mean return and variance (or risk).

Mean Absolute Deviation (MAD): In 1991, Konno & Yamazaki [9] suggested a linear programming model in which portfolio risk is measured with the Mean Absolute Deviation (MAD) instead of Markowitz’s variance-based portfolio. The purpose of the proposal was to remove most of the difficulties associated with the classical Markowitz’s model.

Conditional Value-at-Risk (CVaR): In 2000, Rockafellar & Uryasev [19] introduced a new approach to optimizing a portfolio to reduce the risk of high losses. Value-at-Risk (VaR) played a relevant role in their approach, but the emphasis was on conditional value-at-risk (CVaR) [19, 25].

Hierarchical Risk Parity (HRP): portfolio optimization method suggested by Lopez de Prado [17] in 2016. HRP applies modern concepts on graph theory and machine learning techniques to build a diversified portfolio based on the information contained in the asset return matrix.

5.2 Portfolio Performance Metrics

To measure the performance of stock portfolios, some methods can be used, such as: the Sharpe ratio, the maximum drawdown (difference between the highest and lowest value in a given period) and the cumulative wealth. These indicators are widely used in works involving asset portfolio performance analysis and can be found in [3, 6, 22, 24].

Sharpe Ratio (SR): was created by the Nobel William Forsyth Sharpe [21], it is the most widely adopted index for measuring the risk and return ratio of a portfolio. The SR provides a measure of risk-adjusted return for a portfolio strategy [23]. The higher the SR index the better.

The SR can be obtained using Eq. (8), where \( \overline{r}_\tau \) is the average of \( r_t \) returns from portfolios for the time horizon \( \tau \) (Eq. (9)), and \( \sigma _{\tau } \) is the standard deviation of portfolio returns (Eq. (10)).

$$\begin{aligned} SR=\frac{\overline{r}_\tau }{\sigma _{\tau } } \end{aligned}$$
(8)
$$\begin{aligned} \overline{r}_\tau = \frac{1}{\tau } \sum _{t = 1} ^ {\tau } r_{t} \end{aligned}$$
(9)
$$\begin{aligned} \sigma _{\tau } = \sqrt{\sigma _{\tau } ^ 2}, \sigma _ {\tau } ^ 2 = \frac{1}{\tau } \sum _{t = 1} ^ {\tau } (r_t - \overline{r}_\tau ) ^2 \end{aligned}$$
(10)

Maximum drawdown (MDD) the maximum drawdown is a risk measure widely used by fund managers, as a very high drawdown usually triggers fund redemptions by investors [12]. To obtain the value of the MDD, the Eq. (11) can be used, where \( min (r_{t}) \) is the lowest value observed in the portfolio returns and \( max (r_{t}) \) the highest value found. The lower the MDD index the better.

$$\begin{aligned} MDD = \frac{{ max(r_{t}) - min(r_{t}) }}{ max(r_{t}) } \end{aligned}$$
(11)

Cumulative Wealth (CW): is the cumulative wealth of stock portfolio over the period \( \tau \). Measures the aggregate amount that the investment has gained or lost over time. CW can be calculated by Eq. (12). The higher the CW index the better.

$$\begin{aligned} CW = \prod ^\tau _{t=1} { (1 + r_t) - 1} \end{aligned}$$
(12)

6 Experimental Result

The experiments were performed using the hyperparameters values presented in Table 2, and found as described in Sect. 4.

Table 2. Hyperparameters summary for experiments

The hyperparameter k is the number of iterations that will be performed on the network. The set of stocks A is formed by the stocks in the IBrX100 index. This index is composed by the 100 most traded stocks on the Brazilian stock market. For the purpose of this work, however, the number of stocks used in the experiment was 96 stocks, due to data availability.

The experiments consist in finding a portfolio for the next month, \(t\,+\,1\), giving the stock information (time series) from the previous five years. Experiments were analyzed with the base period January 2018 to December 2019. When analyzing a period with these reference months, for example, October 2019, the time series analyzed was from September 2014 to September 2019, and the month that wanted the best result is October 2019.

The results is the month return averaged for 10 times. To evaluate the proposed approach, the SNPA algorithm is compared with the following benchmarks: the Ibovespa index (IBOV), classic Markowitz’s mean-variance portfolio (MV), Mean Absolute Deviation (MAD) portfolio, Conditional Value at Risk (CVaR) portfolio and Hierarchical Risk Parity (HRP). The results are separated into two sections; Sect. 6.1 focuses on returns, while Sect. 6.2 presents performance analysis.

6.1 Return-Based Results

Table 3 shows the summary results for each portfolio analyzed. As can be seen, the SNPA algorithm obtained the highest mean return when compared to the benchmarks, it also presents a balanced risk value (sd - standard deviation). The value \(\bar{q}\) is the average number of assets each portfolio has suggested.

Table 3. Summary results

Table 4 shows the monthly returns obtained by the algorithm under comparison. As can be seen, SNPA presented higher results in 11 out of the 24 analysed months. Conversely, SNPA have performed worse than all the other in 2 out of the 24 analysed months. It is important to note that in August 2018 the algorithm obtained a positive return, while all the others presented negative values. At last, the SNPA algorithm obtained negative results in 5 of the 24 months evaluated, that is, in 21% of the cases. The benchmarks were, on average, negative 9 months, that is, 37.5%.

Table 4. Monthly returns for the algorithms under comparison. Best results are bold faced and worse results are highlighted in red

Figure 5 shows the cumulative portfolio’s return to the analyzed period. In the cumulative wealth, the algorithm SNPA demonstrated its effectiveness, presenting a total return of 236.3%, being 203.5% above MV portfolio; 181.7% above CVaR portfolio; 175.6% above MAD portfolio, 184.6% above IBOV index and 165.1% above HRP.

Fig. 5.
figure 5

Portfolio’s cumulative return

Table 5. Summary of portfolio performance

6.2 Performance-Based Results

Here are presented the results based on the performance of each portfolio strategy and the suggested algorithm. The results are compared with the benchmarks considering the performance metrics: Shape Ratio (SR), Maximum Drawdown (MDD) and cumulated wealth (CW).

Table 5 shows the results obtained by the algorithm proposed in the present work comparing with the benchmarks. The numbers highlighted were the best observed values.

It is possible to observe in Table 5 that the performance of the algorithm proposed is predominant, obtaining the best values considering all the performance metrics evaluated: SR, MDD and CW with the values 0.67, −1.37 and 236.3% respectively.

The second best performance was obtained by the HRP strategy, considering the performance metrics SR and CW, with the values 0.48 and 71.2% respectively. In the case of MDD, the second best performance was obtained by the MV strategy, with a value of −1.39.

7 Conclusion

We propose the Stock Network Portfolio Allocation (SNPA) algorithm to recommend a profitable stock portfolio. SNPA can efficiently handle a large set of assets by representing them as a complex network, where nodes represent assets and edges how their returns correlate. A random walk process carried in this network defines a portfolio. Simulated investment results show the proposed algorithm presented superior performance when compared to the benchmarks: Ibovespa index (IBOV), classic Markowitz’s mean-variance portfolio (MV), Mean Absolute Deviation (MAD) portfolio, Conditional Value at Risk (CVaR) and Hierarchical Risk Parity (HRP). The results were compared with the benchmarks considering the performance metrics: Shape Ratio (SR), Maximum Drawdown (MDD), and Cumulated Wealth (CW). The performance of the algorithm proposed was predominant, obtaining the best values considering all the performance metrics. SNPA demonstrated its effectiveness, presenting a CW of 236.3%, being 203.5% above MV portfolio; 181.7% above CVaR portfolio; 175.6% above MAD portfolio, 184.6% above IBOV index and 165.1% above HRP. The proposed algorithm also surpassed the benchmarks considering the performance metrics SR and MDD, with values 0.67 and −1.37 respectively, the best results among the benchmarks observed by the HRP strategy, with 0.48 and −1.73 respectively. The downside of the proposed algorithm is its computational complexity due to the calculation of the return matrix and the length of the random walk. Future work includes analyzing larger periods of time considering all stocks from a particular market, for more markets. And, also consider machine learning predictions of the return in place of historical data.