- Community Pick
- Experts Exchange Approved
Introduction
While the average, or arithmetic mean, is probably the most commonly used measure of central tendency, it is certainly not the only such statistic.
In my article Calculating Special Means in Microsoft SQL Server Part 1, I discussed how to calculate the weighted average and harmonic mean. This article will demonstrate how to calculate several two other useful, yet less commonly used means, using Microsoft SQL Server:
Indeed, all four of those means, as well as the standard arithmetic mean, minimum, and maximum, are all specific cases of the generalized power mean.
Sample Data
To facilitate the examples in this article, I used the following T-SQL batch to create and populate three tables:
- tblGeomMean
- tblWeightedGeomMean
- tblQuadMean
If you wish to follow along with the examples on your own, please execute the batch in SQL Query Analyzer (SQL Server 2000), or in the SQL Server Management Studio (SQL Server 2005 or later).
Geometric Mean
The geometric mean is typically used to determine the “average” factor when a series of numbers are to be multiplied together, or in cases of exponential growth or decay. To calculate the geometric mean, take the Nth root of the product obtained by using each member of a data set as a factor:
The geometric mean can only be calculated when all members of the data set are positive.
For example, suppose you have a rectangular prism, with length = 20, width = 10, and height = 5. The volume for that shape is the product of the three dimensions: 20 x 10 x 5 = 1000. Now suppose that you want to know the dimensions of a cube having the same volume. Since the dimensions of a cube are, by definition, equal, then the dimensions are equal to the cube root of 1000, or 10. Thus, 10 is also the geometric mean of the data set {20, 10, 5}.
The simplest way to calculate geometric means in Microsoft SQL Server is by leveraging a property of logarithms: the logarithm of the product of any set of positive factors is equal to the sum of each factor. Thus, we can re-write the basic formula for determining the geometric mean in terms of natural logarithms and the special number e, as SQL Server provides built-in functions, LOG and EXP:
Translating this into a generic SQL statement yields:
If the ValueColumn in your table contains a rate of growth or decay (for example, annual investment returns of 4%, 8%, -15%, and 6%), then we need to adjust these rates by adding one to each item, thus transforming the values into the factors that would be used in determining the end result of the growth or decay. The generic SQL statement then becomes:
A common problem in finance is to determine the compound annual growth rate, or CAGR, for an investment over some number of periods, for which the rates of return for each period are varying but known. In such cases, the CAGR will be the geometric mean.
Consider the per-period returns for the following two investments:
To find the CAGRs for these two investments, use the following SQL statement:
That SQL statement yields the following results:
To validate those results, consider the period-by-period results for Investment A:
Both approaches yield the same result: -0.0040, or -0.40%.
Weighted Geometric Mean
As seen with the harmonic mean in Part 1 of this article, there is also a weighted form for the geometric mean. As the name suggests, use the weighted geometric mean in cases for which the various factors will have unequal weights. For a data set with values {x1, x2, x3, …, xn} and weights {w1, w2, w3, …, wn}, the weighted geometric mean is:
As above for the simple geometric mean, by using the rules of operations with logarithms we are able to simply the formula and place it into terms SQL Server can readily process by using natural logarithms.
As in the simple geometric mean, the values must all be greater than zero. Also, as with the weighted average and weighted harmonic mean, the weights cannot be negative, and there must be at least one non-zero weight. Any SQL statement used for calculating the weighted harmonic mean must test for those conditions.
Translating this into a generic SQL statement yields:
As with the simple geometric mean, if the ValueColumn in your table contains a rate of growth or decay (for example, annual investment returns of 4%, 8%, -15%, and 6%), then we need to adjust these rates by adding one to each item, thus transforming the values into the factors that would be used in determining the end result of the growth or decay. The generic SQL statement then becomes:
This example is similar to Example 5 above, except that this time the rates of return are not stated for each individual period. Instead, our table shows the annual rates of return for periods of a varying number of years.
Thus, to calculate the CAGR for each investment, we must use a weighted geometric mean, for which the weights are the length in years for each segment. To obtain the CAGR for each investment, use the following SQL statement:
This yields the following results:
To validate those results, consider the period-by-period results for Investment A:
Both approaches yield the same result: 0.0309, or 3.09%.
Quadratic Mean
Also sometimes called the root mean square, the quadratic mean is the square root of the average of the squared values in the data set. The general formula for the quadratic mean is:
Like the arithmetic mean and weighted average, but unlike the harmonic mean and geometric mean, the values in the data set can be any real number.
Translating this into a generic SQL statement yields:
The quadratic mean is often used to measure the variability of the pair-wise differences in data points for two data sets. For example, suppose we have two series of experimental data; for each series, our table records the theoretical values for each series, as well as the observed values recorded during the experiment.
To understand the variability of the differences between the theoretical and observed values, we can use the quadratic mean of the differences in the pair-wise values. To calculate the quadratic mean, use the following SQL statement:
This yields the following results:
Generalized Power Mean
All of the means discussed in this article, as well as the familiar arithmetic mean, minimum, and maximum, can all be derived from a generalized power mean. To calculate the generalized mean of a data set for any power p, use the following formula:
For the generalized mean, the following special cases may be of interest:
- p = 1: arithmetic mean
- p = -1: harmonic mean
- p = 0: the generalized mean is undefined for p = 0, but the geometric mean is the limit of the generalized mean as p approaches zero
- Minimum: the limit of the generalized mean approaches negative infinity is the data set minimum
- Maximum: the limit of the generalized mean approaches positive infinity is the data set maximum
Translating this into a generic SQL statement yields (substitute your desired exponent for p):
As with the other means, the generalized mean also has a weighted form:
Translating this into a generic SQL statement yields (substitute your desired exponent for p):
I am not providing examples for the generalized mean, as it is highly unlikely that you will ever need to handle any mean it can generate other than the arithmetic, harmonic, geometric, or quadratic mean, weighted or un-weighted, and this article already provides examples optimized for those sorts of means.
=-=-=-=-=-=-=-=-=-=-=-=-=-
If you liked this article and want to see more from this author, please click here.
If you found this article helpful, please click the Yes button near the:
Was this article helpful?
label that is just below and to the right of this text. Thanks!
=-=-=-=-=-=-=-=-=-=-=-=-=-