Problem 4

Problem 4.a

#Problem 4
## Problem 4.a
unstableGeoMean = function(dataA){prod(dataA)^(1/length(dataA))}

Problem 4.b

#Problem 4
## Problem 4.b
stableGeoMean = function(dataB){exp(mean(log(dataB)))}

Problem 4.c

#Problem 4
## Problem 4.c
x.10 <- (1:10)
unstableGeoMean = function(dataA){prod(dataA)^(1/length(dataA))}
unstableGeoMean(x.10)
## [1] 4.528729
stableGeoMean = function(dataB){exp(mean(log(dataB)))}
stableGeoMean(x.10)
## [1] 4.528729

Problem 4.d

#Problem 4
## Problem 4.d
x.1000 <- (1:1000)
unstableGeoMean = function(dataA){prod(dataA)^(1/length(dataA))}
unstableGeoMean(x.1000)
## [1] Inf
stableGeoMean = function(dataB){exp(mean(log(dataB)))}
stableGeoMean(x.1000)
## [1] 369.4917

When there are a small set of numbers (for example, from 1 to 10), it is generally sufficient to compute the direct product and nth root for the geometric mean. However, as the size of the numbers increases, the direct multiplication could lead to numerical instability with the problem of product becoming too large or too small. The result of ‘infinite’ in Problem 4.d could be the example.

The logarithmic approach changes the multiplication into addition, which can mitigate the numerical stability issues associated with very large or very small numbers. It tends to provide more accurate results, especially with a larger set of numbers. Thus, in Problem 4.d, the result could still be driven with this approach even though there are 1000 numbers.