{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from numpy.linalg import inv, qr"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "x1s = [     3,      2,      4,      2,      3,      2,      5,      4]  # bedrooms\n",
    "x2s = [     2,      1,      3,      1,      2,      2,      3,      2]  # bathrooms\n",
    "ys  = [48_800, 44_300, 53_800, 44_200, 49_700, 44_900, 58_400, 52_900]  # price\n",
    "\n",
    "x1 = np.array(x1s)\n",
    "x2 = np.array(x2s)\n",
    "y  = np.array(ys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3, 2],\n",
       "       [1, 2, 1],\n",
       "       [1, 4, 3],\n",
       "       [1, 2, 1],\n",
       "       [1, 3, 2],\n",
       "       [1, 2, 2],\n",
       "       [1, 5, 3],\n",
       "       [1, 4, 2]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ones = np.array([1, 1, 1, 1, 1, 1, 1, 1])\n",
    "A = np.array([ones, x1, x2]).T\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 3, 2],\n",
       "       [1, 2, 1],\n",
       "       [1, 4, 3],\n",
       "       [1, 2, 1],\n",
       "       [1, 3, 2],\n",
       "       [1, 2, 2],\n",
       "       [1, 5, 3],\n",
       "       [1, 4, 2]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-2.82842712, -8.83883476, -5.65685425],\n",
       "       [ 0.        ,  2.97909382,  1.67836272],\n",
       "       [ 0.        ,  0.        , -1.08770336]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q,R = qr(A)\n",
    "R"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.35355339, -1.0489767 ,  0.22013044],\n",
       "       [ 0.        ,  0.33567254,  0.51795398],\n",
       "       [-0.        , -0.        , -0.91936831]])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Rinv = inv(R)\n",
    "Rinv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.35355339, -0.04195907, -0.06474425],\n",
       "       [-0.35355339, -0.37763161,  0.33667009],\n",
       "       [-0.35355339,  0.29371348, -0.46615858],\n",
       "       [-0.35355339, -0.37763161,  0.33667009],\n",
       "       [-0.35355339, -0.04195907, -0.06474425],\n",
       "       [-0.35355339, -0.37763161, -0.58269823],\n",
       "       [-0.35355339,  0.62938602,  0.0517954 ],\n",
       "       [-0.35355339,  0.29371348,  0.45320973]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-0.35355339, -0.35355339, -0.35355339, -0.35355339, -0.35355339,\n",
       "        -0.35355339, -0.35355339, -0.35355339],\n",
       "       [-0.04195907, -0.37763161,  0.29371348, -0.37763161, -0.04195907,\n",
       "        -0.37763161,  0.62938602,  0.29371348],\n",
       "       [-0.06474425,  0.33667009, -0.46615858,  0.33667009, -0.06474425,\n",
       "        -0.58269823,  0.0517954 ,  0.45320973]])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "QT = Q.T\n",
    "QT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(0.9999999999999999, 1.0, 1.0)"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from numpy.linalg import norm\n",
    "\n",
    "row0 = QT[0]\n",
    "row1 = QT[1]\n",
    "row2 = QT[2]\n",
    "\n",
    "norm(row0), norm(row1), norm(row2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(5.551115123125783e-17, 2.7755575615628914e-17, 8.326672684688674e-17)"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "row0@row1, row0@row2, row1@row2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.00000000e+00, -7.87216087e-18,  0.00000000e+00],\n",
       "       [-7.87216087e-18,  1.00000000e+00,  8.32667268e-17],\n",
       "       [ 0.00000000e+00,  8.32667268e-17,  1.00000000e+00]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "QT@Q"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "ys  = [48_800, 44_300, 53_800, 44_200, 49_700, 44_900, 58_400, 52_900]  # price\n",
    "b = np.array(ys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([48800, 44300, 53800, 44200, 49700, 44900, 58400, 52900])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([35191.66666667,  4133.33333333,   758.33333333])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "xhat = Rinv@QT@b\n",
    "xhat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "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.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
