Lab 1: Math Foundations and Environment Setup

due for completion at 11:59PM Ann Arbor Time on Wednesday, May 6th, 2026

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.


Activities


Activity 1: Environment Setup and Python Basics

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?

Solution

The key fact being assessed here is:

$$ \text{mean} = \frac{\text{sum}}{\text{count}} $$

To find your new average daily sales, we need to find the sum of sales across all 7 days, and divide by the number of days (7).

  • From days 1 to 5, you averaged 50 dollars per day, meaning your total sales from days 1 to 5 were \(50 \cdot 5 = 250\) dollars.

  • Similarly, your total sales from days 6 and 7 combined were \(22 \cdot 2 = 44\) dollars.

So, your average daily sales across all 7 days is:

$$ \frac{50 \cdot 5 + 22 \cdot 2}{7} = \frac{294}{7} = 42 $$

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:

$$ \text{mean speed} = \frac{\text{total distance}}{\text{total time}} $$

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} $$

Putting this all together, we have:

$$ \text{mean speed} = \frac{240 \text{ miles}}{\frac{120}{80} + \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?

Solution

Following the same pattern, we’d have:

$$ \text{mean speed} = \frac{240 \text{ miles}}{\frac{80}{80} + \frac{80}{80} + \frac{80}{50} \text{ hours}} = \frac{3}{\frac{1}{80} + \frac{1}{80} + \frac{1}{50}} \approx 66.67 \text{ miles per hour} $$
c)

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:

$$ \begin{aligned} \text{mean speed} &= \frac{240}{\frac{\frac{240}{n}}{x_1} + \frac{\frac{240}{n}}{x_2} + \ldots + \frac{\frac{240}{n}}{x_n}} \\\\ &= \frac{240}{\frac{240}{n} \left( \frac{1}{x_1} + \frac{1}{x_2} + \ldots + \frac{1}{x_n} \right)} \\\\ &= \frac{1}{\frac{1}{n} \left( \frac{1}{x_1} + \frac{1}{x_2} + \ldots + \frac{1}{x_n} \right)} \\\\ &= \frac{n}{\frac{1}{x_1} + \frac{1}{x_2} + \ldots + \frac{1}{x_n}} \\\\ &= \frac{n}{\sum_{i=1}^n \frac{1}{x_i}} \end{aligned} $$

This formula computes the harmonic mean of the numbers \(x_1, x_2, …, x_n\). Notice that 240 doesn’t appear in the final answer.


Activity 4: The Meaning of Calculus

Here, we’ll review key ideas from Calculus 1. If you’d like a refresher, see Appendix 2 of the course notes, notes.eecs245.org.

Consider the function:

$$ f(x) = (x-3)^2 + (x-4)^2 + (x-5)^2 + (x - 16)^2 $$
a)

What is the shape of \(f(x)\)? Your answer should be a single word.

Solution

\(f(x)\) is a quadratic function, i.e. a parabola. We’re not sure where it’s centered yet — that’s the goal of parts (b) and (c).

b)

Find \(\frac{\text{d}f}{\text{d}x}\), the derivative of \(f(x)\).

Solution
$$ \begin{align*} \frac{\text{d}f}{\text{d}x} &= \frac{\text{d}}{\text{d}x} \left( (x-3)^2 + (x-4)^2 + (x-5)^2 + (x - 16)^2 \right) \\\\ &= 2(x-3) + 2(x-4) + 2(x-5) + 2(x-16) \end{align*} $$
c)

Find \(x^*\), the value of \(x\) that minimizes \(f(x)\), and prove that it is indeed a minimum, rather than a maximum.

Solution

First, we’ll set the derivative we found in part (b) to 0:

$$ 2(x-3) + 2(x-4) + 2(x-5) + 2(x-16) = 0 $$

We can divide both sides by 2:

$$ x-3 + x-4 + x-5 + x-16 = 0 $$

Finally, we have:

$$ 4x = (3 + 4 + 5 + 16) \implies \boxed{x^* = \frac{3 + 4 + 5 + 16}{4} = 7} $$

To show that \(x^*\) is indeed a minimum, we need to show that the second derivative of \(f(x)\) is positive at \(x^*\).

$$ \frac{\text{d}^2f}{\text{d}x^2} = \frac{\text{d}}{\text{d}x} \left( 2(x-3) + 2(x-4) + 2(x-5) + 2(x-16) \right) = 2 + 2 + 2 + 2 = 8 $$

Since the second derivative is positive everywhere, \(f(x)\) is a convex function, and therefore has a global minimum at \(x^*\).

