0

I calculated the theoretical mode for X ~ Expo(lambda) and got it as 0. However, when I simulated the scenario in R, I am getting some values. My code is shown below:

sample_mode <- function(x) {
  t <- table(x)
  m <- max(t)
  as.numeric(names(t[t==m]))
}
simfunc <-function(){
  x <- rexp(10000,2)
  median <- median(x)
  mode <- sample_mode(x)

  cat("The median is", median)
  cat("The mode is", mode)
}

The median matches, but I am getting a vector of values for the mode.

  • 2
    Why don't you look at the data? A quick graph, such as a histogram, ought to answer your question immediately. – whuber Nov 04 '17 at 19:21
  • I had a look at the histogram and it makes sense, but it my theoretical mode wrongly calculated then? –  Nov 04 '17 at 19:23
  • It's the way you're calculating the mode in your sample that's the problem. For some similar points to the ones in Tim's answer, also see the discussion of sample mode [here](https://stats.stackexchange.com/questions/157661/how-to-calculate-mean-median-mode-std-dev-from-distribution/157669#157669) (though the mention of smoothing won't be much use when the mode is at the boundary, unless the smoothing also takes account of that) – Glen_b Nov 04 '17 at 23:42

1 Answers1

4

Exponential distribution is a distribution for a continuous random variable. For continuous random variables $\Pr(X=x)=0$ for any $x$, so for your variable you would expect each of the distinct values not more then once in your sample, so your function for calculating mode would not produce any reasonable result. Here you can find some hints how you could calculate mode for continuous variable.

Tim
  • 108,699
  • 20
  • 212
  • 390