{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "***\n", "# $\\quad$ Lecture 14 - Portfolio optimization\n", "***\n", "$\\newcommand{\\vct}[1]{\\mathbf{#1}}$\n", "$\\newcommand{\\mtx}[1]{\\mathbf{#1}}$\n", "$\\newcommand{\\e}{\\varepsilon}$\n", "$\\newcommand{\\norm}[1]{\\|#1\\|}$\n", "$\\newcommand{\\minimize}{\\text{minimize}\\quad}$\n", "$\\newcommand{\\maximize}{\\text{maximize}\\quad}$\n", "$\\newcommand{\\subjto}{\\quad\\text{subject to}\\quad}$\n", "$\\newcommand{\\R}{\\mathbb{R}}$\n", "$\\newcommand{\\trans}{T}$\n", "$\\newcommand{\\ip}[2]{\\langle {#1}, {#2} \\rangle}$\n", "$\\newcommand{\\zerovct}{\\vct{0}}$\n", "$\\newcommand{\\diff}[1]{\\mathrm{d}{#1}}$\n", "$\\newcommand{\\conv}{\\operatorname{conv}}$\n", "$\\newcommand{\\inter}{{\\operatorname{int}}}$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So far we have only dealt with constrained optimization problems where the objective and the constraints are linear. We now turn attention to general problems of the form\n", "\n", "\\begin{align*}\\tag{1}\n", "\\begin{split}\n", " \\minimize & f(\\vct{x})\\\\\n", " \\subjto & \\vct{g}(\\vct{x})\\leq \\zerovct\\\\\n", " & \\vct{h}(\\vct{x})= \\zerovct,\n", "\\end{split}\n", "\\end{align*}\n", "\n", "where $\\vct{x}\\in \\R^n$, $\\vct{g}=(g_1,\\dots,g_m)^{\\trans}$, $\\vct{g}=(g_1,\\dots,g_{\\ell})$, and the inequalities are componentwise. \n", "The problem above is *convex*, if $f$ and the $g_i$ are convex, and the $h_j$ are linear. We also denote by $\\mathrm{dom}(f)$ the *domain* of $f$, which is the set of points $\\vct{x}$ where $f$ takes a finite value. The feasible set\n", "\n", "\\begin{equation*}\n", " \\mathcal{F} = \\{\\vct{x} \\mid g_i(\\vct{x})\\leq 0, \\ h_j(\\vct{x})=0, \\ 1\\leq i\\leq m, \\ 1\\leq j\\leq \\ell\\}\n", "\\end{equation*}\n", "\n", "for a convex constrained problem is a convex set." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "### 14.1 Quadratic programming and portfolio optimization\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The simplest case of non-linear, constrained convex optimization is **quadratic programming**. Quadratic programming problems are of the form\n", "\n", "\\begin{align*}\n", " \\minimize & \\frac{1}{2}\\vct{x}^{\\trans}\\mtx{Q}\\vct{x}+\\vct{c}^{\\trans}\\vct{x}\\\\\n", " \\subjto & \\mtx{A}\\vct{x}\\leq \\vct{b},\n", "\\end{align*}\n", "\n", "with $\\mtx{Q}$ symmetric and positive semidefinite.\n", "Note that this problem combines two problems we studied in some detail before: minimizing a quadratic function, and linear constraints. Just as we did for unconstrained minimization and for linear programming, we will derive optimality conditions for such problems. Before studying the theory, we first present an important application: portfolio optimization.\n", "\n", "If we invest an amount $x^0$ into a product at period $0$, and at period $1$ (for example, one day) the value is $x^1$, then the \\textbf{relative return} is defined as\n", "\n", "\\begin{equation*}\n", " r = \\frac{x^{1}-x^{0}}{x^{0}}.\n", "\\end{equation*}\n", "\n", "As we can't predict the future, we usually work with the **expected return** $\\mtx{E}[r]=\\mu$, which is a statistical estimate of the future return. One naive method of estimating the future return is by taking the average of past returns, but other more sophisticated methods are possible.\n", "\n", "In **portfolio optimization**, we have a proportion $x_i$ of our available funds that we want to invest in a stock $i$ (in particular, as $x_i$ measures a proportion, we have $\\sum_{i=1}^n x_i = 1$). We may or may not allow $x_i<0$, which would correspond to short-selling or borrowing. Given this allocation, the overall return is $\\vct{x}^{\\trans}\\vct{r}$, where $\\vct{x}=(x_1,\\dots,x_n)^{\\trans}$ and $\\vct{r}=(r_1,\\dots,r_n)^{\\trans}$ is the vector of (relative) returns.\n", "If $\\vct{\\mu}=\\mtx{E}[\\vct{r}]$ denotes the vector of expected returns, then the total expected return is \n", "\n", "\\begin{equation*}\n", "\\mu = \\vct{x}^{\\trans}\\vct{\\mu}=\\sum_{i=1}^n x_i\\mu_i.\n", "\\end{equation*} \n", "\n", "The **risk** is an investment is measured in terms of the **variance** of the returns $r=\\vct{x}^{\\trans}\\vct{r}$. Let $\\mtx{\\Sigma}$ denote the \\textbf{covariance matrix}, where the $(i,j)$-th entry is $\\mathrm{Cov}(r_i,r_j) = \\mtx{E}[(r_i-\\mu_i)(r_j-\\mu_j)]$. The $(i,j)$-th entry measures how much products $i$ and $j$ are correlated. The \\textbf{variance} of the returns $r$ is then the quadratic function\n", "\n", " \\begin{equation*}\n", " \\vct{x}^{\\trans}\\Sigma\\vct{x}.\n", " \\end{equation*} \n", " \n", "A portfolio optimization problem either seeks to maximize the return while bounding the risk,\n", "\n", "\\begin{align*}\n", " \\maximize & \\vct{x}^{\\trans}\\vct{\\mu}\\\\\n", " \\subjto & \\vct{x}^{\\trans}\\mtx{\\Sigma}\\vct{x}\\leq \\sigma,\\\\\n", " & \\sum_{i=1}^n x_i=1\\\\\n", " & x_i\\geq 0,\n", " \\end{align*}\n", "\n", "or minimize the risk given a certain target return $\\mu$,\n", "\n", "\\begin{align*}\n", " \\minimize & \\vct{x}^{\\trans}\\mtx{\\Sigma}\\vct{x}\\\\\n", " \\subjto & \\vct{x}^{\\trans}\\vct{\\mu} = \\mu\\\\\n", " & \\sum_{i=1}^n x_i = 1\\\\\n", " & x_i\\geq 0.\n", "\\end{align*}\n", "\n", "Both of these problems are convex optimization problems. The constraints $x_i\\geq 0$ mean that we are not allowed to short-sell; when dealing with futures or options, or if we are a large institutional investor, we may drop these constraints. As we will see later, dropping the inequality constraints from the second formulation allows the problem to be solved in closed form.\n", "\n", "A third form of the portfolio optimization problem is to combine the expected return and the risk into a single objective function, as follows:\n", "\n", "\\begin{align*}\n", " \\maximize & \\vct{\\mu}^{\\trans}\\vct{x} - \\gamma \\vct{x}^{\\trans}\\mtx{\\Sigma}\\vct{x}\\\\\n", " \\subjto & \\sum_{i=1}^n x_i = 1\\\\\n", " & x_i\\geq 0.\n", "\\end{align*}\n", "\n", "Note that this is a convex quadratic problem: we can transform it into a minimization problem with the matrix $\\mtx{\\Sigma}$ by changing the sign.\n", "The parameter $\\gamma$ is called a **risk aversion parameter**; it adjusts the level of risk we are willing to take. If $\\gamma=0$, then the quadratic term does not feature in the objective and we just aim to maximize the expected return. If $\\gamma$ is big, then the risk term weighs heavily on the objective function, and the optimal value will like be one where $\\vct{x}^{\\trans}\\mtx{\\Sigma}\\vct{x}$ is small. By varying the value of $\\gamma$, we get different expected return / risk (variance) trade-offs. The following code computes this trade-off curve for a portfolio of $10$ stock from the FT100 index. The mean and covariance where computing by averaging over the past 60 trading days (3 months). The $x$-axis is the \\textbf{standard deviation}, which is given as the square root of the variance (risk). " ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from datetime import datetime, date\n", "import pandas as pd\n", "from pandas_datareader import data, wb\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from cvxpy import *" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Set date range\n", "START = datetime(2016,1,1)\n", "END = date.today()\n", "TICKER = ['ADN', 'AZN', 'EZJ', 'GSK', 'ITV', 'LSE', 'TSCO', 'PSON', 'PRU', 'DGE']" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "
\n", " | ADN | \n", "AZN | \n", "EZJ | \n", "GSK | \n", "ITV | \n", "LSE | \n", "TSCO | \n", "PSON | \n", "PRU | \n", "DGE | \n", "
---|---|---|---|---|---|---|---|---|---|---|
Date | \n", "\n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " | \n", " |
2016-01-04 | \n", "2.41197 | \n", "31.926706 | \n", "84.900002 | \n", "37.813187 | \n", "0.4 | \n", "0.002 | \n", "82.800593 | \n", "0.016 | \n", "77.020534 | \n", "1036.770020 | \n", "
2016-01-05 | \n", "2.41197 | \n", "32.404650 | \n", "86.620003 | \n", "38.028194 | \n", "0.4 | \n", "0.001 | \n", "82.741275 | \n", "0.014 | \n", "76.760072 | \n", "1044.900024 | \n", "
2016-01-06 | \n", "2.41197 | \n", "31.869351 | \n", "83.209999 | \n", "37.616878 | \n", "0.4 | \n", "0.002 | \n", "81.604310 | \n", "0.014 | \n", "74.203653 | \n", "1031.810059 | \n", "