d)

What does the value of \(x^*\) have to do with the numbers 3, 4, 5, and 16?

Solution

\(x^*\) is the mean of the numbers 3, 4, 5, and 16.

$$ 4x = (3 + 4 + 5 + 16) \implies x^* = \frac{3 + 4 + 5 + 16}{4} = 7 $$

Activity 5: Basics of Summation Notation

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:

$$ \frac{12\cdot 13}{2}-\frac{3 \cdot 4}{2}=\frac{156-12}{2}=72 $$

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.

Activity 6: The Meaning of Calculus, Continued

$$ f(x) = (x-3)^2 + (x-4)^2 + (x-5)^2 + (x - 16)^2 $$

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):

$$ \begin{align*} \frac{\text{d}g}{\text{d}x} &= 0 \\\\ \frac{\text{d}}{\text{d}x} \left( \frac{1}{4} f(x) \right) &= 0 \\\\ \frac{1}{4} \frac{\text{d}f}{\text{d}x} &= 0 \\\\ \frac{\text{d}f}{\text{d}x} &= 0 \\\\ \end{align*} $$
b)

\(g(x) = -f(2x)\)

Solution

\(g(x)\) is maximized at \(x^* = \frac{7}{2}\).

\(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:

$$ \begin{align*} \frac{\text{d}g}{\text{d}x} &= \frac{\text{d}}{\text{d}x} \left( -f(2x) \right) \\\\ &= - \frac{\text{d}}{\text{d}x} \left( (2x-3)^2 + (2x-4)^2 + (2x-5)^2 + (2x-16)^2 \right) \\\\ &= - 2(2x-3) \cdot 2 - 2(2x-4) \cdot 2 - 2(2x-5) \cdot 2 - 2(2x-16) \cdot 2 \\\\ &= - 4(2x-3) - 4(2x-4) - 4(2x-5) - 4(2x-16) \end{align*} $$

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)\).

Setting the derivative of \(g(x)\) to 0, we have:

$$ \begin{align*} -4(2x-3) - 4(2x-4) - 4(2x-5) - 4(2x-16) &= 0 \\\\ 2x-3 + 2x-4 + 2x-5 + 2x-16 &= 0 \\\\ 8x &= 3 + 4 + 5 + 16 \\\\ x^* &= \frac{3 + 4 + 5 + 16}{8} = \frac{7}{2} \end{align*} $$

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:

$$ \begin{align*} \frac{\text{d}g}{\text{d}x} &= \frac{\text{d}}{\text{d}x} \left( \sqrt{f(x)} \right) \\\\ &= \frac{\text{d}}{\text{d}x} \left( f(x) \right)^{\frac{1}{2}} \\\\ &= \frac{1}{2} \left( f(x) \right)^{-\frac{1}{2}} \cdot \frac{\text{d}f}{\text{d}x} \\\\ &= \frac{1}{2\sqrt{f(x)}} \cdot \frac{\text{d}f}{\text{d}x} \end{align*} $$

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\).

Visualize \(f(x)\) and \(g(x)\) here on Desmos.

d)

\(g(x) = f(x) + cx^2\), where \(c \in \mathbb{R}\) (Hint: This may take more effort than the previous 4 did.)

Solution

\(g(x)\) is minimized at \(x^* = \frac{28}{4 + c}\).

It’s hard to reason about the extrema of \(g(x)\) without taking the derivative, at least at first.

$$ \begin{align*} \frac{\text{d}g}{\text{d}x} &= \frac{\text{d}}{\text{d}x} \left( f(x) + cx^2 \right) \\\\ &= \frac{\text{d}}{\text{d}x} \left( f(x) \right) + \frac{\text{d}}{\text{d}x} \left( cx^2 \right) \\\\ &= 2(x-3) + 2(x-4) + 2(x-5) + 2(x-16) + 2cx \end{align*} $$

Setting the derivative to 0, we have:

$$ \begin{align*} 2(x-3) + 2(x-4) + 2(x-5) + 2(x-16) + 2cx &= 0 \\\\ x-3 + x-4 + x-5 + x-16 + cx &= 0 \\\\ (4+c)x &= 3 + 4 + 5 + 16 \\\\ \end{align*} $$

So:

$$ x^* = \frac{3 + 4 + 5 + 16}{4 + c} = \boxed{\frac{28}{4 + c}} $$

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:

$$ (x - x_1)^2 + (x - x_2)^2 + \ldots + (x - x_n)^2 $$

