## Arithmetic, Harmonic, and Geometric Means with R

One of the most popular posts on EconomistAtLarge.com is an article explaining the differences between arithmetic, harmonic, and geometric means.   Here, we will build on that article by demonstrating differences between the various means (or averages) and providing R functions that you can easily and quickly use.

People casually refer to “averages” in everyday language.  Journalists rely on our understanding of the concept when they describe weather, stock prices, economic indicators, etc.  In fact, most people never put much thought into what they mean when they use the word–however, this often leads to mistakes.

When most people say the average of something is x, what they usually mean is that the arithmetic mean is x.  This is correct for a number of different problems.  The issue is that most people never think about whether that is the right average to use based on the characteristics of the observations.

## The Issue with Outliers

Let’s illustrate a problem with the arithmetic averages by comparing the three averages to two sets of data.  In chart 1, we see a set of 25 observations with no outliers.  The three averages (harmonic, geometric, and arithmetic) are all fairly close (ranging between 4.54 and 4.71).  Since there are no outliers and we have no reason to believe that the observations are inter-dependent (for example, are the returns of a stock), it is safe to use the arithmetic mean as the average.

Chart 1

Now let’s consider what happens when there is a large outlier in the population.  We use the same observations as in chart 1; however, we’ve modified one observation to be significantly larger (by a factor of 2.5) than the others.  See how the arithmetic mean overstates the central tendency of the population?  If you look closely in chart 2, you’ll notice that the arithmetic mean is actually above all but 2 of the observations.  This implies that if you were using the average to forecast (for example, to forecast the next period’s sales), you would be overestimate (by a significant margin).

Chart 2

## Issue with Investment Returns

Another issue arises when evaluating investment returns.  A common mistake is to use the arithmetic average of the returns over time.  Consider the following investment profile.

 Period 1 Period 2 Period 3 Period 4 Period 5 Return 20% 5% 10% -20% 5%

The mistake is to simply add these returns and divide by 5.  Using the arithmetic mean results in an average return of 4%.

However, this is wrong!

### Correct Result using Geometric Mean

Using either R (the code is contained below) or Excel, the geometric mean is 3.09 (rounded).

Next, calculate the final balance of the investment using the geometric mean instead of the arithmetic.

$100&space;*&space;\left&space;(&space;1&space;+&space;.0309&space;\right&space;)^{5}=&space;116.42$

You can compare the result ($116.42) with the table above and confirm that this is correct. Using the arithmetic mean overstated the resulting investment by over 5%. ## When to Use Harmonic or Geometric Instead of Arithmetic Determining which mean to use is straightforward and based on the characteristics of your data. The decision tree below can be a useful visual guide to determining which average you’ll use. However, we recommend to start by visually inspecting your data. Using a tool such as R (preferred) or Excel, load the data and create a bar or line graph. Note whether there are trends or outliers and then follow the map below. The following R code can be used to calculate the harmonic and geometric means of sample data, as well as generate the charts above. R is an easy-to-use and install platform that provides business professionals with a powerful and flexible tool for conducting business analytics. Since it is free (and simple to install), we highly recommend trying this tool out for yourself. You may just find that R is a better alternative than Excel for many tasks. # Economist at Large # www.economistatlarge.com # Last edited May 9, 2013 # Differences between Arithmatic, Harmonic, and Geometric Means library(ggplot2) library(reshape2) # Function to calculate the harmonic mean harmonicMean &lt;- function(array){ if(!is.numeric(array)){ stop(&quot;Passed argument must be an array. Consider using sapply for data frames.&quot;) } if(any(array&lt;0)){ stop(&quot;All values must be greater than zero.&quot;) } length (array) / sum(1 / array) } # Function to calculate the geometric mean geometricMean &lt;- function(array){ if(!is.numeric(array)){ stop(&quot;Passed argument must be an array. Consider using sapply for data frames.&quot;) } if(any(array&lt;0)){ stop(&quot;All values must be greater than zero. If you are attempting to apply this function to rates, convert to +1 format. For example, 5% becomes 1.05 and -20% becomes .8.&quot;) } prod(array)^(1/length(array)) } # Function to capture the three means based on the sample fetchMeans &lt;- function(sample){ #Passed data frame with n number of rows and 2 columns (values and obs) arithmetic &lt;- mean(sample$value)
harmonic &lt;- harmonicMean(sample$value) geometric &lt;- geometricMean(sample$value)
results &lt;- data.frame(arithmetic, harmonic, geometric)

return(results)
}

##### Graphs #####
# Color Scheme
ealred &lt;- &quot;#7D110C&quot;
ealtan &lt;- &quot;#CDC4B6&quot;
eallighttan &lt;- &quot;#F7F6F0&quot;
ealdark &lt;- &quot;#423C30&quot;
ealorange &lt;- &quot;#BB681C&quot;
ealgreen &lt;- &quot;#3e4525&quot;
ealblue &lt;- &quot;#25516d&quot;

# Function that plots the three means for comparison, called below
plot.means &lt;- function(sample) {
# First calculate the various means and then flatten to a data frame that
# can be plotted with ggplot2
results &lt;- fetchMeans(sample)
results.melted &lt;- melt(results, variable.name=&quot;Type&quot;, value.name=&quot;Mean&quot;)

g &lt;- ggplot(sample, aes(x=obs, y=value)) + geom_bar(stat=&quot;identity&quot;, alpha=1, fill=ealtan) +
geom_hline(data=results.melted, aes(yintercept=Mean, color=Type), show_guide=TRUE, size=1) +
scale_color_manual(name=&quot;Type of Mean&quot;,
values=c(ealred, ealorange, ealblue),
breaks=c(&quot;arithmetic&quot;, &quot;harmonic&quot;, &quot;geometric&quot;),
labels=c(paste(&quot;Arithmetic: &quot;, round(results$arithmetic, digits=2)), paste(&quot;Harmonic: &quot;, round(results$harmonic, digits=2)),
paste(&quot;Geometric: &quot;, round(results$geometric, digits=2)))) + scale_x_discrete(breaks=NULL) + labs(x=&quot;Observations&quot;, y=NULL) + theme(panel.background=element_rect(fill=eallighttan)) return(g) } #### Comparison with Normally Distributed Sample #### # First generate 'random' set of n numbers with mean of m. These will be normally # distributed so you expect arithmetic mean, harmonic mean, and geometric # mean to be fairly consistent. n &lt;- 25 m &lt;- 5 sample &lt;- data.frame(&quot;value&quot;=rnorm(n=n, mean=m)) sample$obs &lt;- rownames(sample)

# Next plot the three means, compared with the sample population
g &lt;- plot.means(sample)
g &lt;- g + ggtitle(&quot;Mean Comparison with\nNormally Distributed Sample&quot;)
g
# ggsave(&quot;test.png&quot;)

#### Comparison based on Sample with an Outlier
# Add a few outliers to distort the population
sample.outliers &lt;- sample
sample.outliers[n-2, 1] &lt;- m^2.5

g.outlier &lt;- plot.means(sample.outliers)
g.outlier &lt;- g.outlier + ggtitle(&quot;Mean Comparison using\nSample with Outliers&quot;)
g.outlier