Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Bernoulli random graph #200

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
061f7a7
Fix randbn
aurorarossi Nov 22, 2022
cad14af
Add tests randbn
aurorarossi Nov 22, 2022
c9fe434
Add comment
aurorarossi Nov 22, 2022
3876f6e
Use statistics
aurorarossi Nov 22, 2022
a023ad9
Add std from Statistics for test
aurorarossi Nov 22, 2022
48f12c4
Add bernoulli graph
aurorarossi Nov 22, 2022
1c7bd09
Add correlated bernoulli graphs
aurorarossi Nov 22, 2022
b8eb7f6
Add tests
aurorarossi Nov 22, 2022
ec62227
Export new functions
aurorarossi Nov 22, 2022
923a665
Remove bracket
aurorarossi Nov 22, 2022
a850907
Merge branch 'master' into bernoulli_random_graphs
aurorarossi Nov 30, 2022
e57b04a
Remove merged double test
aurorarossi Nov 30, 2022
fd249a9
Add test for non-isomorphism case
aurorarossi Nov 30, 2022
40c3dfa
Remove test
aurorarossi Nov 30, 2022
ed768f7
Fix typo
aurorarossi Nov 30, 2022
795df03
Add assert size
aurorarossi Dec 5, 2022
1ad8c10
Fix docstrings
aurorarossi Dec 5, 2022
13ddd4a
Add more tests
aurorarossi Dec 6, 2022
dab77a6
Fix seed and rng bernoulli
aurorarossi Dec 6, 2022
283a335
Add parenthesis, remove cor
aurorarossi Dec 6, 2022
7d5f907
Remove double space
aurorarossi Dec 8, 2022
6cec8cc
Add space
aurorarossi Dec 8, 2022
7275adb
Remove brackets in ignore formatter
aurorarossi Dec 13, 2022
7eb11dd
Merge branch 'bernoulli_random_graphs' of https://github.com/auroraro…
aurorarossi Dec 13, 2022
7e8599b
Change Lambda type in Abstract Matrix
aurorarossi Dec 16, 2022
07ae8e7
Remove seed in bernoulli graph
aurorarossi Dec 16, 2022
559e49f
Remove seed in test bernoulli graph
aurorarossi Dec 16, 2022
855eb79
Remove comment
aurorarossi Dec 16, 2022
2a84eac
Remove seed in rho correlated
aurorarossi Dec 16, 2022
7e66910
Remove rng function
aurorarossi Dec 16, 2022
0a1f121
Change in sparse matrix rho correlated
aurorarossi Dec 16, 2022
22ed227
Remove seed test
aurorarossi Dec 16, 2022
7146795
Remove assert
aurorarossi Dec 16, 2022
758e95f
Add check Lambda symmetric
aurorarossi Dec 16, 2022
e958ffd
Add test_throws bernoulli graphs
aurorarossi Dec 16, 2022
0323cbd
Change name variables and formatting
aurorarossi Dec 16, 2022
806f93d
Simplify lines
aurorarossi Dec 16, 2022
0043a16
Remove `Int.`
aurorarossi Dec 16, 2022
fa9e5c7
Add rng in docstrings
aurorarossi Dec 16, 2022
04ed8a2
Add tuple info in docstring
aurorarossi Dec 16, 2022
b5025e3
Remove heavy latex
aurorarossi Dec 16, 2022
6b04036
Add explanation in docstring
aurorarossi Dec 16, 2022
2746f6f
Add error rho interval
aurorarossi Dec 16, 2022
f072757
Add test rho exception
aurorarossi Dec 16, 2022
b9ca077
Format
aurorarossi Dec 16, 2022
64fc9ec
Add eltype specification
aurorarossi Mar 26, 2023
698eb46
Rename function
aurorarossi Feb 14, 2024
39627f6
Rename function and remove latex docstrings
aurorarossi Feb 14, 2024
aea0909
Merge branch 'my_master' into bernoulli_random_graphs
aurorarossi Feb 14, 2024
1d326cb
Merge branch 'my_master' into bernoulli_random_graphs
aurorarossi Feb 14, 2024
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
2 changes: 2 additions & 0 deletions src/Graphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,8 @@ export
kronecker,
dorogovtsev_mendes,
random_orientation_dag,
bernoulli_graph,
correlated_bernoulli_graphs,

