Each lab worksheet will contain several activities, some of which will involve writing code and others that will involve writing math on paper. To receive credit for a lab, you must complete all activities and show your lab TA by the end of the lab section.
While you must get checked off by your lab TA individually, we encourage you to form groups with 1-2 other students to complete the activities together.
In each category, select the option that best describes your (honest) thoughts towards this class so far.
i. Difficulty
Way harder than I expected
Harder than I expected
About as hard as I expected
Easier than I expected
Way easier than I expected
ii. Interest
Way more interesting than I expected
More interesting than I expected
About as interesting as I expected
Less interesting than I expected
Way less interesting than I expected
Any immediate action items we can take to make your experience better?
Activity 1: Transformed Data
Let’s begin by reviewing how transforming a dataset affects a fitted simple linear model.
Suppose we’re given a dataset of \(n\) points, \((x_1, y_1), (x_2, y_2), \dots, (x_n, y_n)\), where \(\bar{x}\) is the mean of \(x_1, x_2, \dots, x_n\) and \(\bar{y}\) is the mean of \(y_1, y_2, \dots, y_n\).
Using this dataset, we create a transformed dataset of \(n\) points, \((x_1’, y_1’), (x_2’, y_2’), \dots, (x_n’, y_n’)\), where:
We decide to fit a simple linear model \(h(x_i’) = w_0 + w_1 x_i’\) on the transformed dataset using squared loss. We find that \(w_0^{\ast} = 7\) and \(w_1^{\ast} = 2\), so \(h^{\ast}(x_i’) = 7 + 2x_i’\).
a)
Suppose we were to fit a simple linear model through the original dataset, again using squared loss. What would the optimal slope on the original dataset be?
Solution
8.
Relative to the dataset with \(x’\), the dataset with \(x\) is compressed by a factor of 4, so the slope increases by a factor of 4: \(2 \cdot 4 = 8\).
Concretely, this can be shown by looking at the formula for the new slope:
Recall, the model \(h^{\ast}(x_i’) = w_0 + w_1 x_i’\) was fit on the transformed dataset, introduced above. \(h^{\ast}(x_i’)\) happens to pass through the point \((\bar{x}, \bar{y})\). What is the value of \(\bar{x}\)? Give your answer as an integer with no variables. Hint: What else does \(h^{\ast}(x_i’)\) pass through?
Solution
\(h^{\ast}(x_i’)\) is guaranteed to pass through \((\bar{x}’, \bar{y}’)\), where \(\bar{x}’\) is the mean of the \(x’\) values and \(\bar{y}’\) is the mean of the \(y’\) values.
Now that we’ve reviewed simple linear regression, let’s turn to vectors. The next three activities explore vector lengths, linear combinations, and their implementation in NumPy.
(Chapters 3.1 and 3.2) The norm of a vector \(\vec v \in \mathbb{R}^n\) measures its length:
This is the default norm for vectors in \(\mathbb{R}^n\), but other norms exist.
(3.1) A linear combination of the vectors \(\vec v_1,\vec v_2, \dots,\vec v_d\) is any vector that can be written as
$$ a_1\vec v_1 + a_2\vec v_2+\dots+a_d\vec v_d $$
where \(a_1, a_2, \dots, a_d\) are scalars. We can think of this as taking bits of each vector and adding them together. The \(a_i\)’s are called the coefficients of the linear combination.
Activity 2: Triangle Inequality
We first use norms to compare the length of a sum with the lengths of the original vectors. The triangle inequality states that for any two vectors \(\vec u, \vec v \in \mathbb{R}^n:\)
$$ \lVert \vec u + \vec v \rVert \leq \lVert \vec u \rVert + \lVert \vec v \rVert $$
a)
For the vectors \(\vec u = \begin{bmatrix} 4 \\ 3 \end{bmatrix}\) and \(\vec v = \begin{bmatrix} -1 \\ -3 \end{bmatrix}\), verify that the triangle inequality holds. That is, show that the left-hand side is less than or equal to the right-hand side.
Solution
First, let’s find \(\lVert \vec u + \vec v \rVert\).
So, in this case, the triangle inequality achieves equality. What you’ll notice is that \(\vec x\) and \(\vec y\) point in the same direction, i.e. \(\vec y = 2 \vec x\).
Activity 3: Linear Combinations
Next, we’ll look at which vectors we can form by scaling and adding other vectors.
Let \(\vec u = \begin{bmatrix} 4 \\ 3 \end{bmatrix}\), \(\vec v = \begin{bmatrix} -1 \\ -3 \end{bmatrix}\), and \(\vec w = \begin{bmatrix} -6 \\ 9 \end{bmatrix}\).
a)
Find values of \(a\) and \(b\) such that \(a \vec u + b \vec v = \vec w\). By finding \(a\) and \(b\), you have written \(\vec w\) as a linear combination of \(\vec u\) and \(\vec v\).
Solution
We can pose this problem as solving a system of equations. By scalar multiplication, we have:
Now, try and write \(\vec w\) as a linear combination of \(\vec u\), \(\vec v\), and \(\begin{bmatrix} 2 \\ 1 \end{bmatrix}\). In other words, try and find values of \(a\), \(b\), and \(c\) such that
$$ a \begin{bmatrix} 4 \\\\ 3 \end{bmatrix} + b \begin{bmatrix} -1 \\\\ -3 \end{bmatrix} + c \begin{bmatrix} 2 \\\\ 1 \end{bmatrix} = \vec w $$
What happens? Why?
Solution
We can start by trying to solve the corresponding system of equations:
$$ \begin{cases} 4a - b + 2c = -6\\\\ 3a - 3b + c = 9 \end{cases} $$
There are 2 equations and 3 unknowns, which means thare are infinitely many solutions for \(a\), \(b\), and \(c\).
What’s the linear algebra reason for this?
With just \(\begin{bmatrix}4\\3\end{bmatrix}\) and \(\begin{bmatrix}-1\\-3\end{bmatrix}\), you can already create any other vector in \(\mathbb{R}^2\). That is, any vector in \(\mathbb{R}^2\) can be written as a linear combination of \(\begin{bmatrix}4\\3\end{bmatrix}\) and \(\begin{bmatrix}-1\\-3\end{bmatrix}\).
That is, for any vector \(\vec w \in \mathbb{R}^2\) (not just the one in this question), there exist unique values of \(a\) and \(b\) such that
$$ a \begin{bmatrix}4\\\\3\end{bmatrix} + b \begin{bmatrix}-1\\\\-3\end{bmatrix} = \vec w $$
Since \(\begin{bmatrix} 4 \\ 3 \end{bmatrix}\) and \(\begin{bmatrix} -1 \\ -3 \end{bmatrix}\) already can create any other vector in \(\mathbb{R}^2\), adding \(\begin{bmatrix} 2 \\ 1 \end{bmatrix}\) to the linear combination doesn’t “unlock” any new vectors — we can still create any other vector in \(\mathbb{R}^2\).
But, because \(\begin{bmatrix} 2 \\ 1 \end{bmatrix}\) already can be created using \(\begin{bmatrix} 4 \\ 3 \end{bmatrix}\) and \(\begin{bmatrix} -1 \\ -3 \end{bmatrix}\), adding it to the linear combination makes it so that there are infinitely many solutions for \(a\), \(b\), and \(c\) in
$$ a \begin{bmatrix}4\\\\3\end{bmatrix} + b \begin{bmatrix}-1\\\\-3\end{bmatrix} + c \begin{bmatrix}2\\\\1\end{bmatrix} = \vec w $$
If there are infinitely many solutions, how do we find them? Let’s treat \(c\) as a free variable, and solve for \(a\) and \(b\) in terms of \(c\).
$$ \begin{cases} 4a - b + 2c = -6\\\\ 3a - 3b + c = 9 \end{cases} $$
So, the values of \(a\), \(b\), and \(c\) that satisfy
$$ a \begin{bmatrix} 4 \\\\ 3 \end{bmatrix} + b \begin{bmatrix} -1 \\\\ -3 \end{bmatrix} + c \begin{bmatrix} 2 \\\\ 1 \end{bmatrix} = \begin{bmatrix} -6 \\\\ 9 \end{bmatrix} $$
are
$$ \boxed{a = -3 - \frac{5}{9}c, \qquad b = -6 - \frac{2}{9}c, \qquad c = c, c \in \mathbb{R}} $$
\(c\) can be anything, which is why there are infinitely many solutions. If we let \(c = 0\), then we get back \(a = -3\) and \(b = -6\) from part a). But, say, if we let \(c = -9\), then we get \(a = 2\) and \(b = -4\), which also works:
Now, try and write \(\vec w\) as a linear combination of \(\begin{bmatrix} 2 \\ 1 \end{bmatrix}\) and \(\begin{bmatrix} -4 \\ -2 \end{bmatrix}\). What happens? Why?
Solution
Note \(\begin{bmatrix}-4\\-2\end{bmatrix}=-2\begin{bmatrix}2\\1\end{bmatrix}\), which means these vectors point in the same direction, or lie on the same line. (The formal term is that these vectors are collinear.)
Since \(\vec w=\begin{bmatrix}-6\\9\end{bmatrix}\) is not a scalar multiple of \(\begin{bmatrix}2\\1\end{bmatrix}\) (ratios \(-6/2=-3\) vs. \(9/1=9\) disagree), no solution exists!
To conclude, because the two vectors \(\begin{bmatrix}2\\1\end{bmatrix}\) and \(\begin{bmatrix}-4\\-2\end{bmatrix}\) are collinear, it is impossible to write \(\vec w\) as a linear combination of them. The only possible linear combinations are of the form \(c \begin{bmatrix}2\\1\end{bmatrix}\) for some \(c \in \mathbb{R}\).
Activity 4: Arrays in NumPy
Now, put vector operations into practice using NumPy arrays. Instead of writing code in a separate Jupyter Notebook for this lab, you will interact with the code cells that exist in the course notes.
In particular, go to Chapter 3.2 of the course notes, scroll all the way to the bottom, and complete Activity 5 there. To get checked off, show your lab TA that you’ve completed the activity — there’s no need to submit your code anywhere.
Introduction: The Dot Product
So far, we’ve measured vector lengths and formed linear combinations. The dot product gives us a way to compare the directions of two vectors. We will study this in tomorrow’s lecture; use the definitions below to explore it in the next two activities.
(3.3) The dot product of two vectors \(\vec u, \vec v \in \mathbb{R}^n\) is defined as:
(3.3) The dot product also has a geometric definition, involving the norms (lengths) of two nonzero vectors and the angle between them:
$$ \vec u \cdot \vec v = ||\vec u|| ||\vec v|| \text{cos}\theta $$
(3.3) The key takeaway from the dot product is that it tells us how similar the directions of two vectors are. When two vectors have a dot product of 0, they are orthogonal. For two nonzero vectors, this means that the angle between them is \(90^\circ\).
Activity 5: The Dot Product
For each pair of vectors below, (1) draw them on the grid to the right and (2) compute their dot product using the component-wise definition.
Since the dot product is \(0\), the vectors are orthogonal.
Solution
Activity 6: Angles and Orthogonality
In the previous activity, we computed dot products using vector components. Now, we will connect those calculations to angles using the geometric definition of the dot product.
so the vectors are orthogonal. Conversely, if \(\vec u\cdot\vec v=0\) (and neither vector is the zero vector), then \(\cos\theta=0\) and \(\theta=90^\circ\).