<!-- MATH 4242 Python Lab 7: sections 4.3 to 4.4 and 5.2 -->
<setup>
  <selectFromSequence name="a" from="2" to="5" step="1" />
  <selectFromSequence name="k" from="1" to="4" step="1" />

  <math name="cZero">$a-1/6</math>
  <math name="cOne">$k+1/2</math>
</setup>

<p>Python Lab 7: Projection and least squares</p>

<p>
  Open <em>07_Least_Squares.ipynb</em> and enter
  <m>a=$a</m> and <m>k=$k</m>. Fit a line <m>y\approx c_0+c_1t</m>
  to the data
  <m>
    (0,$a),\qquad(1,$a+$k),\qquad(2,$a+2*$k+1).
  </m>
</p>

<ol marker="a.">
  <li>
    <p>
      Enter the design matrix for this model by choosing the correct
      NumPy construction.
    </p>
    <choiceInput name="designChoice" forAnswer="$designAnswer">
      <shortDescription>NumPy construction of the line fitting design matrix</shortDescription>
      <choice>np.column_stack((np.ones_like(t), t))</choice>
      <choice>np.array([1, t])</choice>
      <choice>np.diag(t)</choice>
      <choice>np.outer(t, t)</choice>
    </choiceInput>
    <answer name="designAnswer"><award><when>$designChoice.selectedIndex=1</when></award></answer>
  </li>

  <li>
    <p>
      Use <em>np.linalg.lstsq</em> to compute
      <m>\widehat c=\begin{bmatrix}c_0\\c_1\end{bmatrix}</m>:
      <m>c_0=</m>
      <mathInput name="c0Input" forAnswer="$coefficientsAnswer" minWidth="100">
        <shortDescription>least squares intercept</shortDescription>
      </mathInput>,
      <m>c_1=</m>
      <mathInput name="c1Input" forAnswer="$coefficientsAnswer" minWidth="100">
        <shortDescription>least squares slope</shortDescription>
      </mathInput>.
    </p>
    <answer name="coefficientsAnswer">
      <award><when>$c0Input=$cZero and $c1Input=$cOne</when></award>
    </answer>
  </li>

  <li>
    <p>
      Enter the residual <m>r=y-X\widehat c</m>. First component:
      <mathInput name="res1" forAnswer="$residualVectorAnswer" minWidth="65">
        <shortDescription>first residual component</shortDescription>
      </mathInput>; second component:
      <mathInput name="res2" forAnswer="$residualVectorAnswer" minWidth="65">
        <shortDescription>second residual component</shortDescription>
      </mathInput>; third component:
      <mathInput name="res3" forAnswer="$residualVectorAnswer" minWidth="65">
        <shortDescription>third residual component</shortDescription>
      </mathInput>.
    </p>
    <answer name="residualVectorAnswer">
      <award><when>$res1=1/6 and $res2=-1/3 and $res3=1/6</when></award>
    </answer>
  </li>

  <li>
    <p>
      Compute the squared error
      <m>\lVert r\rVert^2=</m>
      <mathInput name="squaredError" forAnswer="$errorAnswer">
        <shortDescription>squared norm of the least squares residual</shortDescription>
      </mathInput>.
    </p>
    <answer name="errorAnswer"><award><when>$squaredError=1/6</when></award></answer>
  </li>

  <li>
    <p>What does the notebook's calculation <m>X^Tr\approx0</m> mean geometrically?</p>
    <choiceInput name="geometryChoice" forAnswer="$geometryAnswer">
      <shortDescription>geometric meaning of the least squares residual</shortDescription>
      <choice>The residual lies in the column space of <m>X</m>.</choice>
      <choice>The residual is orthogonal to the column space of <m>X</m>.</choice>
      <choice>The data vector is orthogonal to every fitted vector.</choice>
      <choice>The design matrix is singular.</choice>
    </choiceInput>
    <answer name="geometryAnswer"><award><when>$geometryChoice.selectedIndex=2</when></award></answer>
  </li>
</ol>
