This course will rely on your understanding of some ideas from calculus and high school algebra. The appendix of the course notes serves to review these ideas.
Sums and averages play an important role in machine learning. Here, weβll review the most relevant properties of summation notation, and use the arithmetic mean as a case study of sorts.
For example, if we take xiβ=i2, then i=1β6βi2 represents the sum of the squares of all integers from 1 to 6:
i=1β6βi2=12+22+32+42+52+62
Notice that both the starting and ending indices (1 and 6, respectively) are included in the sum. Summation notation allows us to express sums conveniently β the left-hand side above is more compact than the right-hand side.
Often, weβll take the sum of the first n terms of a sequence. For example, the sum of the squares of the first n positive integers is:
i=1βnβi2
Note that the index of summation can be any variable name (i is just a typical choice). That is, j=1βnβj2, i=1βnβi2, and zebra=1βnβzebra2 all represent the same sum.
Summation notation can be thought of in terms of a for-loop. In Python, to compute the sum i=aβbβi2, we could write:
total = 0
for i in range(a, b + 1):
total = total + i ** 2
As we mentioned above, the ending index is inclusive in summation notation. This is in contrast to Python, where the ending index is exclusive, which is why we provided b + 1 as the second argument to the range function instead of b.
To illustrate various properties of summation notation, weβll use the following fact:
i=0βnβ2i=20+21+22+...+2n=2n+1β1
The expression 2n+1β1 is called a closed form for the summation. When closed forms exist, they make it easy to compute the value of a summation. Youβll also notice that in addition to writing the sum using summation notation, I also showed the first few and last terms of the sum, with a ... to indicate that the pattern continues in between. As convenient as summation notation is, explicitly writing the first few and last terms of a sum can sometimes make it easier to understand what exactly is being summed.
For example, i=0β3β2i=20+21+22+23=1+2+4+8=15, which is indeed 23+1β1=15. The sequence 20,21,22,β¦,2n is called a geometric sequence, and the resulting sum is called a geometric series.
For convenience, weβll define S(n)=i=0βnβ2i; again, S(n)=2n+1β1.
The best way to learn through these examples is to try to solve them yourself before looking at the solution.
Determine the value of i=8β19β2i. (Find an answer that doesnβt involve summation notation or a sum over many terms.)
Solution
We canβt use the formula for S(n) directly, because the starting index is 8, not 0. But, if we look at the expansion of S(19), weβll see that it contains the sum weβre looking for:
As we did in the very first example, letβs try and write the sum as a difference of two calls to S(n). S(n) involves the sum of terms of the form 2i, and the sum weβre looking for involves the terms 2iβ5, so weβll need to shift the indices.
When i=10, iβ5=5, and when i=20, iβ5=15. So, we can rewrite the sum in question as:
i=10β20β2iβ5=i=5β15β2i
This looks like S(15)βS(4), or 216β1β(25β1)=216β25=65536β32=65504.
As I mentioned at the start of this section, weβll work with sums of data points quite frequently in this class. Weβll often set up a problem by saying we have a sequence of n scalar[1] values, represented by x1β,x2β,β¦,xnβ. For instance, perhaps there are n students in this course, and xiβ represents the height of student i.
Youβve likely seen this definition before. But, an often-forgotten property of the mean is that the sum of the deviations from the mean is zero. By that, I mean (no pun intended) that if you:
compute the mean of a sequence of numbers,
compute the signed difference between each number and the mean, and then
sum all of those differences, the result will be zero.
Letβs first see this in action, then show why it is true in general. Suppose there are only 4 students in the class, with heights 72, 63, 68, and 65 inches. The mean of these heights is:
xΛ=472+63+68+65β=67
The deviations from the mean are:
72β6763β6768β6765β67β=5=β4=1=β2β
The sum of the four deviations, then, is:
5+(β4)+1+(β2)=0
So, the mean deviation from the mean is zero in this example.
This is also true in general. Precisely, Iβm claiming that if x1β,x2β,...,xnβ1β,xnβ are any n numbers, and xΛ is their mean, then i=1βnβ(xiββxΛ)=0.
So, weβve shown that the sum of the deviations from the mean is 0 in general. A consequence of this is that the positive deviations and negative deviations are equal in magnitude, since they need to cancel each other out. In the 72, 63, 68, 65 example, the positive deviations are 5 and 1 and the negative deviations are -4 and -2, and both have magnitude 6. As a result, the mean is sometimes thought of as the βbalance pointβ of the dataset β the point at which the negative deviations are balanced by the positive deviations. More on this in Chapter 1.3.
Since the sum (and average) of deviations from the mean is 0, no matter the dataset, we canβt use the average deviation from the mean to measure how far values tend to be from the mean. The average deviation will be 0, whether the dataset is tightly clustered or spread out.
To measure how far values in a dataset tend to deviate from their mean, then, weβll need to address the fact that some deviations are positive and some are negative. A common approach is to:
Compute the mean of the dataset
Compute the deviation of each value from the mean
Square each deviation
Take the average of the squared deviations
The result of this process is called the variance, denoted s2 or Ο2; its square root is called the standard deviation.
Following the above steps, the variance is given by:
Ο2=n1βi=1βnβ(xiββxΛ)2
(If youβve read Chapter 1.2, youβll notice some similarities between this formula and the formula for mean squared error.)
Suppose the dataset x1β,x2β,β¦,xnβ has mean xΛ and variance Ο2.
Find the mean and variance of the dataset β4x1β+3,β4x2β+3,β¦,β4xnβ+3, and justify your answer rigorously using the definitions of mean and variance.
βScalarβ just means βindividual numberβ, as opposed to a vector or matrix which can contain multiple numbers, as weβll see later in the course.