{ "cells": [ { "cell_type": "markdown", "id": "6b6b0d7a", "metadata": { "editable": true }, "source": [ "" ] }, { "cell_type": "markdown", "id": "eb2769bd", "metadata": { "editable": true }, "source": [ "# Optimization, the central part of any Machine Learning algortithm\n", "\n", "Almost every problem in machine learning and data science starts with\n", "a dataset $X$, a model $g(\\beta)$, which is a function of the\n", "parameters $\\beta$ and a cost function $C(X, g(\\beta))$ that allows\n", "us to judge how well the model $g(\\beta)$ explains the observations\n", "$X$. The model is fit by finding the values of $\\beta$ that minimize\n", "the cost function. Ideally we would be able to solve for $\\beta$\n", "analytically, however this is not possible in general and we must use\n", "some approximative/numerical method to compute the minimum.\n", "\n", "In our discussion on Logistic Regression we studied the \n", "case of\n", "two classes, with $y_i$ either\n", "$0$ or $1$. Furthermore we assumed also that we have only two\n", "parameters $\\beta$ in our fitting, that is we\n", "defined probabilities" ] }, { "cell_type": "markdown", "id": "7ef2e04a", "metadata": { "editable": true }, "source": [ "$$\n", "\\begin{align*}\n", "p(y_i=1|x_i,\\boldsymbol{\\beta}) &= \\frac{\\exp{(\\beta_0+\\beta_1x_i)}}{1+\\exp{(\\beta_0+\\beta_1x_i)}},\\nonumber\\\\\n", "p(y_i=0|x_i,\\boldsymbol{\\beta}) &= 1 - p(y_i=1|x_i,\\boldsymbol{\\beta}),\n", "\\end{align*}\n", "$$" ] }, { "cell_type": "markdown", "id": "64c36bcf", "metadata": { "editable": true }, "source": [ "where $\\boldsymbol{\\beta}$ are the weights we wish to extract from data, in our case $\\beta_0$ and $\\beta_1$. \n", "\n", "Our compact equations used a definition of a vector $\\boldsymbol{y}$ with $n$\n", "elements $y_i$, an $n\\times p$ matrix $\\boldsymbol{X}$ which contains the\n", "$x_i$ values and a vector $\\boldsymbol{p}$ of fitted probabilities\n", "$p(y_i\\vert x_i,\\boldsymbol{\\beta})$. We rewrote in a more compact form\n", "the first derivative of the cost function as" ] }, { "cell_type": "markdown", "id": "0b5a1717", "metadata": { "editable": true }, "source": [ "$$\n", "\\frac{\\partial \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}} = -\\boldsymbol{X}^T\\left(\\boldsymbol{y}-\\boldsymbol{p}\\right).\n", "$$" ] }, { "cell_type": "markdown", "id": "b51524a2", "metadata": { "editable": true }, "source": [ "If we in addition define a diagonal matrix $\\boldsymbol{W}$ with elements \n", "$p(y_i\\vert x_i,\\boldsymbol{\\beta})(1-p(y_i\\vert x_i,\\boldsymbol{\\beta})$, we can obtain a compact expression of the second derivative as" ] }, { "cell_type": "markdown", "id": "276aac60", "metadata": { "editable": true }, "source": [ "$$\n", "\\frac{\\partial^2 \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}\\partial \\boldsymbol{\\beta}^T} = \\boldsymbol{X}^T\\boldsymbol{W}\\boldsymbol{X}.\n", "$$" ] }, { "cell_type": "markdown", "id": "70a606cb", "metadata": { "editable": true }, "source": [ "This defines what is called the Hessian matrix.\n", "\n", "If we can set up these equations, Newton-Raphson's iterative method is normally the method of choice. It requires however that we can compute in an efficient way the matrices that define the first and second derivatives. \n", "\n", "Our iterative scheme is then given by" ] }, { "cell_type": "markdown", "id": "e9908c02", "metadata": { "editable": true }, "source": [ "$$\n", "\\boldsymbol{\\beta}^{\\mathrm{new}} = \\boldsymbol{\\beta}^{\\mathrm{old}}-\\left(\\frac{\\partial^2 \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}\\partial \\boldsymbol{\\beta}^T}\\right)^{-1}_{\\boldsymbol{\\beta}^{\\mathrm{old}}}\\times \\left(\\frac{\\partial \\mathcal{C}(\\boldsymbol{\\beta})}{\\partial \\boldsymbol{\\beta}}\\right)_{\\boldsymbol{\\beta}^{\\mathrm{old}}},\n", "$$" ] }, { "cell_type": "markdown", "id": "be51af28", "metadata": { "editable": true }, "source": [ "or in matrix form as" ] }, { "cell_type": "markdown", "id": "8f626746", "metadata": { "editable": true }, "source": [ "$$\n", "\\boldsymbol{\\beta}^{\\mathrm{new}} = \\boldsymbol{\\beta}^{\\mathrm{old}}-\\left(\\boldsymbol{X}^T\\boldsymbol{W}\\boldsymbol{X} \\right)^{-1}\\times \\left(-\\boldsymbol{X}^T(\\boldsymbol{y}-\\boldsymbol{p}) \\right)_{\\boldsymbol{\\beta}^{\\mathrm{old}}}.\n", "$$" ] }, { "cell_type": "markdown", "id": "9bbd1fdc", "metadata": { "editable": true }, "source": [ "The right-hand side is computed with the old values of $\\beta$. \n", "\n", "If we can compute these matrices, in particular the Hessian, the above is often the easiest method to implement. \n", "\n", "Let us quickly remind ourselves how we derive the above method.\n", "\n", "Perhaps the most celebrated of all one-dimensional root-finding\n", "routines is Newton's method, also called the Newton-Raphson\n", "method. This method requires the evaluation of both the\n", "function $f$ and its derivative $f'$ at arbitrary points. \n", "If you can only calculate the derivative\n", "numerically and/or your function is not of the smooth type, we\n", "normally discourage the use of this method.\n", "\n", "The Newton-Raphson formula consists geometrically of extending the\n", "tangent line at a current point until it crosses zero, then setting\n", "the next guess to the abscissa of that zero-crossing. The mathematics\n", "behind this method is rather simple. Employing a Taylor expansion for\n", "$x$ sufficiently close to the solution $s$, we have" ] }, { "cell_type": "markdown", "id": "fa96322d", "metadata": { "editable": true }, "source": [ "\n", "
\n", "\n", "$$\n", "f(s)=0=f(x)+(s-x)f'(x)+\\frac{(s-x)^2}{2}f''(x) +\\dots.\n", " \\label{eq:taylornr} \\tag{1}\n", "$$" ] }, { "cell_type": "markdown", "id": "ca935f27", "metadata": { "editable": true }, "source": [ "For small enough values of the function and for well-behaved\n", "functions, the terms beyond linear are unimportant, hence we obtain" ] }, { "cell_type": "markdown", "id": "2996d958", "metadata": { "editable": true }, "source": [ "$$\n", "f(x)+(s-x)f'(x)\\approx 0,\n", "$$" ] }, { "cell_type": "markdown", "id": "42b78480", "metadata": { "editable": true }, "source": [ "yielding" ] }, { "cell_type": "markdown", "id": "42e0431e", "metadata": { "editable": true }, "source": [ "$$\n", "s\\approx x-\\frac{f(x)}{f'(x)}.\n", "$$" ] }, { "cell_type": "markdown", "id": "5ba17ae7", "metadata": { "editable": true }, "source": [ "Having in mind an iterative procedure, it is natural to start iterating with" ] }, { "cell_type": "markdown", "id": "9136d7a1", "metadata": { "editable": true }, "source": [ "$$\n", "x_{n+1}=x_n-\\frac{f(x_n)}{f'(x_n)}.\n", "$$" ] }, { "cell_type": "markdown", "id": "a67fa215", "metadata": { "editable": true }, "source": [ "The above is Newton-Raphson's method. It has a simple geometric\n", "interpretation, namely $x_{n+1}$ is the point where the tangent from\n", "$(x_n,f(x_n))$ crosses the $x$-axis. Close to the solution,\n", "Newton-Raphson converges fast to the desired result. However, if we\n", "are far from a root, where the higher-order terms in the series are\n", "important, the Newton-Raphson formula can give grossly inaccurate\n", "results. For instance, the initial guess for the root might be so far\n", "from the true root as to let the search interval include a local\n", "maximum or minimum of the function. If an iteration places a trial\n", "guess near such a local extremum, so that the first derivative nearly\n", "vanishes, then Newton-Raphson may fail totally\n", "\n", "Newton's method can be generalized to systems of several non-linear equations\n", "and variables. Consider the case with two equations" ] }, { "cell_type": "markdown", "id": "f4f7b38b", "metadata": { "editable": true }, "source": [ "$$\n", "\\begin{array}{cc} f_1(x_1,x_2) &=0\\\\\n", " f_2(x_1,x_2) &=0,\\end{array}\n", "$$" ] }, { "cell_type": "markdown", "id": "5d0fd8da", "metadata": { "editable": true }, "source": [ "which we Taylor expand to obtain" ] }, { "cell_type": "markdown", "id": "9c370ae4", "metadata": { "editable": true }, "source": [ "$$\n", "\\begin{array}{cc} 0=f_1(x_1+h_1,x_2+h_2)=&f_1(x_1,x_2)+h_1\n", " \\partial f_1/\\partial x_1+h_2\n", " \\partial f_1/\\partial x_2+\\dots\\\\\n", " 0=f_2(x_1+h_1,x_2+h_2)=&f_2(x_1,x_2)+h_1\n", " \\partial f_2/\\partial x_1+h_2\n", " \\partial f_2/\\partial x_2+\\dots\n", " \\end{array}.\n", "$$" ] }, { "cell_type": "markdown", "id": "c0901da4", "metadata": { "editable": true }, "source": [ "Defining the Jacobian matrix $\\boldsymbol{J}$ we have" ] }, { "cell_type": "markdown", "id": "f8a7dc72", "metadata": { "editable": true }, "source": [ "$$\n", "\\boldsymbol{J}=\\left( \\begin{array}{cc}\n", " \\partial f_1/\\partial x_1 & \\partial f_1/\\partial x_2 \\\\\n", " \\partial f_2/\\partial x_1 &\\partial f_2/\\partial x_2\n", " \\end{array} \\right),\n", "$$" ] }, { "cell_type": "markdown", "id": "648a3018", "metadata": { "editable": true }, "source": [ "we can rephrase Newton's method as" ] }, { "cell_type": "markdown", "id": "ac8ceb0d", "metadata": { "editable": true }, "source": [ "$$\n", "\\left(\\begin{array}{c} x_1^{n+1} \\\\ x_2^{n+1} \\end{array} \\right)=\n", "\\left(\\begin{array}{c} x_1^{n} \\\\ x_2^{n} \\end{array} \\right)+\n", "\\left(\\begin{array}{c} h_1^{n} \\\\ h_2^{n} \\end{array} \\right),\n", "$$" ] }, { "cell_type": "markdown", "id": "100f996f", "metadata": { "editable": true }, "source": [ "where we have defined" ] }, { "cell_type": "markdown", "id": "ac1c137e", "metadata": { "editable": true }, "source": [ "$$\n", "\\left(\\begin{array}{c} h_1^{n} \\\\ h_2^{n} \\end{array} \\right)=\n", " -\\boldsymbol{J}^{-1}\n", " \\left(\\begin{array}{c} f_1(x_1^{n},x_2^{n}) \\\\ f_2(x_1^{n},x_2^{n}) \\end{array} \\right).\n", "$$" ] }, { "cell_type": "markdown", "id": "8d67266e", "metadata": { "editable": true }, "source": [ "We need thus to compute the inverse of the Jacobian matrix and it\n", "is to understand that difficulties may\n", "arise in case $\\boldsymbol{J}$ is nearly singular.\n", "\n", "It is rather straightforward to extend the above scheme to systems of\n", "more than two non-linear equations. In our case, the Jacobian matrix is given by the Hessian that represents the second derivative of cost function." ] }, { "cell_type": "markdown", "id": "7e0fff92", "metadata": { "editable": true }, "source": [ "## Steepest descent\n", "\n", "The basic idea of gradient descent is\n", "that a function $F(\\mathbf{x})$, \n", "$\\mathbf{x} \\equiv (x_1,\\cdots,x_n)$, decreases fastest if one goes from $\\bf {x}$ in the\n", "direction of the negative gradient $-\\nabla F(\\mathbf{x})$.\n", "\n", "It can be shown that if" ] }, { "cell_type": "markdown", "id": "0c457787", "metadata": { "editable": true }, "source": [ "$$\n", "\\mathbf{x}_{k+1} = \\mathbf{x}_k - \\gamma_k \\nabla F(\\mathbf{x}_k),\n", "$$" ] }, { "cell_type": "markdown", "id": "c72f27f1", "metadata": { "editable": true }, "source": [ "with $\\gamma_k > 0$.\n", "\n", "For $\\gamma_k$ small enough, then $F(\\mathbf{x}_{k+1}) \\leq\n", "F(\\mathbf{x}_k)$. This means that for a sufficiently small $\\gamma_k$\n", "we are always moving towards smaller function values, i.e a minimum.\n", "\n", "The previous observation is the basis of the method of steepest\n", "descent, which is also referred to as just gradient descent (GD). One\n", "starts with an initial guess $\\mathbf{x}_0$ for a minimum of $F$ and\n", "computes new approximations according to" ] }, { "cell_type": "markdown", "id": "e25ec34a", "metadata": { "editable": true }, "source": [ "$$\n", "\\mathbf{x}_{k+1} = \\mathbf{x}_k - \\gamma_k \\nabla F(\\mathbf{x}_k), \\ \\ k \\geq 0.\n", "$$" ] }, { "cell_type": "markdown", "id": "9a43f87d", "metadata": { "editable": true }, "source": [ "The parameter $\\gamma_k$ is often referred to as the step length or\n", "the learning rate within the context of Machine Learning.\n", "\n", "Ideally the sequence $\\{\\mathbf{x}_k \\}_{k=0}$ converges to a global\n", "minimum of the function $F$. In general we do not know if we are in a\n", "global or local minimum. In the special case when $F$ is a convex\n", "function, all local minima are also global minima, so in this case\n", "gradient descent can converge to the global solution. The advantage of\n", "this scheme is that it is conceptually simple and straightforward to\n", "implement. However the method in this form has some severe\n", "limitations:\n", "\n", "In machine learing we are often faced with non-convex high dimensional\n", "cost functions with many local minima. Since GD is deterministic we\n", "will get stuck in a local minimum, if the method converges, unless we\n", "have a very good intial guess. This also implies that the scheme is\n", "sensitive to the chosen initial condition.\n", "\n", "Note that the gradient is a function of $\\mathbf{x} =\n", "(x_1,\\cdots,x_n)$ which makes it expensive to compute numerically.\n", "\n", "The gradient descent method \n", "is sensitive to the choice of learning rate $\\gamma_k$. This is due\n", "to the fact that we are only guaranteed that $F(\\mathbf{x}_{k+1}) \\leq\n", "F(\\mathbf{x}_k)$ for sufficiently small $\\gamma_k$. The problem is to\n", "determine an optimal learning rate. If the learning rate is chosen too\n", "small the method will take a long time to converge and if it is too\n", "large we can experience erratic behavior.\n", "\n", "Many of these shortcomings can be alleviated by introducing\n", "randomness. One such method is that of Stochastic Gradient Descent\n", "(SGD), see below." ] }, { "cell_type": "markdown", "id": "c271932e", "metadata": { "editable": true }, "source": [ "## Convex functions\n", "\n", "Ideally we want our cost/loss function to be convex(concave).\n", "\n", "First we give the definition of a convex set: A set $C$ in\n", "$\\mathbb{R}^n$ is said to be convex if, for all $x$ and $y$ in $C$ and\n", "all $t \\in (0,1)$ , the point $(1 − t)x + ty$ also belongs to\n", "C. Geometrically this means that every point on the line segment\n", "connecting $x$ and $y$ is in $C$ as discussed below.\n", "\n", "The convex subsets of $\\mathbb{R}$ are the intervals of\n", "$\\mathbb{R}$. Examples of convex sets of $\\mathbb{R}^2$ are the\n", "regular polygons (triangles, rectangles, pentagons, etc...).\n", "\n", "**Convex function**: Let $X \\subset \\mathbb{R}^n$ be a convex\n", "set. Assume that the function $f: X \\rightarrow \\mathbb{R}$ is\n", "continuous, then $f$ is said to be convex if\n", "$f(tx_1 + (1-t)x_2) \\leq tf(x_1) + (1-t)f(x_2)$\n", "for all\n", "$x_1, x_2 \\in X$ and for all $t \\in [0,1]$.\n", "\n", "If $\\leq$ is replaced with a strict inequality in the\n", "definition, we demand $x_1 \\neq x_2$ and $t\\in(0,1)$ then $f$ is said\n", "to be strictly convex. For a single variable function, convexity means\n", "that if you draw a straight line connecting $f(x_1)$ and $f(x_2)$, the\n", "value of the function on the interval $[x_1,x_2]$ is always below the\n", "line as discussed below.\n", "\n", "In the following we state first and second-order conditions which\n", "ensures convexity of a function $f$. We write $D_f$ to denote the\n", "domain of $f$, i.e the subset of $R^n$ where $f$ is defined. For more\n", "details and proofs we refer to: [S. Boyd and L. Vandenberghe. Convex Optimization. Cambridge University Press](http://stanford.edu/boyd/cvxbook/, 2004).\n", "\n", "**First order condition**: Suppose $f$ is differentiable (i.e $\\nabla f(x)$ is well defined for\n", "all $x$ in the domain of $f$). Then $f$ is convex if and only if $D_f$\n", "is a convex set and $f(y) \\geq f(x) + \\nabla f(x)^T (y-x)$ holds\n", "for all $x,y \\in D_f$. This condition means that for a convex function\n", "the first order Taylor expansion (right hand side above) at any point\n", "is a global under estimator of the function. To convince yourself you can\n", "make a drawing of $f(x) = x^2+1$ and draw the tangent line to $f(x)$ and\n", "note that it is always below the graph. \n", "\n", "**Second order condition**: Assume that $f$ is twice\n", "differentiable, i.e the Hessian matrix exists at each point in\n", "$D_f$. Then $f$ is convex if and only if $D_f$ is a convex set and its\n", "Hessian is positive semi-definite for all $x\\in D_f$. For a\n", "single-variable function this reduces to $f''(x) \\geq 0$. Geometrically this means that $f$ has nonnegative curvature\n", "everywhere.\n", "\n", "This condition is particularly useful since it gives us an procedure for determining if the function under consideration is convex, apart from using the definition.\n", "\n", "The next result is of great importance to us and the reason why we are\n", "going on about convex functions. In machine learning we frequently\n", "have to minimize a loss/cost function in order to find the best\n", "parameters for the model we are considering. \n", "\n", "Ideally we want the\n", "global minimum (for high-dimensional models it is hard to know\n", "if we have local or global minimum). However, if the cost/loss function\n", "is convex the following result provides invaluable information:\n", "\n", "**Any minimum is global for convex functions.**\n", "\n", "Consider the problem of finding $x \\in \\mathbb{R}^n$ such that $f(x)$\n", "is minimal, where $f$ is convex and differentiable. Then, any point\n", "$x^*$ that satisfies $\\nabla f(x^*) = 0$ is a global minimum.\n", "\n", "This result means that if we know that the cost/loss function is convex and we are able to find a minimum, we are guaranteed that it is a global minimum." ] }, { "cell_type": "markdown", "id": "87f52dcb", "metadata": { "editable": true }, "source": [ "### Some simple problems\n", "\n", "1. Show that $f(x)=x^2$ is convex for $x \\in \\mathbb{R}$ using the definition of convexity. Hint: If you re-write the definition, $f$ is convex if the following holds for all $x,y \\in D_f$ and any $\\lambda \\in [0,1]$ $\\lambda f(x)+(1-\\lambda)f(y)-f(\\lambda x + (1-\\lambda) y ) \\geq 0$.\n", "\n", "2. Using the second order condition show that the following functions are convex on the specified domain.\n", "\n", " * $f(x) = e^x$ is convex for $x \\in \\mathbb{R}$.\n", "\n", " * $g(x) = -\\ln(x)$ is convex for $x \\in (0,\\infty)$.\n", "\n", "3. Let $f(x) = x^2$ and $g(x) = e^x$. Show that $f(g(x))$ and $g(f(x))$ is convex for $x \\in \\mathbb{R}$. Also show that if $f(x)$ is any convex function than $h(x) = e^{f(x)}$ is convex.\n", "\n", "4. A norm is any function that satisfy the following properties\n", "\n", " * $f(\\alpha x) = |\\alpha| f(x)$ for all $\\alpha \\in \\mathbb{R}$.\n", "\n", " * $f(x+y) \\leq f(x) + f(y)$\n", "\n", " * $f(x) \\leq 0$ for all $x \\in \\mathbb{R}^n$ with equality if and only if $x = 0$\n", "\n", "Using the definition of convexity, try to show that a function satisfying the properties above is convex (the third condition is not needed to show this)." ] }, { "cell_type": "markdown", "id": "5add3827", "metadata": { "editable": true }, "source": [ "## Standard steepest descent\n", "\n", "Before we proceed, we would like to discuss the approach called the\n", "**standard Steepest descent** (different from the above steepest descent discussion), which again leads to us having to be able\n", "to compute a matrix. It belongs to the class of Conjugate Gradient methods (CG).\n", "\n", "[The success of the CG method](https://www.cs.cmu.edu/~quake-papers/painless-conjugate-gradient.pdf)\n", "for finding solutions of non-linear problems is based on the theory\n", "of conjugate gradients for linear systems of equations. It belongs to\n", "the class of iterative methods for solving problems from linear\n", "algebra of the type" ] }, { "cell_type": "markdown", "id": "d54a453e", "metadata": { "editable": true }, "source": [ "$$\n", "\\boldsymbol{A}\\boldsymbol{x} = \\boldsymbol{b}.\n", "$$" ] }, { "cell_type": "markdown", "id": "b1b84a57", "metadata": { "editable": true }, "source": [ "In the iterative process we end up with a problem like" ] }, { "cell_type": "markdown", "id": "880db06e", "metadata": { "editable": true }, "source": [ "$$\n", "\\boldsymbol{r}= \\boldsymbol{b}-\\boldsymbol{A}\\boldsymbol{x},\n", "$$" ] }, { "cell_type": "markdown", "id": "d2cc7ff0", "metadata": { "editable": true }, "source": [ "where $\\boldsymbol{r}$ is the so-called residual or error in the iterative process.\n", "\n", "When we have found the exact solution, $\\boldsymbol{r}=0$.\n", "\n", "The residual is zero when we reach the minimum of the quadratic equation" ] }, { "cell_type": "markdown", "id": "8dc530e6", "metadata": { "editable": true }, "source": [ "$$\n", "P(\\boldsymbol{x})=\\frac{1}{2}\\boldsymbol{x}^T\\boldsymbol{A}\\boldsymbol{x} - \\boldsymbol{x}^T\\boldsymbol{b},\n", "$$" ] }, { "cell_type": "markdown", "id": "813d3073", "metadata": { "editable": true }, "source": [ "with the constraint that the matrix $\\boldsymbol{A}$ is positive definite and\n", "symmetric. This defines also the Hessian and we want it to be positive definite. \n", "\n", "We denote the initial guess for $\\boldsymbol{x}$ as $\\boldsymbol{x}_0$. \n", "We can assume without loss of generality that" ] }, { "cell_type": "markdown", "id": "3d0d9d18", "metadata": { "editable": true }, "source": [ "$$\n", "\\boldsymbol{x}_0=0,\n", "$$" ] }, { "cell_type": "markdown", "id": "fe5d0507", "metadata": { "editable": true }, "source": [ "or consider the system" ] }, { "cell_type": "markdown", "id": "bfabfe26", "metadata": { "editable": true }, "source": [ "$$\n", "\\boldsymbol{A}\\boldsymbol{z} = \\boldsymbol{b}-\\boldsymbol{A}\\boldsymbol{x}_0,\n", "$$" ] }, { "cell_type": "markdown", "id": "27b361b6", "metadata": { "editable": true }, "source": [ "instead.\n", "\n", "One can show that the solution $\\boldsymbol{x}$ is also the unique minimizer of the quadratic form" ] }, { "cell_type": "markdown", "id": "402e450f", "metadata": { "editable": true }, "source": [ "$$\n", "f(\\boldsymbol{x}) = \\frac{1}{2}\\boldsymbol{x}^T\\boldsymbol{A}\\boldsymbol{x} - \\boldsymbol{x}^T \\boldsymbol{x} , \\quad \\boldsymbol{x}\\in\\mathbf{R}^n.\n", "$$" ] }, { "cell_type": "markdown", "id": "fa3d7c41", "metadata": { "editable": true }, "source": [ "This suggests taking the first basis vector $\\boldsymbol{r}_1$ (see below for definition) \n", "to be the gradient of $f$ at $\\boldsymbol{x}=\\boldsymbol{x}_0$, \n", "which equals" ] }, { "cell_type": "markdown", "id": "3e6d73b8", "metadata": { "editable": true }, "source": [ "$$\n", "\\boldsymbol{A}\\boldsymbol{x}_0-\\boldsymbol{b},\n", "$$" ] }, { "cell_type": "markdown", "id": "02c8e436", "metadata": { "editable": true }, "source": [ "and \n", "$\\boldsymbol{x}_0=0$ it is equal $-\\boldsymbol{b}$.\n", "\n", "We can compute the residual iteratively as" ] }, { "cell_type": "markdown", "id": "18100e11", "metadata": { "editable": true }, "source": [ "$$\n", "\\boldsymbol{r}_{k+1}=\\boldsymbol{b}-\\boldsymbol{A}\\boldsymbol{x}_{k+1},\n", "$$" ] }, { "cell_type": "markdown", "id": "ac2290e7", "metadata": { "editable": true }, "source": [ "which equals" ] }, { "cell_type": "markdown", "id": "33b872cd", "metadata": { "editable": true }, "source": [ "$$\n", "\\boldsymbol{b}-\\boldsymbol{A}(\\boldsymbol{x}_k+\\alpha_k\\boldsymbol{r}_k),\n", "$$" ] }, { "cell_type": "markdown", "id": "b0ef2463", "metadata": { "editable": true }, "source": [ "or" ] }, { "cell_type": "markdown", "id": "9bdb71be", "metadata": { "editable": true }, "source": [ "$$\n", "(\\boldsymbol{b}-\\boldsymbol{A}\\boldsymbol{x}_k)-\\alpha_k\\boldsymbol{A}\\boldsymbol{r}_k,\n", "$$" ] }, { "cell_type": "markdown", "id": "8b526371", "metadata": { "editable": true }, "source": [ "which gives" ] }, { "cell_type": "markdown", "id": "f2304289", "metadata": { "editable": true }, "source": [ "$$\n", "\\alpha_k = \\frac{\\boldsymbol{r}_k^T\\boldsymbol{r}_k}{\\boldsymbol{r}_k^T\\boldsymbol{A}\\boldsymbol{r}_k}\n", "$$" ] }, { "cell_type": "markdown", "id": "4b00e41e", "metadata": { "editable": true }, "source": [ "leading to the iterative scheme" ] }, { "cell_type": "markdown", "id": "0e67a647", "metadata": { "editable": true }, "source": [ "$$\n", "\\boldsymbol{x}_{k+1}=\\boldsymbol{x}_k-\\alpha_k\\boldsymbol{r}_{k},\n", "$$" ] }, { "cell_type": "code", "execution_count": 1, "id": "f96e45cc", "metadata": { "collapsed": false, "editable": true }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/td/3yk470mj5p931p9dtkk0y6jw0000gn/T/ipykernel_9414/483257001.py:18: MatplotlibDeprecationWarning: Calling gca() with keyword arguments was deprecated in Matplotlib 3.4. Starting two minor releases later, gca() will take no keyword arguments. The gca() function should only be used to get the current axes, or if no axes exist, create new axes with default keyword arguments. To create a new axes with non-default arguments, use plt.axes() or plt.subplot().\n", " ax = fig.gca(projection=\"3d\")\n" ] }, { "data": { "text/plain": [ "