One of the nice things about data science is that it allows us to explore the symmetry between mathematics and coding. We saw this with translating equations into functions. For example, we have now seen how to translate something like this:
def y(x):
return 1.3*x + 20
In this section we'll see how we can use coding to better understand a mathematical series and summation.
- Understand how to represent a mathematical series
- Understand how indices are represented
- Understand how to represent a summation with sigma notation
In Python, you are familiar with a list as looking like the following.
my_list = [1, 6, 11, 16, 21]
Now looking at the numbers above, you can see that these numbers start at one and increment by five for every number. So below we generate these numbers with code, and also use code to express how these numbers increment.
initial_i = 0
ending_i = 4
terms = []
for i in range(initial_i, ending_i + 1):
current_element = 5*(i) + 1
terms.append(current_element)
terms
Our code above expresses a pattern in the numbers:
- initialize a number (our index) at zero
- increase the index until it reaches four
- Each element in the list equals
$5*i + 1$ , for the index 0 up to and including 4
In Python we call this ordered collection a list. In math, an ordered list of numbers is called a sequence. We express sequences not with [], but with parentheses or curly brackets. For example:
- (1, 6, 11, 16, 21) or
- {1, 6, 11, 16, 21}
Each component of a sequence is called an element, or a term. So in our sequence above, 1 is a term, as is 6 and 21.
Another mathematical notation for expressing the above sequence is the following:
Read the above line as the following:
- A sequence of numbers,
$x_i$ from the indices of 0 to 4, where$x_i$ equals$5*i + 1$ . - This gives us {1, 6, 11, 16, 21}
In describing a sequence in math, the initial index is placed at the bottom, and the stopping point at the top. And the common term for the sequence is also described.
Notice the similarity to our previous code:
initial_i = 0
ending_i = 4
terms = []
for i in range(initial_i, ending_i + 1):
current_element = 5*(i) + 1
terms.append(current_element)
terms
In our first sequence, the term changed based on the value of
But sequences don't have to depend on the value of the index. For example, here is another sequence:
Can you determine how this sequence would look?
And the code would be:
initial_i = 0
ending_i = 4
terms = []
for i in range(initial_i, ending_i + 1):
current_term = 5
terms.append(current_term)
terms
A series in mathematics is just the sum of the terms of a sequence. So the series of the sequence above is this:
Let's see how we would write something like this using Python. Here is the sequence that we previously generated written another way. Python's range
function can accept a third argument, which tells it how much to increment each element. In other words, we start at 1, increment each element by 5, then stop at 21 because 26 < 22
is False.
num_range = list(range(1, 22, 5))
num_range
Now to turn this into a series, we just add up those terms.
total = 0
for i in num_range:
total += i
total
Or we can use Python's built-in sum function to add up the elements in num_range
.
num_range
sum(num_range)
We have just seen different ways for adding a sequence of numbers in code. Adding a sequence of numbers is called a summation. Let's see how to express a summation of numbers in mathematics using sigma notation. This is sigma notation for adding up the numbers from one to ten, {1 + 2 + 3+ 4+ 5+ 6+ 7 + 8 + 9+ 10}.
Ok, so let's break down the syntax. The greek letter
total = 0
for i in range(1, 11):
total += i
total
Let's try another of these. What does adding up the numbers one through five look like in sigma notation? Well, we know to start at one and end at five. Then we add each of those numbers,
Ok, not too bad. The above is the equivalent of $ 1 + 2 + 3 + 4 + 5 = 15 $.
If we want to be a little more long winded about this, we can express this with our series notation as the following:
For
So far we have discussed adding numbers one after the other. Now let's use our notation to describe adding any terms in a sequence. This is one way to describe a sum of a sequence:
We can read that as, for an element in the series with
This equals 75. Do you see why?
So by using sigma notation we can express the a summation more succinctly. And in doing so, we can get more to the core of what we are trying to express.
In this section, we saw the similarity between abstractions in mathematics and in coding. We saw that a sequence is an ordered list of elements or terms. And how we can describe a sequence by indicating the initial value, the stopping point and the general case, as in:
A statement like that means start where $i = 0 $ and end where
We then saw how to add the terms in a sequence using the sigma notation as in: