{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"Some more details on Floating point arithmetic discussed.\n",
"\n",
"List of disasters due to bad numerical computing - I\n",
"\n",
"List of disasters due to bad numerical computing - II\n",
"$\\newcommand{\\magn}[1]{\\lVert#1\\rVert}$\n",
"$\\newcommand{\\abs}[1]{\\lvert#1\\rvert}$\n",
"$\\newcommand{\\Rb}{\\mathbb{R}}$\n",
"$\\newcommand{\\bkt}[1]{\\left(#1\\right)}$\n",
"\n",
"### Condition number\n",
"\n",
"In almost all applications, we are interested in obtaining an output $f(x)$ for a given input $x$. However, there are inherent uncertainties in the input data $x$. These uncertainties could arise not only because of our inability to measure the input precisely but also due to the fact that numbers need not be represented exactly on the machine (as seen in the previous section). Hence, it is vital to understand how the solution to a problem gets affected by small perturbations in the input data. A given problem is said to be ***well-conditioned*** if \"small\" perturbations in $x$ result in \"small\" changes in $f(x)$. An ***ill-conditioned*** problem is one where \"small\" perturbations in $x$ lead to a \"large\" change in $f(x)$. The notion of \"small\" and \"large\" often depends on the problem and application of interest.\n",
"\n",
"Conditioning quantifies how small/large the change $\\delta f$ in output is for a $\\delta x$ perturbation of the input.\n",
"\n",
"\n",
"#### Absolute condition number\n",
"\n",
"One way to measure conditioning\" of a problem is as follows. Let $\\delta x$ denote a small perturbation of $x$ (the input) and let $\\delta f = f(x+\\delta x) - f(x)$ be the corresponding change in the output. The \\emph{absolute condition number} $\\hat{\\kappa}\\bkt{x,f}$ of the problem $f$ at $x$ is defined as\n",
"$$\\hat{\\kappa} = \\lim_{\\delta \\to 0} \\sup_{\\magn{\\delta x} = \\delta} \\dfrac{\\magn{\\delta f}}{\\magn{\\delta x}}$$\n",
"Note that if $f$ is differentiable at $x$, and $J(x)$ is the Jacobian of $f(x)$ at $x$, we obtain that\n",
"$$\\hat{\\kappa} = \\magn{J(x)}$$\n",
"\n",
"#### Relative condition number\n",
"Note that since the input ($x$) and the output ($f(x)$) are on different spaces, a more appropriate measure of conditioning is to measure the changes in the input and output in terms of relative changes. The \\emph{relative condition number} $\\kappa\\bkt{x,f}$ of the problem $f$ at $x$ is defined as\n",
"$$\\kappa = \\lim_{\\delta \\to 0} \\sup_{\\magn{\\delta x} = \\delta} \\bkt{\\dfrac{\\magn{\\delta f}}{\\magn{f}} \\bigg/ \\dfrac{\\magn{\\delta x}}{\\magn{x}}}$$\n",
"As before, if $f$ is differentiable at $x$, we can express this in terms of the Jacobian $J(x)$ as\n",
"$$\\kappa = \\dfrac{\\magn{J(x)}}{\\magn{f(x)}/\\magn{x}}$$\n",
"Even though both the above notions have their uses, relative condition number is more appropriate since as we saw earlier, floating point arithmetic introduces only relative errors.\n",
"\n",
"#### Conditioning of subtraction\n",
"Consider **subtracting two positive numbers**, i.e., $f(a,b) = a-b$. If we perturb $a$ by $a+\\delta_a$ and $b$ by $b+\\delta_b$, we have the condition number in $2$-norm to be\n",
"$$\\kappa(f,a,b) = \\lim_{r \\to 0} \\sup_{\\magn{\\delta}_2 = r} \\dfrac{\\abs{\\delta_a-\\delta_b}/\\abs{a-b}}{\\sqrt{\\delta_a^2+\\delta_b^2}/\\sqrt{a^2+b^2}} = \\lim_{r \\to 0} \\sup_{\\magn{\\delta}_2 = r} \\dfrac{\\abs{\\delta_a-\\delta_b}/\\abs{a-b}}{r/\\sqrt{a^2+b^2}} = \\dfrac{\\sqrt{2}\\sqrt{a^2+b^2}}{\\abs{a-b}}$$\n",
"Hence, we see that for large values of $a$ and $b$ such that $a-b$ is small (i.e., $a$ is close to $b$), the problem is ill-conditioned.\n",
"\n",
"#### Conditioning of solving for roots of polynomials\n",
"Consider finding the **roots of the polynomial $ax^2+bx+c$**. Here the function $f: \\Rb^3 \\mapsto \\Rb^2$, where $f(a,b,c) = \\begin{bmatrix} r_1 & r_2 \\end{bmatrix}$, where $r_1,r_2$ are the roots of $ax^2+bx+c$. Now let's look at the condition at $(a,b,c) = (1,-2,1)$. The roots are $1,1$. Let's perturb the $2$ by $\\delta$. We have\n",
"\\begin{align*}\n",
"\t\\kappa & = \\lim_{\\delta \\to 0} \\dfrac{\\magn{f(1,-(2+\\delta),1)-f(1,-2,1)}/\\magn{f(1,-2,1)}}{\\magn{(1,-(2+\\delta),1)-(1,-2,1)}/\\magn{(1,-2,1)}} = \\dfrac{\\magn{(1,-2,1)}}{\\magn{f(1,-2,1)}}\\lim_{\\delta \\to 0} \\dfrac{\\magn{\\dfrac{\\delta+\\sqrt{\\delta^2+4\\delta}}2,\\dfrac{\\delta-\\sqrt{\\delta^2+4\\delta}}2}}{\\delta}\\\\\n",
"\t& = \\dfrac{\\sqrt6}{2\\sqrt2} \\lim_{\\delta \\to 0} \\dfrac{\\sqrt{2\\delta^2+2\\delta^2+8\\delta}}{\\delta} = \\sqrt3 \\lim_{\\delta \\to 0} \\dfrac{\\sqrt{\\delta^2+2\\delta}}{\\delta} = \\sqrt3 \\lim_{\\delta \\to 0} \\sqrt{1+2/\\delta} = \\infty\n",
"\\end{align*}\n",
"\t\t\t\n",
"#### Conditioning of matrix-vector products\n",
"\n",
"We have $f(x) = Ax$. The Jacobian is nothing but the matrix $A$. Hence, we have\n",
"$$\\kappa\\bkt{x,Ax} = \\dfrac{\\magn{A}\\magn{x}}{\\magn{Ax}}$$\n",
"Note that\n",
"$$\\magn{x} = \\magn{A^{-1}\\bkt{Ax}} \\leq \\magn{A^{-1}} \\magn{Ax}$$\n",
"Hence, we obtain that\n",
"$$\\kappa\\bkt{x,Ax} = \\dfrac{\\magn{A}\\magn{x}}{\\magn{Ax}} \\leq \\dfrac{\\magn{A}\\magn{A^{-1}} \\magn{Ax}}{\\magn{Ax}} = \\magn{A} \\magn{A^{-1}}$$\n",
"where the bound is independent of $x$. Hence, $\\magn{A} \\magn{A^{-1}}$ is called as the condition number of the matrix $A$ and is denoted as $\\kappa(A)$.\n",
"\n",
"#### Conditioning of a system of equations\n",
"We are interested in solving the linear system $Ax=b$. In this case, we have $f(b) = A^{-1}b$. The Jacobian of $f(b)$ is nothing but the matrix $A^{-1}$. Hence, we have\n",
"$$\\kappa\\bkt{b,x} = \\dfrac{\\magn{A^{-1}}\\magn{b}}{\\magn{A^{-1}b}} = \\dfrac{\\magn{A^{-1}}\\magn{A \\bkt{A^{-1}b}}}{\\magn{A^{-1}b}} \\leq \\dfrac{\\magn{A^{-1}}\\magn{A} \\magn{A^{-1}b}}{\\magn{A^{-1}b}} = \\kappa(A)$$"
]
}
],
"metadata": {
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}