-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathin_class_grid.R
78 lines (46 loc) · 1.64 KB
/
in_class_grid.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# in - class grid search
set.seed(1859)
x_obs <- rbinom(10, 6, 0.6)
x_obs
# 1. make a vector of probabilities for $p$ using `seq(from = 0, to = 1, length.out = n)`
theta <- seq(from = 0.01, to = 0.99, by = 0.01)
# 1. Think about the prior you might use. Look at `curve(dbeta(x))`
prior_unif <- dunif(theta, 0, 1)
pr_lik <- sapply(X = theta, function(X)
exp(sum(dbinom(x_obs, size = 6, prob = X, log = TRUE))))
pr_lik <- sapply(X = theta, function(X)
prod(dbinom(x_obs, size = 6, prob = X, log = FALSE)))
plot(theta, pr_lik)
numerator <- pr_lik * prior_unif
# normalizing constant
denom <- sum(numerator)
posterior <- numerator/denom
sum(posterior)
plot(theta, posterior)
points(theta, prior_unif, col = "red")
xx <- 1:8
sum(xx/sum(xx))
curve(dbeta(x, 1 + sum(x_obs), 1 + sum(6 - x_obs)))
plot(theta, posterior)
posterior2 <- dbeta(theta, shape1 = (1 + sum(x_obs)), shape2 = (1 + sum(6 - x_obs)))
lines(theta, posterior2/sum(posterior2))
theta[which.max(posterior)]
# conjugate prior mean
(1 + sum(x_obs)) / (1 + sum(x_obs) + 1 + sum(6 - x_obs))
###
alpha_new <- 10 + sum(x_obs)
beta_new <- 90 + sum(6 - x_obs)
conj <- dbeta(theta, alpha_new, beta_new)
lines(theta, conj/sum(conj))
alpha_new / (alpha_new + beta_new)
curve(dbeta(x, 2, 2))
curve(dbeta(x, .8*6, (1-.8)*6))
curve(dbeta(x, 2, 2))
curve(dunif(x))
# 1. find the probability of each of these values of $p$ via `dbeta`
prior_dens <- dbeta(p, 2, 2)
plot(p, prior_dens)
# 1. find the likelihood for each of these values of $p$ via `dbinom`
log_lik <- function(x) (sum(dbinom(x_obs, size = 6, prob = x, log = TRUE)))
# 1. multiply these two columns together
# 1. normalize (divide by the sum)