naresh-dscience / Portfolio-Optimization-using-Genetic-Algorithm

Portfolio optimization using Genetic algorithm.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Portfolio-Optimization-using-Genetic-Algorithm

Background:

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.

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.

Problem Statement:

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.

Given Data:

Four years monthly Closing Stock values of HDFC, ITC, L&T, M&M, Sun Pharma and TCS.

Approach:

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.

Tasks:

  1. Read the data and combine them into one dataframe.

  2. Calculate the historical returns for 3 months, 6 months, 12 months, 24 months and 36 months for each of the stocks.

  3. Define Gene (Scalar): A fraction of the total capital assigned to a stock.

  4. Define Chromosome (1D Array): Set of genes i.e. fractions of total capital assigned to each stock. Check! Sum of each chromosome should be equal to 1.

  5. Generate Initial Population (2D Array): A set of randomly generated chromosomes.

  6. Fitness function (Define a Function): 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 follows:

             S = (µ − r)/σ
    

    Here µ is the return of the portfolio over a specified period or Mean portfolio return, r is the risk-free rate over the same period and σ is the standard deviation of the returns over the specified period or Standard deviation of portfolio return.

    Mean portfolio return = Mean Return * Fractions of Total Capital (Chromosome). Risk-free rate = 0.0697 ( as per google.in) Standard deviation of portfolio return = (chromosome * Standard deviation)**2 + Covariance * Respective weights in chromosome.

  7. Select Elite Population (Define a Function): It filters the elite chromosomes which have highest returns, which was calculated in fitness function.

  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.

  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. The offspring are created according to the equation: Off_spring A = Best Parent + β ∗ ( Best Parent − Worst Parent) Off_spring B = Worst Parent - β ∗ ( Best Parent − Worst Parent) Where β is a random number between 0 and 1. This crossover type is good for real-valued genomes.

  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.

  11. Iterate the process: Iterate the whole process till their is no change in maximum returns or for fixed number of iterations.

References:

  1. https://www.researchgate.net/publication/286952225_A_heuristic_crossover_for_portfolio_selection
  2. https://pdfs.semanticscholar.org/9888/061ea3326ff9b41c807ed21f0c10463b7879.pdf
  3. https://www.math.kth.se/matstat/seminarier/reports/M-exjobb12/121008.pdf

About

Portfolio optimization using Genetic algorithm.


Languages

Language:Jupyter Notebook 100.0%