jeudi 23 mai 2019

Calculating probability using simulation

Let Xi~U(0,30) be random variables. If one takes a sample of n=12, then we are interested in knowing the probability of the proportion to be greater than 18 i.e. $P(\overline X_{12}>18)$

Without using simulation and applied the CLT, the answer is .1150

Here is my approach to implement this in R using simulation:

SimProb<-function(N)
{
n=12

M<-matrix(runif(1,0,30),N,n)
rowMeann<-rowMeans(M)

for(i in 1:N)
{
  if(rowMeann[i]>18)
    c=cumsum(rowMeann[i])
    c

}
prob<-1-c
return(prob)
}

The code does not show any error but does not work properly.

When I was checking, it turns out c= 6.083532 which can't be.

For different values of N, SimProb returns only negative values, I don't understand why.

Could someone please help me ?

Thank you




Aucun commentaire:

Enregistrer un commentaire