Soit \((\Omega, \mathcal{A},\mathbb{P})\) un espace de probabilité. Soient \(X\) et \(Y\) deux évènements de \(\mathcal{A}\). Alors \[ \mathbb{P}\left( X\mid Y\right)= \displaystyle\frac{ \mathbb{P}(X\cap Y)}{ \mathbb{P}(Y)}\]
On considĆØre un test pour une infection. On suppose que la sensitivitĆ© du test est Ć©gale Ć 99\(\%\) (la probabilitĆ© quāun infectĆ© soit dĆ©tĆ©ctĆ© positif) et que la spĆ©cificitĆ© du test est Ć©gale Ć 97\(\%\) (la probabilitĆ© quāune personne saine soit dĆ©tĆ©ctĆ©e nĆ©gative). Sachant que la proportion des personnes infectĆ©es dans une population est Ć©gale Ć 1\(\%\).
R
> N=10000 # Taille de la population
> p=0.01 # ProbabilitƩ d'infection
> population=rbinom(N,1,prob = p)
> N_infected=sum((population==1));N_infected ## Nombre d'infectƩs
[1] 101
> infected=which(population==1)
> sensitivity=.99
> specificity=.97
> test=rbinom(N,1,(population==1)*sensitivity+(population==0)*(1-specificity))
> N_test=sum((test==1));N_test ## Nombre de positifs.
[1] 393
test
x population
> confusion=xtabs(~population+test);confusion
test
population 0 1
0 9607 292
1 0 101
Donc la probabilitĆ© quāune personne ayant eu un rĆ©sultat positif au test soit dĆ©tectĆ©e infectĆ©e se calcule de la faƧon suivante
> confusion[2,2]/(confusion[1,2]+confusion[2,2])
[1] 0.2569975
> test2=rbinom(N,1,(population==1)*sensitivity+(population==0)*(1-specificity))
population
, test
et test2
> confusion2=xtabs(~population+test2+test);confusion2
, , test = 0
test2
population 0 1
0 9298 309
1 0 0
, , test = 1
test2
population 0 1
0 280 12
1 0 101
> confusion2[2,2,2]/(confusion2[2,2,2]+confusion2[2,1,2])
[1] 1
On note par \(M\) lāĆ©vĆ©nement quāune personne soit infecetĆ©e et par \(P\) quāelle soit dĆ©tectĆ©e positive. Alors \[\mbox{sensitivitĆ©}=\mathbb{P}(P\mid M)=\displaystyle\frac{\mathbb{P}(P\cap M)}{\mathbb{P}(M)}=.99\] et \[\mbox{spĆ©cificitĆ©}=\mathbb{P}(\overline{P}\mid \overline{M})=\displaystyle\frac{\mathbb{P}(P\cap M)}{\mathbb{P}(\overline{M})}=.97\]
Donc la la probabilitĆ© quāune personne ayant eu un rĆ©sultat positif au test soit dĆ©tectĆ©e infectĆ©e se calcule
\[ \mathbb{P}(M\mid P)=\displaystyle\frac{\mathbb{P}(P\cap M)}{\mathbb{P}(P)}=\displaystyle\frac{\mathbb{P}(P\mid M)\mathbb{P}(M)}{\mathbb{P}(P\mid \overline{M})\mathbb{P}(\overline{M})+\mathbb{P}(P\mid M)\mathbb{P}(M)}=\displaystyle\frac{.99 \times .01}{(1-.97)\times (1-.01)+ .99 \times .01}=0.25\]
Soit \(\mathcal{M}\) un modĆØle paramĆ©trique : Soit \(\underline{X}=(X_1,\ldots,X_n)\) un \(n-\)Ć©chantillon de loi \(\mathbb{P}_\theta\) dĆ©pendant dāun paramĆØtre \(\theta\in \Theta \subseteq \mathbb{R}^p\).
Rappelons un estimateur de \(\theta\) est une fonction du \(n-\)Ʃchantillon \(\underline{X}\) : \[\widehat{\theta}(\underline{X})\in \mathcal{E}\supseteq \Theta\]
Comment Ć©valuer la qualitĆ© dāun estimateur \(\widehat{\theta}\) ?
Fonction de Perte : \[L\,:\,\Theta \times \mathcal{E} \longrightarrow \mathbb{R}^+\] telle que \(L(\theta,\theta)=0\) pour tout \(\theta\in \Theta\). Donc lāerreur de lāestimation de \(\widehat{\theta}\) est mesurĆ©e par \(L(\theta,\widehat{\theta})\).
Fonction de Risque \[\theta\longmapsto R(\theta,\widehat{\theta})=\mathbb{E}\left[L(\theta,\widehat{\theta})\right]\]
\(L(\theta,\widehat{\theta})=(\theta-\widehat{\theta})^2\) et le risque associƩe est le risque quadratique \[ R(\theta,\widehat{\theta})=\mathbb{E}\left[(\theta-\widehat{\theta})^2\right]\]
\(L(\theta,\widehat{\theta})=|\theta-\widehat{\theta}|\) et le risque associƩe est le risque quadratique \[ R(\theta,\widehat{\theta})=\mathbb{E}\left[|\theta-\widehat{\theta}|\right]\]
Intuitivement plus le risque de lāestimateur est faible plus lāestimateur \(\widehat{\theta}\) est considĆ©rĆ© comme performant.
Soit \(\underline{X}=(X_1,\ldots,X_n)\) telle que pour tout \(i=1,\ldots,n\), \(X_i\sim \mathcal{N}(\theta,1)\). On considĆØre \(\overline{X}\) comme estimateur de \(\theta\). Calculer le risque quadratique de \(\overline{X}\).
Dans le cas bayĆ©sien on suppose quāon dispose dāune certaine information a priori sur le paramĆØtre \(\theta\) quāon la traduit avec une distribution de probabilitĆ© \(\theta\) notĆ©e \(\pi(\theta)\).
Notre objectif est de mettre Ć jour cette information une fois quāon a observĆ© lāĆ©chantillon des donnĆ©es.
Si on suppose que \(\underline{X}=(X_1,\ldots,X_n)\) est issu dāune loi de probabilitĆ© de densitĆ© \(f(x\mid \theta)\). Alors la fonction vraisemblance \[L(\theta,\underline{x})=\prod_{i=1}^n f(x_i\mid \theta)\] On calcule alors ce quāon appelle la loi a posteriori de \(\theta\) : \[\pi(\theta\mid \underline{x}) \propto L(\theta,\underline{x}) \pi(\theta)\]
Calculer la loi a posteriori dans les cas suivants
Lāestimateur \(\widehat{\theta}^B(\underline{X})\) est appelĆ© estimateur de Bayes associĆ© Ć la fonction de perte \(L\) et Ć lāa priori \(\pi\) cāest lāestimateur qui minimise le risque bayĆ©sien \[ \mathcal{R}_\pi\left(\widehat{\theta}^B(\underline{X})\right)= \min_{\widehat{\theta}(\underline{x})\in \mathcal{E}} \mathcal{R}_\pi\left(\widehat{\theta}(\underline{x})\right)\]
Remarque : Lāexpression de lāestimateur bayĆ©sien dĆ©pend du choix de la loi a priori \(\pi\) et de la fonction perte \(L\).
Alors \[\widehat{\theta}(\underline{X})=\widehat{\theta}^B(\underline{X})\]
Si \(L(\theta,\theta')=(\theta-\theta')^2\) est la fonction perte quadratique, alors lāestimateur bayĆ©sien est donnĆ© par la moyenne a posteriori \[ \widehat{\theta}^B(\underline{X})=\mathbb{E}(\theta\mid X)\] De plus \(\mathcal{R}_\pi\left(\widehat{\theta}^B(\underline{X})\right)=\mathbb{E}\left[\mathbb{V}(\theta\mid X)\right]\)
Si \(L(\theta,\theta')=|\theta-\theta'|\), alors lāestimateur bayĆ©sien est donnĆ© par la mĆ©diane a posteriori \[ \widehat{\theta}^B(\underline{X})=\mbox{MĆ©diane}_{\theta|X}\]
Soit \(X=(X_1,\ldots,X_n)\) un Ć©chantillon i.i.d de loi normale de moyenne \(\theta\) inconnue et de variance \(\sigma^2\). ConsidĆ©rons la loi a priori \(\pi=\mathcal{N}(0,\sigma_0^2)\) 1. Calculer la loi a posteriori sur \(\theta\). 2. En dĆ©duire lāexpression de lāestimateur bayĆ©sien associĆ© Ć la fonction perte quadratique. 3. Calculer le risque bayĆ©sien de lāestimateur bayĆ©sien. 4. RĆ©pondre aux questions 1,2,3 avec la fonction perte \(L(\theta,\theta')=(\theta-\theta')^2 \exp(\theta^2)\). On considĆ©rera le cas où \(\sigma^2=1\).
Pour construire une loi a priori on doit distinguer 3 cas possibles :
On dispose dāune information a priori partielle, donnĆ©e par lāexpĆ©rience.
On dispose dāune quantitĆ© limitĆ© : lois conjuguĆ©es.
On dispose dāune quantitĆ© limitĆ© ou mĆŖme aucune information sur \(\theta\) : la loi a priori de Jeffrey.
Une loi a priori conjuguée sur \(\theta\) pour un modèle statistique donné si la loi a posteriori sur \(\theta\) appartient à la même famille de la loi a priori.
Calculer dans les cas suivants la loi a priori conjuguƩe sur \(\theta\) et calculer la loi a posteriori :
On suppose que le \(n-\)Ć©chantillon \(X_1,\ldots,X_n\) de densitĆ© \(f(\,\bullet\mid \theta)\). La loi de Jefferey se construit Ć partir seulement la connaissance de la densitĆ© \(f(\,\bullet\mid \theta)\). Cette famille de lois est basĆ©e sur le calcul de lāinformation de Fisher : \[ I(\theta)=\mathbb{E}_\theta\left[ \left( \displaystyle\frac{\partial \mbox{ log }f(X\mid \theta)}{\partial \theta} \right)^2\right]\] Dans le cas de modĆØles rĆ©guliers. \[ I(\theta)=\mathbb{E}_\theta\left[ \displaystyle\frac{\partial^2 \mbox{ log }f(X\mid \theta)}{\partial \theta^2} \right]=\mathbb{E}_\theta\left[ \displaystyle\frac{\partial^2 \mbox{ log }l_n( \theta)}{\partial \theta^2} \right]\] où \(l_n\) est le log-vraisemblance. Ces lois sont souvent des lois a priori non-informatives.
DĆ©finition la loi a priori de Jeffrey quāon note $^ cāest la loi qui admet la densitĆ© suivante
Si \(\Theta\subseteq \mathbb{R}\) \[ q(\theta)\propto I^{-1/2}(\theta)\]
Si \(\Theta\subseteq \mathbb{R}^d\) \[ q(\theta)\propto |I(\theta)|^{-1/2}\]
Reprendre les modĆØles statistiques de lāexercice prĆ©cedent et calculer les lois a priori de Jeffrey correspondantes.
On considĆØre \(\underline{x}=\left(x_{1},\ldots,x_{n}\right)\) un Ć©chantillon i.i.d dāune variable \(X\) de loi de densitĆ© \[ f\left(x\mid\theta,\lambda\right)\propto\exp\left(-\theta x^{2}+\lambda x\right) \]
dƩfinie sur tout \(\mathbb{R}\).
Montrer que la loi de \(X\) est une loi \(\mathcal{N}\left({\displaystyle \frac{\lambda}{2\theta}},\,{\displaystyle \frac{1}{2\theta}}\right)\)
En dƩduire la constante de normalisation de la densitƩ \(f\).
Ecrire la fonction vraisemblance \(L\left(\theta,\lambda\mid\underline{x}\right).\)
On considĆØre le code suivant
> p = seq(0.05, 0.95, by = 0.1)
> prior = c(1, 5.2, 8, 7.2, 4.6, 2.1, 0.7, 0.1, 0, 0)
> prior = prior/sum(prior)
> Likelyhood=function(p,s,f) p^s*(1-p)^f
> Lp=sapply(p,function(x) Likelyhood(x,11,16))
> post=Lp*prior
> post=post/sum(post)
> round(post,4)
[1] 0.0000 0.0023 0.1291 0.4768 0.3338 0.0559 0.0021 0.0000 0.0000 0.0000
\(y_1,\ldots,y_n\) un \(n-\)Ʃchantillon de loi Bernoulli \(\mathcal{B}(p)\).
La loi a priori conjuguƩe
\[g(p) = \displaystyle\frac{\Gamma(\alpha+\beta)}{\Gamma(\alpha)\Gamma(\beta)} p^{\alpha-1}\, (1-p)^{\beta-1}\]
De moyenne
\[E(p)=\displaystyle\frac{\alpha}{\alpha+\beta}\]
De Variance
\[\mbox{Var}(p)=\displaystyle\frac{\alpha\beta}{(\alpha+\beta)^2(\alpha+\beta+1)}\]
> curve(dbeta(x,4,3),from = 0,to = 1,col="red",lwd=2,xlab = "",ylab="")
> library(LearnBayes)
> quantile2=list(p=.9,x=.5)
> quantile1=list(p=.5,x=.3)
> beta.select(quantile1,quantile2)
[1] 3.26 7.19
La loi a posteriori \[g(p\mid \mbox{ donnƩes })\propto p ^{s+\alpha-1} (1-p)^{N-s+\beta-1}\]
ReprƩsentation des lois a priori, a posteriori et vraisemblance.
> a = beta.select(quantile1,quantile2)[1]
> b = beta.select(quantile1,quantile2)[2]
> s = 11
> f = 16
> curve(dbeta(x,a+s,b+f), from=0, to=1,
+ xlab="p",ylab="Density",lty=1,lwd=4)
> curve(dbeta(x,s+1,f+1),add=TRUE,lty=2,lwd=4)
> curve(dbeta(x,a,b),add=TRUE,lty=3,lwd=4)
> legend(.7,4,c("Prior","Likelihood","Posterior"),
+ lty=c(3,2,1),lwd=c(3,3,3))
> 1 - pbeta(0.5,a+s,b+f)
[1] 0.0690226
\[f(\widetilde{y})=\int f(\widetilde{y}\mid p)g(p) dp.\]
On lance une piĆØce de monnaie \(n\) fois, on observe \(y\) fois piles, on considĆØre une loi a priori sur \(p\). Supposons quāon veut jeter \(m\) fois la piĆØce et on veut prĆ©dire le nombre \(\widetilde{y}\) dāapparitions de la face Pile.
Solution frƩquentiste : estimer \(p\) par \(\widehat{p}=y/n\), et prƩdire \(\widetilde{y}\) par \(\widetilde{y}=m\widehat{p}=my/n\) ?
Solution bayƩsienne : calculer la densitƩ prƩdictive a posteriori de \(\widetilde{y}\).
\[\begin{array}{rcl} g(\widetilde{y}\mid y)&=&\int g(\widetilde{y},\, p\mid y) dp \\ &=& \int g(\widetilde{y}\mid \, p) \, g(p\mid y) dp \\ \end{array}\]
On a dĆ©jĆ vu que si \(p\sim \mbox{Beta}(a,b)\) alors \(p\mid y\sim Beta(y+a,n-y+b)\). La loi densitĆ© prĆ©dictive a posteriori sāĆ©crit
\[\begin{array}{rcl} g(\widetilde{y}\mid y)& = & \displaystyle\frac{1}{B(y+a,n-y+b)} \displaystyle\int_0^1 C_m^{\widetilde{y}} p^{\widetilde{y}}\\ & & (1-p)^{m-\widetilde{y}}\, p^{y+a}(1-p)^{n-y+b}\, dp\\ & & \\ &=& C_m^{\widetilde{y}} \displaystyle\frac{B(\widetilde{y}+y+a,m-\widetilde{y}+n-\widetilde{y}+b)}{B(y+a,n-y+b)} \end{array}\] On dit que \(\widetilde{y}\) suit une loi Beta-Binomiale de paramĆØtres \(m,y+a,n-y+b\).
On observe pour \(n=40,\) \(y=13\) et on veut prĆ©dire les probabilitĆ©s dāobserver le nombre de Piles aprĆØs \(m=13\)
> library(emdbook)
> y=13
> n=40
> ### a priori beta 2,4
> a=2
> b=4
> m=13
> yy=0:m
> ## Calcul de la loi predictive a posteriori.
> predy=sapply(yy,function(x) dbetabinom(x,shape1 = y+a,shape2 = n-y+b,size = m))
> names(predy)=yy
> plot(yy,predy,xlab="y",ylab="loi predictive a posteriori",
+ type="h",lwd=3)
Supposons quāon a jetĆ© plusieurs fois une pi`{e}ce de monnaie Ć©quilibrĆ©e et on vous dit quāon a obtenu ``faceāā 13 fois.
Question : Pourrions-nous estimer le nombre de jets de la pi`{e}ce ?
Solution : On considĆØre un a priori non-informative sur \(n\) : \(g(n)\propto 1\) et la vraisemblance est alors \[L(n)\propto C_n^{13} \left( \frac{1}{2} \right)^n.\] Ainsi la loi a posteriori sāexprime \[g(n\mid x)\propto L(n) g(n)= C_n^{13} \left( \frac{1}{2} \right)^n.\]
> n=13:100
> posteriori=function(n,p) choose(n,13)*p^13*(1-p)^(n-13)
> ### le cas p=.5
> postn=sapply( n,function(n) posteriori(n,.5))
> postn=postn/sum(postn)
> #### Graphique et comparer avec 2*13=26.
> plot(n,postn,type="h",lwd=.8,col="red",xlab="n",
+ ylab="a posteriori")
> abline(v=26,col="blue",lwd=1.5)
> p=seq(0.1,0.8,by=0.1)
> par(mfrow=c(2,4))
> for(i in 1:8){
+ postn=c()
+ postn=sapply( n,function(n) posteriori(n,p[i]))
+ postn=postn/sum(postn)
+ plot(n,postn,type="h",lwd=.8,col="red",xlab="n",
+ ylab="a posteriori", main=paste("p=",p[i],sep=""))
+ abline(v=13/p[i],col="blue",lwd=1.5)
+ }