0

I have an R function called Likelihood that works well when observations in my code is seq(1, 30). However, I can't understand why observations = seq(1, 50) it starts to give all ZEROS. I appreciate ideas regarding how to overcome this problem?

Here is my R code:

observations = seq(1, 60)  
n = length(observations)  
x_bar = mean(observations) 
SIGMA = 2            
SE = SIGMA / sqrt(n)       
x.min = x_bar - 4*SE
x.max = x_bar + 4*SE

Likelihood = function(x) sapply(lapply(x, dnorm, x = observations, SIGMA), prod) # Natural Log??
curve(Likelihood, from = x.min, to = x.max, col = 'red', lwd = 3)
  • `log(x)` does ln(x) by default. Though your function always seem to return 0. Not sure what you originally intended – OganM Apr 29 '17 at 00:39
  • @OganM, well this is supposed to be the likelihood function, Like stands for likelihood. However, the result of multiplication within the Like function results in funny numbers so it's better to take the natural log of Like. But now I'm getting the different problem you just noticed. –  Apr 29 '17 at 00:53

1 Answers1

3

This is simply numerical error.

Observe dnorm(50, sd = 2) is already getting close to .Machine$double.xmin:

dnorm(60, sd = 2)
# [1] 7.368231e-197
.Machine$double.xmin
# [1] 2.225074e-308

So when you prod a bunch of these they'll become (numerically) 0:

prod(dnorm(58:60, sd = 2))
# [1] 0

See here. If you're trying to plot a normal likelihood, there's no point in going more than 3 standard deviations from the mean.

MichaelChirico
  • 33,841
  • 14
  • 113
  • 198
  • They want to take the log of each element and then sum instead of taking the prod, right? – Frank Apr 29 '17 at 01:10
  • 1
    @Frank if they're doing some maximum likelihood, yes, this is one of the many reasons it's better to deal with log-likelihood! ;-) – MichaelChirico Apr 29 '17 at 01:11
  • Thank you Michael, is there any way so I could be able to plot the likelihood function regardless of the value of SIGMA? –  Apr 29 '17 at 01:12
  • @user138773 as Frank emphasizes, the likelihood itself is often not so useful. The log-likelihood share the important topological properties of the likelihood (i.e., same max/min) and doesn't suffer from the same numerical issues. – MichaelChirico Apr 29 '17 at 01:13