#Chapitre 4
#page 208
#Exercice 4.2
#2)
dnorm(0)
## [1] 0.3989423
pnorm(2.58)
## [1] 0.99506
qnorm(0.975)
## [1] 1.959964
rnorm(50)
##  [1] -1.75023587 -1.16515315 -0.33832566 -0.16087825 -1.84759466  1.67408683
##  [7] -0.69901709  0.29042837 -0.52170308 -0.04732824 -0.27755556 -2.42689540
## [13] -0.15876096  0.48181983  0.30932138  1.11028663  0.17016577 -1.71105935
## [19]  0.87234769  0.02073609  0.10693418  0.82554466 -0.82555066 -0.59648054
## [25]  1.19336014 -0.11665939 -0.92837073 -2.64119412 -0.43138485 -1.36364740
## [31] -2.05205604  1.17532804 -0.56626106 -0.28492800  1.54513661  1.28153066
## [37] -1.33335215 -1.27985636  1.01968988  0.34876720 -0.51687264 -0.82906139
## [43]  2.74151162  0.50684698  1.42258633  1.73631605 -0.16866625  0.19863208
## [49] -1.57226248 -0.53002303
rnorm(20,mean=10,sd=2)
##  [1] 10.138587  8.405797 12.287277  8.440219 10.588264 10.850547 12.517191
##  [8] 10.923820  9.525717 13.260574  7.660464 10.377082 12.355166 13.440012
## [15] 11.642910  9.798165  6.268943 10.397289 10.823757  9.884312
x=seq(-5,5,0.1) ;pdf=dnorm(x) ;plot(x,pdf,type="l",main="Densit\'e d'une loi 
  normale centr\'ee et r\'eduite")
library(ggplot2)

ggplot(data.frame(x=c(-5,5)),aes(x))+stat_function(fun=dnorm)+
  ggtitle("Densit\'e d'une loi normale centr\'ee et r\'eduite")+ylab("Densit\'e")

runif(10)
##  [1] 0.2004745 0.8643460 0.5800663 0.7539867 0.6410979 0.8302413 0.9097534
##  [8] 0.9781148 0.5080315 0.1315861
rt(10,20)
##  [1]  0.2147940  2.0844332 -2.6900174 -2.1538266 -1.3685199  1.3780932
##  [7] -0.4843694 -0.9472755 -0.5597388  0.7662008
#Exercice 4.1
#page 211
#1)
#couleurs <- gray(c(0,.25,.5,.75)) #En nuances de gris comme dans le livre
couleurs<-c("black","red","green","blue") #En couleurs

fd<-function(x) {dbinom(x,5,0.5)}
plot(cbind(0:5,sapply(0:5,fd)),xlim=c(0,20),ylim=c(0,.40),type="p",ylab="",xlab="",
  pch=15,cex=2,lwd=3,col=couleurs[1],cex.axis=2)
fd<-function(x) {dbinom(x,10,0.5)}
points(cbind(0:10,sapply(0:10,fd)),xlim=c(0,20),ylim=c(0,.40),type="p",ylab="",xlab="",
  pch=16,cex=2,lwd=3,col=couleurs[2])
#L'option new=TRUE n'est pas n\'ecessaire pour que la fonction points ajoute les points 
# au graphique d\'ej\`a existant
fd<-function(x) {dbinom(x,20,0.5)}
points(cbind(0:20,sapply(0:20,fd)),xlim=c(0,20),ylim=c(0,.40),type="p",ylab="",xlab="",
  pch=17,cex=2,lwd=3,col=couleurs[3])
#L'option new=TRUE n'est pas n\'ecessaire pour que la fonction points ajoute les points 
# au graphique d\'ej\`a existant
legtxt<-c(expression(paste(italic(n)," = 5",sep="")),expression(paste(italic(n)," = 10",
  sep="")),expression(paste(italic(n)," = 20",sep="")))
legend("topright",legtxt,title=expression(paste(italic(p)," = 0,5",sep="")),pch=c(15,16,
  17),col=c(couleurs[1],couleurs[2],couleurs[3]),cex=2,bg="white",inset=.075)

#page 212
#2)
dhypergeom<-function(x,N,n,p) (choose(N*p,x)*choose(N*(1-p),n-x)/choose(N,n))
fd<-function(x) {dhypergeom(x,14,10,0.5)}
plot(cbind(0:10,sapply(0:10,fd)),xlim=c(0,10),ylim=c(0,.5),type="p",ylab="",xlab="",
  pch=15,cex=2,lwd=3,col=couleurs[4],cex.axis=2)
fd<-function(x) {dhypergeom(x,20,10,0.5)}
points(cbind(0:10,sapply(0:10,fd)),xlim=c(0,10),ylim=c(0,.5),type="p",ylab="",xlab="",
  pch=16,cex=2,lwd=3,col=couleurs[3],new=T)
fd<-function(x) {dhypergeom(x,50,10,0.5)}
points(cbind(0:10,sapply(0:10,fd)),xlim=c(0,10),ylim=c(0,.5),type="p",ylab="",xlab="",
  pch=17,cex=2,lwd=3,col=couleurs[2],new=T)
fd<-function(x) {dbinom(x,10,0.5)}
points(cbind(0:10,sapply(0:10,fd)),xlim=c(0,10),ylim=c(0,.5),type="p",ylab="",xlab="",
  pch=18,cex=2,lwd=3,col=couleurs[1],new=T)
legtxt<-c(expression(paste(italic(N)," = 14",sep="")),expression(paste(italic(N)," = 20",
  sep="")),expression(paste(italic(N)," = 50",sep="")),expression(paste(italic(B),
  "(10;0,5)",sep="")))
legend("topright",legtxt,title=expression(paste(italic(n)," = 10 et ",italic(p)," = 0,5",
  sep="")),pch=c(15,16,17,18),col=c(couleurs[4],couleurs[3],couleurs[2],couleurs[1]),
  cex=1.6,bg="white",inset=.0)

#3)
fr<-function(x) {pchisq(x,1)}
curve(fr,from=-1,to=9,ylab="",xlab="",lty=1,lwd=3,col=couleurs[1],type="n",cex.axis=2)
curve(fr,from=-1,to=-0.000001,ylab="",xlab="",lty=5,lwd=3,add=TRUE,col=couleurs[1])
curve(fr,from=0.000001,to=9,ylab="",xlab="",lty=5,lwd=3,add=TRUE,col=couleurs[1])
fr<-function(x) {pchisq(x,3)}
curve(fr,from=-1,to=-0.000001,ylab="",xlab="",lty=1,lwd=3,col=couleurs[3],add=TRUE)
curve(fr,from=0.000001,to=9,ylab="",xlab="",lty=4,lwd=3,col=couleurs[3],add=TRUE)
fr<-function(x) {pchisq(x,2)}
curve(fr,from=-1,to=-0.000001,ylab="",xlab="",lty=2,lwd=3,add=TRUE,col=couleurs[2])
curve(fr,from=0.000001,to=9,ylab="",xlab="",lty=2,lwd=3,add=TRUE,col=couleurs[2])
fr<-function(x) {pchisq(x,6)}
curve(fr,from=-1,to=-0.000001,ylab="",xlab="",lty=4,lwd=3,add=TRUE,col=couleurs[1])
#la fin de cette instruction est sur la page 212
curve(fr,from=0.000001,to=9,ylab="",xlab="",lty=1,lwd=3,add=TRUE,col=couleurs[4])

#page 213
legtxt<-c(expression(paste(italic(p)," = 1",sep="")),expression(paste(italic(p)," = 2",
  sep="")),expression(paste(italic(p)," = 3",sep="")),expression(paste(italic(p)," = 6",
  sep="")))
legend("bottomright",legtxt,lty=c(5,2,4,1),lwd=3,col=c(couleurs[1],couleurs[2],
  couleurs[3],couleurs[4]),cex=2,bg="white",inset=.0375)

#4)
fd<-function(x) {dnorm(x)}
curve(fd,from=-4,to=4,ylab="",xlab="",lty=5,lwd=3,add=FALSE,col=couleurs[1],cex.axis=2)
fd<-function(x) {dt(x,1)}
curve(fd,from=-4,to=4,ylab="",xlab="",lty=1,lwd=3,add=TRUE,col=couleurs[2])
fd<-function(x) {dt(x,2)}
curve(fd,from=-4,to=4,ylab="",xlab="",lty=2,lwd=3,add=TRUE,col=couleurs[3])
fd<-function(x) {dt(x,5)}
curve(fd,from=-4,to=4,ylab="",xlab="",lty=4,lwd=3,add=TRUE,col=couleurs[4])
legtxt<-c(expression(paste(italic(n)," = 1",sep="")),expression(paste(italic(n)," = 2",
  sep="")),expression(paste(italic(n)," = 5",sep="")),expression(paste(italic(N),"(0;1)",
  sep="")))
legend("topleft",legtxt,lty=c(1,2,4,5),lwd=3,col=c(couleurs[2],couleurs[3],couleurs[4],
  couleurs[1]),cex=1.6,bg="white",inset=.0375)

#Exercice 4.2
dnorm(0)
## [1] 0.3989423
#page 214
1/sqrt(2*pi)
## [1] 0.3989423
pnorm(2.58)
## [1] 0.99506
qnorm(0.975)
## [1] 1.959964
rnorm(50)
##  [1] -1.37601456 -0.99029939  0.23948586  0.33883693  0.10132752  0.79271957
##  [7] -0.46091495  1.69547683  0.33278003  0.45347079  0.92417076 -0.18062408
## [13]  0.63362662 -0.54121538 -1.07895089  0.72729541  1.02235014  0.60805999
## [19]  1.37788624  1.86304558 -1.27723246  0.54035376 -0.95220073 -0.16126697
## [25]  0.13518886  0.20433516 -0.82727850 -1.27328052 -0.45556086  0.56624169
## [31]  1.23748409  1.02704003 -2.29228085  0.07425695  0.65310535 -0.47300568
## [37]  0.69461591  0.78934709  0.93488507  0.86480977  1.71789539  0.42706195
## [43]  0.62724736 -1.27334070  1.83704996 -0.77658946 -1.66694068 -0.29863899
## [49]  0.05341075  0.78458103
rnorm(20,mean=10,sd=2)
##  [1] 11.104852  9.141281  9.366384  9.769360  7.697745  8.323448  8.304909
##  [8]  6.270275 12.433411  6.079080  6.897922  6.967557 12.086838  9.395228
## [15]  9.741755 10.384851  5.508039 12.356227  8.591180  9.054785
#page 215
x=seq(-5,5,0.1) ;pdf=dnorm(x) ;plot(x,pdf,type="l",
  main="Densit\'e de la loi normale centr\'ee et r\'eduite")

runif(10)
##  [1] 0.11028185 0.70434666 0.47876525 0.53792348 0.79025492 0.58787752
##  [7] 0.47982017 0.68275839 0.24812872 0.07324685
#page 216
rt(10,20)
##  [1] -0.82566572  0.64713594 -0.89981167  0.04181529 -0.32861640  0.96021948
##  [7] -2.50781906 -0.75937499  1.03846304  1.00150766
#Exercice 4.3
#1)
dbinom(5,150,0.02)
## [1] 0.1011484
#page 217
pbinom(3,150,0.02)
## [1] 0.6472395
qbinom(0.99,150,0.02)
## [1] 8
#page 218
#Exercice 4.4
#1)
qbinom(0.95,230,0.85,lower.tail = FALSE)
## [1] 186
qbinom(0.95,240,0.85,lower.tail = FALSE)
## [1] 195
qbinom(0.95,246,0.85,lower.tail = FALSE)
## [1] 200
plot(230:250,qbinom(0.95,230:250,0.85,lower.tail = FALSE))
abline(h=200)
abline(v=246)

#page 219
#2)
which.max(dbinom(0:330,330,.85))
## [1] 282
plot(0:330,dbinom(0:330,330,.85),xlab="n",ylab="Probabilit\'e",lwd=2)

plot(270:285,dbinom(270:285,330,.85),xlab="n",ylab="Probabilit\'e",lwd=2)

#En plus : code figure 424
old.par <- par(no.readonly = TRUE)
layout(t(1:2))
plot(0:330,dbinom(0:330,330,.85),xlab="n",ylab="Probabilit\'e",lwd=2)
plot(270:285,dbinom(270:285,330,.85),xlab="n",ylab="Probabilit\'e",lwd=2)
abline(v=281)

layout(1)
par(old.par)

old.par <- par(no.readonly = TRUE)
pdf("figure424.pdf",h=6,w=9)
layout(t(1:2))
par(oma=rep(0,4));par(mar=c(4, 4, 2, 2) + 0.1)
plot(0:330,dbinom(0:330,330,.85),xlab="n",ylab="Probabilit\'e",lwd=2)
plot(270:285,dbinom(270:285,330,.85),xlab="n",ylab="Probabilit\'e",lwd=2)
abline(v=281)
layout(1)
dev.off()
## quartz_off_screen 
##                 2
par(old.par)

#page 220
#Exercice 4.5
#1)
1-pnorm(80,92,8)
## [1] 0.9331928
#page 221
#2)
(1-pnorm(80,92,8))*6000
## [1] 5599.157
#3)
which.max(dbinom(0:6000,6000,.9331928))
## [1] 5601
plot(0:6000,dbinom(0:6000,6000,.9331928),xlab="n",ylab="Probabilit\'e",lwd=2)

plot(5500:5700,dbinom(5500:5700,6000,.9331928),xlab="n",ylab="Probabilit\'e",lwd=2)

plot(5590:5610,dbinom(5590:5610,6000,.9331928),xlab="n",ylab="Probabilit\'e",lwd=2)

dbinom(5599,6000,.9331928)
## [1] 0.02061832
#page 222
dbinom(5600,6000,.9331928)
## [1] 0.02062328
#En plus : code figure 425
old.par <- par(no.readonly = TRUE)
layout(matrix(c(1,2,1,3),nrow=2))
par(oma=rep(0,4));par(mar=c(4, 4, 2, 2) + 0.1)
plot(0:6000,dbinom(0:6000,6000,.9331928),xlab="n",ylab="Probabilit\'e",lwd=2)
plot(5500:5700,dbinom(5500:5700,6000,.9331928),xlab="n",ylab="Probabilit\'e",lwd=2)
plot(5590:5610,dbinom(5590:5610,6000,.9331928),xlab="n",ylab="Probabilit\'e",lwd=2)

layout(1)
par(old.par)

old.par <- par(no.readonly = TRUE)
pdf("figure425.pdf",h=6,w=9)
layout(matrix(c(1,2,1,3),nrow=2))
par(oma=rep(0,4));par(mar=c(4, 4, 2, 2) + 0.1)
plot(0:6000,dbinom(0:6000,6000,.9331928),xlab="n",ylab="Probabilit\'e",lwd=2)
plot(5500:5700,dbinom(5500:5700,6000,.9331928),xlab="n",ylab="Probabilit\'e",lwd=2)
plot(5590:5610,dbinom(5590:5610,6000,.9331928),xlab="n",ylab="Probabilit\'e",lwd=2)
layout(1)
dev.off()
## quartz_off_screen 
##                 2
par(old.par)