naiveBayes does not give expected probabilities I do not understand how the naiveBayes method from the e1071 package is calculating probabilities when classes are perfectly separable (more generally in categories where there are only one class present). For example:
model = naiveBayes(y~x, data.frame(x=factor(c(0,1,0,1)), y=factor(c(0,1,0,1))))
predict(model, data.frame(x=factor(c(0,1))), type='raw')
# Expected
# 1 0
# 0 1
# Obtained
# 0.999000999 0.000999001
# 0.000999001 0.999000999

What am I missing ?
Thanks in advance
 A: Computer does the calculations for you, computers do not operate on real numbers, but approximate them. The results would never be exact, and depending on how exactly the calculations were done and how the numbers were represented (e.g. 32-bit, 64-bit), this would limit the precision of estimates. I highly recommend you read What Every Computer Scientist Should Know About Floating-Point Arithmetic by David Goldberg as a starter. The results you show are nearly ones and nearly zeros, so nothing seems wrong about them.
A: Providing an answer to this old question in case someone else stumbles here late as I did. It seems like the e1071 package does not compute the posterior probabilities correctly, or perhaps it uses some sort of non-standard calculation. 
I happened to notice this when I was experimenting with the Laplace smoothing constant.
A relatively new package naivebayes seems to work as expected:
> library(naivebayes)
> model = naive_bayes(y~x, data.frame(x=factor(c(0,1,0,1)), y=factor(c(0,1,0,1))))
> predict(model, data.frame(x=factor(c(0,1))), type='prob')
     0 1
[1,] 1 0
[2,] 0 1

Hope this helps!
A: Perhaps you're unfamiliar with the Bayesian method. In the Bayesian view the model doesn't make predictions about the evidence, instead the evidence (data) is certain and it leads us to make predictions about the model.
This is like flipping a biased coin, if you flipped a coin 5 times and got 5 heads you would doubt that it's a fair coin but you wouldn't jump to the conclusion that it lands on head 100% of the time. If you flipped it 100 times and got 100 heads then you would think it's biased but you don't have proof that the chance of heads is $1$, it could be $0.999$ as in your example. You would have to flip an infinite number of coins and never get tails to be 100% sure that a tails cannot occur in the future.
You only have two pieces of data, if we could only make inference from two pieces of data we wouldn't be able to conclude much (like only flipping a coin twice). But in the naive bayes training method the algorithm "observes" these data again and again like flipping a coin repeatedly and observing the same results. This allows its predictions to converge. In your case, once the probability reached 0.999000999 it decided that the algorithm had converged sufficiently so it stopped learning. If it was allowed to continue it would get closer and closer to a probability of 1.
