Write a function that calculates the arithmetic mean (average), geometric mean,
ID: 3759556 • Letter: W
Question
Write a function that calculates the arithmetic mean (average), geometric mean, rms ( root mean square) average and harmonic mean of a set of numbers.
For a set of numbers x1,x2,x3,...,xN :
1/ The function file (calc_means.m) should take as input a vector x containing the numbers to be averaged and should calculate the different means (arithm_mean, geom._mean, rms_ave and harm_mean). You must use vectorization to calculate the different means. Check two Matlab built in functions called “sum” and “prod” that allow you to calculate the sum and product of the elements of a vector.
2/ Create a script file to ask the user how many input values he wants to enter. Then ask the user to enter the values (one by one in a for loop). Call the function and then print out the results
3/ Test the program for the following set of values:
(a) 3,3,3,3,3,3,3,3,3 (b) 4,3,2,5,1,3,6,1,2
Explanation / Answer
Solution:
========
Calculating Geometric Means with Zero Values
The calculation of the Geometric Mean may appear impossible if one or more of the data points is zero (0). In these cases, however, the convention used is that a value of either '1', one half the limit of detection, or some other substitution is allowed for each zero or "less than" value, so that the information contained in these data is not lost. For example, the US Food and Drug Administration in its shellfish sanitation program regulations requires the substitution of a value that is one significant digit less than the detection limit [i.e. "less than 2" becomes "1.9"]. Because of how geometric mean is calculated, the precise substitution value generally does not appreciably affect the result of the calculation, and ensures that all the data remains usable. To see how to construct a spreadsheet formula to change censored values to one significant digit, see the Spreadsheet Tips section below.
Here is an example with a non detect (and assuming the detection limit was 2 bacteria per 100 milliliters):
1100
0 ("less than 2")
30
13000
Geometric Mean = 4th root of 1100 X 1 X 30 X 13000
= 4th root of 429,000,000
Geometric Mean = 143.9
Incidentally, substituting 1.9 for the less than value results in a geometric mean of 169.0, which is nearly statistically different (alpha=0.05) using a t-test using the substituted value 1.0. See additional comments in the bacteria data section below.
Debate on the use of substitutions of below reporting limits and other censored data
Many statisticians have criticized common procedures for providing substituted values for non-detects or below-reported-limits value data. Other alternatives, such as "delta log-normal models" have also received criticism and even legal challenges when applied to regulatory discharges permits. These problems and alternative analysis strategies are presented in Helsel (1990, 2005) and EPA (2002). These references also contain useful citations to other publications.
Calculating Geometric Means with Negative Values
Like zero, it is impossible to calculate Geometric Mean with negative numbers. However, there are several work-arounds for this problem, all of which require that the negative values be converted or transformed to a meaningful positive equivalent value. Most often this problem arises when it is desired to calculate the geometric mean of a percent change in a population or a financial return, which includes negative numbers.
For example, to calculate the geometric mean of the values +12%, -8%, and +2%, instead calculate the geometric mean of their decimal multiplier equivalents of 1.12, 0.92, and 1.02, to compute a geometric mean of 1.0167. Subtracting 1 from this value gives the geometric mean of +1.67% as a net rate of population growth (or in financial circles is called the Compound Annual Growth Rate-CAGR).
Incidentally, if you do not have a negative percent value in a data set, you should still convert the percent values to the decimal equivalent multiplier. It is important to recognize that when dealing with percents, the geometric mean of percent values does not equal the geometric mean of the decimal multiplier equivalents.
For example:
Geometric mean of [12%, 4%, 2%] does not equal the Geometric mean of [1.12,1.04,1.02].
4.6% does not equal 5.9%
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.