Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The geometric mean ( Qn i = 1 ai ) 1 / n may cause numerical instability if the products become too large. One simple

The geometric mean (
Qn
i=1 ai)
1/n may cause numerical instability if the products become too large. One
simple way of overcoming this issue is to use the identity
Yn
i=1
ai
!1/n
= exp
1
n
Xn
i=1
log ai
!
The code chunk below implements the identity above as the function gmean:
gmean <- function(x) exp(mean(log(x)))
Mike would like to compute the geometric mean for each of 10 columns of a numeric matrix y. He did it
using a for loop, see the code he used in the chunk below.
y_gmean <- numeric(10)
for(i in 1:10){
y_gmean[i]<- gmean(y[,i])
}
Sally saw Mikes code and said he could have achieved the same result using vectorisation (i.e. without using
for or while), which is more efficient computationally. Write down a piece of code that would achieve the
same result as Mikes code above, but using vectorisation instead of a for loop

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions