# Binomial distribution without Distributions.jl?

**URL:** <https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176>\
**Category:** Statistics\
**Created:** [April 11, 2017, 7:55pm UTC](https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176 "2017-04-11T19:55:48Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 11, 2017, 7:55pm UTC](https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176/1 "2017-04-11T19:55:48Z")

</div>

Hi all,

This is not intended to be a knock on the excellent Distributions.jl package - we’ve used it happily for several years in LightGraphs and it’s working wonderfully.

We currently have an initiative to make LightGraphs lighter weight in terms of its dependencies, though, and we’re running into a bit of an issue. We use Distributions for exactly one thing: a random number generator using binomial distributions for one of our random graph generators. The problem is that this single function adds at least 12 new dependencies to our dependency tree, including some heavy hitters. Again, we’re trying to lighten up LightGraphs right now, and to have this number of dependencies for a single function seems like a bit of an overkill.

So - is there any other way to get a binomial random number generator that would allow us to achieve our weight-loss goal?

---

<div class="post-metadata">

**Author:** ![mkborregaard](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mkborregaard/32/556_2.png) [@mkborregaard](https://discourse.julialang.org/u/mkborregaard)\
**Post date:** [April 11, 2017, 8:09pm UTC](https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176/2 "2017-04-11T20:09:58Z")

</div>

The code for that in Distributions is exactly

```julia
rand(d::Binomial) = convert(Int, StatsFuns.RFunctions.binomrand(d.n, d.p))

```

I guess you could simply copy that? StatsFuns itself requires only Rmath and SpecialFunctions.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 11, 2017, 8:14pm UTC](https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176/3 "2017-04-11T20:14:51Z")

</div>

Right. Rmath requires BinDeps and a few other things, though, and I’d really like to avoid BinDeps if at all possible since it introduces cross-platform fragility and has a lot of overhead. (Again, not knocking it, just trying to put the “Light” back in “LightGraphs”.)

---

<div class="post-metadata">

**Author:** ![ararslan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ararslan/32/3825_2.png) [@ararslan](https://discourse.julialang.org/u/ararslan)\
**Post date:** [April 11, 2017, 8:19pm UTC](https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176/4 "2017-04-11T20:19:28Z")

</div>

If you want to go _really_ lightweight and you know that your sample sizes will be sufficiently large, you can use `Base.randn`, since the binomial distribution converges to a standard normal as n increases (see [Central Limit Theorem](https://en.wikipedia.org/wiki/Central_Limit_Theorem)).

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 11, 2017, 8:27pm UTC](https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176/5 "2017-04-11T20:27:50Z")

</div>

We’re using it exactly once per graph, in order to get a single value that represents the number of edges to generate (m = `|V|^2`, and I don’t know about `p`) so I’m not sure `randn` will work – but I will defer to stats experts on this.

---

<div class="post-metadata">

**Author:** ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)\
**Post date:** [April 11, 2017, 8:38pm UTC](https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176/6 "2017-04-11T20:38:25Z")

</div>

Check out [https://github.com/JuliaStats/Distributions.jl/blob/master/src/samplers/binomial.jl](https://github.com/JuliaStats/Distributions.jl/blob/master/src/samplers/binomial.jl). There are some samplers written in Julia. We don’t use them by default but I think they work.

---

<div class="post-metadata">

**Author:** ![malmaud](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/malmaud/32/29_2.png) [@malmaud](https://discourse.julialang.org/u/malmaud)\
**Post date:** [April 11, 2017, 8:45pm UTC](https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176/7 "2017-04-11T20:45:55Z")

</div>

It’s weird that there isn’t a pure-Julia version of Distributions.jl - writing efficient samplers seems to be something Julia is uniquely suited for.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 11, 2017, 9:34pm UTC](https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176/8 "2017-04-11T21:34:32Z")

</div>

> [@malmaud](#):
>
> It’s weird that there isn’t a pure-Julia version of Distributions.jl - writing efficient samplers seems to be something Julia is uniquely suited for.

Yeah, but people want to “get everything done” first.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 11, 2017, 10:40pm UTC](https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176/9 "2017-04-11T22:40:26Z")

</div>

So, here’s what I wound up doing:

```julia
# taken from http://stackoverflow.com/questions/23561551/a-efficient-binomial-random-number-generator-code-in-java
function _getBinomial(n::Integer, p::Real, seed::Integer=-1)
    rng = getRNG(seed)
    log_q = log(1.0 - p)
    x = 0
    sum = 0.0
    while true
        sum += log(rand(rng)) / (n - x)
        sum < log_q && break
        x += 1
    end
    return x
end

```

Passes all tests so far.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [April 11, 2017, 11:21pm UTC](https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176/10 "2017-04-11T23:21:50Z")

</div>

You probably need to have an attribution in your license for this. Make sure you add that.

---

<div class="post-metadata">

**Author:** ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)\
**Post date:** [April 11, 2017, 11:44pm UTC](https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176/11 "2017-04-11T23:44:07Z")

</div>

Just wanted to say that the fact that LightGraphs has so many dependencies was a main reason for me not to use it and instead roll my own, so I very much appreciate this direction.

---

<div class="post-metadata">

**Author:** ![anon94023334](https://avatars.discourse-cdn.com/v4/letter/a/e274bd/32.png) [@anon94023334](https://discourse.julialang.org/u/anon94023334)\
**Post date:** [April 11, 2017, 11:57pm UTC](https://discourse.julialang.org/t/binomial-distribution-without-distributions-jl/3176/12 "2017-04-11T23:57:19Z")

</div>

@tkoolen - thanks. Take a look at [https://github.com/JuliaGraphs/LightGraphs.jl/issues/557#issuecomment-292806096](https://github.com/JuliaGraphs/LightGraphs.jl/issues/557#issuecomment-292806096) for the current status. We will be down to 4 direct dependencies, with 8 total. I think that’s as good as we’re going to get.