This is a generalization of your discovery from part (d), where \(x_1, x_2, …, x_n\) were the numbers 3, 4, 5, and 16.

When we added \(cx^2\) to \(f(x)\), it was almost like adding the value \(x^2\), or equivalently, \((x-0)^2\), \(c\) times. In other words:

$$ g(x) = (x-3)^2 + (x-4)^2 + (x-5)^2 + (x-16)^2 + \underbrace{x^2 + x^2 + \ldots + x^2}_{c \text{ times}} $$

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.

b)

\(\displaystyle \sum_{i=1}^n (x_i + y_i)^2=\sum_{i=1}^n x_i^2 + \sum_{i=1}^n y_i^2\)

Solution

Expanding out the square in the sum gives us the following:

$$ \displaystyle \sum_{i=1}^{n}(x_i+y_i)^2=\sum_{i=1}^{n}(x_i^2+2x_i y_i + y_i^2) $$

Then, we split the terms:

$$ \displaystyle \sum_{i=1}^{n}(x_i+y_i)^2=\sum_{i=1}^{n}x_i^2+ \sum_{i=1}^{n} 2x_i y_i +\sum_{i=1}^{n}y_i^2 $$

There’s no way for us to get rid of the second term, so the answer is false in general.

c)

\(\displaystyle \sum_{i=2}^n x_i=\sum_{i=2}^k x_i + \sum_{i=k}^n x_i\)

Solution

False, this is an off by one error. Since summations are inclusive, the right hand side is double counting \(x_k\).

d)

\(\displaystyle \sum_{i=1}^n (x_i - \bar{x})=\sum_{i=1}^n x_i - n\bar x\), where \(\displaystyle \bar{x}=\frac{1}{n}\sum_{i=1}^{n}x_i\)

Solution

Once again, we’ll start by splitting the sum into two parts:

$$ \displaystyle \sum_{i=1}^n (x_i - \bar{x})=\sum_{i=1}^n x_i - \sum_{i=1}^n \bar x $$

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\).

$$ \begin{align*} 1 + 2 + 3 + \ldots + n &= \sum_{i=1}^n i = \frac{n(n+1)}{2} \\\\ 1^2 + 2^2 + 3^2 + \ldots + n^2 &= \sum_{i=1}^n i^2 = \frac{n(n+1)(2n+1)}{6} \end{align*} $$

Using the formulas above, determine the values of each of the following sums.

a)

\(\displaystyle \sum_{i = 5}^{15} i^2\)

Solution

The key is recognizing that we can express the sum we’re looking for as the difference of two other sums that have closed-form expressions:

$$ \sum_{i=5}^{15} i^2 = \sum_{i=1}^{15} i^2 - \sum_{i=1}^{4} i^2 $$

Given that, we have:

$$ \begin{align*} \sum_{i=5}^{15} i^2 &= \sum_{i=1}^{15} i^2 - \sum_{i=1}^{4} i^2 \\\\ &= \frac{15(16)(31)}{6} - \frac{4(5)(9)}{6} \\\\ &= 1240 - 30 \\\\ &= 1210 \end{align*} $$
b)

\(\displaystyle \sum_{i = 4}^{9} 3\)

Solution

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.

So, the sum is:

$$ \sum_{i=4}^{9} 3 = 3 + 3 + 3 + 3 + 3 + 3 = 3 \cdot (9 - 4 + 1) = 3 \cdot 6 = 18 $$
c)

\(\displaystyle \sum_{j = 1}^{20} (1 - 3j)^2\)

Solution

We’ll have to expand a fair bit here:

$$ \begin{align*} \sum_{j=1}^{20} (1 - 3j)^2 &= \sum_{j=1}^{20} (1 - 6j + 9j^2) \\\\ &= \sum_{j=1}^{20} 1 - \sum_{j=1}^{20} 6j + \sum_{j=1}^{20} 9j^2 \\\\ &= 20 - 6 \sum_{j=1}^{20} j + 9 \sum_{j=1}^{20} j^2 \\\\ \end{align*} $$

We know that \(\displaystyle \sum_{j=1}^{20} j = \frac{20 \cdot 21}{2} = 210\) and \(\displaystyle \sum_{j=1}^{20} j^2 = \frac{20 \cdot 21 \cdot 41}{6} = 2870\). So, we have:

$$ \begin{align*} \sum_{j=1}^{20} (1 - 3j)^2 &= 20 - 6 \cdot 210 + 9 \cdot 2870 \\\\ &= 20 - 1260 + 25830 \\\\ &= 24590 \end{align*} $$