Project RohanKumarPandey 001910503006
Project RohanKumarPandey 001910503006
Genetic Algorithm
By
Jadavpur University
Kolkata-700032
1
To Whom It May Concern
I hereby recommend that the thesis entitled “Portfolio Optimization Using Sharpe Ratio
with Genetic Algorithm” has been carried out by Rohan Kumar Pandey (University
Registration No.: 149869 of 2019-20, Class Roll No: 001910503006) under my guidance and
supervision and be accepted in partial fulfillment of the requirement for the Degree of
Master of Computer Application in the Department of Computer Science and Engineering,
Jadavpur University.
Dean
2
FACULTY OF ENGINEERING AND TECHNOLOGY
JADAVPUR UNIVERSITY
Certificate of Approval
This is to certify that the thesis entitled “Portfolio Optimization Using Sharpe Ratio with
Genetic Algorithm” is a bona-fide record of work carried out by Rohan Kumar Pandey in partial
fulfillment of the requirements for the award of the degree of Master of Computer Application
in the Department of Computer Science and Engineering, Jadavpur University. It is understood
that by this approval the undersigned do not necessarily endorse or approve any statement
made, opinion expressed or conclusion drawn therein but approve the thesis only for the
purpose for which it has been submitted.
Signature of Examiner 1
Date:
Signature of Examiner 2
Date:
3
FACULTY OF ENGINEERING AND TECHNOLOGY
JADAVPUR UNIVERSITY
I hereby declare that this thesis entitled “Portfolio Optimization Using Sharpe Ratio with
Genetic Algorithm” contains literature survey and original research work by the undersigned
candidate, as part of his Master of Computer Application studies. All information in this
document has been obtained and presented in accordance with academic rules and ethical
conduct. I also declare that, as required by thesis rules and conduct, I have fully cited and
referenced all materials and results that are not original to this work.
Thesis Title – “Portfolio Optimization Using Sharpe Ratio with Genetic Algorithm”
4
Acknowledgement
The writing of the thesis as well as the related work has been a long journey with input
from many individuals, right from the first day till the development of the final project.
I would like to express my deepest gratitude to my supervisor, Prof. Susmita Ghosh,
Professor, Department of Computer Science and Engineering, Jadavpur University for
her admirable guidance, care, patience and for providing me with an excellent
atmosphere for doing research. Our numerous scientific discussions and her many
constructive comments have greatly improved this work. I feel deeply honored that I
got the opportunity to work under her. I would like to thank all the faculty members of
the Department of Computer Science and Engineering of Jadavpur University for their
continuous support.
This thesis would not have been completed without the inspiration and support of a
number of wonderful individuals including my batch mates of Master of Computer
Application in Jadavpur University — my thanks and appreciation to all of them for
being part of this journey and making this thesis possible.
5
CONTENTS
Certificate of Recommendation
Certificate of Approval
Acknowledgements
1. Introduction 7
2. Sharpe Ratio 9
3. Genetic Algorithm 10
3.1 Chromosome Representation 10
3.2 Fitness Function 10
3.3 Genetic Operator 10
3.3.1 Mutation Operator 11
3.3.2 Crossover Operator 11
3.3.3 Elitism Operator 12
Flowchart 12
7. Conclusion 18
8. References 19
6
Chapter 1
1. Introduction
Markowitz proposed the Mean-Variance (M-V) model in his Portfolio Theory, which defines
variance as a measure of economic risk [1]. Alternative risk measures such as Value-at-Risk
(VaR), Conditional Value-at-Risk (CVaR), Mean-Absolute Deviation (MAD), Minimax (MM),
and Variance with Skewness (VwS) portfolio optimization models have been suggested in
the portfolio theory literature [2, 3].
Portfolio optimization selects assets that show promise on their own, then looking at how
the assets are connected to each other to create a portfolio that will meet financial needs
[6].
All investment professionals are dependent on portfolio optimization techniques grounded
in Modern Portfolio Theory (MPT) to structure investment portfolios for individual investors
[6]. Using statistical techniques and computer-assisted modeling, investment advisers are
able to combine different types of assets such as stocks, bonds, real estate, and hedge funds
to create portfolios that claim to offer the best possible return for specified level of risk, or
to minimize the amount of risk for an investor and assume to achieve a specified amount of
return.
7
1.2 Brief of Genetic Algorithm
Portfolio optimization using genetic algorithms gives better or superior results compared to
other heuristic [16].
Genetic Algorithm has been developed by John Holland, his students and his colleagues at
the Michigan University in 1975. Book “Adaptation in Natural and Artificial Systems” was the
first by them that discusses Genetic Algorithm. Genetic Algorithm is search algorithm based
on mechanics of natural selection and natural genetics. Firstly, they combined survival of
the fittest among string structures with a structured yet randomized information exchange
to make a search algorithm with some of the innovative flair of human touch. In every new
generation (iteration) a new set of strings is created using bits and pieces of fittest of the
old. Randomized, genetic algorithm is not simple random walk. They efficiently exploit
historical information to make guesses on new search points with expected improved
performance [7]. The Central theme of research on genetic algorithm has been robustness
the balance between efficiency and efficacy necessary for survival in many different
environments. Genetic Algorithms are theoretically & empirically proven to provide robust
search in complex spaces. This algorithm are computationally simple and yet powerful in
their search for improvement.
Portfolio optimization is a process where an investor receives the right guidance with
respect to selection of assets from the range of other options. Modern portfolio theory was
introduced by Harry Markowitz [1]. In 1990, Harry Markowitz shared Nobel Prize in
Economics for his work on modern portfolio theory. After Markowitz many researchers
worked on this domain and came across different model for improvement. For the
completion of my work “Portfolio Optimization using Genetic Algorithm”, I took help from
many thesis and their work. In every model there is some room for improvement and
researchers improves those work later. Like in mean variance model there is some drawback
and those drawbacks were removed by the Konno and Yamazaki in their Mean absolute
deviation model [13]. In different model different parameter is used for risk measurement.
In mean-variance model, variance is measured as risk. In mean absolute deviation model,
mean of the absolute deviations of the portfolio return in all periods as risk measure. In min
max model proposed by Young, risk is measured by the minimum return over all periods.
Investing in any stocks, fund, assets is very tedious and risky matter. Investor should have a
good knowledge of the market and the previous year return of a particular stock. So for a
normal investor it is a quite challenging for choosing the correct stocks to invest. In this
work, we aim to maximize the sharpe-ratio for a certain number of given stocks. Thus this
work can simplify the investment plan for an investor.
8
Chapter 2
2. Sharpe Ratio
Sharpe ratio is the measure of risk-adjusted return of a financial portfolio. A portfolio with a
higher Sharpe ratio is considered superior relative to its peers [8]. The measure was named
after William F Sharpe, a Nobel laureate and professor of finance, emeritus at Stanford
University.
Sharpe ratio is a measure of excess portfolio return over the risk-free rate relative to its
standard deviation. Normally, the 90-day Treasury bill rate is taken as the proxy for risk-free
rate.
The formula for calculating the Sharpe ratio is {R (p) – R (f)} /s (p)
Where
9
Chapter 3
3. Genetic Algorithm
A genetic algorithm is a search heuristic that is inspired by Charles Darwin’s theory of
natural evolution. This algorithm reflects the process of natural selection where the fittest
individuals are selected for reproduction in order to produce offspring of the next generation
[7].
10
chromosome [10, 12].
In GM-µ, for each parent, two offspring are generated in the following way:
• The largest gene (weight) is increased and the smallest gene is decreased by a factor µ;
• The largest gene (weight) is decreased and the smallest gene is increased by a factor µ.
In BM-µ, for each parent, two offspring are generated by modifying a percentage of
the parent’s chromosome. A percentage, x, of the parent’s chromosome is
randomly selected, then half of those genes are increased and half are decreased
by a factor µ.
The factor µ of the GM-µ and BM-µ can be selected by the user, and its value is
between 0 and 1 [9].
For the standard flat crossover λ is a uniform random number between 0 and 1. In
this project’s experiments the value of flat crossover lambda set to 0.7 has
produced the best results.
The blend crossover (BLX-α) was introduced in [11] as a generalization of the flat
crossover. The blend crossover generates offspring by uniformly picking values
within an interval that contains the two parents. The lower and upper bound of the
interval are calculated from the parent’s genes as follows:
UpperBoundi = max(wi,parentA , wi,parentB ) + α · di
LowerBoundi = min(wi,parentA , wi,parentB ) − α · di
di =| wi,parentA − wi,parentB |
where the coefficient, α, is selected within the interval [0, 1]. Several simulations
with different portfolios and different time horizons have been run in order to find
11
the best value for the coefficient [10].
Source: https://medium.com
Flowchart:
Source: https://www.mdpi.com/2078-2489/11/12/587/htm
12
Chapter 4
The 'upward-spirals' and 'double-loops' are two bubble net feeding maneuvers.
There are three stages to the 'double-loops' maneuvers: coral loop, lob tail, and
capture loop.
To chase the prey, WOA used a random or the best search agent to simulate hunting
behavior.
WOA uses a spiral to simulate a humpback whale's bubble-net attack mechanism.
Surrounding the Prey
The current best candidate solution is assumed to be close to the target prey, while
other solutions adjust their positions in relation to the best agent.
Encircling Prey:
The current best candidate solution is assumed to be close to the target prey, while
other solutions adjust their positions in relation to the best agent [13].
13
Chapter 5
I have used Genetic Algorithm to find the weights of stocks such that we maximize
the returns and at the same time minimize the risk. Sharpe Ratio will be used to
evaluate the fitness.
i. Read the data and combine them into one data-frame stocks from
https://finance.yahoo.com using pandas data-reader I combine them and
formed a data-frame
ii. Calculate the historical returns for 252 days for each of the stocks.
iii. Define Gene (Scalar): A fraction of the total capital assigned to a stock.
iv. Define Chromosome (1D Array): Set of genes i.e. fractions of total capital
assigned to each stock. Sum of each chromosome should be equal to 1.
chromosomes.
vi. Fitness function (Define a Function): The Sharpe ratio, S, is a measure for
quantifying the performance (Fitness) of the portfolio which works on "
Maximization of return (mean) and Minimization of risk (Variance) simultaneously
" and is computed as follows: S = {R (p) – R (f)} /s (p)
Here R (p) is the return of the portfolio over a specified period or Mean portfolio
return, R (f) is the risk-free rate over the same period and s (p) is the standard
deviation of the returns over the specified period or Standard deviation of
portfolio return.
I calculated the mean of the historical return, standard deviation of the historical return
and the covariance of the historical return using the given data.
14
viii. Crossover: When two chromosomes from the same generation (referred to
as "parent chromosomes") interact, certain genes from one chromosome are
swapped for genes from the corresponding positions in the other. Two new
chromosomes are produced by this process.. The crossover probability is 0.4.
Iterate the process: Iterate the whole process till there is no change in maximum
returns.
15
Chapter 6
I have executed both Genetic Algorithm and Whale Optimization Algorithm 10 times with 5
different population sizes 100,200,300,400 and 500 iterations and also the average of 10
times is calculated.
The best results of both the Genetic Algorithm and Whale Optimization Algorithm are listed
in the below tables:
No. of Sharpe Sharpe Sharpe Sharpe Sharpe Sharpe Sharpe Sharpe Sharpe Sharpe AVG
Chromosomes Ratio Ratio Ratio Ratio Ratio Ratio Ratio Ratio Ratio Ratio
It 1 It 2 It 3 It 4 It 5 It 6 It 7 It 8 It 9 It 10
100
1.5451 1.5457 1.5454 1.546 1.5458 1.5456 1.5457 1.5452 1.5451 1.5458 1.5455
200
1.5485 1.5491 1.5488 1.5494 1.5492 1.549 1.5491 1.5486 1.5485 1.5492 1.5489
300
1.5489 1.5495 1.5492 1.5495 1.5496 1.5494 1.5498 1.549 1.5489 1.5496 1.5493
400 1.5434 1.544 1.5437 1.5443 1.5441 1.5439 1.544 1.5435 1.5434 1.5441 1.5438
500
1.5468 1.5474 1.5471 1.5477 1.5475 1.5473 1.5474 1.5469 1.5468 1.5475 1.5472
No. of Sharpe Sharpe Sharpe Sharpe Sharpe Sharpe Sharpe Sharpe Sharpe Sharpe AVG
Populations Ratio Ratio Ratio Ratio Ratio Ratio Ratio Ratio Ratio Ratio
It 1 It 2 It 3 It 4 It 5 It 6 It 7 It 8 It 9 It 10
100 0.9856 0.9754 0.9778 0.9987 1.0032 0.9898 0.9878 0.9768 0.9845 0.9687 0.98483
200 0.9789 0.9997 0.9968 0.9779 0.9812 0.9661 0.9732 0.9665 0.9928 0.9888 0.98219
300 0.9989 1.076 0.9891 0.9845 0.9934 0.9712 0.9768 0.9718 0.9923 0.9823 0.99363
400 0.9768 0.9718 0.9923 0.9665 0.9928 0.9888 1.076 0.9891 0.9823 0.9887 0.99251
500 0.9991 0.9789 0.9997 0.9768 0.9845 0.9687 0.9779 0.9812 0.9661 0.9661 0.9799
By looking at the above results generated by Genetic Algorithm and Whale Optimization
Algorithm for 5 different population sizes executed 10 times each we observed that:
The best result was given by the Genetic Algorithm in 300 chromosome size and 7th Iteration
1.5498.
The best result of Whale Optimization Algorithm was in 300 population size and 1 st iteration
0.9989
16
However considering all the iterations of each population and calculating
Average of the best result was given by GA is 1.5493 and the best average in Whale
Optimization Algorithm is 0.99363.
GA was more consistent in results in comparison to Whale Optimization Algorithm.
The graphs of best individual result of both PSO and GA are given in the page below:
17
Chapter 7
7. Conclusion:
This project report presents genetic algorithm and investigates how it can be utilized to
solve optimization problems for portfolios. It has been shown that evolutionary algorithm
can be used to maximize the sharpe-ratio in a relative short time frame.
I have compared GA with WOA, The best result in the individual iteration was given by GA.
18
Chapter 8
8. References:
1. H.M Markowitz , Portfolio selection,J-finance,1952.
2. H M Markowitz, Efficient Diversification of Investment, Yale university press,1959.
3. P. Jorion, Value at Risk: the new Benchmark for controlling Market Risk, Irwin
Professional Pub,1997.
4. D. Blenstock, Computational study of a family of mixed-integer quadratic programming
problems, Math. Program (1996) 121-140.
5. M.J. Magill, G.M. Constantinides, Portfolio Selection with transaction costs,J. Econ.
Theor.(1976) 245-263.
6. Financial Portfolio Optimization: Computationally guided agents to investigate, analyse
and invest!? By Ankit Dangi CENTRE FOR MODELING AND SIMULATION UNIVERSITY OF
PUNE
7. Article SAIPO-TAIPO and Genetic Algorithms for Investment Portfolios Juan Frausto
Solis * , José L. Purata Aldaz, Manuel González del Angel, Javier González Barbosa and
Guadalupe Castilla Valdez
8. https://economictimes.indiatimes.com
9. Genetic Algorithm Application to Portfolio Optimisation Emanuele Stomeo
10. N. J. Radcliffe, "Genetic neural networks on MIMD computers", Ph.D. Thesis (Edinburgh
University), 1990.
11. L. J. Eshelman and J. D. Schaffer, “Real-coded genetic algorithms and interval
schemata,” Foundations of Genetic Algorithms, 1993, 2, 185–202
12. N. J. Radcliffe, “Equivalence class analysis of Genetic Algorithms,” Complex Systems,
1991, 5-2, 183-205
13. Mean-Absolute Deviatzon Portfolio Optimization Model Under Transaction Costs
Hiroshi Konno Annista Wljayanayake Tokyo Institute of technology
14. https://www.sciencedirect.com/science/article/pii/S0965997816300163
15. Whale Optimization Algorithm for Multiconstraint Second-Order Stochastic Dominance
Portfolio Optimization Q. H. Zhai,1 T. Ye,2 M. X. Huang,3,4 S. L. Feng , 4 and H. Li4
16. Woodside-Oriakhi M, Lucas C and Beasley JE ,Heuristic Algorithms for the Cardinality
19