Giter Club home page Giter Club logo

Comments (3)

alexpghayes avatar alexpghayes commented on September 12, 2024

This is an intentional choice. If you want to count "undirected" edges you should just consider the upper triangle here. That is, the edge count is based on the actual edges in the graph rather the elements in the sparse matrix representation of the graph. I spent a lot longer than I anticipated running into confusing inconsistencies between the undirected and directed samplers until I settled on this convention, and am pretty convinced it is the right way to implement things.

If you want to specify E[A] for an undirected SBM according to row means, we can consider adding an option for that, but I don't think we should call rowMeans(E[A]) the average degree.

library(fastRG)
#> Loading required package: Matrix
library(magrittr)

n <- 1000
pop <- n / 2
a <- .1
b <- .05
B <- matrix(c(a, b, b, a), nrow = 2)

b_model <- sbm(
  n = n, k = 2,
  B = B, edge_distribution = "bernoulli"
)

A <- sample_sparse(b_model)

mean(rowSums(A))  # double counts undirected edges
#> [1] 156.269
mean(rowSums(triu(A)))  # right way to count undirected edges in E[A]
#> [1] 78.194


pop * a + pop * b  # expected "undirected edge degree"
#> [1] 75

Created on 2021-06-29 by the reprex package (v2.0.0)

from fastrg.

alexpghayes avatar alexpghayes commented on September 12, 2024

Crap there's some inconsistency in edge counting. Will resolve.

library(fastRG)
#> Loading required package: Matrix
library(magrittr)

n <- 1000
pop <- n / 2
a <- .1
b <- .05
B <- matrix(c(a, b, b, a), nrow = 2)

b_model <- sbm(
  n = n, k = 2,
  B = B, edge_distribution = "bernoulli"
)

A <- sample_sparse(b_model)

mean(rowSums(A))  # double counts undirected edges
#> [1] 156.845
mean(rowSums(triu(A)))  # right way to count undirected edges in E[A]
#> [1] 78.479


pop * a + pop * b  # expected "undirected edge degree"
#> [1] 75

model2 <- sbm(n = n, k = 2, B = B, edge_distribution = "bernoulli", expected_degree = 75)
A2 <- sample_sparse(model2)

mean(rowSums(A2))  # double counts undirected edges
#> [1] 76.393
mean(rowSums(triu(A2)))  # right way to count undirected edges in E[A]
#> [1] 38.225

Created on 2021-06-29 by the reprex package (v2.0.0)

from fastrg.

alexpghayes avatar alexpghayes commented on September 12, 2024

Undirected edge, degree and density counts should now be consistent. Example below.

library(fastRG)
#> Loading required package: Matrix
library(magrittr)

n <- 1000
pop <- n / 2
a <- .1
b <- .05
B <- matrix(c(a, b, b, a), nrow = 2)

b_model <- sbm(
  n = n, k = 2,
  B = B, edge_distribution = "bernoulli"
)

# model display corrected, both expected degree and density
b_model
#> Undirected Stochastic Blockmodel
#> --------------------------------
#> 
#> Nodes (n): 1000 (arranged by block)
#> Blocks (k): 2
#> 
#> Traditional SBM parameterization:
#> 
#> Block memberships (z): 1000 [factor] 
#> Block probabilities (pi): 2 [numeric] 
#> Edge distribution: bernoulli
#> 
#> Factor model parameterization:
#> 
#> X: 1000 x 2 [dgeMatrix] 
#> S: 2 x 2 [dsyMatrix] 
#> 
#> Expected edges: 78329
#> Expected degree: 78.3
#> Expected density: 0.15681

A <- sample_sparse(b_model)

mean(rowSums(A))  # double counts undirected edges
#> [1] 155.641
mean(rowSums(triu(A)))  # right way to count undirected edges in E[A]
#> [1] 77.876

sum(triu(A)) / choose(n, 2)  # correct "undirected" density
#> [1] 0.1559079


pop * a + pop * b  # expected "undirected edge degree"
#> [1] 75

model2 <- sbm(n = n, k = 2, B = B, edge_distribution = "bernoulli", expected_degree = 75)
A2 <- sample_sparse(model2)

mean(rowSums(A2))  # double counts undirected edges
#> [1] 155.995
mean(rowSums(triu(A2)))  # right way to count undirected edges in E[A]
#> [1] 78.053

sum(triu(A)) / choose(n, 2)  # correct "undirected" density 
#> [1] 0.1559079

Created on 2022-06-29 by the reprex package (v2.0.1.9000)

from fastrg.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.