For those programmers new to statistics, this section covers the Sigma operator, Σ.
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. Since statistical and mathematical texts often used 1-based indexing, some care is required when translating formulae to programming languages that use 0-based indexing.
Our two statistical algorithms have a form more like the following function. 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.
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
Java implementation of Sigma. This computes two values, the sum,
sum and the number of elements,
n.
Example 15.1. Java Sigma Iteration
int sum= 0;
for( int i= 0; i != theList.size(); ++i ) {
int xi= ((Integer)theList.get(i)).intValue();
// int fxi = processing of xi
sum += fxi;
}
int n= theList.size();
This is the Python implemention of Sigma. This computes two
values, the sum, sum and the number of elements,
n.
Example 15.2. Python Sigma Iteration
sum= 0 for i in range(len(theList)):xi= theList[i]
# fxi = processing of xi
sum += fxi n= len(theList)
More advanced Python programmers may be aware that there are
several ways to shorten this loop down to a single expression using
the sum function as well as list displays.
In the usual mathematical notation, an integer index, i is used. In Java or 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. In Java, the use of an iterator is shown below.
Example 15.3. Java Sample Values by Iterator
int sum= 0;
for( Iterator i= theList.iterator(); i.hasNext(); ) {
int xi= ((Integer)i.next()).intValue();
// int fxi = processing of xi
sum += fxi;
}
int n= theList.size();
In Python, the iterator is implied, and the processing simplifies to the following.
Example 15.4. Python Sample Values by Iterator
for xi in theList:# fxi = processing of xi
sum += fxi n= len(theList)
These iterator-based formulations can be slightly faster when
using a LinkedList because this class is
optimized for exactly this kind of sequential access.