{ "cells": [ { "cell_type": "markdown", "id": "c57b2ce5", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Introduction to Optimization\n", "\n", "\n", "Feng Li\n", "\n", "School of Statistics and Mathematics\n", "\n", "Central University of Finance and Economics\n", "\n", "[feng.li@cufe.edu.cn](mailto:feng.li@cufe.edu.cn)\n", "\n", "[https://feng.li/statcomp](https://feng.li/statcomp)\n" ] }, { "cell_type": "code", "execution_count": 3, "id": "c085cebf", "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "options(jupyter.plot_scale=0.8)" ] }, { "cell_type": "markdown", "id": "b31a0a73", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Motivation: Why Optimize?\n", "\n", "### Optimization in Business\n", "\n", "- Many problems in business require something to be minimized or\n", " maximized\n", "\n", " - Maximizing Revenue\n", "\n", " - Minimizing Costs\n", "\n", " - Minimizing Delivery Time\n", "\n", " - Maximizing Financial Returns" ] }, { "cell_type": "markdown", "id": "2a3de9fc", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Input and output\n", "\n", "- For many of these problems there is some control over the input\n", "\n", " - Maximizing Revenue - Price\n", "\n", " - Minimizing Costs - Number of Workers\n", "\n", " - Minimizing Delivery Time - Driving Route\n", "\n", " - Maximizing Financial Returns - Portfolio weights" ] }, { "cell_type": "markdown", "id": "7f11faed", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Optimization in Statistics\n", "\n", "- In statistics, many estimators maximize or minimize a function\n", "\n", " - Maximum Likelihood\n", "\n", " - Least Squares\n", "\n", " - Method of Moments\n", "\n", " - Posterior Mode" ] }, { "cell_type": "markdown", "id": "93738c5b", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- Suppose we want to find an minimum or maximum of a function $f(x)$\n", "\n", "- Sometimes $f(x)$ will be very complicated\n", "\n", "- Are there computer algorithms that can help?\n", "\n", "- YES!\n", "\n", " - Newton’s Method\n", "\n", " - Quasi-Newton\n", "\n", " - Nelder Mead\n" ] }, { "cell_type": "markdown", "id": "09dd92f9", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Newton’s Method\n", "\n", "### Finding a root\n", "\n", "\n", "- Consider the problem of finding the **root** or **zero** a function.\n", "\n", "- For the function $g(x)$ the **root** is the point $x^*$ such that\n", " $g(x^*)=0$\n", "\n", "- An algorithm for solving this problem was proposed by Newton and\n", " Raphson nearly 500 years ago.\n", "\n", "- We will use this algorithm to find the root of\n", " $g(x)=3x^5-4x^4+6x^3+4x-4$\n", "\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "d08f0aea", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [], "source": [ "gx<-function(x){\n", " y=(3*(x^5))-(4*(x^4))+(6*(x^3))+(4*x)-4\n", " return(y) \n", "}\n", "\n", "dgx<-function(x){\n", " y=(15*(x^4))-(16*(x^3))+(18*(x^2))+4\n", " return(y) \n", "}" ] }, { "cell_type": "code", "execution_count": 12, "id": "67b3430b", "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAAeAAAAFoCAMAAAC46dgSAAAC/VBMVEUAAAABAQECAgIDAwME\nBAQFBQUGBgYHBwcICAgJCQkKCgoLCwsMDAwNDQ0ODg4PDw8QEBARERESEhITExMUFBQVFRUW\nFhYXFxcYGBgZGRkaGhobGxscHBwdHR0eHh4fHx8gICAhISEiIiIjIyMkJCQlJSUmJiYnJyco\nKCgpKSkqKiorKyssLCwtLS0uLi4vLy8wMDAxMTEyMjIzMzM0NDQ1NTU2NjY3Nzc4ODg5OTk6\nOjo7Ozs8PDw9PT0+Pj4/Pz9AQEBBQUFCQkJDQ0NERERFRUVGRkZHR0dISEhJSUlKSkpLS0tM\nTExNTU1OTk5PT09QUFBRUVFSUlJTU1NUVFRVVVVWVlZXV1dYWFhZWVlaWlpbW1tcXFxdXV1e\nXl5fX19gYGBhYWFiYmJjY2NkZGRlZWVmZmZnZ2doa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YS4JzHBb2UN++oVyachj8LAx/hMd5LIxb\nVIxLcxl8WyHe+r4aS4Zf+SyvDZemjGpsHUU34LEwxisqxqW5TLlvdD6wvsI33VzBpSkvQ2kq\nxo6u+0lVBjfBpWhqLjMEH2sJH3BaFJemPAn9reNkkEoHpQC+grU1l0GCb74lUPq8/41pQYHp\nnJbFpynPwADrOBn+5LA0xlew1uYySPBZMUWM9T21DU2gr7YdsG1ZnJqyxK+rddzJj9OdUo6C\nNTeX0ZvoadD6O46L49OU0bHWUUwjHgtjPAVrby6DBa+AoQXua8mHT1MOg2PCMBOG8VgY4yiY\nQ3MZK9jSqlEe1wXyacpt8DchtiGwg8fCGD/BPJrLWMEnoG7vUi7wWSCnpkyBHlO6Kku0KwUv\nwTyay1jB31Tk5POsExLLm53DOs/hsigRXoJ5NBc9IIscEowcEowcEowcEowcEowcEowcEowc\nEowcEowcEowcEowcEowcEowcEowcEowcEowcEowcEowcEowcEowcEowcEowcEowcEowcEowc\nEowcEowcEowcEowcEowcEowcEowcEowcEowcEowcEowcEsxYpv+9wrCwTeQZsyPRARIsMB2W\nMzYTPjQ7Dj0gwQIFbSLPZwf2MzsMXSDBInt9h/eI4JRL2MMgwVaeBVhpdgz6QIKtZEPwVbNj\n0AcSbKW/P4w1OwZ9IMEiH8L8QT67zI5CF0iwwNk67Yv/GxbPNVW5p0CCBQb6/chYOrxqdhx6\nQIIZW23tELvk7oCjZkeiAyQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQY\nOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQYOSQY\nOf8DPLA9ERlYiSQAAAAASUVORK5CYII=", "text/plain": [ "Plot with title “Root Finding”" ] }, "metadata": { "image/png": { "height": 450, "width": 600 } }, "output_type": "display_data" } ], "source": [ "gridx<-seq(-2,2,0.01)\n", "gridy<-gx(gridx)\n", "plot(gridx,gridy,\"l\",main='Root Finding', ylim= c(-200, 100), \n", " xlab = 'x', ylab=expression(y=3*x^5-4*x^4+6*x^3+4*x-4))\n", "lines(c(-2,2),c(0,0))" ] }, { "cell_type": "markdown", "id": "b24f9dca", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Finding the Tangent\n", "\n", "- To find the tangent evaluate the first derivative of $g(x)$.\n", "\n", "- The function is $$g(x)=3x^5-4x^4+6x^3+4x-4$$\n", "\n", "- The first derivative is $$g'(x)=15x^4-16x^3+18x^2+4$$\n" ] }, { "cell_type": "markdown", "id": "e189bffd", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "![rootfinding](figures/rf7.png)" ] }, { "cell_type": "markdown", "id": "a6158085", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Find the crossing point\n", "\n", "- Find the crossing point From basic Geometry\n", "\n", "$$g'(x_0)=\\frac{g(x_0)}{x_0-x_1}$$\n", "\n", "\n", "- Rearrange\n", "\n", "\\begin{aligned}\n", " x_0-x_1&=\\frac{g(x_0)}{g'(x_0)}\\\\\n", " -x_1&=-x_0+\\frac{g(x_0)}{g'(x_0)}\\\\\n", " x_1&=x_0-\\frac{g(x_0)}{g'(x_0)}\n", " \\end{aligned}" ] }, { "cell_type": "markdown", "id": "8b5b2e43", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Stopping Rule\n", "\n", "- With each step the algorithm should get closer to the root.\n", "\n", "- However, it can run for a long time without reaching the **exact**\n", " root\n", "\n", "- There must be a **stopping rule** otherwise the program could run\n", " forever.\n", "\n", "- Let $\\epsilon$ be an extremely small number e.g. $1\\times 10^{-10}$\n", " called the **tolerance level**\n", "\n", "- If $|g(x^*)|<\\epsilon$ then the solution is close enough and there\n", " is a root at $x^*$" ] }, { "cell_type": "markdown", "id": "644826ac", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Newton-Raphson algorithm for root finding\n", "\n", "1. Select initial value $x_0$ and set $n=0$\n", "\n", "2. Set $x_{n+1}=x_{n}-\\frac{g(x_n)}{g'(x_n)}$\n", "\n", "3. Evaluate $|g(x_{n+1})|$\n", "\n", " - If $|g(x_{n+1})|\\leq\\epsilon$ then stop.\n", "\n", " - Otherwise set $n=n+1$ and go back to step 2." ] }, { "cell_type": "markdown", "id": "2e472147", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Your task: Write R/Python code to find the root\n", "\n", "$g(x)=3x^5-4x^4+6x^3+4x-4$\n", "\n", "Tips:\n", "\n", "- Write functions for $g(x)$ and $g'(x)$ first.\n", "\n", "- These can be inputs into a function that carries out the Newton\n", " Raphson method. Code should be flexible.\n", "\n", "- Use loops!" ] }, { "cell_type": "markdown", "id": "d22e51a2", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Your task: a different target function\n", "\n", "- Now use your Newton-Raphson code to find the root of\n", " $g(x)=\\sqrt{|x|}$\n", "\n", "- The derivative has two parts $$g'(x)=\\left\\{\n", " \\begin{array}{l}\n", " 1/(2\\sqrt{x})\\quad\\mbox{if x>0}\\\\\n", " -1/(2\\sqrt{-x})\\quad\\mbox{if x<0}\n", " \\end{array}\n", " \\right.$$\n", "\n", "- Use 0.25 as the starting value." ] }, { "cell_type": "markdown", "id": "aff0ae58", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Learn from mistakes\n", "\n", "- Newton-Raphson does not always converge\n", "\n", "- Be careful using *while*. Avoid infinite loops.\n", "\n", "- Don’t always assume the answer given by code is correct. Check\n", " carefully!\n", "\n", "- Print warning messages in code" ] }, { "cell_type": "markdown", "id": "ccde220c", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- For some functions, using some certain starting values leads to a\n", " series that **converges**, while other starting values lead to a\n", " series that **diverges**\n", "\n", "- For other functions different starting values converge to different\n", " roots.\n", "\n", "- Be careful when choosing the initial value.\n", "\n", "- Newton-Raphson doesn’t work if the first derivative is zero.\n", "\n", "- When can this happen?" ] }, { "cell_type": "markdown", "id": "b988bb7d", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Rough Proof of Quadratic Convergence\n", "\n", "- Can we prove anything about the rate of convergence for the Newton\n", " Raphson Method?\n", "\n", "- To do so requires the **Taylor Series**\n", "\n", "- Let $f(x)$ have a root at $\\alpha$. The Taylor approximation states\n", " that\n", "\n", " $$f(\\alpha)\\approx f(x_n)+f'(x_n)(\\alpha-x_n)+\\frac{1}{2}f''(x_n)(\\alpha-x_n)^2$$\n", "\n", "- The quality of the approximation depends on the function and how\n", " close $x_n$ is to $\\alpha$\n" ] }, { "cell_type": "markdown", "id": "d6db2a23", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "- Since $\\alpha$ is a root, $f(\\alpha)=0$ This implies\n", "\n", " $$0\\approx f(x_n)+f'(x_n)(\\alpha-x_n)+\\frac{1}{2}f''(x_n)(\\alpha-x_n)^2$$\n", "\n", "- Dividing by $f'(x_n)$ and rearranging gives:\n", " $$\\frac{f(x_n)}{f'(x_n)}+(\\alpha-x_n)\\approx\\frac{-f''(x_n)}{2f'(x_n)}(\\alpha-x_n)^2$$\n", "\n", "- More rearranging\n", " $$\\alpha-\\left(x_n-\\frac{f(x_n)}{f'(x_n)}\\right)\\approx\\frac{-f''(x_n)}{2f'(x_n)}(\\alpha-x_n)^2$$\n", "\n", "- The term in brackets on the left hand side is the formula used to\n", " update $x$ in the Newton Raphson method\n", " $$(\\alpha-x_{n+1})\\approx\\frac{-f''(x_n)}{2f'(x_n)}(\\alpha-x_n)^2$$\n", "\n", "- This can be rewritten in terms of errors $e_{n+1}=\\alpha-x_{n+1}$\n", " and $e_n=\\alpha-x_n$\n", " $$e_{n+1}\\approx\\frac{-f''(x_n)}{2f'(x_n)}e_n^2$$" ] }, { "cell_type": "markdown", "id": "fe95d8ac", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Conclusion\n", "\n", "- Why did we spend so much time on finding roots of an equation?\n", "\n", "- Isn’t this topic meant to be about optimization?\n", "\n", "- Can we change this algorithm slightly so that it works for\n", " optimization?" ] } ], "metadata": { "celltoolbar": "Slideshow", "kernelspec": { "display_name": "R", "language": "R", "name": "ir" }, "language_info": { "codemirror_mode": "r", "file_extension": ".r", "mimetype": "text/x-r-source", "name": "R", "pygments_lexer": "r", "version": "4.3.2" } }, "nbformat": 4, "nbformat_minor": 5 }