<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Calculating Special Means in Microsoft Access Part 2: Geometric Mean and Quadratic Mean

Published on
17,716 Points
7,416 Views
3 Endorsements
Last Modified:
Awarded

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 Access 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 Access:

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, which we'll also examine in this article.


Note: the SQL statements required for these “special” means are somewhat complex, but should be within the grasp of any intermediate or advanced Access user.  For that reason, I have not provided VBA versions of these functions.  Further, by relying solely on native Access functions, your application will exhibit faster performance.

Sample File

For examples of all of the cases discussed below, please refer to the attached sample file:
Special-Means-Part-2.mdb
This file contains all of the data and query definitions used in the four numbered examples in this article, and you may find the file useful for extending these examples or creating your own new examples.


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:

Geometric MeanThe 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 Access is by leveraging a property of logarithms: the logarithm of the product of any set of positive factors is equal to the sum of the logarithms 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 Access provides built-in functions, Log() and Exp():

Geometric MeanTranslating this into a generic SQL statement yields:

SELECT {group by columns if desired,} IIf(Min([ValueColumn]) > 0,
    Exp(Avg(Log(IIf([ValueColumn] > 0,[ValueColumn], 1)))), Null) AS GeomMean
FROM {tables}
{GROUP BY {group by columns}}

Open in new window

Note: This generic SQL statement uses IIf expressions to handle the constraint that all of the values in the data set must either be null or be positive numbers.  The “outer” IIf expression forces a return of Null if any member of the data set is less than or equal to zero.  The “inner” IIf expression avoids the error that would arise from passing an invalid value to the Log() function; this inner IIf statement is necessary because Access always evaluates both the “if true” and “if false” parts of an IIf expression.
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:

SELECT {group by columns if desired,} IIf(Min([ValueColumn]) > -1,
    Exp(Avg(Log(1 + IIf([ValueColumn] > -1, [ValueColumn], 1)))) - 1, Null) AS GeomMean
FROM {tables}
{GROUP BY {group by columns}}

Open in new window

Note: This generic SQL statement uses IIf expressions similarly, except that instead of testing for greater than zero, we instead test for greater than negative one: since we are already adding one to each item, any table value greater than negative one will become positive after this adjustment.

Example 5: qryGeomMean
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:

tblGeomMeanTo find the CAGRs for these two investments, use the following SQL statement:

SELECT Investment, IIf(Min([AnnualReturn]) > -1,
    Exp(Avg(Log(1 + IIf([AnnualReturn] > -1,[AnnualReturn], 1)))) - 1, Null) AS CAGR
FROM tblGeomMean
GROUP BY Investment;

Open in new window

That SQL statement yields the following results:
Example 5 ResultsTo validate those results, consider the period-by-period results for Investment A:
Geometric Mean ValidationBoth 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:

Weighted Geometric MeanAs 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 that Access 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:
SELECT {group by columns if desired,} IIf(Min([WeightColumn]) >= 0 And Max([WeightColumn]) > 0
    And Min([ValueColumn]) > 0,
    Exp(Sum([WeightColumn] * Log(IIf([ValueColumn] > 0, [ValueColumn], 1))) /
    Sum([WeightColumn])) - 1, Null) AS WtdGeomMean
FROM {tables}
{GROUP BY {group by columns}}

Open in new window

Note: This generic SQL statement uses IIf expressions to handle the constraints that all of the values in the data set must either be null or be positive numbers, that weights cannot be negative, and that there must be at least one non-zero weight.  The “outer” IIf expression forces a return of Null if any value in the data set is less than or equal to zero, or if the weights are invalid.  (Any items with a zero weight are ignored.)

The “inner” IIf expression avoids the error that would arise from passing an invalid value to the Log() function; this inner IIf statement is necessary because Access always evaluates both the “if true” and “if false” parts of an IIf expression.
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:

SELECT {group by columns if desired,} IIf(Min([WeightColumn]) >= 0 And Max([WeightColumn]) > 0
    And Min([ValueColumn]) > -1,
    Exp(Sum([WeightColumn] * Log(1 + IIf([ValueColumn] > -1, [ValueColumn], 1))) /
    Sum([WeightColumn])) - 1, Null) AS WtdGeomMean
FROM {tables}
{GROUP BY {group by columns}}

Open in new window

Note: This generic SQL statement uses IIf expressions similarly, except that instead of testing for greater than zero, we instead test for greater than negative one: since we are already adding one to each item, any table value greater than negative one will become positive after this adjustment.

Example 6: qryWeightedGeomMean
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.

tblWtdGeomMean
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:

SELECT Investment, IIf(Min([Years]) >= 0 And Max([Years])>0 And Min([AnnualReturn]) > -1,
    Exp(Sum([Years] * Log(1 + IIf([AnnualReturn] > -1, [AnnualReturn], 1))) /
    Sum([Years])) - 1, Null) AS CAGR
FROM tblWtdGeomMean
GROUP BY Investment;

Open in new window

This yields the following results:
Example 6 ResultsTo validate those results, consider the period-by-period results for Investment A:

Weighted Geometric Mean ValidationBoth 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:

Quadratic MeanLike 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:

SELECT {group by columns if desired,} Avg([ValueColumn] ^ 2) ^ 0.5 AS QuadMean
FROM {tables}
{GROUP BY {group by columns}}

Open in new window


Example 7: qryQuadMean
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.

tblQuadMean
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:

SELECT Series, Avg(([Theory] - [Observed]) ^ 2) ^ 0.5 AS QuadMean
FROM tblQuadMean
GROUP BY Series;

Open in new window

This yields the following results:
Example 7 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:

Power Mean
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 as p approaches negative infinity is the data set minimum
Maximum: the limit of the generalized mean as p approaches positive infinity is the data set maximum

Translating this into a generic SQL statement yields (substitute your desired exponent for p):

SELECT {group by columns if desired,} Avg([ValueColumn] ^ p) ^ (1 / p) AS GeneralMean
FROM {tables}
{GROUP BY {group by columns}}

Open in new window

Note: Since division by zero is undefined, this general form cannot be used to determine the geometric mean.  This generalized form is also inappropriate for the harmonic mean unless you are confident that all of the values are positive numbers.
As with the other means, the generalized mean also has a weighted form:

Weighted Power MeanTranslating this into a generic SQL statement yields (substitute your desired exponent for p):
SELECT {group by columns if desired,} (IIf(Min([WeightColumn]) >= 0 And Max([WeightColumn]) > 0,
    Sum([WeightColumn] * [ValueColumn] ^ p), Null) / Sum([WeightColumn])) ^ (1 / p) AS WtdGeneralMean
FROM {tables}
{GROUP BY {group by columns}}

Open in new window


Note: The same caveats as for the un-weighted generalized mean apply here.  In addition, as with the weighted average and weighted harmonic mean, the weights cannot be negative, and you must have at least one positive weight.  There is no need to test for the sum of weights being zero: if the sum of weights is zero, the numerator will already be Null, and so Access will not attempt to perform the division.

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!
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
3
Comment
0 Comments

Featured Post

Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

Join & Write a Comment

Suggested Articles

With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month