├── Portfolio_Optimization_Using_GA.ipynb ├── README.md └── Stocks_Data ├── hdfc.csv ├── itc.csv ├── l&t.csv ├── m&m.csv ├── sunpha.csv └── tcs.csv /README.md: -------------------------------------------------------------------------------- 1 | # Portfolio-Optimization-using-Genetic-Algorithm 2 | 3 | ## Background: 4 | 5 | **Portfolio optimization** is one of the most interesting fields of study of financial mathematics. Since the birth of Modern Portfolio Theory (MPT) by Harry Markowitz, many scientists have studied a lot of analytical and numerical methods to build the best investment portfolio according to a defined set of assets. The power of **genetic algorithms** makes it possible to find the optimal portfolio. 6 | 7 | In dealing with this kind of Optimization problem, Harry Markowitz 1959 developed a quantitative model, also called **mean-variance model**. The mean-variance model has been usually considered as either the minimization of an objective function representing the portfolio variance (risk) for a given level of return or the maximization of an objective function representing the portfolio return for a given level of risk. 8 | 9 | ## Problem Statement: 10 | Let’s say we have selected N financial assets we want to invest in. They can be stock, funds, bonds, ETF etc. Each one of them has many historical returns, that is the price relative difference from one period to another. Periods can be days, weeks, months and so on. Build an investment portfolio with a mix of many assets (They can be stock, funds, bonds, ETF) together allocating a fraction x of total capital to each one of them. Each fraction is called weight. The goal of portfolio optimization is to find the values of the weights that Maximize returns and minimize risk simultaneously of our portfolio under some constraints. 11 | 12 | ## Given Data: 13 | Four years monthly Closing Stock values of HDFC, ITC, L&T, M&M, Sun Pharma and TCS. 14 | 15 | ## Approach: 16 | 17 | We shall be using **Genetic Algorithm** to find the weights such that we maximize the returns and at the same time minimize the risk. **Sharpe Ratio** will be used to evaluate the fitness. Coding of the same will be carried out using python language. 18 | 19 | ## Tasks: 20 | 21 | 1. Read the data and combine them into one dataframe. 22 | 2. Calculate the historical returns for 3 months, 6 months, 12 months, 24 months and 36 months for each of the stocks. 23 | 3. Define **Gene** (Scalar): A fraction of the total capital assigned to a stock. 24 | 4. Define **Chromosome** (1D Array): Set of genes i.e. fractions of total capital assigned to each stock. 25 | Check! Sum of each chromosome should be equal to 1. 26 | 5. Generate **Initial Population** (2D Array): A set of randomly generated chromosomes. 27 | 6. **Fitness function** (Define a Function): 28 | The **Sharpe ratio**, S, is a measure for quantifying the performance (Fitness) of the portfolio which works on "Maximisation of return (mean) and minimisation of risk (Variance) simultaneously" and is computed as 29 | follows: 30 | 31 | S = (µ − r)/σ 32 | 33 | Here µ is the return of the portfolio over a specified period or Mean portfolio return, 34 | r is the risk-free rate over the same period and 35 | σ is the standard deviation of the returns over the specified period or Standard deviation of portfolio return. 36 | 37 | 38 | Mean portfolio return = Mean Return * Fractions of Total Capital (Chromosome). 39 | Risk-free rate = 0.0697 ( as per google.in) 40 | Standard deviation of portfolio return = (chromosome * Standard deviation)**2 + Covariance * Respective weights in chromosome. 41 | 42 | 7. Select **Elite Population** (Define a Function): It filters the elite chromosomes which have highest returns, which was calculated in fitness function. 43 | 44 | 8. **Mutation**: A function that will perform mutation in a chromosome. Randomly we shall choose 2 numbers between 0, 5 and those elements we shall swap. 45 | 46 | 9. Crossover: **Heuristic crossover** or **Blend Crossover** uses the fitness values of two parent chromosomes to ascertain the direction of the search. It moves from worst parent to best parent. 47 | The offspring are created according to the equation: 48 | Off_spring A = Best Parent + β ∗ ( Best Parent − Worst Parent) 49 | Off_spring B = Worst Parent - β ∗ ( Best Parent − Worst Parent) 50 | Where β is a random number between 0 and 1. 51 | This crossover type is good for real-valued genomes. 52 | 53 | 10. **Next Generation** (define a Function): A function which does mutation,mating or crossover based on a probability and builds a new generation of chromosomes. 54 | 55 | 11. **Iterate the process**: Iterate the whole process till their is no change in maximum returns or for fixed number of iterations. 56 | 57 | ### References: 58 | 1. https://www.researchgate.net/publication/286952225_A_heuristic_crossover_for_portfolio_selection 59 | 2. https://pdfs.semanticscholar.org/9888/061ea3326ff9b41c807ed21f0c10463b7879.pdf 60 | 3. https://www.math.kth.se/matstat/seminarier/reports/M-exjobb12/121008.pdf 61 | -------------------------------------------------------------------------------- /Stocks_Data/hdfc.csv: -------------------------------------------------------------------------------- 1 | Date,Close 2 | June 2018,2108.05 3 | May 2018,2136.15 4 | Apr 2018,1944.6 5 | Mar 2018,1891.45 6 | Feb 2018,1883.8 7 | Jan 2018,2006.35 8 | Dec 2017,1873.55 9 | Nov 2017,1852.05 10 | Oct 2017,1808.8 11 | Sep 2017,1803.05 12 | Aug 2017,1775 13 | July 2017,1783.35 14 | June 2017,1652.25 15 | May 2017,1633.35 16 | Apr 2017,1542.15 17 | Mar 2017,1442.3 18 | Feb 2017,1389.2 19 | Jan 2017,1286.95 20 | Dec 2016,1204.2 21 | Nov 2016,1199.6 22 | Oct 2016,1253.7 23 | Sep 2016,1271.7 24 | Aug 2016,1290.75 25 | July 2016,1246.55 26 | June 2016,1175.9 27 | May 2016,1181.35 28 | Apr 2016,1133.45 29 | Mar 2016,1071.2 30 | Feb 2016,972.55 31 | Jan 2016,1048.95 32 | Dec 2015,1082.75 33 | Nov 2015,1075.95 34 | Oct 2015,1097.05 35 | Sep 2015,1068.9 36 | Aug 2015,1028.15 37 | July 2015,1111.2 38 | June 2015,1067.45 39 | -------------------------------------------------------------------------------- /Stocks_Data/itc.csv: -------------------------------------------------------------------------------- 1 | Date,Close 2 | June 2018,266.05 3 | May 2018,271.6 4 | Apr 2018,281.45 5 | Mar 2018,255.9 6 | Feb 2018,265.1 7 | Jan 2018,271.25 8 | Dec 2017,263.1 9 | Nov 2017,255.65 10 | Oct 2017,266.1 11 | Sep 2017,258.25 12 | Aug 2017,282.2 13 | July 2017,285.15 14 | June 2017,323.85 15 | May 2017,311.9 16 | Apr 2017,278.6 17 | Mar 2017,280.45 18 | Feb 2017,262.15 19 | Jan 2017,258.05 20 | Dec 2016,240.95 21 | Nov 2016,232.9 22 | Oct 2016,241.9 23 | Sep 2016,241.55 24 | Aug 2016,260.05 25 | July 2016,252.45 26 | June 2016,245.38 27 | May 2016,234.38 28 | Apr 2016,216.51 29 | Mar 2016,218.68 30 | Feb 2016,197.18 31 | Jan 2016,212.98 32 | Dec 2015,218.38 33 | Nov 2015,228.78 34 | Oct 2015,223.11 35 | Sep 2015,219.24 36 | Aug 2015,216.84 37 | July 2015,217.44 38 | June 2015,210.01 39 | -------------------------------------------------------------------------------- /Stocks_Data/l&t.csv: -------------------------------------------------------------------------------- 1 | Date,Close 2 | June 2018,1271.3 3 | May 2018,1367.6 4 | Apr 2018,1400.6 5 | Mar 2018,1311.9 6 | Feb 2018,1319.1 7 | Jan 2018,1416.6 8 | Dec 2017,1256.95 9 | Nov 2017,1216.85 10 | Oct 2017,1220.6 11 | Sep 2017,1141.2 12 | Aug 2017,1136 13 | July 2017,1192.1 14 | June 2017,1125.26 15 | May 2017,1173.23 16 | Apr 2017,1165.42 17 | Mar 2017,1051.79 18 | Feb 2017,980.22 19 | Jan 2017,963.21 20 | Dec 2016,899.64 21 | Nov 2016,921.75 22 | Oct 2016,984.48 23 | Sep 2016,954.48 24 | Aug 2016,1009.28 25 | July 2016,1038.72 26 | June 2016,997.52 27 | May 2016,982.92 28 | Apr 2016,835.74 29 | Mar 2016,810.84 30 | Feb 2016,719.54 31 | Jan 2016,734.84 32 | Dec 2015,850.48 33 | Nov 2015,916.08 34 | Oct 2015,940.81 35 | Sep 2015,977.85 36 | Aug 2015,1070.25 37 | July 2015,1193.09 38 | June 2015,1188.56 39 | -------------------------------------------------------------------------------- /Stocks_Data/m&m.csv: -------------------------------------------------------------------------------- 1 | Date,Close 2 | June 2018,896.8 3 | May 2018,923.5 4 | Apr 2018,872.65 5 | Mar 2018,740.2 6 | Feb 2018,728.75 7 | Jan 2018,763.45 8 | Dec 2017,751.05 9 | Nov 2017,705.25 10 | Oct 2017,672.5 11 | Sep 2017,626.88 12 | Aug 2017,672.38 13 | July 2017,701 14 | June 2017,673.83 15 | May 2017,708.18 16 | Apr 2017,667.28 17 | Mar 2017,642.35 18 | Feb 2017,653.48 19 | Jan 2017,619.68 20 | Dec 2016,592.23 21 | Nov 2016,592.98 22 | Oct 2016,659.03 23 | Sep 2016,702.98 24 | Aug 2016,719 25 | July 2016,733.45 26 | June 2016,714.48 27 | May 2016,662.35 28 | Apr 2016,665.88 29 | Mar 2016,604.83 30 | Feb 2016,613.4 31 | Jan 2016,616.35 32 | Dec 2015,635.78 33 | Nov 2015,683.63 34 | Oct 2015,591.25 35 | Sep 2015,630.35 36 | Aug 2015,611.48 37 | July 2015,680.8 38 | June 2015,640.6 39 | -------------------------------------------------------------------------------- /Stocks_Data/sunpha.csv: -------------------------------------------------------------------------------- 1 | Date,Close 2 | June 2018,560.55 3 | May 2018,480.15 4 | Apr 2018,528.15 5 | Mar 2018,495.4 6 | Feb 2018,535.35 7 | Jan 2018,579.35 8 | Dec 2017,570.8 9 | Nov 2017,539.95 10 | Oct 2017,553.4 11 | Sep 2017,503.2 12 | Aug 2017,480.35 13 | July 2017,531.7 14 | June 2017,554.5 15 | May 2017,501.4 16 | Apr 2017,642.55 17 | Mar 2017,687.7 18 | Feb 2017,678.95 19 | Jan 2017,631.55 20 | Dec 2016,629.75 21 | Nov 2016,710.3 22 | Oct 2016,748.25 23 | Sep 2016,742.7 24 | Aug 2016,774.85 25 | July 2016,829.75 26 | June 2016,763.6 27 | May 2016,762.7 28 | Apr 2016,811.3 29 | Mar 2016,819.45 30 | Feb 2016,855.05 31 | Jan 2016,873 32 | Dec 2015,819.95 33 | Nov 2015,730.65 34 | Oct 2015,889 35 | Sep 2015,868.45 36 | Aug 2015,897.7 37 | July 2015,822.75 38 | June 2015,874.2 39 | -------------------------------------------------------------------------------- /Stocks_Data/tcs.csv: -------------------------------------------------------------------------------- 1 | Date,Close 2 | June 2018,1847.2 3 | May 2018,1744.8 4 | Apr 2018,1765.7 5 | Mar 2018,1424.65 6 | Feb 2018,1519.13 7 | Jan 2018,1555.88 8 | Dec 2017,1350.2 9 | Nov 2017,1317.13 10 | Oct 2017,1308.15 11 | Sep 2017,1218.5 12 | Aug 2017,1248.38 13 | July 2017,1247.03 14 | June 2017,1182.18 15 | May 2017,1272.18 16 | Apr 2017,1136.05 17 | Mar 2017,1215.55 18 | Feb 2017,1233.25 19 | Jan 2017,1114.95 20 | Dec 2016,1180.98 21 | Nov 2016,1138.38 22 | Oct 2016,1197.3 23 | Sep 2016,1213.6 24 | Aug 2016,1256.28 25 | July 2016,1309.28 26 | June 2016,1275.4 27 | May 2016,1287.55 28 | Apr 2016,1265.03 29 | Mar 2016,1258.03 30 | Feb 2016,1090.95 31 | Jan 2016,1195.6 32 | Dec 2015,1219.6 33 | Nov 2015,1182.63 34 | Oct 2015,1248.65 35 | Sep 2015,1293.85 36 | Aug 2015,1282.63 37 | July 2015,1255 38 | June 2015,1276.1 39 | --------------------------------------------------------------------------------