-
Notifications
You must be signed in to change notification settings - Fork 0
/
alpha_adjustment_experiments.R
131 lines (111 loc) · 2.7 KB
/
alpha_adjustment_experiments.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
source("alpha_adjustment_functions.R")
# validate alpha adjustment (simple case)
alpha <- 0.05
N <- 1000
K <- 100
M <- 10000
system.time(gamma1 <- adjust_alpha_simulate(alpha, N = N, K = K, M = M))
gamma1
system.time(gamma2 <- adjust_alpha_optimize(alpha, N = N, K = K))
gamma2
# should roughly match original alpha
empirical_alpha(gamma1, N = N, K = K, M = M)
empirical_alpha(gamma2, N = N, K = K, M = M)
# example for power calculation
k <- 1.3
M <- 10000
reject <- numeric(M)
for (m in seq_len(M)) {
x <- 1 - (1 - runif(N))^k
reject[m] <- test_uniformity(x, alpha = gamma2, K = K)
}
mean(reject)
# alpha adjustment (discrete case)
alpha <- 0.05
N <- 1000
K <- 5
M <- 10000
system.time(
gamma1 <- adjust_alpha_simulate(alpha, N = N, K = K, ncases=K, M = M)
)
gamma1
system.time(
gamma2 <- adjust_alpha_optimize(alpha, N = N, K = K)
)
gamma2
# should roughly match original alpha
empirical_alpha(gamma1, N = N, K = K, ncases = K, M = M)
empirical_alpha(gamma2, N = N, K = K, ncases = K, M = M)
# validate alpha adjustment (multiple chains)
alpha <- 0.05
niterations <- 50
nchains <- 3
K <- 50
M <- 10000
system.time(
gamma1 <- adjust_alpha_simulate_chains(
alpha, niterations = niterations,
nchains = nchains, K = K, M = M
)
)
gamma1
system.time(
gamma2 <- adjust_alpha_optimize_chains(
alpha, niterations = niterations,
nchains = nchains, K = K
)
)
gamma2
# should roughly match original alpha
empirical_alpha_chains(
gamma1, niterations = niterations,
nchains = nchains, K = K, M = M
)
empirical_alpha_chains(
gamma2, niterations = niterations,
nchains = nchains, K = K, M = M
)
# validate alpha adjustment (multiple chains discrete case)
alpha <- 0.05
niterations <- 100
nchains <- 4
K <- 10
M <- 10000
system.time(
gamma_max <- adjust_alpha_simulate_chains(
alpha, niterations = niterations,
nchains = nchains, K = K, ncases = K,
M = M, ties.method = 'max'
)
)
gamma_max
system.time(
gamma_rnd <- adjust_alpha_simulate_chains(
alpha, niterations = niterations,
nchains = nchains, ncases = K,
M = M, ties.method = 'random'
)
)
gamma_rnd
# should roughly match original alpha
empirical_alpha_chains(
gamma_max, niterations = niterations,
nchains = nchains, K = K, ncases = K,
M = M, ties.method = 'max'
)
empirical_alpha_chains(
gamma_rnd, niterations = niterations,
nchains = nchains, ncases = K,
M = M, ties.method = 'random'
)
# example for power calculation
M <- 10000
reject <- numeric(M)
for (m in seq_len(M)) {
x <- rnorm(niterations * nchains)
x <- matrix(x, nrow = niterations, ncol = nchains)
# chain 1 has a slightly different mean
x[, 1] <- x[, 1] + 0.2
reject[m] <- test_uniformity_chains(x, alpha = gamma, K = K)
}
mean(reject)