-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexponential_example.R
More file actions
100 lines (85 loc) · 3.84 KB
/
exponential_example.R
File metadata and controls
100 lines (85 loc) · 3.84 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# Exponential with Gamma prior
library(ggplot2)
set.seed(2020)
theta_star <- list(lambda = 2)
hyperparams <- list(alpha = 2, beta = 2)
epsilon <- c(0.01, 0.05, 0.1, 0.5, 1, 2)
nobservation <- 1
nthetas <- 1024
y <- matrix(rexp(nobservation, theta_star$lambda), ncol = 1)
# Gamma prior
rprior <- function(n){ return(rgamma(n, shape = hyperparams$alpha, rate = hyperparams$beta)) }
# generating process
simulate <- function(n, theta){ return(matrix(rexp(n, rate = theta), ncol = 1)) }
# initialize dataframe to store the true density
thetavals <- seq(0, 7.5, length.out = nthetas)
posterior_df <- data.frame(thetavals = thetavals,
true_posterior = NA,
abc_posterior = NA
)
# true posterior
posterior_df$true_posterior <- dgamma(thetavals,
shape = hyperparams$alpha + nobservation,
rate = hyperparams$beta + sum(y))
# exact abc posterior (for n = 1)
abcposterior_func <- function(theta, epsilonval){
const <- gamma(hyperparams$alpha) * (1 / (hyperparams$beta + c(y) - epsilonval)^hyperparams$alpha -
1 / (hyperparams$beta + c(y) + epsilonval)^hyperparams$alpha
)
return(
theta^(hyperparams$alpha - 1) * exp(- (hyperparams$beta + c(y)) * theta) *
( exp(epsilonval * theta) - exp(- epsilonval * theta) ) * (theta > 0) / const
)
}
posterior_df$abc_posterior <- abcposterior_func(thetavals, epsilon[1])
# initialize arguments
source("src/rej_abc.R")
args <- list(nthetas = nthetas, y = y,
rpiror = rprior,
simulate = simulate,
kernel = "uniform",
discrepancy = l2norm
)
# initialize dataframes to store samples
method_names <- paste("epsilon =", epsilon)
abc_df <- data.frame(methods = rep(method_names, each = nthetas),
samples = NA
)
posterior_df2 <- data.frame(methods = rep(method_names, each = nthetas),
thetas = rep(thetavals, length(method_names)),
densities = NA
)
# rejection abc
for (i in 1:length(epsilon)){
# samples_df <- rej_abc(N = nthetas, epsilon = epsilon[i], y = y, rprior = rprior, simulate = simulate)
args$epsilon <- epsilon[i]
samples_df <- rej_abc(args)
abc_df$samples[(1 + (i - 1) * nthetas): (i * nthetas)] <- samples_df$samples
# exact abc posterior densities
posterior_df2$densities[(1 + (i - 1) * nthetas): (i * nthetas)] <- abcposterior_func(thetavals, epsilon[i])
}
# plot results
plt_color <- init_discrete_grad_colours(length(epsilon))
plt <- ggplot(posterior_df) +
geom_line(aes(x = thetavals, y = true_posterior)) +
geom_ribbon(aes(x = thetavals, ymax = true_posterior), ymin = 0, alpha = 0.5) +
geom_density(data = abc_df, aes(x = samples, colour = methods)) +
scale_color_manual(values = plt_color) +
geom_vline(xintercept = theta_star$lambda, linetype = "dashed") +
labs(x = "theta", y = "density") +
xlim(0, 7.5) +
change_sizes(16, 20) +
add_legend(0.95, 0.95)
ggsave(plt, file = "plots/soft_abc/exponential_eg.pdf", height = 5)
# exact abc posterior densities
plt <- ggplot(posterior_df) +
geom_line(aes(x = thetavals, y = true_posterior)) +
geom_ribbon(aes(x = thetavals, ymax = true_posterior), ymin = 0, alpha = 0.5) +
geom_line(data = posterior_df2, aes(x = thetas, y = densities, colour = methods)) +
scale_color_manual(values = plt_color) +
geom_vline(xintercept = theta_star$lambda, linetype = "dashed") +
labs(x = "theta", y = "density") +
xlim(0, 7.5) +
change_sizes(16, 20) +
add_legend(0.95, 0.95)
ggsave(plt, file = "plots/soft_abc/exponential_eg_exact.pdf", height = 5)