Valentin Mercier
Valentin Mercier

Reputation: 385

true in math , false in R

i'm here because i'm using R to simulate some math functions , i'm using this equality

And when i simulate it on R i have two differents graph and i'm not finding where am i wrong .... Can you help me ? Here is the code : Ps: i've also try to make the integral calc by the function integrate but it make the same thing

here we have:

    setwd("~/Desktop/stage rapport")

#Pi probabilité de la réussite d'un épreuve de bernouilli en intégrale
#p : la probabilité d'un tirage de bernouilli
#t : le nombre total de tirage avant échec de l'épreuve
#n : nombre de succés necessaire à la réussite de l'épreuve
#prec : la precision de l'intégrale
Pi = function(t,n,p,prec){
  resintg = 0
  for (i in seq(p,1,by=prec)){
    resintg = resintg + (i^(t-n))*((1-i)^(n-1))*prec
  }
  return(resintg*factorial(t)/(factorial(n-1)*factorial(t-n)))
}

#Ps probabilité de la réussite d'un épreuve de bernouilli en somme totale
#p : la probabilité d'un tirage de bernouilli
#t : le nombre total de tirage avant échec de l'épreuve
#n : nombre de succés necessaire à la réussite de l'épreuve
#prec : la precision de l'intégrale
Ps = function(t,n,p){
  val = 0
  for (k in seq(0,t-n)){
    list = seq(k+1,k+n-1)
    val = val + prod(list)*(p)^(k)
  }

  return((val*((1-p)^n))/factorial(n-1))
}

###################################################
#Le Main :
###################################################


#paramètre
t = 24
n = 8
i = 1
preci = 0.1

#calcul de P(p)1
x = seq(0,1,by=0.01)
y = NULL
for (p in x){
  y = c(y,Pi(t,n,p,preci))
} 

#calcul de P(p)2
x_ = seq(0,1,by=0.01)
y_ = NULL
for (p in x){
  y_ = c(y_,Ps(t,n,p))
} 
#tracer des courbes
png("courbeforum.png",width = 1200, height = 700)
plot(x,y,type = "l",col = "red",ylim = c(0,1))
lines(x_,y_,col="blue")
dev.off()

Upvotes: 1

Views: 95

Answers (1)

rbm
rbm

Reputation: 3253

Increase the precision. If you set it to

preci = 0.001

you'll get an output:

enter image description here

Upvotes: 2

Related Questions