Statistical methods in water resources with Python
Now Hatariwater is Hatarilabs!
Please visit our site www.hatarilabs.com
The following text deals with statistics on water resources coupled with related Python codes. The theory comes from Chapter 1 of Statistical Methods in Techniques of Water Resources Investigations, Helsel (2002). Scripts are written in Python with the IPython interphase.
Classical Measure - the Mean
The mean (X) is computed as the sum of all data values Xi, divided by the sample size. Data points further from the center exert a stronger downward force than those closer to the center. If one point near the center was removed, the balance point would only need a small adjustment to keep the data set in balance. But, if one outlying value was removed, the balance point would shift dramatically. Mean is not a "resistant" measure of location.
Figure 1. Mean dependancy from extreme values from Helsel (2002).
Resistant Measure - the Median
The median, or 50th percentile P0.50, is the central value of the distribution when data are ranked in order of magnitude. The median is only minimally affected by the magnitude of a single observation. This resistance to the effect of a change in value or presence of outlying observations is often a desirable property.
In[]: import numpy as np a = np.array([2,4,8,9,11,11,12]) b = np.array([2,4,8,9,11,11,120]) print "The arithmetic median of the set a is:", np.mean(a) print "The arithmetic median of the set b is:", np.mean(b) print "But" print "The mean value of the set a is:", np.median(a) print " The mean value of the set b is:", np.median(b) Out[]: The arithmetic median of the set a is: 8.14285714286 The arithmetic median of the set b is:23.5714285714 But The mean value of the set a is:9.0 The arithmetic median of the set b is:9.0
Other Measures of Location
Three other measures of location are less frequently used: the mode, the geometric mean, harmonic mean and the trimmed mean.
It is the value having the highest bar in a histogram. It is far more applicable for grouped data, which are recorded only as falling into a finite number of categories, than for continuous data. It is very easy to obtain, but a poor measure of location for continuous data, as its value often depends on the arbitrary grouping of those data.
In[]: import numpy as np import scipy.stats a = np.array([1,2,3,1,2,1,1,1,3,2,2,1]) b = (3,4,5,6,7,8,5,5,6,2,3,1,2,5,5,1,3,2,2,1) print "The mode of the set a is: ", scipy.stats.mode(a) print "The mode of the set b is:", scipy.stats.mode(b) #if we do not like to see the parentheses we should use the following script print "The mode of the set a is:", scipy.stats.mode(a)[0][0], "and it is repeted",scipy.stats.mode(a)[1][0] ," times" print " The mode of the set a is:", scipy.stats.mode(b)[0][0], "and it is repeted",scipy.stats.mode(a)[1][0], " times " Out[]: The mode of the set a is:(array([ 1.]), array([ 6.])) The mode of the set b is: (array([ 5.]), array([ 5.])) The mode of the set a is: 1.0 and it is repeted 6.0 times The mode of the set b is: 5.0 and it is repeted 6.0 times
Helsel, D.R. and R. M. Hirsch, 2002. Statistical Methods in Water Resources Techniques of Water Resources Investigations, Book 4, chapter A3. U.S. Geological Survey. 522 pages.