What Σ means
The capital Greek letter (sigma) means "add up". Here is a sum written out in full and in sigma notation:
Read it as "the sum of for from 1 to 4". The letter is the index. It starts at the number below the , goes up by 1 each time, and stops at the number on top. The expression to the right of is the term: work it out for each and add the results.
Rule: Sigma notation
Start the index at , add one term for each whole number up to , and include itself. That makes terms.
Try it
A sum is a loop that keeps a running total. Python's sum does the same in one line.
Both need range(1, 5) to reach , because a range stops just before its second number. The upper limit becomes range(m, n + 1).
Sums of data
Most sums in this course add up a list of values. With , , , :
The second formula is the average from lesson 0.2.1, written with . When the limits are the whole list, books often drop them and write , or just .
The name of the index does not matter. and are the same sum, just as renaming a loop variable does not change a loop.
Try it
For a list of numbers, sum(x) is . NumPy has np.sum and np.mean, which work on whole arrays at once.
Three rules for sums
Write a sum out in full and these rules are ordinary arithmetic. They let you pull numbers out of a sum and split it into simpler pieces.
Rule: Constant multiple
A number that multiplies every term can come out in front. Doubling every value doubles the total.
Rule: Sum of two sequences
A sum of sums can be split into two sums, and two sums can be joined into one.
Rule: Sum of a constant
Adding the same number once for each of terms gives times . For example, .
Together they handle sums like with the four values above. Split it, pull out the 2, and add the constant four times: .
Try it
Check the rules on the same data. Each line prints the long way and the short way.
Weighted sums
A weighted sum multiplies each value by its own weight, then adds:
With weights and values , that is . When the weights add up to 1, as here, the result is a weighted average. Case study 0.2.4 uses exactly this sum for a portfolio.
Try it
Pair up the weights and values with zip, or let NumPy do it. The @ operator multiplies in pairs and adds, which is a weighted sum in one symbol.
Products with Π
The capital Greek letter (pi) does for multiplication what does for addition:
Rule: Pi notation
Multiply the term for each index from to . Multiplying the same times gives .
Products appear whenever something grows in steps. Say a value grows by 10%, then falls by 5%, then grows by 20%. Write for the growth in step , so , and .
So the value ends up multiplied by 1.254. Lesson 1.4.3 shows how logarithms turn a into a .
Try it
math.prod is the product of a list, as sum is its sum.
The second line prints 1.2539999999999998. That is rounding inside the computer, not an error.
Empty sums and empty products
What if the top limit is below the bottom one, as in ? Then there are no terms at all. By convention an empty sum is 0 and an empty product is 1.
These are the values that change nothing: adding 0 or multiplying by 1 leaves a total as it was. Python follows the same convention, and the coding exercise in lesson 0.2.3 relies on it.
Try it
range(1, 1) is empty, so both of these have nothing to add or multiply.
Common mistakes
Try it
- The sum of squares is not the square of the sum. For 1, 2, 3: , but .
- The sum of products is not the product of sums. With the weights above, , but .
- Count the terms. is , not . And
range(1, n)stops one short of .
Sums and products
0 of 9 correct
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
How to type answers
- Powers with
^:x^7,2^(1/3),a^-2. - Multiply with
*or by writing side by side:3x^2,2(x+1). ln(x)is the natural log,log(x, 2)is log base 2,eis Euler's number,sqrt(x)is a square root.sin(x),cos(x)andtan(x)take radians; writepifor π andsin(x)^2for sin²x.- Your first answer to each question is saved: it counts towards your score and brings the question back for review.
- Exact answers like
ln(20)/ln(3)are best. Decimals need about 4 significant figures.
Summary card
- adds the term for every index from to , both included: terms. In Python,
sum(a(i) for i in range(m, n + 1)). - Constants come out: . Sums split: . And .
- A weighted sum is , or
w @ xin NumPy. - multiplies the terms;
math.prodin Python. - An empty sum is 0 and an empty product is 1.
- and .