# community
modularity,
Expand Down
2 changes: 2 additions & 0 deletions src/SimpleGraphs/SimpleGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export AbstractSimpleGraph,
static_scale_free,
kronecker,
random_orientation_dag,
bernoulli_graph,
correlated_bernoulli_graphs,
# generators
complete_graph,
star_graph,
Expand Down
51 changes: 51 additions & 0 deletions src/SimpleGraphs/generators/randgraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1555,3 +1555,54 @@ function random_orientation_dag(
end
return g2
end

"""
bernoulli_graph(Λ; rng = nothing, nodes_type = Int64)

Given the symmetric matrix `Λ ∈ [0,1]^{n × n}`, return a Bernoulli graph with `n` vertices. Each edge `(i,j)` exists with probability `Λ[i,j]`.
"""
function bernoulli_graph(
Λ::AbstractMatrix{<:AbstractFloat};
rng::Union{Nothing,AbstractRNG}=nothing,
nodes_type::Type=Int64,
)
size(Λ)[1] != size(Λ)[2] &&
throw(ArgumentError("The probability matrix must be a square matrix"))
!issymmetric(Λ) && throw(ArgumentError("Λ must be symmetric"))
n = size(Λ)[1]
g = SimpleGraph{nodes_type}(n)
for j in 1:n
for i in (j + 1):n
aurorarossi marked this conversation as resolved.
Show resolved Hide resolved
if rand(rng) <= Λ[i, j]
add_edge!(g, i, j)
end
end
end
return g
end

"""
correlated_bernoulli_graphs(Λ, ρ; rng = nothing, nodes_type = Int64)

Given the symmetric matrix `Λ ∈ [0,1]^{n × n}` and a real number `ρ ∈ [0,1]` return a Tuple with two ρ - correlated Bernoulli graphs with `n` vertices. It means that, calling `A` and `B` the adjacency matrix of the outputed graphs, for `i,j` such that `i!=j` the Pearson correlation coefficient for `A_{i,j}` and `B_{i,j}` is `ρ`.
"""
function correlated_bernoulli_graphs(
Λ::AbstractMatrix{<:AbstractFloat},
ρ::Float64;
rng::Union{Nothing,AbstractRNG}=nothing,
nodes_type::Type=Int64,
)
(ρ < 0.0 || ρ > 1.0) && throw(ArgumentError("ρ must be in [0,1]"))
n = size(Λ)[1]
g2 = SimpleGraph{nodes_type}(n)
g1 = bernoulli_graph(Λ; rng, nodes_type=eltype(g2))
g1_adj = Int.(adjacency_matrix(g1))
for j in 1:n
for i in (j + 1):n
if rand(rng) <= ((1 - ρ) * Λ[i, j] + ρ * g1_adj[i, j])
add_edge!(g2, i, j)
end
end
end
return g1, g2
end
29 changes: 29 additions & 0 deletions test/simplegraphs/generators/randgraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -423,4 +423,33 @@
@test μ1 - sv1 <= 0.3 * 5 <= μ1 + sv1 # since the stdev of μ1 is around sv1/sqrt(N), this should rarely fail
@test μ2 - sv2 <= 0.7 * 3 <= μ2 + sv2
end

@testset "bernoulli graphs" begin
n = 50
Λ = sparse(rand(n, n))
@test_throws ArgumentError("Λ must be symmetric") bernoulli_graph(Λ; rng)
@test_throws ArgumentError("The probability matrix must be a square matrix") bernoulli_graph(
sparse(rand(2, 3)); rng
)
Λ = Symmetric(Λ)
@test_throws ArgumentError("ρ must be in [0,1]") correlated_bernoulli_graphs(
Λ, -1.0; rng
)
ρ = 1.0 # isomorphism case
aurorarossi marked this conversation as resolved.
Show resolved Hide resolved
g1, g2 = correlated_bernoulli_graphs(Λ, ρ; rng)
g1_adj = adjacency_matrix(g1)
g2_adj = adjacency_matrix(g2)
@test g1_adj == g2_adj
@test diag(g1_adj) == diag(g2_adj) == zeros(n)
ρ = 0.5 # non isomorphism case
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we do a probabilistic test on the correlation with ample error margins?

g3, g4 = correlated_bernoulli_graphs(Λ, ρ; rng)
g3_adj = adjacency_matrix(g3)
g4_adj = adjacency_matrix(g4)
@test g3_adj != g4_adj
@test diag(g3_adj) == diag(g4_adj) == zeros(n)
g5 = bernoulli_graph(Λ; rng)
for g in testgraphs(g5)
@test nv(g) == n
end
end
end
Loading