Skip to content

Commit 66409e3

Browse files
aurorarossinatemasimonschoelly
authored
Fix randbn(n,p) (#195)
* Fix randbn * Add tests randbn * Add comment Co-authored-by: Emanuele Natale <[email protected]> * Use statistics Co-authored-by: Emanuele Natale <[email protected]> * Add std from Statistics for test * Add special case test Co-authored-by: Simon Schölly <[email protected]> Co-authored-by: Emanuele Natale <[email protected]> Co-authored-by: Simon Schölly <[email protected]>
1 parent ea6bcfe commit 66409e3

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/SimpleGraphs/generators/randgraphs.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ function randbn(
114114
log_q = log(1.0 - p)
115115
x = 0
116116
sum = 0.0
117-
while true
117+
for _ in 1:n
118118
sum += log(rand(rng)) / (n - x)
119119
sum < log_q && break
120120
x += 1

test/runtests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using Compat
1111
using DelimitedFiles
1212
using Base64
1313
using Random
14-
using Statistics: mean
14+
using Statistics: mean, std
1515
using StableRNGs
1616

1717
const testdir = dirname(@__FILE__)

test/simplegraphs/generators/randgraphs.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,4 +364,24 @@
364364
@test isvalid_simplegraph(rog3)
365365
@test !is_cyclic(rog3)
366366
end
367+
368+
@testset "randbn" begin
369+
for i in 0:10
370+
@test Graphs.SimpleGraphs.randbn(i, 0.0; rng=rng) == 0
371+
@test Graphs.SimpleGraphs.randbn(i, 1.0; rng=rng) == i
372+
end
373+
N = 30
374+
s1 = zeros(N)
375+
s2 = zeros(N)
376+
for i in 1:N
377+
s1[i] = Graphs.SimpleGraphs.randbn(5, 0.3)
378+
s2[i] = Graphs.SimpleGraphs.randbn(3, 0.7)
379+
end
380+
μ1 = mean(s1)
381+
μ2 = mean(s2)
382+
sv1 = std(s1)
383+
sv2 = std(s2)
384+
@test μ1 - sv1 <= 0.3 * 5 <= μ1 + sv1 # since the stdev of μ1 is around sv1/sqrt(N), this should rarely fail
385+
@test μ2 - sv2 <= 0.7 * 3 <= μ2 + sv2
386+
end
367387
end

0 commit comments

Comments
 (0)