Article Type : Research Article
Authors : Al Hallaq ,Ajlouni MM and Al Saadi MF
Keywords : Hybrid genetic algorithm; Analytic hierarchy process; Portfolio selection; Optimum portfolio; Amman stock exchange
The aim of this study is to investigate the ability of a
hybrid genetic algorithm (HGA) and analytic hierarchy process (AHP) in
selecting the optimum portfolio. This of course, helps investors to decide the
most appropriate investment alternatives. For that purpose, the study creates
portfolios using daily returns of the companies listed in Amman Stock Exchange,
for the period from January 1, 2015 to December 31, 2015. The results show that
HGA can identify portfolios that are in the efficient frontier. HGA has
advantages over disadvantages for the portfolio selection cases in which the
scale of the problem or the nonlinear constraints cannot be solved by linear or
quadratic models. In addition, the results reveal that AHP can select the
optimum portfolio among the portfolios obtained by HGA.
Selecting investment portfolio is one of the most
important research areas in modern finance science; it is seeking to better
allocation of funds between baskets of securities. Portfolio selection was
introduced first by "Harry Markowitz" in 1952, in his paper
"portfolio selection." He explained the concept of diversification,
and suggested that investors focus on portfolio selection depending on their overall
risk-reward characteristics [1]. Markowitz explained the best way to allocate
the money over risky asset in a fixed position. The model of Markowitz consists
of only two factors, which are the expected return and variance, and presume
investors are risk averse. The idea of the model is that an investor cannot
achieve a high return without increasing portfolio risk [2].
Since Markowitz until today, many attempts been
conducted in the portfolio management to find new mathematical approaches to
select the optimum portfolio. The computational capacity of the 21st century
and the wide availability of computers have allowed the development of a new
generation of intelligent computer techniques such as hybrid genetic algorithm
(HGA). HGA is a problem-solving technique motivated by biological evolution,
based on an artificially simulated natural selection process or the survival of
the fittest, known as Darwinian evolution. HGA can only identify the portfolios
being in the efficient frontier and has been used in many recent works to find
the optimum portfolios, due to multiple objective functions in portfolio
selection. Therefore, the Analytic Hierarchy Process (AHP) approach is tied to
the HGA to select the optimal portfolio based on all criteria between the
portfolios obtained by HGA. The main objective of this study is to investigate
the ability of HGA and AHP in selecting the optimum portfolio using data of the
Jordanian companies listed in Amman Stock Exchange (ASE) [3-5].
Problem
of the study
The huge amount of data and the complex function in
discrete sampling space, for example, nonlinear function and constraints
methods like a gradient method, simplex method, and dual simplex, the portfolio
selection and optimization problem proves to be not efficiently solved. For the
above-mentioned reasons, the current study switched to HGA and AHP for finding
the optimum portfolio. Thus, the study problem can be expressed by the
following research questions:
· Can HGA and AHP be used
in selection of optimum portfolio?
· Can Efficient Frontier
(EF) be identified using HGA?
· Can AHP select the
optimum portfolio among the portfolios obtained by HGA?
Objectives
of the study
The main objective of this study is to investigate the
ability of hybrid genetic algorithm and analytic hierarchy process in selecting
the optimum portfolio. In particular, the study aims to identify the efficient
frontier using HGA and selecting the optimum portfolio according to AHP from
HGA's EF.
Importance
of the study
A review of literature on the optimum portfolio
selection using a hybrid genetic algorithm and analytic hierarchy process
indicates that most of the studies on this topic have been conducted in
developed countries. This is probably attributed to the fact that the conduct
of such studies requires a relevant level of disclosure data and analysing
techniques [6-10]. Thus, this study will contribute to the literature by
producing answers regarding the selection of optimum portfolio using a hybrid
genetic algorithm and analytic hierarchy process. HGA and AHP are new
techniques, which are added to portfolio selection to reach EF and optimum
portfolio. Therefore, the importance of this methodology is derived from the
importance of optimum portfolio, which all investors wish to reach [11].
Structure
of the study
The paper is organized as follows: In addition to
introduction, section two presents the theoretical background of the study.
Section three discusses literature review of the study, and what distinguish
the current study. Section four describes the procedures used to gather data
for the study, the methodology used. Section five presents the results of
analysis HGA and AHP. Final section presents recommendations and policy
implications of the study in question [12-16].
Theories developed to explain the portfolio selection
and genetic algorithm will be discussed in the upcoming lines. The beginning of
the modern investment theory traced back to 1952, when Harry Markowitz (1952)
published an article titled "Portfolio Selection" [17]. He showed how
to create a frontier of investment portfolios, so that everyone had the highest
expected rate of return given its level of risk or the minimum level of risk
given its rate of return. The calculation technique was very complex,
especially given the technology of the time. In the past, the optimization of
Markowitz's portfolio was used, mostly in the asset allocation decision. The
investor decides on the amounts to invest in certain basic classes such as
stock, bonds and real estate assets. The computing power needed to optimize
more than a few asset classes is only a small fraction of what is needed to
optimize more than thousands of stocks. There is a need to utilize the
available quantitative data to solve the optimization problem. Prior to the
spread of portfolio theory in the real world, three scholars simultaneously and
independently asked the following questions: Assume that everyone successfully
makes his investments using portfolio theory and invested in portfolios on the
frontier, how would this affect the price of securities? In response to this
question, Sharpe (1964), Lintner (1965) and Mossion (1966) [1-3] developed the
Capital Asset Pricing Model (CAPM), which is widely used in the real world to
measure portfolio performance; securities value, make capital budgeting
decisions, and even regulate utilities. However, the model was challenged by
Richard Roll [4,5], who argued that the model should be discard because it was
impossible to verify empirically its single economic forecast. This
controversial issue remains the subject of a lively debate today. At the same
time, Steve Ross [6] developed an alternative model to CAPM. This model was
called the Arbitrage Pricing Theory (APT) where expected return should be linked
to risk so that no single investor could create unlimited return through
arbitrage. The question of how to price option contracts has puzzled
researchers in finance until a paper of Fisher Black and Myron Scholes was
published in 1973 [7]. They argued that you could make a riskless hedged
position with an option by taking a position in both the option and the stock
it is written on. Although researchers in the finance science were trying to
determine the nature of the price structure in the securities markets, the
issue of how efficient the market is in pricing to its structure was called
into question. Fama (1970) stated that a market is efficient if security prices
immediately and fully reflect all available relevant information [8]. Fama, [9]
divided the overall EMH and the empirical test of the hypothesis into three
sub-hypotheses depending on the information set including: Weak-Form of
Efficiency, Semi Strong-Form of Efficiency, and Strong-Form of Efficiency [10].
The Markowitz theory is now known as the Modern
Portfolio Theory (MPT). It is an investment theory that aims to maximize the
expected return of the portfolio for a given level of portfolio risk, or in an
equivalent way, to minimize the risk for a given expected return, by carefully
choosing the proportions of different asset levels [18]. Although MPT is widely
used in practice in the financial sector in recent years, the basic assumptions
of the same have been largely questioned. MPT, as an improvement over
traditional investment models, is an important mathematical modelling for the
advancement of finance [19].
Portfolio theory assumes that investors are
essentially risk averse, meaning that given the choice between two assets with
equal rates of return, they would select the asset with the lowest risk [11].
This combination of risk preference and risk aversion can be explained by an
attitude to risk that depends on the amount of money involved. Although
diversity of attitudes is recognized, the basic assumption is that most
investors who use large sums of money to develop an investment portfolio are
averse to risk. As a result, there is a positive relationship between expected
return and expected risk in the optimal selection process [12]. The optimal
portfolio is a combination of investment, each of which has desirable
individual risk-return characteristics that are also adjusted according to
their correlations [13]. The optimal portfolio is the efficient frontier
portfolio that has the greatest value for a given investor. It is at the point
of tangency between the curve of the efficient frontier and the curve with the
highest potential utility [20].
To expand Markowitz's model portfolio and the
assumptions of EMH the risk-free rate of return should be considered. Correlation
and covariance of any asset with a risk-free asset is zero. So any combination
of an asset or portfolio in the risk-free asset generates a linear return and
risk function. As a result, the combination of the risk-free asset with a risky
asset in the Markowitz efficient frontier gives rise to linear portfolio
opportunities while the dominant line is that which tangents to the efficient
frontier. This dominant line is known as the Capital Market Line (CML) [10].
Because all investors want to invest in the portfolio that is risky at the
point of tangency, this portfolio - known market portfolio must contain all
risky assets pro rata to their relative market values. In addition, the
investment decision and financing decision can be separated because, although
everyone wants to invest in the market portfolio, investors make different
financing decisions on loans or borrow depending on their preferences of
individual risk [14]. Given the CML and the predominance of the market
portfolio, the relevant risk measure for an active individual risk is its
covariance with the market portfolio. That is, it is systematic risk, when the
covariance for the market portfolio is standardized, a known beta measure of
the systematic risk and market line, which relates to the expected or required
rate of return of an asset with its beta version [15]. Individual securities
and portfolios are represented in the Security Market Line (SML) to determine
the expected return because of a systematic risk (beta). Alternatively, assuming
that markets are not always fully effective, one can identify undervalued and
overvalued securities by comparing the estimated rate of return of an
investment to its expected rate of return. The systematic variable risk (beta)
for an active individual risk is calculated using a regression model that
generates an equation referred to as the assets characteristic line [16].
The main objective of this section is to investigate
the ability of a hybrid genetic algorithm (HGA) and analytic hierarchy process
(AHP) in selecting the optimum portfolio, in ASE. This will be done, focusing
on the methodology, data collection and sources, the study sample selection
rules and empirical background of the study variables. Along with their
operational definitions, measurements and hypotheses of the study. Finally, it
presents the empirical models to be used to test the study hypotheses [21].
Population
and sample
The study population consists of all companies listed
in Amman Stock Exchange (ASE) during the period of January 1, 2015 to December
31, 2015. At the end of 2015, the total number of companies listed in was 228
companies. The current study sample been selected according to the criteria of
continuous data availability. Therefore, the sample is limited to companies
that met the following criteria:
1. Companies
should have complete data availability during the study period.
2. Companies
should have been established before 15 years.
3. Companies
should not have been engaged in acquisitions or merger during the study period.
By applying these criteria, the final sample of this
study is restricted to 60 companies.
Sources
of data
The study depends on secondary data that have been
published in annual reports issued by Jordanian companies listed on Amman Stock
Exchange (ASE), reports and trading data issued by ASE, statistical databases
issued by the Central Bank of Jordan (CBJ), books and references, studies,
previous researches that are related to the subject matter, and network
(internet) publications.
Variables
of the study
To achieve the purposes of the study, the following
variables will be used: return, risk, beta, liquidity ratio, Sharpe ratio,
Treynor's ratio and Jensen's alpha.
The current study aims to test the ability of HGA and
AHP in selecting the optimum portfolio of shares listed in Amman Stock
Exchange. The model used in this study is based on the Solimanpur et al. [17]
models.
Notations
The following notations are used to formulate
portfolio selection problem:
I:
Index for stocks.
N:
total number of stocks.
ri:
Return of stock i.
Rp:
Return of portfolio.
?p:
Risk of portfolio
xj:
Percentage of stock j in portfolio.
Objective
functions
The attempted mathematical model includes two
objective functions. Namely, return and risk of portfolio. These objective
functions are formulated in the following:
1. Return
of Portfolio: The portfolio return is defined as the weighted average returns
of the portfolio shares and is expressed as follows [10]:
2. Risk
of portfolio: Risk of a stock is defined as the tolerance of stock’s return
from the mean. The risk of a portfolio is expressed as follows [10]:
Where:
: The covariance between returns of stock i and j expressed as
: is the correlation coefficient between i and
j.
3. Constraints:
Constraints of the attempted model
are as follows:
It should be noted that the non-negativity of the
decision variables is used to prevent short selling.
4. Proposed
Hybrid Genetic Algorithm (HGA):
Portfolio
display
In the proposed HGA, any portfolio is represented by bits genes in which N is the number of companies and num_bits is the number of binary bits used for representing the share of each company in the portfolio. The share of company i in the portfolio can be calculated by Solimanpur [17]:
Where:
: The decimal value of the binary code
dedicated for company i.
This formula ensures that, after the execution of
genetic operations, the constraints of mathematical models will be satisfied.
Fitness
function
Fitness of any chromosome in the proposed HGA is calculated in K directions. Fitness of chromosome S in direction k is computed by Solimanpur [17]:
Where:
: is the fitness value of chromosome S in direction k.
: is the normalized value of return of
chromosome S.
: is the normalized value of risk of
chromosome S.
is the weight of return in direction k.
: is the weight of risk in direction k.
The normalized values of return and risk of portfolio
S in the population are defined as follows Solimanpur [17]: ?
(5)
(6)
Selection
In the proposed HGA, selection probability of chromosome S in direction k it is proportional to the quality of this chromosome in the direction k, i.e . In other words, the higher the fitness of a chromosome, the higher should be selection probability [22].
The method proposed an evaluation procedure comprising
the following steps [18]:
Step 1: Identify all the criteria to be taken into
account in the choice of the portfolio (evaluation) and to build a hierarchy of
decision-making.
Step 2: Calculate weights of criteria using AHP
method.
These steps are performed in the following
subsections.
Evaluation
criteria
Selecting the appropriate portfolio of performance
measures to provide the necessary information to investors to assess the
effectiveness with which they can invest their money is a vital issue. The
performance appraisal is primarily related to the determination of how a
particular investment portfolio has performed compared to a benchmark
comparison. To effectively manage portfolio selection, it is necessary to
consider the critical factors that reflect investor behaviour and the state of
the financial market. The different stakeholders within a decision process can
be relatively diverse, with different objectives and conflicts of value systems
[23-29]. In this respect, a key concept is the relationship between risk and
return. Each performance index provides a different perspective on the balance
between the level of return and the exposure to risk. Therefore, in this study,
seven measures, return, risk, beta, liquidity ratio, Sharpe ratio, Treynor’s
Ratio (TR) and Jensen’s alpha (Alpha ratio) have been identified as criteria,
which affect decision of investors in portfolio selection. Other criteria are
discussed in the following subsections.
Beta
Beta measures the price volatility of a share compared
to the rest of the market. This measure of risk is defined as [10]:
Where:
: The covariance between the rates of return
of shares i and the return of the market or index.
: The rate of return of the market portfolio.
Liquidity
ratio
This ratio indicates the annual liquidity of a stock.
Haugen can then calculate liquidity of a portfolio [10]:
Where:
: The liquidity of share i.
: The percentage of share i in the portfolio.
Sharpe
ratio
Sharpe ratio or RVAR measures the return into the
portfolio risk (standard deviation of return) [20]. It can be defined as this
ratio:
(9)
Where:
: The mean of portfolio return in a certain
period.
: The mean of non-risk return rate in time set.
: The standard deviation of portfolio return
in time set.
: The surplus return of portfolio.
This ratio measures the surplus return of portfolio
versus a risk unit. The higher the RVAR of a portfolio, the higher will be its
desirability.
Treynor’s
ratio (TR)
TR measures the proportion of extra return on beta
[20]. Treynor defines this ratio:
Where:
: The systematic risk of portfolio (beta
ratio).
This ratio measures the excess return of portfolio
versus portfolio’s systematic risk unit.
Alpha
ratio
Alpha ratio is linked to Treynor’s ratio and, hence,
it provides a classification that is close to the performance of a portfolio
[21]. This ratio is defined as:
The weakness and strength of a portfolio’s performance
has two sources: The first is the portfolio manager's ability to select appropriate
shares, and the second is their ability to make appropriate decisions over time
and to assess threats and market opportunities. Obviously, the manager who
considers these aspects will have a better performance in managing the
portfolio. The benefit of using this ratio is the possibility to measure and
at the same time [10].
The
analytic hierarchy process methodology
AHP includes six basic steps, as summarized in the
following [18,22]:
Step 1: AHP breaks down a complex multi-criteria decision-making
problem to several small sub-problems each with a single criterion. Thus, the
first step is to break the problem of decision in a hierarchy with a goal at
the top, criteria and sub-criteria at levels and sub-levels and alternative
decisions at the bottom of the hierarchy.
Step 2: The decision matrix, which is based on Saaty’s
nine-point scale, is constructed. The decision-maker uses the fundamental scale
defined by Saaty to assess the priority score.
Step3: It involves pairwise comparison of the elements
of the constructed hierarchy. The aim is to set their relative priorities with
respect to each of the elements at the next higher level.
Step 4: AHP also calculates an inconsistency index to
reflect the consistency of decision maker’s judgments during the evaluation
phase. The inconsistency index in both the decision matrix and in pairwise
comparison matrices can be calculated by the following equation [22]:
The closer the inconsistency index to zero; the greater will be the consistency of decision-maker’s judgments. The consistency of the assessments is ensured if the equality holds for all criteria. The relevant index should be lower than 0.10 to accept the AHP results as consistent. If this is not the case, the decision-maker should go back to Steps 2 and 3 and redo the assessments and comparisons.
Step 5: Before any calculation, the comparison matrix
has to be normalized. To normalize, each column is divided by the sum of
entries of the corresponding column. In that way, a normalized matrix is
obtained in which the sum of the elements of each column is 1.
Step 6: The relative values obtained in the third step
should satisfy:
(13)
Where:
A: Represents the pairwise comparison matrix.
: The highest eigenvalue.
W: Weight of the vector or elements.
If there are elements at the higher levels of the
hierarchy, the obtained weight vector is multiplied by the weights of the
elements at the higher levels, until the top of the hierarchy is reached. The
alternative with the highest weight is finally considered as the best
alternative.
Questions of the Study
The study questions are:
·
Can HGA and AHP be used
in selection of optimum portfolio?
·
Can Efficient Frontier
(EF) be identified using HGA?
·
Can AHP select the
optimum portfolio among the portfolios obtained by HGA?
The study provides the population, sample, sources of
data and variables of the study. Mathematical model and the methodology of the
HGA and AHP are also reviewed and in the last hypotheses of the study. The
following section presents data analysis, process and implementation of the
HGA, AHP and their results.
Data Analysis
This section shows the descriptive statistics of the
study variables, and then examines the ability of the HGA and AHP to select the
optimum portfolio. In particular this section aims to answer the questions and
test the hypotheses of this study.
Hybrid
genetic algorithm process and implementation
This section is going to discuss the implementation of the methodology of the HGA. To implement the previously discussed methodology, we created an application using Visual Studio 2010, and C#.NET as a programming language [30]. Visual studio 2010 is an integrated development environment used to create a variety of computer programs, you can select from many available programming languages such as C++,C# ,Visual Basic...etc. This study used C# language, C# is a general-purpose programming language used to create windows forms applications for any purpose. As Figure 1 show, the first step in this work is to read the stocks data. In this step, the program reads the stocks data and creates an object for each stock, and then loads the daily data return for each stock. After that it uses the stocks, data to create 10,000 portfolios, where by each portfolio consists of six stocks. After that it created the portfolios, it uses the daily data return of the assets to calculate the portfolios risk and return. Then it use the portfolios risk and return to evaluate the fitness of the portfolios and select the top 10 portfolios to be used in the HGA. After that, it uses the top 10 portfolios in the crossover process to generate the second generation of portfolios. Finally, it selects the top 10 portfolios from the second-generation portfolios and draws the efficient frontier.
Figure 1: Flowchart of the
Implementation Process.
The study gathered the stocks data from Amman Stock
Exchange website for the year 2015 and used the data to create excel sheets and
notepad files.
As Figure 2 shows, using the file menu and clicking the “Select Data” option, the program will open a select file dialog which allows us to select the stocks data from a notepad (.txt) file.
Figure 2: Creating a File and Selecting Data Using C#.
Then the program will read the file data and loads it
to the system and create an object for each stock which consists of five
characteristics, which are:
1. Stock
ID: unique integer value varies between 60-120 which is used to identify each
stock.
2. Stock
Code: Company code as in Amman Stock Exchange.
3. Return:
Stock expected return.
4. Risk:
Stock risk.
5. Daily
Data: The stock daily return.
We have assigned a unique ID for each stock which
varies from 60-120. The ID will be used to calculate the weight of each stock
in the portfolio, in a manner that the stock with the highest expected return
has the highest ID and so on. It would be natural to choose the ID’s from 1-60
but we chose to select the ID’s from 60-120 for the following reason:
In the 1-60 ID’s the share of each stock will
dramatically vary between the following values:
·
Highest weight would be
the result of company with ID 60 to be assigned in a portfolio with the stocks
of ID’s 1,2,3,4 and 5. In this situation the Stock with ID 60 will have a
weight of 60/(60+1+2+3+4+5) = 80%.
·
Lowest weight would be
the result of company with ID 1 to be assigned in a portfolio with the stocks
ID’s 60, 59, 58, 57 and 56. In this situation the Stock with the ID 1 will have
a weight of 1/ (60+59 + 58+57+56+ 1)= 0.34%
However, in the situation of ID’s 60-120 we had the
weights vary between 9.2% and 27.9%. As we can see, the second approach gives a
better chance for the assets to affect the portfolio return and risk, and
eliminates the possibility of a single asset to dominate the portfolio. After
we have loaded the stocks data, we can load the stocks daily data by clicking
the “Load daily data” option as shown. By clicking, the “Load daily data”
option the program will show a select file dialog box, which allows us to
select notepad file (.txt) which contains each stock and its daily data. Thus,
the program will load the daily data for each stock into the stocks objects,
which is going to be used in the process of portfolios risk and return
calculation [31-36].
Having the stocks are created and the daily data are loaded, we can create the portfolios, by clicking the “Create Portfolios” option shown in Figure 2. The program will use the stocks data to create 10,000 portfolios where each portfolio consists of six different stocks to be selected randomly from the previously created 60 stocks. When the portfolios creation is done, we can calculate the risk and return for each portfolio by clicking the “Portfolio Risk & Return” option from the Calculate menu, as in Figure 3.
Figure 3: Calculate risk and return.
By clicking the “Portfolio Risk and Return” option the
program will calculate the risk and return for each portfolio as follows.
Portfolio return will be calculated according to the following formula [10]:
Where:
W: The weight of each stock in the portfolio.
R: The rate of return of each stock in the portfolio.
The weight for each stock in the portfolio is
calculated by dividing the asset ID over the summation of the total assets IDs
of the portfolio.
The 6 asset portfolio risk will be calculated
according to the following formula [10]:
And
To calculate the covariance we used the stocks daily
data to calculate the mean and variance for each stock and followed the
following formula to calculate the covariance between each two stocks in any
portfolio:
Where:
and : are the sample means return averages.
: is the sample size.
After we
calculate the risk and return for each portfolio, we apply the following
fitness function to calculate the fitness factor for each portfolio [17]:
(18)
Where:
: fitness value of chromosome S in direction k.
: normalized value of return of chromosome .
: normalized value of risk of chromosome .
: weight of return in direction .
weight of risk in direction .
To produce our results we used the following weights
for risk and return as shown in (Table 1). After we calculate the fitness value
for each portfolio, we select the top 10 portfolios based on the highest
fitness value.
Table 1: Fitness function weights.
Weight of risk |
Weight of return |
50% |
50% |
40% |
60% |
80% |
20% |
20% |
80% |
60% |
40% |
Now after we have determined the top 10 portfolios it
is the time to use HGA. In the HGA, each portfolio is treated as a gene, where
each gene consists of six chromosomes which represent the assets of the
portfolio. A chromosome for a portfolio consists of the following assets {114,
120, 86, 74, 62, 91} would be [1110010 1111000 1010110 1001010 0111110 1011011]
where each seven digits represent the chromosome for each asset which are the
result of the ID conversion from decimal to binary. The purpose of the HGA is
to swap chromosomes between the best genes to generate better chromosomes. To
do so, we separated the assets of the top 10 portfolios and used these assets
to create the second generation of the portfolios using HGA [37]. After we
separated the genes from which the chromosomes are created, we used these genes
as the basis to create the new portfolios, which is known as the Crossover
operation. We have created portfolios six times the number of the successful
genes. The created portfolios are referred to as the second-generation
portfolios [38-41].
After we created the
second-generation portfolios, we selected the top 10 portfolios from the
second-generation portfolios and created the efficient frontier graph, as Figure
3.
So far, we have discussed the workflow of the program in general. In this section, we are going to discuss the work in more technical details and we are going to describe the code we used to create the application. Figure 4 shows the interface of the program we created. As we can see, the interface contains three menus, five grid views and a graph space to draw the efficient frontier and a navigation bar which is used to select the weight of the risk and return that used in the fitness function.
Figure 4: Program
interface.
The left grid views are used to list the stocks used
in creation of the portfolios. Where the right grid views are used to list the
portfolios created in the different stages of the program run. The top left
grid view is used to list all the available stocks which we created from the
data we extracted from Amman Stock Exchange website. The bottom left grid view
is used to list the stocks which are used to create the optimum portfolios to
be used in the HGA to create the second generation portfolios [42]. The right
top grid view is used to display the 10000 portfolios we created using the
stocks listed in the top left grid view, where each portfolio consists of six
different stocks.
The right middle grid view is used to list the optimum portfolios which consist of the stocks listed in the bottom left grid view. The right bottom grid view is used to list the second generation portfolios which are the result of the genetic algorithm and the crossover operation. As Figure 5 shows we have five different options which work as follows:
Figure 5: File
Menu.
Figure 6 shows the code that runs when the “Select Data” option is clicked from the file menu. The first line of code defines a list of strings named lines to store the lines. The second line of code defines and creates an open file dialog box which is shown when the select data option is clicked. The dialog box allows us to select the file which contains the stocks data. The file should be in .txt format. The “IF statement” block is used to make sure a file is selected and then add the file lines into the list of lines which we defined in the first line.
Figure 6: Select
data code.
Figure 7 shows the stocks data file. Each line in the
file represents a stock where the entries represent the stock code, expected
return, ID and risk respectively. The entries are separated by tabs. The “For
loop” block of code is used to create a stock for each line and assign the
code, expected return, ID and risk for each stock and then add the stock to a
list of stocks. The last two lines of code are used to list the data on the
application interface grid view.
Figure 8 shows the code which runs when the “Create portfolios” option is selected from the file menu. As we can see, the function contains a for-loop statement which is used to repeat the code within the block 10,000 times. In the first line of code we define a temporary portfolio, and then we define a temporary stock. After that, we keep repeating the code within the while-loop block until the number of stocks the portfolio contains is six. In the while loop, we create random number generator. The random number generator is defined as a random number that is bigger or equal to its lower limit, in our case 60, and less than its maximum limit, in our case 121, which gives us a random number from 60-120.
Figure 7: Stocks data file.
Then, we use the random number and search for the
stock that has the same number as its ID and assign its value to the temporary
stock that we defined earlier.
After that, we make sure that the portfolio does not
contain this stock to make sure a portfolio does not have the same stock more
than one time.
When the portfolio number of stocks is six the while-loop stops from adding more stocks to the portfolio. Then, we add the portfolio to a list of portfolios and proceed to create another portfolio until the predefined number, in our case 10,000 is created and the loop stops [43-49].
Figure 8: Creating portfolios code.
Figure 9 shows the code which runs when “Load Daily data” option is clicked from the file menu. The first line is used to define a list of string values named lines. The second line is used to create an open file dialog box which is used to select a file that contains the daily data of the stocks.
Figure 9: Load daily data code.
The If-statement block is used to make sure a file is selected and then it reads the content of the file and adds them into the array of lines.
Figure 10: Daily data file.
The first line in the for-loop block splits each line based on the tab spaces between each value and assign them into an array named daily. The second line finds the stock that has the same code as found in the selected data file. Then, in the inner for-loop we add the daily data of the stock into the stock.
Figure 11: Save Portfolios Code.
Figure 10 shows the daily data file. The file contains
a line for each stock, where each line starts by the stock code, and then it
contains the daily return of each stock. The values are separated by tabs.
Figure 11 shows the code which runs when “Save
portfolios” option from the file menu is clicked. Since the process of creation
of 10,000 seems to be a time consuming process, where we need to repeat the
process of creation, random an ID number, search for the stock, check if the
portfolio contains the stock or not, adding the stock and repeating this
operation for 10,000 times, we figured that we can create the portfolios and
save them, then we can load the portfolios later to assure the results
consistency. The function saves the ID’s of the stocks which the portfolio
contains as a string then writes the string to a file named “portfolios.txt”
[50].
Figure 12: Load Portfolios Code.
Figure 12 shows the code which runs when the “Load
portfolios” option from the file menu is clicked. As we can see the code,
creates a file reader and read the contents of the “portfolios.txt” file and
use the saved ID’s to create new portfolios using these ID’s.
Figure 13 shows the options of the calculate menu, the
menu contains two options as shown. The “Portfolio Risk Return” option is used
to calculate the Risk and Return of the portfolios, while the “Apply Genetic
Phase 1” is used to create the second generation portfolios.
Figure 13: Calculate menu options.
Figure 14 shows the code which is used to calculate
the risk and return for each portfolio, as we can see in the figure, the code
calls the functions from the portfolio class to calculate the risk and return.
Figure 14: Calculate risk and
return code.
Figure 15 shows the code used to calculate the
portfolio return. At first we defined a dummy variable named sum then we
calculated the sum of the stocks ID’s which is used to calculate the weight for
each stock. Where the weight for any stock is the stock ID divided by the sum
of stocks ID’s. Then in the second for-loop we calculate the value of each
stock return multiplied by its weight and add it to the variable named “re”.
Finally the function returns the Return value to be assigned as the portfolio
expected return.
Figure 15: Calculate Return Code.
Figure 16 shows the code used to calculate the
portfolio risk. To simplify the risk equation we divided the equation into
three loops, in the first for-loop we calculate the weights of the stocks and
add them into a list for an easier access in the later loops. In the second
loop we calculated the terms which contain the square value of the weight
multiplied by the square value of the stock risk then add them into the
variable named “variance”.
Figure 16: Calculate risk code.
The last for-loop is used to calculate the terms where
the covariance is required, as we can see the loop calls a function named
“Covariance (i,j)” which is used to calculate the covariance between the stocks
referred to as “i” and “j”.
Figure 17: Covariance Code.
Figure 17 shows the code used to calculate the
covariance between two stocks. The line inside the for-loop block is where the
work is done, to calculate the risk we subtract the expected return of the
stock from its daily value then multiply the result by the difference between
the second stock daily value and its expected return then add the result to the
variable named “cov”. This process is repeated for the number of available
daily data for the stocks. Finally the function divides the “cov” value by the
number of available daily data and returns the result as the covariance between
the stocks “i” and “j”.
Figure 18: Apply Genetic Phase1
Code.
Figure 18 shows the code to apply
HGA and the crossover operation. The first line reads the return weight
selected from the track bar shown in the application interface, while the
second line calculates the weight of the risk weight from the same track bar.
Then the for-loop is used to calculate the fitness for each portfolio given the
risk and return weights as selected in the track bar.
Figure 19: Calculate Fitness Code.
Figure 19 shows the fitness
calculation code which is simply the result of multiplying the weight of risk
with the expected risk of the portfolio added to the result of multiplying the
risk weight by the expected portfolio risk. Then, we call the function
“Select_Top_10” passing the list of portfolios named “GP1_Portfolios” which is
going to contain the top 10 portfolios.
Figure 20: Select Top 10 Portfolios Code.
Figure 20 shows the code to select
the top 10 portfolios. In the first for-loop we add the first 10 portfolios to
the list, then in the second loop we compare each portfolio fitness with the
fitness of the selected 10 portfolios and if the fitness of any portfolio is
greater than the fitness of one of the selected portfolios, we replace the
selected portfolio with the one with the greater fitness and break from the
comparison loop to prevent replacing more than one portfolio with the same
portfolio. The two lines of code after the top 10 selection are used to format
and view the data in the interface grid view. Then we use a function called
“Extract_GP1_Stocks” followed by a function called “Crossover”.
Figure 21: Extract GP1 Stocks Code.
Figure 21 shows the code used to extract
the GP1 stocks. GP1 is the name of the list that contains the top 10 selected
portfolios based on the highest fitness. This function extracts the stocks that
are used to create these top 10 portfolios, which are the genes to be used in
the crossover operation later on. In our application, we list those genes in
the bottom left grid view. The function checks if the list names “GP1_Stocks”
contains the stocks of each portfolio and if it does not contain that stock, it
adds the stock into the list, then it updates the grid view shown in the
application interface.
Figure 22: Crossover Code.
Figure 22 shows the code used to
create the crossover operation which is the core function of HGA. The crossover
operation is the operation in which the genes of the optimum selected
chromosomes are swapped between chromosomes in promises of generating a more
optimum chromosome. Since the crossover is basically a genes swapping
operation, instead of swapping a single gene at a time, we extracted the genes
and created a pool of genes using the “Extract_GP1_Stocks” function. In this
function, we use those genes to create a new generation of chromosomes
“portfolios”. At first, we create a temporary portfolio and temporary stock.
Then, we random a number between 0 and the number of extracted stocks. Then, we
add that stock to the portfolio after checking if the portfolio already
contains that stock or not, then we make sure that the portfolio we have
created does not match another portfolio that have been created earlier. Then,
we repeat this operation to generate six times the number of extracted stocks,
and then apply genetic phase2 and select the top 10 portfolio from the
portfolios.
Figure 23: Generate Menu.
Figure 23 shows the generate menu
which contains only one option used to generate the efficient frontier graph,
which is shown in the bottom left interface of the application. A point worth
mentioning here that, we have to normalize the risk and return values for the
portfolios so we can generate an efficient frontier graph.
Figure 24 shows the code used to
normalize the risk and return values for each portfolio. The normalized value
of the return is the result of dividing the portfolio return by the maximum
return in the portfolios. While the normalized risk is the result of dividing
the portfolios minimum risk by the portfolio expected risk. To do this, we
started by finding the maximum risk, minimum risk, maximum return and minimum
return. Then we shifted the intervals to a positive interval in order to
prevent the divide by 0 mathematical errors and calculated the normalized
values.
Figure 24: Normalization Code.
The author has no
relevant affiliations or financial involvement with a financial interest in or financial with the subject matter or materials discussed in
the manuscript.
There
is no conflict of interest.