{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Issues with the normal equations"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import numpy.linalg as la"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Here's an example matrix to use with the normal equations:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[[ 1.                      1.                    ]\n",
            " [ 0.01000000000000000021  0.                    ]\n",
            " [ 0.                      0.01000000000000000021]]\n",
            "[[ 1.00009999999999998899  1.                    ]\n",
            " [ 1.                      1.00009999999999998899]]\n"
          ]
        }
      ],
      "source": [
        "eps = 1e-2  # set to 1e-5, 1e-10\n",
        "\n",
        "A = np.array([\n",
        "        [1, 1],\n",
        "        [eps, 0],\n",
        "        [0, eps],\n",
        "        ])\n",
        "\n",
        "np.set_printoptions(precision=20)\n",
        "print(A)\n",
        "print(A.T @ A)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "* What do you notice about the entries of $A^T A$?"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "60358.505352514032"
            ]
          },
          "execution_count": 3,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "n = 5\n",
        "\n",
        "A = np.random.randn(5, 5) * 10**-np.linspace(0, -5, n)\n",
        "la.cond(A)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 4,
      "metadata": {
        "collapsed": false
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "3643149168.4817424"
            ]
          },
          "execution_count": 4,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "la.cond(np.dot(A.T, A))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "* What do you notice about the condition number?\n",
        "* What's a general bound? $\\operatorname{cond}(AB)\\le \\dots$?"
      ]
    }
  ],
  "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.5.1+"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}