{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "![MOSEK ApS](https://www.mosek.com/static/images/branding/webgraphmoseklogocolor.png )\n", "\n", "# Linear Regression techniques using MOSEK Fusion API\n", "\n", "\n", "Regression is one of the most used techniques in finance, statistic, biology and in general any application where it is necessary to construct models that approximate input data.\n", "\n", "The aim of this tutorial is two-fold:\n", "\n", "1. to show some modeling techniques to define regression problems;\n", "2. to show how to efficiently implement those problems using MOSEK Fusion API.\n", "\n", "\n", "This tutorial is largerly based on:\n", "\n", " [1] Schmelzer, T., Hauser, R., Andersen, E., & Dahl, J. (2013). Regression techniques for Portfolio Optimisation using MOSEK. arXiv preprint arXiv:1310.3397.\n", " [2] Boyd, S., & Vandenberghe, L. (2004). Convex optimization. Cambridge university press.\n", "\n", "\n", "The code is written in Python3 and uses the [MOSEK Fusion API](https://mosek.com/documentation)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "MOSEK version (9, 0, 40)\n" ] } ], "source": [ "import mosek\n", "from mosek.fusion import *\n", "print(\"MOSEK version\", mosek.Env.getversion())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# The basics of linear regression\n", "\n", "A linear model is used to explain the relation between $m$ input variables $x_1,\\ldots,x_m$ and a dependent variable $y$ (the output). To this extent we assume that there exists a set of weights $w\\in \\mathbb{R}^{m}$ such that ideally\n", "\n", "$$\n", "y = x^T w,\n", "$$\n", "\n", "where $x=(x_1,\\ldots,x_m)$.\n", "\n", "In practice, we are given $n$ observations of the relation that links $x$ and $y$. We store them row-wise in a matrix $X\\in \\mathbb{R}^{n\\times m}$. Corresponding outputs $y=(y_i,\\ldots,y_n)$ are known as well. If the relation we want to describe is truly linear we can hope to solve the linear system\n", "\n", "$$\n", "X w =y.\n", "$$\n", "\n", "But usually this is not the case and we can only settle for an approximation which minimizes (in some sense) the residual vector\n", "\n", "$$\n", "r = Xw - y,\n", "$$\n", "\n", "and we obtain an optimization problem\n", "\n", "$$\n", "\\begin{array}{ll}\n", " \\mbox{minimize} & \\phi(r)\\\\ \n", " s.t. & r = Xw - y, \\\\\n", " & w\\in W.\n", "\\end{array}\n", "$$\n", "\n", "The choice of the cost function $\\phi(\\cdot)$ determines the way errors on the residuals are weighted. Here we demonstrate the following variants:\n", "\n", "* $\\ell_2$-norm,\n", "* $\\ell_1$-norm,\n", "* $\\ell_p-$norm ,\n", "* *Chebyshev* norm (also known as *Min-max*),\n", "* *Deadzone-linear*,\n", "* *$k$-largest residuals*." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We create simple input data sets which attempt to lead to the solution $w=(1,\\ldots,1)$ and we add small normally distributed error as follows." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import sys\n", "%matplotlib inline\n", "import matplotlib.pyplot as plt\n", "\n", "def dataset(m, n):\n", " X = np.random.uniform(-1.0, 1.0, (n,m))\n", " y = X.dot(np.ones(m)) + np.random.normal(0.0, 0.2, n)\n", " return X, y" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## $\\ell_2$-norm\n", "\n", "The regression problem with respect to the $\\ell_2$-norm (i.e. linear least squares) has the conic quadratic formulation\n", "\n", "$$\n", "\\begin{array}{ll}\n", "\\mbox{minimize} & t\\\\\n", "\\mbox{s.t.} & t\\geq \\|Xw-y\\|_2 \\quad \\mathrm{i.e.}\\ (t,Xw-y)\\in \\mathcal{Q}^{n+1},\\\\\n", " & t\\in\\mathbb{R}, w\\in\\mathbb{R}^m.\n", "\\end{array}\n", "$$\n", "\n", "This model models the maximum likelihood of the fit precisely under the assumption that residuals have normal distribution.\n", "\n", "Below is the model in Fusion. " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def l2norm(X,y):\n", " n,m=X.shape\n", " \n", " M = Model(\"l2norm\")\n", " w = M.variable(m)\n", " t = M.variable()\n", " \n", " M.objective(ObjectiveSense.Minimize, t)\n", " \n", " res = Expr.sub(Expr.mul(X,w), y)\n", " M.constraint(Expr.vstack(t, res), Domain.inQCone())\n", " \n", " #M.setLogHandler(sys.stdout)\n", " M.solve()\n", " \n", " # Return the weights vector and the residuals\n", " w = w.level()\n", " return w, X.dot(w)-y\n", "\n", "X,y = dataset(10,1000)\n", "w, res = l2norm(X,y)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If everything went well, we should have recovered the weight vector of all ones and normally distributed residuals, as below:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|w-1|_2: 0.025785996710141198\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEICAYAAABPgw/pAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAF71JREFUeJzt3X20ZXV93/H3R3B8AgRkwAmIo3VQ0UbQG8Ra6wOShaIyTdBg0Q6W1VlKYmMS206euky07ZA0GtfSmlA1jtYHkChQzIM4QtQsUAZRFFBHcYCRcWYURhjxAeTbP/a+erice8++z/fueb/Wuuvsh985+3P2Pfd7f+e3z94nVYUkafl70GIHkCTNDQu6JPWEBV2SesKCLkk9YUGXpJ6woEtST1jQl4kk1yd53mLnWExJ/m2SW5PsTXL8PG7nOUm+PsX69yV5yxxsZ3WSSrL/DO57bJItA/PbkrxwtpkWWpKXJfnIYufoCwv6EjDsjzHJWUk+Nz5fVU+pqitGPM6MC8Qy8b+A36qqA6rq2vnaSFV9tqqeOF+PP0feTLM/HiDJuiTXJLkzyfYkf7ZUXxNVdQnw1CS/vNhZ+sCCrs6WQFF4LHB9l4ZLIOu8SbIKeD5w0SRNHg68ATgMeCZwEvDGBcg1033+YWD9XGbZV1nQl4nBXnySE5JsaXtgO5O8tW32mfZ2Tzss8awkD0ryR0luTr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l6SesKBLUk9Y0CWpJyzoktQTFnRJ6on/DzEGHLd0X6TOAAAAAElFTkSuQmCC\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "print(\"|w-1|_2:\", np.linalg.norm(w-np.ones(10)))\n", "plt.hist(res,40)\n", "plt.title(\"Histogram of residuals (l2 norm)\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## $\\ell_1$-norm\n", "\n", "Under the $\\ell_1$-norm we minimize $\\sum_i |r_i|$. The corresponding linear formulation is\n", "\n", "$$\n", "\\begin{array}{ll}\n", "\\mbox{minimize} & \\sum_i t_i \\\\\n", "\\mbox{s.t.} & t\\geq Xw-y,\\\\\n", " & t \\geq -(Xw-y), \\\\\n", " & w\\in\\mathbb{R}^m, t\\in\\mathbb{R}^n,\n", "\\end{array}\n", "$$ \n", "\n", "which simply expresses the bound $t_i\\geq |r_i|$. Below is a corresponding Fusion model." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|w-1|_2: 0.034229179355081116\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEICAYAAABPgw/pAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAGIhJREFUeJzt3XuU3GV9x/H3R0LkThKyiRHEYAlotAq4xqC1IBcPGiWpRYUiDTanOYpary3x0mrVtqEq6jlSNRV1tQLBiCTFGzESFQ8gy91wMRACicRkEQKEe+TbP37PlmEzs/Ob3bnsPPm8ztkzv8szM5/57ex3n3l+l1FEYGZm3e9ZnQ5gZmbN4YJuZpYJF3Qzs0y4oJuZZcIF3cwsEy7oZmaZcEHvEpLWSDq60zk6SdJfSdogaZukw1v4PK+RdNsw678l6TNNeJ7pkkL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bgV7gqJYmasyw+VPH5QsUf5tjTZltP45i2OVoik9Gv5L0kojY2uJsNY3Jgh4Rx9VaJ2mzpGkRsSkVjWofczYC10XEunSfi4HZtKmgNyH/kcBrJJ1BMf4/XtK2iKi5U6lZmpAdSfsAPwQ+HhFXtihqGWUuTzHYZqOkccC+wH3tiVdXqctrSDqO4h/uURHxeJuylVEv/97AS4DVqePyHGCFpBMjor9tKasr+965MiKeBO5MFx6cQXEWfUd045DLCmB+mp4PLK/S5mpgoqTBsdtjgJvbkK2Muvkj4tSIODAipgMfBr7djmJeQt3s6TIQP6DI/L02ZqumzOUpKl/TScDPI+3hGgPq5k9DFl8DThwLY7hDDJs/Ih6IiMkRMT2916+keB2dLuZQ7r1zMcVOaSRNphiCWdfWlEN1eq9soz8U45urgLXpdlJa3kvxrUmD7Y4HbgRuAr4FjO909kbyV7Q/nbFzlEvd7MDbgSeB6yt+Dutg5jcAv6MYx/9YWvYpisIBsBvwPeB24DfACzq9nRvM/zNgc8W2XtHpzI3kH9J2NWPkKJeS217A2RSdxZuAkzud2af+m5llohuHXMzMrAoXdDOzTLigm5llwgXdzCwTLuhmZplwQTczy4QLuplZJv4PHYpdXs+2RQoAAAAASUVORK5CYII=\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def l1norm(X,y):\n", " n,m=X.shape\n", " \n", " M = Model(\"l1norm\")\n", " w = M.variable(m)\n", " t = M.variable(n)\n", " \n", " M.objective(ObjectiveSense.Minimize, Expr.sum(t))\n", " \n", " res = Expr.sub(Expr.mul(X,w), y)\n", " M.constraint(Expr.sub(t, res), Domain.greaterThan(0))\n", " M.constraint(Expr.add(t, res), Domain.greaterThan(0))\n", " \n", " #M.setLogHandler(sys.stdout)\n", " M.solve()\n", " \n", " # Return the weights vector and the residuals\n", " w = w.level()\n", " return w, X.dot(w)-y\n", "\n", "w, res = l1norm(X,y)\n", "print(\"|w-1|_2:\", np.linalg.norm(w-np.ones(10)))\n", "plt.hist(res,40)\n", "plt.title(\"Histogram of residuals (l1 norm)\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## $\\ell_p$-norm\n", "\n", "The $\\ell_p$ norm\n", "\n", "$$\\phi(r) = \\| r \\|_p = \\left( \\sum_i |r_i|^p \\right)^{1/p}$$\n", "\n", "For a general $p>1$ we can model it using the power cone. The standard representation of the epigraph $t\\geq\\|r\\|_p$ as a conic model can be found in https://docs.mosek.com/modeling-cookbook/powo.html#norm-cones\n", "\n", "$$\n", "\\begin{array}{l}\n", "\\sum_i s_i = t\\\\\n", "s_it^{p-1} \\geq |r_i|^p, \\ i=1,\\ldots,n\n", "\\end{array}\n", "$$\n", "\n", "and the last constraint can be expressed using a power cone. In Fusion all of these individual conic constraints can be specified at once in matrix for, where, as always in Fusion, \"matrix in a cone\" is a shorthand for \"each row of the matrix is in the cone\"." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|w-1|_2: 0.025786431917413328\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEICAYAAABPgw/pAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAF79JREFUeJzt3XuUnHV9x/H3B0IEuYXIJkQurpSARqsB1hhqLSrgQVCSWlQo2mBzmqOo9UbbeGkPXtqGalHPkaqpqNEqBiOSCFWJkah4AFmIgCFgIAQSE5IVEkJALpFv/3h+a4dlZueZ3bns/PJ5nTNnnstv5vnMM7Pf/c1v5nlGEYGZmXW/PTodwMzMmsMF3cwsEy7oZmaZcEE3M8uEC7qZWSZc0M3MMuGC3iUkrZb0qk7n6CRJfylpg6Sdko5t4XZeKenOYdZ/TdInm7CdXkkhadwIbjtNUn/F/HpJJ482U7tJOkPStzudIxcu6GN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vI/iF5MG250C3ArcBnwNGN8t2Svan8vY+pZL3fzAW4EngV9VXKZ3MPNpwG8oxvE/kpZ9nKJ4AOwNfAe4C/glcGSn93MD2X8MbKnYz8s6nbmR/EParmSMfMul5L4XcBFFR/E24KxOZx68+NB/M7NMdNuQi5mZ1eCCbmaWCRd0M7NMuKCbmWXCBd3MLBMu6GZmmXBBNzPLxP8BH10TkMSEnFYAAAAASUVORK5CYII=\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def lpnorm(X,y,p):\n", " n,m=X.shape\n", " \n", " M = Model(\"lpnorm\")\n", " w = M.variable(m)\n", " t = M.variable()\n", " s = M.variable(n)\n", " \n", " M.objective(ObjectiveSense.Minimize, t)\n", " \n", " M.constraint(Expr.sub(Expr.sum(s),t), Domain.equalsTo(0))\n", " \n", " res = Expr.sub(Expr.mul(X,w), y)\n", " M.constraint(Expr.hstack(s, Var.repeat(t,n), res), Domain.inPPowerCone(1.0/p))\n", " \n", " #M.setLogHandler(sys.stdout)\n", " M.solve()\n", "\n", " # Return the weights vector and the residuals\n", " w = w.level()\n", " return w, X.dot(w)-y\n", "\n", "w, res = lpnorm(X,y,p=2)\n", "print(\"|w-1|_2:\", np.linalg.norm(w-np.ones(10)))\n", "plt.hist(res,40)\n", "plt.title(\"Histogram of residuals (lp norm)\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Deadzone-linear\n", "\n", "This approach steams from the $\\ell_1$ norm approximation, but ignoring small errors. Given a threshold $a\\geq 0$, we define \n", "\n", "$$\\phi(r) = \\sum_i \\max(0, |r_i|-a )$$\n", "\n", "The conic optimization model takes the form:\n", "\n", "$$\n", "\\begin{array}{ll}\n", "\\mbox{minimize} & \\sum_i t_i\\\\\n", "\\mbox{s.t.} & t \\geq |Xw-y|-a,\\\\\n", "& t\\geq 0 \n", "\\end{array}\n", "$$ \n", "\n", "Again, removing the absolute value we obtain in compact form:\n", "\n", "$$\n", "\\begin{array}{ll}\n", "\\mbox{minimize} & \\sum_i t_i\\\\\n", "\\mbox{s.t.} & t \\geq Xw-y-a,\\\\\n", " & t \\geq -(Xw-y)-a,\\\\\n", "& t\\geq 0.\n", "\\end{array}\n", "$$ " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|w-1|_2: 0.03798164802353423\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEICAYAAABPgw/pAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAGKBJREFUeJzt3X20XXV95/H3B0JAeQ65CeHxag0ooALeiTAOigRaCpakLSIWabCspkjtjK3OmGmdqVU7E6ZVYC1d2iiO0SoEU5BUZlSMUHwAh8uDKCAGQiAhMbk8RIgUEPnOH/t36eZwzt373nse7vndz2uts85+3p+z77nf8zu/s88+igjMzKz/7dTrAGZm1h4u6GZmmXBBNzPLhAu6mVkmXNDNzDLhgm5mlgkX9ClG0l2STux1jl6S9LuSNkraIemYDu7nBEn3jjH/C5I+1ob9DEoKSTMmsO4RkobHmN+WjE22O+HMbdr/6yT9oBf77mcu6F0kaYOkkxumnSfpe6PjEXFkRNxQsZ2e/rN1wd8D742IPSLi9k7tJCK+GxGHd2r7bfJRiuMxrUTEncB2Sb/T6yz9xAXdXmIKvFAcCtxVZ8EpkLVjJM0D3gp8rddZeuTLwJ/0OkQ/cUGfYsqteEkLJA1LekLSVkmfSIvdmO63p26J4yXtJOlDkh6UtE3SFyXtXdruH6Z5j0r6bw37+bCk1ZL+UdITwHlp3zdJ2i5pi6RPSppZ2l5IulDSOklPSvqopN9I6zwh6cry8g2PsWlWSbtK2gHsDPxI0v0t1g9JfyppHbAuTXu1pOskPSbpXklnlZY/TdLdKefDkj6Qpp8oaVNp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oMnfqtvgH4Iwp1o87ZvaI+EVEzI6IwfTcvpniMfS6mEO958vXKD6QRtJsii6Y9V1NOZZefyo70RtFP+daYF26n5WmD1H8etLocqcAdwI/Br4AzOyX7KXlz2NqnOVSmRt4F/Ar4I7S7egeZj4N+BlFP/5fpWkfoSgiALsBXwXuA/4f8MpeH+eaub8NbC0d4zW9zlw3e8OyNzBFznKpedwFfIKiYfhj4OxeZy7f/NV/M7NM9HOXi5mZlbigm5llwgXdzCwTLuhmZplwQTczy4QLuplZJlzQzcwy8f8BJDve1TLY/PIAAAAASUVORK5CYII=\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def deadzone(X,y,a):\n", " n,m=X.shape\n", " \n", " M = Model(\"deadzone\")\n", " w = M.variable(m)\n", " t = M.variable(n, Domain.greaterThan(0))\n", " \n", " M.objective(ObjectiveSense.Minimize, Expr.sum(t))\n", " \n", " res = Expr.sub(Expr.mul(X,w), y)\n", " M.constraint(Expr.sub(t, res), Domain.greaterThan(-a))\n", " M.constraint(Expr.add(t, res), Domain.greaterThan(-a))\n", " \n", " #M.setLogHandler(sys.stdout)\n", " M.solve()\n", " \n", " # Return the weights vector and the residuals\n", " w = w.level()\n", " return w, X.dot(w)-y\n", "\n", "w, res = deadzone(X,y,0.05)\n", "print(\"|w-1|_2:\", np.linalg.norm(w-np.ones(10)))\n", "plt.hist(res,40)\n", "plt.title(\"Histogram of residuals (deadzone)\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Chebyshev Approximation\n", "\n", "Sometimes referred as the *minmax* approximation, it uses \n", "\n", "$$\\phi(r) = \\max_i( |r_i| ) = \\|r\\|_{\\infty}$$\n", "\n", "as penalty function. It is a limit case of $\\ell_p-$norms as $p\\rightarrow \\infty$. The effect is to only look at the largest error in absolute value. The formulation of our problem is therefore\n", "\n", "$$\n", "\\begin{array}{ll}\n", "\\mbox{minimize} & t\\\\\n", "\\mbox{s.t.} & te \\geq Xw-y,\\\\\n", " & te \\geq -(Xw-y),\n", "\\end{array}\n", "$$ \n", "where $e$ is the all-ones vector." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|w-1|_2: 0.16455126712218598\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAEICAYAAACktLTqAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAFmZJREFUeJzt3X+UZGV95/H3RxA08lsGRHAcjZCIbgJmRFzXIyrmsGKEzaLBRXbIspmjJNlNjO6SmF2NuglsEsSzupuQYBxNFAjyayGuIDpRPGD4pSiiIjgIMjIojPxQEeS7f9w7pmy6p6q7q/rHM+/XOX26qu5z636f6u5PPfXUradTVUiS2vK4xS5AkjR+hrskNchwl6QGGe6S1CDDXZIaZLhLUoMM92UgyY1JDlvsOhZTkn+T5PYkDyQ5eILHeXGSr25l+weSvGsMx1mVpJJsP4d9D0xyzRyPu7J/DLeby/6TkuS8JEcsdh0tMdwXWZINSQ6fctsJSa7Ycr2qnlNV64fcz5zDYpn4M+C3qmqnqrp+Ugepqs9U1c9N6v7H5J10j8esVdU3+8fwx2Ouab5OAf7HYhfREsNdI1kCTxpPB24cpeESqHVikuwDvBS4YLFrGaeq+idglySrF7uWVhjuy8Dg6D7JIUmuSXJfkruSnNY3+3T/fXP/svuFSR6X5A+T3JZkU5IPJtl14H7/fb/tu0n+25TjvD3JuUn+Nsl9wAn9sa9MsjnJxiTvTbLDwP1VkpOS3Jzk/iTvTPKz/T73JTlnsP2UPk5ba5IdkzwAbAd8IcktM+xfSX4zyc3Azf1tP5/ksiT3JPlqktcOtH9lki/3dX4ryZv72w9LcsdAu4OTXNe3Oxt4wsC2n3qFNVDHs/rLRya5vu/77Unev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B/WFVXTajUcRhlyYwtbe5Isj2wK3DPwpQ3LyMtB5LkcLon7pdU1UMLVNs4DOvfzsBzgfX9AOopwEVJXl1V1yxYlXMz6u/lVVX1MPCNJF+lC/urZ7rThZiWuQhY019eA1w4TZurgd2TbJmrfRnw5QWobRyG9q+qjquqlVW1Cngz8MGlEOwjGNq3fumJ8+n69PcLWNtcjLJkxmCfjwE+Wf07WEvc0L710xZ/Cbx6lDnbJWar/auq71XVnlW1qv87u4qun0s92GG038sL6N4QJ8medNM0t271XhfgneAnA5cDN/ff9+hvXw389UC7VwA3AF8EPgDssNjvYo+zfwPtT2D5nC0ztG/A64GHgc8PfB202LVvpU+vBL5G977AW/vb3kEXBABPAP4e+DrwT8AzF7vmMfbtE8BdAz+nixa75nH2b0rb9SyTs2VG/NkFOI1u0PtF4Nhh9+nyA5LUID+hKkkNMtwlqUGGuyQ1yHCXpAYZ7pLUIMNdkhpkuEtSg/4/2eyUIU3uKWsAAAAASUVORK5CYII=\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def minmax(X,y):\n", " n,m=X.shape\n", " \n", " M = Model(\"minmax\")\n", " w = M.variable(m)\n", " t = M.variable()\n", " \n", " M.objective(ObjectiveSense.Minimize, t)\n", " \n", " res = Expr.sub(Expr.mul(X,w), y)\n", " t_repeat = Var.repeat(t,n)\n", " M.constraint(Expr.sub(t_repeat, res), Domain.greaterThan(0))\n", " M.constraint(Expr.add(t_repeat, res), Domain.greaterThan(0))\n", " \n", " #M.setLogHandler(sys.stdout)\n", " M.solve()\n", " \n", " # Return the weights vector and the residuals\n", " w = w.level()\n", " return w, X.dot(w)-y\n", "\n", "w, res = minmax(X,y)\n", "print(\"|w-1|_2:\", np.linalg.norm(w-np.ones(10)))\n", "plt.hist(res,40)\n", "plt.title(\"Histogram of residuals (minmax)\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Sum of the largest $k$ residuals\n", "\n", "The penalty function $\\phi$ is defined as \n", "\n", "$$\\phi(u) = \\sum_{i=1}^k | r_{[i]} |,$$\n", "\n", "where the notation $r_{[i]}$ indicates the $i$-th element of $r$ sorted. It means that the penalty depends on the $k$ largest (in absolute value) residuals. This is a generalization of the minmax norm, which here corresponds to $k=1$, and of the $\\ell_1$-norm, which corresponds to $k=n$. As shown in https://docs.mosek.com/modeling-cookbook/linear.html#sum-of-largest-elements whis problem can be modeled in a linear fashion:\n", "\n", "$$\n", "\\begin{array}{ll}\n", "\\mbox{minimize} & ks+\\sum u_i\\\\\n", "\\mbox{s.t.} & se + u \\geq Xw -y\\\\\n", " & se + u \\geq -(Xw -y)\\\\\n", " & u\\geq 0,\\\\\n", " & u\\in \\mathbb{R}^n,s\\in \\mathbb{R},w\\in\\mathbb{R}^m.\n", "\\end{array}\n", "$$" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "|w-1|_2: 0.08698784890647508\n" ] }, { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAXQAAAEICAYAAABPgw/pAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvNQv5yAAAGKpJREFUeJzt3XmUXGWZx/HvT0JAZQ10YgAxIgGNjBJsI446IIsHQUlGUXGQCQ5jjqKOuI1xmRHF0TAqwhkdNYoSFzYjQgZciJGoeAjSEEBDwEAMJhCTRggkKEvgmT/u21p0qrtuVdfWb/8+5/Spu7z33ue9Vf3UW+/dFBGYmdno95ROB2BmZs3hhG5mlgkndDOzTDihm5llwgndzCwTTuhmZplwQu8yklZIOrzTcXSSpH+UtFbSFknTW7idV0i6fZj550v6VBO2M0VSSBrXwLLTJPWNNIY6t9lwvE2O41JJx3QyhtHGCb2NJK2RdNSgaadIumZgPCKeHxFLa6ynK/7hWuhzwLsiYq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dogaXJErE9JotpPlnXA8ohYnZa5DDiUNif0JtTjpcArJJ1GcRxgvKQtETHkAaNma0IdkLQLcCXwsYhY1qJQ61Xm9hQDZdZJGgfsCtzXnvBKK3WbDUlHUXwJHxYRj7QptnrUqsfOwEHA0tS4eQawSNLxEdHXtiiHV/YztSwiHgN+n25IOJXi6vqW6tYul0XA7DQ8G7i8Spnrgd0lDfTVHgHc2obY6lGzHhFxUkTsGxFTgA8A32pnMi+hZh3SrR9+QBH799oYWy1lbk9RWb8TgJ9FOpLVRWrWI3VVfBU4vp19tnUath4R8UBE7BkRU9L/wzKK+nRLModyn6nLKA5SI2lPii6Y1W2JrtNHjYc4krwHsARYlV4npOm9FE9GGih3NHAL8BvgfGB8p2NvpB4V5U+h+85yqVkH4C3AY8BNFX8Hdzr2FNuxwO8o+vQ/mqZ9kiJRAOwIfA+4A/g1sF+nY26wHj8FNlTs/0WdjrmRegwqu5QuO8ul5Hsh4GyKBuZvgBPbFZsv/Tczy0S3drmYmVmdnNDNzDLhhG5mlgkndDOzTDihm5llwgndzCwTTuhmZpn4f8iMPPvgNHtBAAAAAElFTkSuQmCC\n", "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "def sumkmax(X,y,k):\n", " n,m=X.shape\n", " \n", " M = Model(\"sumkmax\")\n", " w = M.variable(m)\n", " s = M.variable()\n", " u = M.variable(n, Domain.greaterThan(0))\n", " \n", " M.objective(ObjectiveSense.Minimize, Expr.add(Expr.sum(u), Expr.mul(k,s)))\n", " \n", " res = Expr.sub(Expr.mul(X,w), y)\n", " lhs = Expr.add(u, Var.repeat(s,n))\n", " M.constraint(Expr.sub(lhs, res), Domain.greaterThan(0))\n", " M.constraint(Expr.add(lhs, res), Domain.greaterThan(0))\n", " \n", " #M.setLogHandler(sys.stdout)\n", " M.solve()\n", " \n", " # Return the weights vector and the residuals\n", " w = w.level()\n", " return w, X.dot(w)-y\n", "\n", "w, res = sumkmax(X,y,20)\n", "print(\"|w-1|_2:\", np.linalg.norm(w-np.ones(10)))\n", "plt.hist(res,40)\n", "plt.title(\"Histogram of residuals (sumkmax)\")\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Some observations\n", "\n", "All the regression models presented in this tutorial lead to either a linear or a second-order cone or a power cone optimization problem. That means that\n", "\n", "* they can be solved in polynomial time by standard optimization algorithms;\n", "* add additional (conic)constraints on the weights can be easily included;\n", "* sparsity in the input, i.e. in $X$, can be easily handled and usually leads to great savings in time and memory;\n", "* the solver will automatically choose between the primal and dual formulation;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "
This work is licensed under a Creative Commons Attribution 4.0 International License. The **MOSEK** logo and name are trademarks of Mosek ApS. The code is provided as-is. Compatibility with future release of **MOSEK** or the Fusion API are not guaranteed. For more information contact our [support](mailto:support@mosek.com). " ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.4" } }, "nbformat": 4, "nbformat_minor": 1 }