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 as many of the activities as you can in 2 hours and submit a PDF of your work to Gradescope. We will provide specific instructions on how to submit programming activities (e.g. submitting the notebook or including a screenshot of some output).
Feel free to work with others in the course, but you must submit individually.
Labs and homeworks will both involve writing some Python code in a Jupyter Notebook.
There are two ways to access the supplemental Jupyter Notebook:
Option 1 (preferred): Set up a Jupyter Notebook environment locally, use git to clone our course repository, and open labs/lab01/lab01.ipynb. For instructions on how to do this, see the Environment Setup page of the course website.
Option 2: Click here to open lab01.ipynb on DataHub. Before doing so, read the instructions on the Environment Setup page on how to use the DataHub.
Read the Environment Setup section of the course website, eecs245.org/env-setup, for detailed steps on setting up a local environment on your machine. Take the time to follow the steps under Option 1: Local Setup, and let us know if you have any questions.
Then, open the notebook labs/lab01/lab01.ipynb, read it, and complete the tasks inside. Once you’re done, include a screenshot of your completed Task 5 implementation in your PDF submission of Lab 1 to Gradescope, making sure to include proof that the (local) autograder passed.
Optionally, you can submit your completed notebook itself to the Lab 1 Notebook (for practice) assignment on Gradescope; this is not required for credit, but it’s a good way to practice submitting code to Gradescope, which you’ll need to do for some homeworks.
Activity 2: Running Mean
Over the break, you ran a hot chocolate stand. On days 1 through 5 (inclusive), you averaged 50 dollars per day in sales. On days 6 and 7, you averaged 22 dollars per day in sales. What were your average daily sales from days 1 through 7?
Note that the first expression above can be written as:
$$ \frac{5}{7} \cdot 50 + \frac{2}{7} \cdot 22 $$
This is a weighted average or weighted mean of the numbers 50 and 22, with weights \(\frac{5}{7}\) and \(\frac{2}{7}\), respectively. Weighted averages appear all of the time in machine learning, but even in day-to-day life: your GPA is a weighted average of your grades in each class, where the weights are the number of credits earned.
Activity 3: A New Meaning
Over the break, in addition to running your hot chocolate stand, you took a road trip to Chicago, 240 miles away.
a)
For the first 120 miles, you averaged 80 miles per hour (mph). For the second 120 miles, you averaged 50 mph. What was your average speed throughout the entire journey? Leave your answer unsimplified in terms of fractions, but plug it into a calculator to get an approximation.
Solution
Following the same principle of \(\text{mean} = \frac{\text{sum}}{\text{count}}\) from the Running Mean activity, we have that:
The total distance traveled was 120 miles. What was the total time taken? We can break this up into \(\text{time for Segment 1} + \text{time for Segment 2}\).
In Segment 1, we traveled 80 miles per hour for 120 miles, so:
$$ 80 \text{ miles per hour} = \frac{120 \text{ miles}}{\text{time for Segment 1}} \implies \text{time for Segment 1} = \frac{120}{80} \text{ hours} $$
In Segment 2, we traveled 50 miles per hour for 120 miles, so:
$$ \text{time for Segment 2} = \frac{120}{50} \text{ hours} $$
Notice that both the numerator and denominator have a factor of 120. Pulling this out, we have:
$$ \text{mean speed} = \frac{2}{\frac{1}{80} + \frac{1}{50}} \approx 61.54 \text{ miles per hour} $$
b)
Suppose, instead, you drove 3 segments of 80 miles each, in which you averaged 80 mph, 80 mph, and 50 mph. What was your average speed throughout the entire journey?
In general, suppose you drove \(n\) segments of equal length, and averaged \(x_i\) mph in segment \(i\) (\(i = 1, 2, …, n\)). What was your average speed throughout the entire journey? Give your answer using summation notation. Your answer is the formula for the harmonic mean of the numbers \(x_1, x_2, …, x_n\).
Solution
If we generalize the calculations from the previous two parts, we have:
Here, we’ll review the basics of summation notation. If you’d like a refresher, see Appendix 1 of the course notes, notes.eecs245.org.
Consider the following formula involving the first \(n\) natural numbers, \(1,2,\dots, n\).
$$ 1 + 2 + 3 + \ldots + n = \sum_{i=1}^n i = \frac{n(n+1)}{2} $$
Using the fact above, find \(\displaystyle \sum_{k = 4}^{12} (k+2)\). Verify your answer by calculating the sum directly.
Solution
We can separate the sum into two smaller sums:
$$ \sum_{k=4}^{12} (k+2) = \sum_{k=4}^{12} k + \sum_{k=4}^{12} 2 $$
The first sum, \(\displaystyle \sum_{k=4}^{12} k\), can be rewritten as \(\displaystyle \sum_{k=1}^{12} k-\sum_{k=1}^{3} k\), or the sum of the first \(12\) natural numbers minus the sum of the first \(3\). Using the formula gives us the following:
The second sum, \(\displaystyle \sum_{k=4}^{12} 2\), is just \(2\) added together, \(9\) times, which is \(2 \cdot 9 = 18\).
So, the full sum is:
$$ \sum_{k=4}^{12} (k+2) = \sum_{k=4}^{12} k + \sum_{k=4}^{12} 2=68+18=90 $$
Another way of arriving at the solution is to recognize that:
$$ \sum_{k=4}^{12} (k+2) = \sum_{k=6}^{14} k = \left( \sum_{k=1}^{14} k \right) - \left( \sum_{k=1}^{5} k \right) = \frac{14 \cdot 15}{2} - \frac{5 \cdot 6}{2} = \frac{210-30}{2}=90 $$
The rest of this worksheet is extra practice. Don’t feel pressured to answer all of these problems in lab, but make sure to attempt them at some point.
For each of the following functions \(g(x)\), identify all extrema (that is, maximums and/or minimums). You don’t need to take the derivative in each case, but explain your reasoning.
a)
\(g(x) = \frac{1}{4} f(x)\)
Solution
\(g(x)\) is minimized at \(x^* = 7\).
\(g(x)\) has the same vertex as \(f(x)\), but it is scaled vertically by a factor of \(\frac{1}{4}\).
If that’s not convincing, note that the derivative of \(g(x)\) is just \(\frac{1}{4}\) times the derivative of \(f(x)\). When we set the derivative of \(g(x)\) to 0, we’ll end up solving the same equation for \(x^*\) as we did in part (c):
\(g(x)\) is a downward-facing parabola, compressed horizontally by a factor of 2. It’s a little more difficult to reason about horizontal compressions, so let’s work through the derivative:
In the second-last line above, the additional factors of two are the result of the chain rule (the derivative of \(2x - 3\) with respect to \(x\) is \(2\)), and you’ll notice that each term in parentheses involves \(2x\), not just \(x\) as with \(f(x)\).
Intuitively, if we set \(u = 2x\), then \(g(x) = -f(u)\), and we know that \(-f(u)\) is maximized where \(f(u)\) is minimized, which is at \(u^* = 7\). Since \(u = 2x\), we have \(x^* = \frac{u^*}{2} = \frac{7}{2}\).
c)
\(g(x) = \sqrt{f(x)}\)
Solution
\(g(x)\) is minimized at \(x^* = 7\).
\(\sqrt{x}\) is a strictly monotonically increasing function across its domain, which is \([0, \infty)\). What this means is that if \(a > b\), then \(\sqrt{a} > \sqrt{b}\), or in other words, as we move from left to right, the graph of the function only increases, never stays the same or decreases. Strictly monotonically increasing functions preserve the order of their inputs.
\(\log(x)\) is also a strictly monotonically increasing function. \(x^2\) is not, because, for example, \((-3)^2 > 2^2\), but \(-3\) is not greater than \(2\).
What does this have to do with finding the extrema of \(g(x)\)? Well, since we know that \(f(7)\) is at the bottom of the graph of \(f(x)\), we know that \(\sqrt{f(7)}\) is at the bottom of the graph of \(\sqrt{f(x)}\), because of the fact that \(\sqrt{x}\) is strictly monotonically increasing, meaning that order is preserved.
If that’s not a convincing argument, we can also work through the derivative:
To solve for the extrema of \(g(x)\), we need to set its derivative to 0. Its derivative contains two factors, one of which is \(\frac{\text{d}f}{\text{d}x}\) — which we know is 0 at \(x^* = 7\) — and the other is \(\frac{1}{2\sqrt{f(x)}}\), which can never be 0 (think about why). So, \(g(x)\) must be minimized at \(x^* = 7\).
You’ll notice that if \(c = 0\), then \(x^* = 7\), which is the same as the minimum of \(f(x)\), as this equates to “turning off” the new \(cx^2\) term.
How else could we have reasoned about \(x^*\)? One way to think about it is that \(\text{Mean}(x_1, x_2, …, x_n)\) minimizes:
So, knowing that the mean minimizes the sum of squared errors, we can conclude that \(x^*\) should be the mean of the numbers \(3, 4, 5, 16, 0, …, 0\) (where there are \(c\) zeros). The mean of these numbers is their sum over their count; their sum is \(3 + 4 + 5 + 16 + 0 + … + 0 = 28\), and their count is \(4 + c\). So, \(x^* = \frac{28}{4 + c}\).
This logic is not perfect, since \(c\) didn’t need to be an integer, but it helps build intuition for the answer.
Activity 7: Summation Notation Properties
Suppose \(x_1, x_2, \dots, x_n\) and \(y_1, y_2, \dots, y_n\) are both lists of numbers. Determine whether each of the following expressions is true or false.
a)
\(\displaystyle \sum_{i=1}^n (a x_i + b) = a \sum_{i=1}^n x_i + bn\), where \(a\) and \(b\) are constants.
Solution
We’ll start by splitting the sum into two parts:
$$ \displaystyle \sum_{i=1}^n (a x_i + b)=\sum_{i=1}^n ax_i + \sum_{i=1}^n b $$
We can factor out the constant in the first sum, \(\displaystyle \sum_{i=1}^n ax_i\), to rewrite it as \(\displaystyle a\sum_{i=1}^n x_i\).
The second sum, \(\displaystyle \sum_{i=1}^n b\) can be rewritten as \(bn\) because it’s equivalent to adding \(b\) together \(n\) times. So, the statement is true.
The key to this problem is that \(\bar x\) is the mean of the \(x_i\)’s, and the mean is a fixed value. In other words, \(\bar x\) is a constant, not a variable! Using this fact, we can simplify the second term further:
$$ \sum_{i=1}^n x_i - \sum_{i=1}^n \bar x=\sum_{i=1}^n x_i - n \bar x $$
So, the answer is true. Furthermore, \(\sum_{i = 1}^n x_i = n \bar x\), so both sides of the equation are equal to \(0\).
Activity 8: Manipulating Sums
Consider the following summations involving the first \(n\) natural numbers, \(1, 2, 3, …, n\).
Notice that the sum we’re looking for is just \(3\) added together, several times — it does not involve \(i\).
How many times are we adding \(3\)? It’s tempting to think it’s \(5\) times, since \(9 - 4 = 5\), but that is one short. Count out the numbers from \(4\) to \(9\) to see that the range includes 6 numbers, when we include both endpoints.