{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### <center>San Jose State University<br>Department of Applied Data Science</center>\n",
    "#  <center>DATA 220<br>Mathematical Methods for Data Analysis</center>\n",
    "### <center>Spring 2021<br>Instructor: Ron Mak</center>\n",
    "#  <center>Assignment #11<br>Matrix Operations Problem Set</center>\n",
    "#### <center>100 points total (20 points each)<br><br>Work together with your lab partner.<br>Write your solutions in one or more cells after each problem.<br>Whenever possible, use matrix operations and not loops.<br>You can add your own functions or other support code.</center>\n",
    "#### <center>Due May 6, 2020</center>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### <strong>PROBLEM 1.</strong> Amalgamated Widgets, Inc. manufactures several products using various amounts of materials per product:\n",
    "<table>\n",
    "    <tr>\n",
    "        <th>Product</th>\n",
    "        <th>Metal</th>\n",
    "        <th>Wood</th>\n",
    "        <th>Plastic</th>\n",
    "        <th>Cloth</th>\n",
    "        <th>Paper</th>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>A</td><td>0</td><td>1.3</td><td>0.2</td><td>0.8</td><td>0.4</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>B</td><td>0</td><td>0</td><td>1.5</td><td>0.4</td><td>0.3</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>C</td><td>0.25</td><td>0</td><td>0</td><td>0.2</td><td>0.7</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>D</td><td>0</td><td>0</td><td>0.3</td><td>0.7</td><td>0.5</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>E</td><td>0.15</td><td>0</td><td>0.5</td><td>0.4</td><td>0.8</td>\n",
    "    </tr>\n",
    "</table>\n",
    "\n",
    "#### On a certain day, the following total amounts of materials were used:\n",
    "<table>\n",
    "    <tr>\n",
    "        <th>Metal</th>\n",
    "        <th>Wood</th>\n",
    "        <th>Plastic</th>\n",
    "        <th>Cloth</th>\n",
    "        <th>Paper</th>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>51.0</td><td>312.0</td><td>215.4</td><td>373.1</td><td>356.0</td>\n",
    "    </tr>\n",
    "</table>\n",
    "\n",
    "#### How many of each product were made that day?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<hr>\n",
    "\n",
    "#### <strong>PROBLEM 2.</strong> Find the least-squares regression line for the points (1,1), (2,2), (3,4), (4,4), and (5,6) using the matrix form of the normal equations <em>A.T</em>@<em>A</em>@<strong>x</strong> = <em>A.T</em>@<strong>b</strong>."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<hr>\n",
    "\n",
    "#### <strong>PROBLEM 3.</strong> The following table shows the mean distances <em>x</em> in astonomical units (multiples of the distance between the sun and the earth) and the periods <em>y</em> in years (how many earth-years to orbit the sun):\n",
    "<table>\n",
    "    <tr>\n",
    "        <th>Planet</th>\n",
    "        <th>Mercury</th>\n",
    "        <th>Venus</th>\n",
    "        <th>Earth</th>\n",
    "        <th>Mars</th>\n",
    "        <th>Jupiter</th>\n",
    "        <th>Saturn</th>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>Distance </em>x</em></td><td>0.387</td><td>0.723</td><td>1.000</td><td>1.524</td><td>5.203</td><td>9.537</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>Years </em>y</em></td><td>0.241</td><td>0.615</td><td>1.000</td><td>1.881</td><td>11.862</td><td>29.457</td>\n",
    "    </tr>\n",
    "</table>\n",
    "\n",
    "#### It turns out that if you take the natural logarithm of the numbers in the table, the transformed points lie roughly in a straight line. Find the least-squares regression line using the matrix form of the normal equations <em>A.T</em>@<em>A</em>@<strong>x</strong> = <em>A.T</em>@<strong>b</strong>. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<hr>\n",
    "\n",
    "#### <strong>PROBLEM 4.a.</strong> Here's more data about that certain day at Amalgamated Widgets, Inc. There are three more products and the cost  to make one of each product.\n",
    "<table>\n",
    "    <tr>\n",
    "        <th>Product</th>\n",
    "        <th>Metal</th>\n",
    "        <th>Wood</th>\n",
    "        <th>Plastic</th>\n",
    "        <th>Cloth</th>\n",
    "        <th>Paper</th>\n",
    "        <th>Cost</th>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>A</td><td>0</td><td>1.3</td><td>0.2</td><td>0.8</td><td>0.4</td><td>21.60</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>B</td><td>0</td><td>0</td><td>1.5</td><td>0.4</td><td>0.3</td><td>17.20</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>C</td><td>0.25</td><td>0</td><td>0</td><td>0.2</td><td>0.7</td><td>4.70</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>D</td><td>0</td><td>0</td><td>0.3</td><td>0.7</td><td>0.5</td><td>9.60</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>E</td><td>0.15</td><td>0</td><td>0.5</td><td>0.4</td><td>0.8</td><td>15.70</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>F</td><td>1.00</td><td>0.90</td><td>0.00</td><td>0.65</td><td>0.00</td><td>26.80</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>G</td><td>0.22</td><td>0.00</td><td>1.80</td><td>1.00</td><td>1.25</td><td>25.50</td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td>H</td><td>0.20</td><td>0.75</td><td>0.65</td><td>1.00</td><td>1.25</td><td>22.40</td>\n",
    "    </tr>\n",
    "</table>\n",
    "\n",
    "#### The manufacturing cost of a product is related to the amounts of materials used to make the product. Find the least-squares regression line using the matrix form of the normal equations <em>A.T</em>@<em>A</em>@<strong>x</strong> = <em>A.T</em>@<strong>b</strong> that will compute an estimated manufacturing cost of a product."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<hr>\n",
    "\n",
    "#### <strong>PROBLEM 4.b.</strong> Find the least-squares regression line for the same data using <em>QR</em> factorization. Verify that matrix <em>R</em> is upper-triangular and invertible. Verify that the columns of matrix <em>Q</em> are orthonormal. "
   ]
  }
 ],
 "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
}
