Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions tests/testthat/helper-invariants.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# shared helpers for property-based tests. these check invariants that
# must hold for any model, rather than pinning particular values

# column of the (single) nonzero entry in each row of a block membership
# matrix, with 0 for rows that are entirely zero
membership_column <- function(X) {
X <- as.matrix(X)
out <- max.col(X != 0, ties.method = "first")
out[rowSums(X != 0) == 0] <- 0L
out
}

# dense P = E[A | X, S] for any factor model
dense_expectation <- function(model) {
as.matrix(expectation(model))
}

# z-score of an observed total of `reps` independent Poisson(mu) draws
poisson_total_z <- function(total, mu, reps = 1) {
(total - reps * mu) / sqrt(reps * mu)
}

# Bonferroni-corrected two-sided normal critical value for `m` simultaneous
# z-tests at overall level `alpha`
bonferroni_z <- function(m, alpha = 1e-3) {
stats::qnorm(1 - alpha / (2 * m))
}

# check the internal consistency of a (undirected) dcsbm-like model: z, X,
# theta and pi all describe the same nodes, in the same order
expect_dcsbm_consistent <- function(m) {
X <- as.matrix(m$X)
n <- m$n
k <- m$k

expect_equal(dim(X), c(n, k))
expect_s3_class(m$z, "factor")
expect_length(m$z, n)
expect_equal(nlevels(m$z), k)
expect_length(m$theta, n)
expect_length(m$pi, k)
expect_equal(sum(m$pi), 1)
expect_true(all(m$pi >= 0))

# each row of X has exactly one nonzero entry, in column z[i], and it is
# theta[i]
expect_true(all(rowSums(X != 0) <= 1))
nonzero <- m$theta > 0
expect_equal(membership_column(X)[nonzero], as.integer(m$z)[nonzero])
expect_equal(unname(rowSums(X)), as.numeric(m$theta))

# S is symmetric and the right size
expect_equal(dim(m$S), c(k, k))
expect_true(isSymmetric(as.matrix(m$S)))

invisible(m)
}

# check the internal consistency of a directed_dcsbm
expect_directed_dcsbm_consistent <- function(m) {
X <- as.matrix(m$X)
Y <- as.matrix(m$Y)

expect_equal(dim(X), c(m$n, m$k1))
expect_equal(dim(Y), c(m$d, m$k2))
expect_equal(dim(m$S), c(m$k1, m$k2))

expect_length(m$z_out, m$n)
expect_length(m$z_in, m$d)
expect_equal(nlevels(m$z_out), m$k1)
expect_equal(nlevels(m$z_in), m$k2)
expect_length(m$theta_out, m$n)
expect_length(m$theta_in, m$d)
expect_length(m$pi_out, m$k1)
expect_length(m$pi_in, m$k2)
expect_equal(sum(m$pi_out), 1)
expect_equal(sum(m$pi_in), 1)

expect_equal(membership_column(X), as.integer(m$z_out))
expect_equal(membership_column(Y), as.integer(m$z_in))
expect_equal(unname(rowSums(X)), as.numeric(m$theta_out))
expect_equal(unname(rowSums(Y)), as.numeric(m$theta_in))

invisible(m)
}

# all four samplers, run from the same seed. they all consume the RNG the
# same way, so they describe the same graph
sample_all <- function(model, seed, multigraph = TRUE) {
set.seed(seed)
el <- sample_edgelist(model, multigraph = multigraph)
set.seed(seed)
A <- sample_sparse(model)
set.seed(seed)
ig <- sample_igraph(model, multigraph = multigraph)
set.seed(seed)
tg <- sample_tidygraph(model, multigraph = multigraph)
list(el = el, A = A, ig = ig, tg = tg)
}
177 changes: 177 additions & 0 deletions tests/testthat/test-edge-cases.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
# tiny graphs, empty graphs, empty blocks and other boundary cases

test_that("tiny undirected and directed models sample with every sampler", {
set.seed(100)
models <- list(
er_1 = erdos_renyi(n = 1, p = 0.5),
er_2 = erdos_renyi(n = 2, p = 0.5),
sbm_2 = sbm(n = 2, B = diag(2) * 0.5),
pp_3 = planted_partition(n = 3, k = 3, within_block = 0.5, between_block = 0.1),
directed_er_1 = directed_erdos_renyi(n = 1, p = 0.5),
directed_2 = directed_dcsbm(theta_out = c(1, 2), theta_in = c(2, 1), B = diag(2)),
rectangular_1_by_3 = directed_factor_model(matrix(1), matrix(0.5), matrix(1, 3, 1))
)

for (name in names(models)) {
model <- models[[name]]
directed <- inherits(model, "directed_factor_model")
d <- if (directed) model$d else model$n

for (seed in 1:5) {
s <- sample_all(model, seed)
expect_equal(dim(s$A), c(model$n, d), label = name)
expect_s3_class(s$el, "tbl_df")
expect_equal(igraph::ecount(s$ig), nrow(s$el), label = name)
}
}
})

test_that("models with no edges give empty graphs of the right shape", {
models <- list(
undirected = undirected_factor_model(matrix(1, 6, 1), matrix(0)),
directed_square = directed_erdos_renyi(n = 6, p = 0),
directed_rectangular = directed_factor_model(matrix(1, 6, 1), matrix(0), matrix(1, 4, 1)),
zero_block_sbm = sbm(n = 6, B = matrix(c(0, 0, 0, 0), 2), poisson_edges = FALSE)
)

for (name in names(models)) {
model <- models[[name]]
expect_equal(expected_edges(model), 0, label = name)

for (multigraph in c(TRUE, FALSE)) {
s <- sample_all(model, 1, multigraph = multigraph)
expect_equal(nrow(s$el), 0, label = name)
expect_equal(sum(s$A), 0, label = name)
expect_equal(igraph::ecount(s$ig), 0, label = name)
expect_equal(igraph::ecount(s$tg), 0, label = name)
if (!multigraph) {
expect_named(s$el, c("from", "to", "weight"))
}
}
}
})

