An efficient way to do this in Python is to use reduce().
To solve
3 Σ i i=1You can use the following:
from functools import reduce result = reduce(lambda a, x: a + x, [0]+list(range(1,3+1))) print(result)reduce() will take arguments of a callable and an iterable, and return one value as specified by the callable. The accumulator is a and is set to the first value (0), and then the current sum following that. The current value in the iterable is set to x and added to the accumulator. The final accumulator is returned.
The formula to the right of the sigma is represented by the lambda. The sequence we are summing is represented by the iterable. You can change these however you need.
For example, if I wanted to solve:
Σ π*i^2 iFor a sequence I [2, 3, 5], I could do the following:
reduce(lambda a, x: a + 3.14*x*x, [0]+[2,3,5])You can see the following two code lines produce the same result:
>>> reduce(lambda a, x: a + 3.14*x*x, [0]+[2,3,5]) 119.32 >>> (3.14*2*2) + (3.14*3*3) + (3.14*5*5) 119.32For those programmers new to statistics, this section provides background on the Sigma operator, Σ.
Equation 13.3. Basic Summation
The Σ operator has three parts to it. Below it is a bound variable, i and the starting value for the range, written as i=0. Above it is the ending value for the range, usually something like n. To the right is some function to execute for each value of the bound variable. In this case, a generic function, f( i). This is read as “sum f( i) for i in the range 0 to n”.
One common definition of Σ uses a
closed range, including the end values of 0 and n. However, since this is not a helpful definition for software, we will define Σ to use a half-open interval. It has exactly n elements, including 0 and n-1; mathematically,
Consequently, we prefer the following notation, but it is not often used. Most statistical and mathematical texts seem to use 1-based indexing, consequently some care is required when translating formulae to programming languages that use 0-based indexing.
Equation 13.4. Summation with Half-Open Interval
Our two statistical algorithms have a form more like the following. In this we are applying some function, f(), to each value, xi of an array. When computing the mean, there is no function. When computing standard deviation, the function involves subtracting and multiplying.
Equation 13.5. Summing Elements of an Array, x
We can transform this definition directly into a for loop that sets the bound variable to all of the values in the range, and does some processing on each value of the list of integers. This is the Python implemention of Sigma. This computes two values, the sum, sum, and the number of elements, n.
Example 13.2. Python Sigma Iteration
sum= 0 for i in range(len(theList)):
| Get the length of theList. Execute the body of the loop for all values of i in the range 0 to the number of elements-1. |
| Fetch item i from theList and assign it to xi. |
| For simple mean calculation, this statement does nothing. For standard deviation, however, this statement computes the measure of deviation from the average. |
More advanced Python programmers may be aware that there are several ways to shorten this loop down to a single expression using the reduce function as well as list comprehensions.
An Optimization. In the usual mathematical notation, an integer index, i is used. In Python it isn't necessary to use the formal integer index. Instead, an iterator can be used to visit each element of the list, without actually using an explicit numeric counter. The processing simplifies to the following.
Example 13.3. Python Sample Values by Iterator
for xi in theList:
| Execute the loop assigning each item of of theList to xi. |
| For simple mean calculation, this statement does nothing. For standard deviation, however, this statement computes the measure of deviation from the average. |