|
| 1 | +#################################################################### |
| 2 | +################ SESSIÓ 2 #################################### |
| 3 | +#################################################################### |
| 4 | + |
| 5 | + |
| 6 | +#################### 2.2 ASMA ################################################# |
| 7 | + |
| 8 | + |
| 9 | +# prior distribution |
| 10 | + |
| 11 | +prior <- c(a = 1 , b = 1) |
| 12 | + |
| 13 | + |
| 14 | +par(mfrow=c(1,1)) |
| 15 | + |
| 16 | + |
| 17 | +plot(function(x)dbeta(x, prior[1],prior[2]), xlim=c(0,1), ylab="", xlab = "theta") |
| 18 | +curve(dbeta(x, prior[1],prior[2]), xlim=c(0,1), ylab="", xlab = "theta", n=10000) |
| 19 | + title(paste("Prior: Beta","(","a=",prior[1],",","b=",prior[2],")")) |
| 20 | + |
| 21 | +prior <- c(a = 1.5 , b = 28.5 ) |
| 22 | +curve(dbeta(x, prior[1],prior[2]), xlim=c(0,1), ylab="", xlab = "theta", n=10000) |
| 23 | +title(paste("Prior: Beta","(","a=",prior[1],",","b=",prior[2],")")) |
| 24 | + |
| 25 | +# prior predictive distribution |
| 26 | + |
| 27 | +M <- 1000000 |
| 28 | +simulated_values <- numeric(M) |
| 29 | + |
| 30 | +th.prior <- rbeta(M, prior[1], prior[2]) |
| 31 | +#sample size of 50 |
| 32 | +pre.prior <- rbinom(M, 50, th.prior) |
| 33 | + |
| 34 | +th.posterior <- rbeta(M, posterior[1], posterior[2]) |
| 35 | +pre.posterior <- rbinom(M, 50, th.posterior) |
| 36 | +table(pre.prior) |
| 37 | + |
| 38 | +plot(table(pre.prior)/M, type = "h", xlim= c(0,50), col="skyblue") |
| 39 | + |
| 40 | +#pre.prior <- rbeta(M, 1.25, 19) |
| 41 | +#plot(table(pre.prior)/M, type = "l") |
| 42 | +# data |
| 43 | + |
| 44 | +N <- 200 |
| 45 | +y <- 11 |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | +# likelihood |
| 50 | + |
| 51 | +curve(dbinom(y, N, x),ylab="",xlab=expression(theta), xlim=c(0,1), n=10000) |
| 52 | + abline(v=y/N, lty=2, col="blue") |
| 53 | + |
| 54 | +K <- integrate(function(th)dbinom(y,N,th), lower=0, upper=1)$value |
| 55 | + |
| 56 | +curve(dbeta(x, prior[1], prior[2]), xlim=c(0,1), ylab="", xlab =expression(theta), ylim=c(0,25), n=10000) |
| 57 | +curve(dbinom(y, N, x)/K, add=T, lty=2) |
| 58 | + legend("topright", c("prior","likelihood"),lty=c(1,2)) |
| 59 | + title("prior & likelihood") |
| 60 | + |
| 61 | +# the straight line corresponds to the uniform prior in the plot |
| 62 | + |
| 63 | + |
| 64 | +# posterior distribution |
| 65 | + |
| 66 | + |
| 67 | +posterior <- c(a = prior[1] + y, b = prior[2] +N -y) |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +# DIBUIX DE LA DISTRIBU DISTRIBUCIO A PRIORI, A POSTERIORI I LA VERSEMBLANÇA |
| 72 | + |
| 73 | +curve(dbeta(x, posterior[1], posterior[2]), xlim=c(0,1), ylab="", xlab =expression(theta), n=10000) |
| 74 | + curve(dbinom(y, N, x)/K, add=T, lty=3, n=10000) |
| 75 | + curve(dbeta(x, prior[1], prior[2]), add=T, lty=2, n=10000) |
| 76 | + |
| 77 | + legend("topright", c("prior","posterior","likelihood"), lty = c(2,1,3)) |
| 78 | + title("prior , posterior & likelihood") |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +# summnary |
| 83 | + |
| 84 | +sortida <- matrix(nrow = 7, ncol = 2) |
| 85 | + |
| 86 | +colnames(sortida) <- c('prior', 'posterior') |
| 87 | +rownames(sortida) <- c('alpha', 'beta', 'mean', 'variance', '2,5%', 'median', '97.5%') |
| 88 | + |
| 89 | +sortida[1:2, 1] <- prior |
| 90 | +sortida[3, 1] <- prior[1]/(prior[1] + prior[2]) |
| 91 | +sortida[4, 1] <- (prior[1]*prior[2])/(((prior[1]+prior[2])^2)*(prior[1]+prior[2]+1)) |
| 92 | +sortida[5, 1] <- qbeta(0.025, prior[1], prior[2]) |
| 93 | +sortida[6, 1] <- qbeta(0.5, prior[1], prior[2]) |
| 94 | +sortida[7, 1] <- qbeta(0.975, prior[1], prior[2]) |
| 95 | + |
| 96 | +sortida[1:2, 2] <- posterior |
| 97 | +sortida[3, 2] <- posterior[1]/(posterior[1] + posterior[2]) |
| 98 | +sortida[4, 2] <- (posterior[1]*posterior[2])/(((posterior[1]+posterior[2])^2)*(posterior[1]+posterior[2]+1)) |
| 99 | +sortida[5, 2] <- qbeta(0.025, posterior[1], posterior[2]) |
| 100 | +sortida[6, 2] <- qbeta(0.5, posterior[1], posterior[2]) |
| 101 | +sortida[7, 2] <- qbeta(0.975, posterior[1], posterior[2]) |
| 102 | + |
| 103 | + |
| 104 | +round(sortida, 3) |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +# prior and posterior predictive distribution |
| 110 | + |
| 111 | +plot(table(pre.posterior)/M, type="h", xlim= c(0,25)) |
| 112 | + |
| 113 | + |
0 commit comments