#
#Chapitre 4
#page 207
#Exercice 4.2
#2)
dnorm(0)
## [1] 0.3989423
pnorm(2.58)
## [1] 0.99506
qnorm(0.975)
## [1] 1.959964
rnorm(50)
##  [1]  0.21571230 -0.45034552  1.27575296  1.23096299 -2.46651886
##  [6]  0.08429348 -0.16603582  0.35814515  0.83358634 -0.17166951
## [11]  0.07718835  0.08165268  0.18863266  1.61720033  1.34333091
## [16]  0.89599585  2.19928316 -0.29935424  0.64830664 -0.63220332
## [21]  0.52411390  1.00453824 -0.04799698  0.12885820 -1.17559867
## [26]  1.37280361 -0.70543828 -0.71184202  0.01831735 -1.22553891
## [31] -1.81531152 -0.49006113 -0.25050303 -0.68145337  0.22276978
## [36] -0.35570927  1.14479695  1.17858715  0.42961613 -0.27447964
## [41]  2.02439214 -0.28708170 -0.27812632  0.61506611  0.66188517
## [46]  1.84235188  1.18142864  0.38810714 -0.68386585 -1.06227777
rnorm(20,mean=10,sd=2)
##  [1]  8.846860  9.147399  9.031005 12.206306 11.072784 12.053238 10.230073
##  [8] 12.324338 12.230484 10.230533 10.223009 12.183202 12.010867  9.856257
## [15] 11.070046  7.448400  9.268622  7.678449 11.421589 11.587796
x=seq(-5,5,0.1) ;pdf=dnorm(x) ;plot(x,pdf,type="l",main="Densité d'une loi 
  normale centrée et réduite")
library(ggplot2)

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

runif(10)
##  [1] 0.97436114 0.24806053 0.41041724 0.06464187 0.95366624 0.77728044
##  [7] 0.59035857 0.19677347 0.25283772 0.62045207
rt(10,20)
##  [1]  0.5284153  0.4046336  0.1280543  0.5681407 -2.5962727 -0.1589529
##  [7]  0.6214603 -1.2367510 -0.3272505 -2.2183527
#Exercice 4.1
#page 210
#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écessaire pour que la fonction points ajoute les points 
# au graphique déjà 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écessaire pour que la fonction points ajoute les points 
# au graphique déjà 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 211
#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)