test_that("empty blocks and zero-probability blocks get no nodes and no edges", {
set.seed(101)
B <- matrix(c(0.5, 0.2, 0.1, 0.2, 0.4, 0.3, 0.1, 0.3, 0.6), 3)

m <- sbm(n = 20, B = B, block_sizes = c(8, 0, 12))
expect_equal(sort(as.vector(table(m$z))), c(0, 8, 12))
empty <- which(table(m$z) == 0)
expect_equal(sum(as.matrix(m$X)[, empty]), 0)

m <- dcsbm(theta = rexp(20) + 0.5, B = B, pi = c(0.5, 0, 0.5))
empty <- which(m$pi == 0)
expect_length(empty, 1)
expect_equal(sum(m$z == levels(m$z)[empty]), 0)

# sampling never touches the empty block
P <- dense_expectation(m)
for (seed in 1:5) {
set.seed(seed)
A <- sample_sparse(m)
expect_true(all(as.matrix(A)[P == 0] == 0))
}

# directed models with empty blocks on either side
set.seed(102)
dm <- directed_dcsbm(
theta_out = rep(1, 15),
theta_in = rep(1, 15),
B = matrix(runif(6), 2),
block_sizes_out = c(0, 15),
pi_in = c(0.5, 0, 0.5)
)
expect_true(any(table(dm$z_out) == 0))
expect_true(any(table(dm$z_in) == 0))
expect_equal(dim(sample_sparse(dm)), c(15, 15))
})

test_that("balanced default block sizes differ by at most one", {
for (n in c(5, 7, 10, 23)) {
for (k in 1:5) {
sizes <- balanced_block_sizes(n, k)
expect_equal(sum(sizes), n)
expect_length(sizes, k)
expect_lte(max(sizes) - min(sizes), 1)

set.seed(103)
m <- sbm(n = n, B = diag(k) * 0.5 + 0.1)
expect_equal(sort(as.vector(table(m$z))), sort(sizes))
}
}
})

test_that("rank one models of every kind work", {
set.seed(104)
models <- list(
sbm = sbm(n = 10, B = matrix(0.3)),
dcsbm = dcsbm(theta = rexp(10), B = matrix(0.3)),
dcsbm_unsorted = dcsbm(theta = rexp(10), B = matrix(0.3), sort_nodes = FALSE),
planted_partition = planted_partition(n = 10, k = 1, within_block = 0.3, between_block = 0.1),
overlapping = overlapping_sbm(n = 10, B = matrix(0.3)),
directed = directed_dcsbm(theta_out = rexp(10), theta_in = rexp(10), B = matrix(0.3)),
directed_pp = directed_planted_partition(n = 10, k = 1, within_block = 0.3, between_block = 0.1)
)

for (name in names(models)) {
model <- models[[name]]
P <- dense_expectation(model)
expect_equal(qr(P)$rank, 1, label = name)
expect_no_error(sample_sparse(model))
}
})

test_that("planted_partition() validates its inputs", {
expect_error(
planted_partition(n = 2, k = 3, within_block = 0.5, between_block = 0.1),
"less than or equal to `n`"
)
expect_error(
planted_partition(n = 10, k = 2, within_block = 0.5, between_block = c(0.1, 0.2)),
"`between_block` must be a scalar"
)
expect_error(
planted_partition(n = 10, k = 2, within_block = 0.5, between_block = 1.5),
"`between_block` must be between zero and one"
)
expect_error(
planted_partition(n = 10, k = 2, within_block = -0.5, between_block = 0.1),
"non-negative"
)
})

test_that("planted_partition() a and b parameterization divides by n", {
set.seed(105)
m <- planted_partition(n = 50, k = 2, a = 10, b = 2)
expect_equal(m$within_block, 10 / 50)
expect_equal(m$between_block, 2 / 50)
S <- as.matrix(m$S)
expect_equal(unique(diag(S)), 0.2)
expect_equal(unique(S[row(S) != col(S)]), 0.04)
})

test_that("sample_edgelist() works directly on matrices", {
set.seed(106)
X <- matrix(rexp(10), 5)
S <- matrix(c(0.5, 0.1, 0.2, 0.3), 2)
Y <- matrix(rexp(14), 7)

el <- sample_edgelist(X, S, Y, TRUE, TRUE, TRUE)
expect_s3_class(el, "tbl_df")
expect_true(all(el$from %in% 1:5))
expect_true(all(el$to %in% 1:7))

set.seed(106)
el_matrix <- sample_edgelist(Matrix::Matrix(X), Matrix::Matrix(S), Matrix::Matrix(Y), TRUE, TRUE, TRUE)
set.seed(106)
el_base <- sample_edgelist(X, S, Y, TRUE, TRUE, TRUE)
expect_identical(el_matrix, el_base)

# undirected sampling puts every edge in the upper triangle
el <- sample_edgelist(X, S, X, FALSE, TRUE, TRUE)
expect_true(all(el$from <= el$to))

expect_error(sample_edgelist(X, S, Y, "yes", TRUE, TRUE))
expect_error(sample_edgelist(X, S, Y, TRUE, TRUE, TRUE, multigraph = NA), "multigraph")
})
Loading
Loading