#1.) data<-c(rep(0,4),rep(1,6),rep(2,6),rep(3,8),rep(4,3),6,6,10) table(data) verohodnost<-function(x){ verohodnost<-prod(dpois(data,x))} #a) apriorni hustota - rovnomerna apost<-function(x){ verohodnost(x)*1 } K<-integrate(Vectorize(apost),0,Inf)$value apost2<-function(x){ apost2<-apost(x)/K return(apost2)} #stale ale integral neni roven 1 KK=integrate(Vectorize(apost2),0,Inf)$value KK apost3<-function(x){ apost3<-apost2(x)/KK return(apost3)} #ted uz konecne ano integrate(Vectorize(apost3),0,Inf) ind<-seq(1,4, by=0.01) #graf vykreslime na mrizce od 1 do 4 plot(Vectorize(apost3)(ind)~ind, type="l",xlab=expression(lambda), ylab=expression(paste(pi,"(",lambda,"|x)" )),main='Graf aposteriorni hustoty', ylim=c(0,1.5)) curve(dgamma(x,shape=77,scale=1/30),add=TRUE,col=7) #skutecna aposteriorni hustota #b) apriorni hustota - exponencialni apost_E<-function(x){ verohodnost(x)*dexp(x,1/5)} K2<-integrate(Vectorize(apost_E),0,Inf)$value apost2_E<-function(x){ apost2_E<-apost_E(x)/K2 return(apost2_E)} #stale ale integral neni roven 1 KK2=integrate(Vectorize(apost2_E),0,Inf)$value KK2 apost3_E<-function(x){ apost3_E<-apost2_E(x)/KK2 return(apost3_E)} #ted uz konecne ano integrate(Vectorize(apost3_E),0,Inf) par(new=TRUE) plot(Vectorize(apost3_E)(ind)~ind, type="l",col=2,ylim=c(0,1.5), xlab=expression(lambda), ylab=expression(paste(pi,"(",lambda,"|x)" ))) #c) apriorni hustota - Jeffreysova apost_J<-function(x){ verohodnost(x)*1/sqrt(x)} K3<-integrate(Vectorize(apost_J),0,Inf)$value apost2_J<-function(x){ apost2_J<-apost_J(x)/K3 return(apost2_J)} #stale ale integral neni roven 1 KK3=integrate(Vectorize(apost2_J),0,Inf)$value KK3 apost3_J<-function(x){ apost3_J<-apost2_J(x)/KK3 return(apost3_J)} #ted uz konecne ano integrate(Vectorize(apost3_J),0,Inf) par(new=TRUE) plot(Vectorize(apost3_J)(ind)~ind, type="l",col=3,ylim=c(0,1.5), xlab=expression(lambda), ylab=expression(paste(pi,"(",lambda,"|x)" ))) #2.) #a) graf pro pozitivne diagnostikovane aposterP<-function(x){ aposterP<-0.7*x/(0.7*x+0.02*(1-x)) return(aposterP)} curve(aposterP,0,1,xlab='Prevalence', ylab='Aposteriorni pravdepodobnost',main='Graf aposteriorni pravdepodobnosti',col=2) #b) graf pro negativne diagnostikovane aposterN<-function(x){ aposterN<-0.98*(1-x)/(0.3*x+0.98*(1-x)) return(aposterN)} curve(aposterN,col=4,add=TRUE) abline(v=0.03,lty=3)