# Lab 5 - TSdD 2024 # BF # Es. 1 (ipotesi semplici) # th=th.0 vs th=th.1 th.0=0 th.1=0.8 n=10 sig=1 #dati=round(rnorm(n,th.0,sig),2) dati=c(1.88,0.14,1.15,0.16,-0.39,0.47,-0.81,1.23,-0.46,1.09) x.med=mean(dati) x.med sig.n=sig/sqrt(n) sig.n L.0=dnorm(th.0,x.med,sig.n) L.1=dnorm(th.1,x.med,sig.n) B.01=L.0/L.1 B.01 # Post prob #P(H.0|z.n)=L.0*p.0/m(z.n) #P(H.1|z.n)=L.1*p.1/m(z.n) #m(z.n)=L.0*p.0+L.1*p.1 p.0=0.2 p.1=0.8 prior.odds=p.0/p.1 prior.odds m=L.0*p.0+L.1*p.1 p.0.post=L.0*p.0/m p.1.post=L.1*p.1/m p.0.post p.1.post post.odds=p.0.post/p.1.post post.odds post.odds/prior.odds # se invece # p.0=0.8 # p.1=0.2 # B.01 invariati # post.odds cambiano # Es. 2 (one-sided) # theta <= th.0 vs theta>th.0 mu.0=th.0 n.0=5 sig.0=sig/sqrt(n.0) p.0.prior=pnorm(th.0,mu.0,sig.0) p.1.prior=1-p.0.prior prior.odds=p.0.prior/p.1.prior prior.odds p.0.prior p.1.prior prior.odds mu.p=(n.0*mu.0+n*x.med)/(n+n.0) mu.p sig.p=sig/sqrt(n+n.0) sig.p p.0.post=pnorm(th.0,mu.p,sig.p) p.1.post=1-p.0.post post.odds=p.0.post/p.1.post p.0.post p.1.post post.odds B.01=post.odds/prior.odds B.01 # se cambio mu.0 mu.0=1 mu.0=-1 # ES. 3 (two-sided) mu.0=1.5 th.0=1.5 n.0=5 x.med=2 n=10 sig.2=1 sig=sqrt(sig.2) BF.fun=function(y){ Num=dnorm(x.med,mu.0,sig*sqrt(1/n+y)) Den=dnorm(x.med,th.0,sig*sqrt(1/n)) BF=Num/Den return(BF) } BF.fun(0) curve(BF.fun(x),from=0, to=10)