Skip to content

Commit b4caa4b

Browse files
authored
Create tutorial_3.R
1 parent 11af3c7 commit b4caa4b

File tree

1 file changed

+246
-0
lines changed

1 file changed

+246
-0
lines changed

Session 3/tutorial_3.R

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
####################################################################
2+
################ SESSIÓ 2 ####################################
3+
####################################################################
4+
5+
6+
7+
#################### 2.1 SEPIA VERDA ##############################
8+
9+
##### prior distribution and prior predictive distribution
10+
11+
12+
prior <- c(alpha =19 , beta =1)
13+
14+
prior <- c(alpha =9 , beta =0.5)
15+
16+
par(mfrow=c(1,2))
17+
18+
plot(function(x)dgamma(x, prior[1], prior[2]), xlim=c(0,60), ylab="", xlab = expression(lambda))
19+
title("prior distribution")
20+
21+
22+
23+
# simulated prior predictive distribution
24+
M <- 100000
25+
prior.sim <- rgamma(M, prior[1], prior[2])
26+
pre.prior.sim <- rpois(M, prior.sim)
27+
28+
29+
plot(table(pre.prior.sim)/M, ty="h")
30+
abline(v= c(5,40), lty= 2, col= "blue")
31+
32+
###### data
33+
34+
y <- read.table("G:\\200611 - ANALISI BAYESIANA\\data\\sepiaverda.txt", header = F)
35+
36+
n <- dim(y)[1]
37+
38+
y
39+
40+
####### standardaized likelihood function
41+
42+
sd.like <- function(th) {
43+
(th^sum(y)*exp(-n*th))/integrate(function(th)(th^sum(y)*exp(-n*th)), lower = 0, upper = 50)$value
44+
}
45+
46+
47+
par(mfrow=c(1, 1))
48+
plot(function(th)sd.like(th), xlim=c(0,60), ylab="", xlab = expression(lambda))
49+
plot(function(th)dgamma(th, prior[1], prior[2]), xlim=c(0,60), add=T, lty=2)
50+
legend("topright", c("prior", "likelihood"), lty = c(2,1))
51+
52+
53+
54+
######### posterior distribution
55+
56+
57+
posterior <- c(a = prior[1] + sum(y) , b = prior[2] + n )
58+
59+
60+
61+
# prior, likelihood and posterior
62+
63+
plot(function(th)dgamma(th, posterior[1], posterior[2]), xlim=c(0,60), lty=1)
64+
plot(function(th)sd.like(th), xlim=c(0,60), ylab="", xlab = expression(lambda), add=T, lty=3)
65+
plot(function(th)dgamma(th, prior[1], prior[2]), xlim=c(0,60), add=T, lty=2)
66+
legend("topright", c("prior", "likelihood", "posterior"), lty = c(2,3,1))
67+
68+
69+
70+
# summary results
71+
72+
results <- matrix(nrow = 7, ncol = 2)
73+
74+
colnames(results ) <- c('prior', 'posterior')
75+
rownames(results ) <- c('alpha', 'beta', 'mean', 'variance', '2,5%', 'median', '97.5%')
76+
77+
results [1:2, 1] <- prior
78+
results [3, 1] <- prior[1]/prior[2]
79+
results [4, 1] <- prior[1]/prior[2]^2
80+
results [5, 1] <- qgamma(0.025, shape = prior[1], prior[2])
81+
results [6, 1] <- qgamma(0.5, shape = prior[1], prior[2])
82+
results [7, 1] <- qgamma(0.975, shape = prior[1], prior[2])
83+
84+
results [1:2, 2] <- posterior
85+
results [3, 2] <- posterior[1]/posterior[2]
86+
results [4, 2] <- posterior[1]/posterior[2]^2
87+
results [5, 2] <- qgamma(0.025, shape = posterior[1], posterior[2])
88+
results [6, 2] <- qgamma(0.5, shape = posterior[1], posterior[2])
89+
results [7, 2] <- qgamma(0.975, shape = posterior[1], posterior[2])
90+
91+
round(results , 3)
92+
93+
94+
95+
# priror and posterior predictive distribution
96+
97+
par(mfrow=c(2,1))
98+
99+
M <- 100000
100+
101+
prior.sim <- rgamma(M, prior[1], prior[2])
102+
pre.prior.sim <- rpois(M, prior.sim)
103+
104+
post.sim <- rgamma(M, shape = posterior[1], posterior[2])
105+
post.pre.sim <- rpois(M, post.sim)
106+
107+
plot(table(pre.prior.sim)/M, ty="h", xlim=c(0, 60), ylab = "", xlab = "", col="blue", lwd=0.5)
108+
title("prior predictive distribution")
109+
110+
plot(table(post.pre.sim)/M, ty="h", xlim=c(0, 60), ylab = "", xlab = "", col="blue", lwd=0.5)
111+
title("posterior predictive distribution")
112+
113+
114+
115+
116+
first <- round(sum(post.pre.sim < 16)/M, 4)
117+
second<- round(sum(post.pre.sim > 24)/M, 4)
118+
119+
120+
121+
h1 <- pgamma(15, posterior[1], posterior[2])
122+
h3 <- 1 - pgamma(20, posterior[1], posterior[2])
123+
h2 <- 1-h1-h3
124+
125+
126+
127+
128+
129+
130+
131+
#################### 2.2 ASMA #################################################
132+
133+
134+
# prior distribution
135+
136+
prior <- c(a = 1.25, b = 25 )
137+
138+
139+
par(mfrow=c(1,1))
140+
141+
#plot(function(x)dbeta(x, prior[1],prior[2]), xlim=c(0,1), ylab="", xlab = "theta")
142+
curve(dbeta(x, prior[1],prior[2]), xlim=c(0,1), ylab="", xlab = "theta", n=10000)
143+
title(paste("Prior: Beta","(","a=",prior[1],",","b=",prior[2],")"))
144+
145+
146+
147+
# prior predictive distribution
148+
149+
M <- 1000000
150+
th.prior <- rbeta(M, prior[1],prior[2])
151+
pre.prior <- rbinom(M, 50, th.prior)
152+
153+
plot(table(pre.prior)/M, xlim=c(0,50),ty="h", ylab="")
154+
155+
156+
157+
158+
# data
159+
160+
N <- 200
161+
y <- 11
162+
163+
164+
165+
# likelihood
166+
167+
curve(dbinom(y, N, x),ylab="",xlab=expression(theta), xlim=c(0,1), n=10000)
168+
abline(v=y/N, lty=2, col="blue")
169+
170+
K <- integrate(function(th)dbinom(y,N,th), lower=0, upper=1)$value
171+
172+
curve(dbeta(x, prior[1], prior[2]), xlim=c(0,1), ylab="", xlab =expression(theta), ylim=c(0,25), n=10000)
173+
curve(dbinom(y, N, x)/K, add=T, lty=2)
174+
legend("topright", c("prior","likelihood"),lty=c(1,2))
175+
title("prior & likelihood")
176+
177+
178+
179+
180+
# posterior distribution
181+
182+
183+
posterior <- c(a = prior[1] + y, b = prior[2] + N -y )
184+
185+
186+
187+
# DIBUIX DE LA DISTRIBU DISTRIBUCIO A PRIORI, A POSTERIORI I LA VERSEMBLANÇA
188+
189+
curve(dbeta(x, posterior[1], posterior[2]), xlim=c(0,1), ylab="", xlab =expression(theta), n=10000)
190+
curve(dbinom(y, N, x)/K, add=T, lty=3, n=10000)
191+
curve(dbeta(x, prior[1], prior[2]), add=T, lty=2, n=10000)
192+
193+
legend("topright", c("prior","posterior","likelihood"), lty = c(2,1,3))
194+
title("prior , posterior & likelihood")
195+
196+
197+
198+
# summnary
199+
200+
sortida <- matrix(nrow = 7, ncol = 2)
201+
202+
colnames(sortida) <- c('prior', 'posterior')
203+
rownames(sortida) <- c('alpha', 'beta', 'mean', 'variance', '2,5%', 'median', '97.5%')
204+
205+
sortida[1:2, 1] <- prior
206+
sortida[3, 1] <- prior[1]/(prior[1] + prior[2])
207+
sortida[4, 1] <- (prior[1]*prior[2])/(((prior[1]+prior[2])^2)*(prior[1]+prior[2]+1))
208+
sortida[5, 1] <- qbeta(0.025, prior[1], prior[2])
209+
sortida[6, 1] <- qbeta(0.5, prior[1], prior[2])
210+
sortida[7, 1] <- qbeta(0.975, prior[1], prior[2])
211+
212+
sortida[1:2, 2] <- posterior
213+
sortida[3, 2] <- posterior[1]/(posterior[1] + posterior[2])
214+
sortida[4, 2] <- (posterior[1]*posterior[2])/(((posterior[1]+posterior[2])^2)*(posterior[1]+posterior[2]+1))
215+
sortida[5, 2] <- qbeta(0.025, posterior[1], posterior[2])
216+
sortida[6, 2] <- qbeta(0.5, posterior[1], posterior[2])
217+
sortida[7, 2] <- qbeta(0.975, posterior[1], posterior[2])
218+
219+
220+
round(sortida, 3)
221+
222+
223+
224+
225+
# prior and posterior predictive distribution
226+
227+
228+
M <- 1000000
229+
th.prior <- rbeta(M, prior[1],prior[2])
230+
pre.prior <- rbinom(M, 50, th.prior)
231+
232+
th.posterior <- rbeta(M, posterior[1],posterior[2])
233+
pre.posterior <- rbinom(M, 50, th.posterior)
234+
235+
236+
par(mfrow=c(2,1))
237+
238+
plot(table(pre.prior)/M, xlim=c(0,50),ty="h", ylab="")
239+
title("prior predictive distribution")
240+
plot(table(pre.posterior)/M, xlim=c(0,50),ty="h", ylab="")
241+
title("posterior predictive distribution")
242+
243+
244+
245+
246+

0 commit comments

Comments
 (0)