Compute the Poisson distribution

25 Views Asked by At

I have the following problem:

" In a city two serious accidents happen per week on average. In particular, we assume that the number of serious accidents is Poisson distributed. Calculate the probability that more than five serious accidents happen per week "

$$ p(k) = \text{P}(X = k) = e^{-\lambda}\frac{\lambda^k}{k!} $$

I want to write the code to give me the answer:

I know that there is the R built-in function ppois and indeed 1-ppois(5, lambda=2) gives me the correct answer (0.01656361). However, as an exercice, I want to implement the equation in code myself. However the following code doesn't give me the correct answer

lambda = 2
k = 5
p_k = exp(-lambda)*lambda^k/factorial(k)
p_k

Is it because I should write the sum of the probabilities $\text{P}(X > k)$?

Edit

Here is the answer:

lambda = 2
p_k = 0
for (k in 0:5){
 p_k = p_k + exp(-lambda)*lambda^k/factorial(k)
 p_k
}
1-p_k
1

There are 1 best solutions below

0
On

You've computed $P(X=5)$, not $P(X>5)=1-P(X\le 5)=1-\sum_{i=0}^5 P(X=i)$ as you should have done.