-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutility.R
46 lines (38 loc) · 1.12 KB
/
utility.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
library(mvtnorm)
graph_generation <- function(p = 100, q = 500,
graph_type = c("random", "hub"),
iv_sufficient = FALSE) {
if (graph_type == "random") {
sparsity <- 1 / p
u <- matrix(rbinom(p * p, 1, sparsity), p, p)
u[lower.tri(u, diag = TRUE)] <- 0
w <- matrix(0, nrow = q, ncol = p)
for (k in 1:(p - 1)) {
w[k, k] <- 1
if (!iv_sufficient) w[k, k + 1] <- 1
w[k + p, k] <- 1
w[k + p, k + 1] <- 1
}
w[p, p] <- 1
w[2 * p, p] <- 1
} else {
# generate graph
num_of_hub <- 2
idx <- rep(1:num_of_hub, length.out = p)
u <- matrix(0, p, p)
for (k in 1:num_of_hub) {
u[k, idx == k] <- 1
}
diag(u) <- 0
w <- matrix(0, nrow = q, ncol = p)
for (k in 1:(p - 1)) {
w[k, k] <- 1
if (!iv_sufficient) w[k, k + 1] <- 1
w[k + p, k] <- 1
w[k + p, k + 1] <- 1
}
w[p, p] <- 1
w[2 * p, p] <- 1
}
list(u = u, w = w)
}