Skip to content

Instantly share code, notes, and snippets.

@alexpghayes
Created November 11, 2021 18:43
Show Gist options
  • Save alexpghayes/e9f0590ba69af7aab988293737bd22ec to your computer and use it in GitHub Desktop.
Save alexpghayes/e9f0590ba69af7aab988293737bd22ec to your computer and use it in GitHub Desktop.
library(tidyverse)
library(invertiforms) # RoheLab/invertiforms
library(igraphdata)
library(igraph)
library(RSpectra)
library(scales)
data("enron", package = "igraphdata")
A <- enron %>%
as_adjacency_matrix()
rank <- 10
num_tau <- 10
tau <- 10^seq(-2, 4, length.out = num_tau)
laplacians <- tibble(tau = tau) %>%
mutate(
scaler = map(tau, ~ RegularizedLaplacian(A, .x, .x)),
L_tau = map(scaler, ~ transform(.x, A))
)
decomposed <- laplacians %>%
mutate(
svd = map(L_tau, svds, k = rank)
)
cumulative_participation <- function(U) {
sum(rowSums(U^2)^2)
}
localization <- decomposed %>%
mutate(
u = map_dbl(svd, ~ cumulative_participation(.x$u)),
v = map_dbl(svd, ~ cumulative_participation(.x$v))
)
# these are the same now but might differ for rectangular A
mean_rs <- mean(rowSums(A))
mean_cs <- mean(colSums(A))
localization %>%
select(-scaler, -L_tau, -svd) %>%
gather(subspace, localization, u, v) %>%
ggplot() +
aes(tau, localization, color = subspace, group = subspace) +
geom_line() +
geom_vline(xintercept = mean_rs, linetype = "dashed") +
geom_vline(xintercept = mean_cs, linetype = "dashed") +
scale_color_viridis_d(begin = 0.15, end = 0.85) +
scale_x_log10(
breaks = trans_breaks("log10", function(x) 10^x),
labels = trans_format("log10", math_format(10^.x))
) +
theme_minimal()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment