# Sum or convolution of discrete uniform random variable

**URL:** <https://discourse.julialang.org/t/sum-or-convolution-of-discrete-uniform-random-variable/15426>\
**Category:** Statistics\
**Tags:** package\
**Created:** [September 24, 2018, 1:43pm UTC](https://discourse.julialang.org/t/sum-or-convolution-of-discrete-uniform-random-variable/15426 "2018-09-24T13:43:19Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![leclere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leclere/32/3644_2.png) [@leclere](https://discourse.julialang.org/u/leclere)\
**Post date:** [September 24, 2018, 1:43pm UTC](https://discourse.julialang.org/t/sum-or-convolution-of-discrete-uniform-random-variable/15426/1 "2018-09-24T13:43:19Z")

</div>

Hello,

I am interested in the convolution of random independent discrete variables (and in particular uniform discrete random variables). Is there an efficient way of constructing this ? Or should I just write my own convolution function ?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 24, 2018, 2:01pm UTC](https://discourse.julialang.org/t/sum-or-convolution-of-discrete-uniform-random-variable/15426/2 "2018-09-24T14:01:50Z")

</div>

Can you be more precise about what you want? Eg the probability mass function?

---

<div class="post-metadata">

**Author:** ![leclere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leclere/32/3644_2.png) [@leclere](https://discourse.julialang.org/u/leclere)\
**Post date:** [September 24, 2018, 2:13pm UTC](https://discourse.julialang.org/t/sum-or-convolution-of-discrete-uniform-random-variable/15426/3 "2018-09-24T14:13:30Z")

</div>

Well, the probability mass function would be nice. But to be precise I want to construct a distribution which is the sum of n independant DiscreteUniform over {1,…,k}. This is thus a Categorical distribution over {n, n+1, …, nk}. I can write the convolution by myself, but was wondering if I missed a function allowing to construct a new discrete random variable as the sum of two (independent) random variables.

Oh, and I thought I had specified it in the tags, but I am using Distributions.jl

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 24, 2018, 2:51pm UTC](https://discourse.julialang.org/t/sum-or-convolution-of-discrete-uniform-random-variable/15426/4 "2018-09-24T14:51:42Z")

</div>

But the question is what you want to do with that? E.g. if you just want to sample from it, you don’t have to construct it at all:

```julia
julia> mysample(k, N) = sum(rand(1:k) for i in 1:N)
mysample (generic function with 1 method)

julia> mysample(5, 10)
36

julia> mysample(5, 10)
23

julia> mysample(5, 10)
35

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 24, 2018, 2:52pm UTC](https://discourse.julialang.org/t/sum-or-convolution-of-discrete-uniform-random-variable/15426/5 "2018-09-24T14:52:27Z")

</div>

But I agree that conceptually it would be nice to be able to write `Z = X + Y` for random variables `X` and `Y`.

---

<div class="post-metadata">

**Author:** ![leclere](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/leclere/32/3644_2.png) [@leclere](https://discourse.julialang.org/u/leclere)\
**Post date:** [September 24, 2018, 2:58pm UTC](https://discourse.julialang.org/t/sum-or-convolution-of-discrete-uniform-random-variable/15426/6 "2018-09-24T14:58:21Z")

</div>

I want to have it as a Distribution object, to be able to do stuff with it 😉

Simple example in mind : finding the probability that rolling 100 6-faced dices give less than 50 12-faced dices.

I am actually cooking up hands on session around the CLT and its limits, and I hoped that a `Z = X + Y` method would exist instead of having to code it by myself.

---

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [September 24, 2018, 3:23pm UTC](https://discourse.julialang.org/t/sum-or-convolution-of-discrete-uniform-random-variable/15426/7 "2018-09-24T15:23:41Z")

</div>

There’s packages like [Turing](https://github.com/TuringLang/Turing.jl) but I think it only support sampling (which is the preferred method when models become complicated), and I’m not sure it actually supports adding random variables.

Usually I just write down the convolution by hand, something like:

```julia
a = Distributions.DiscreteUniform(0,10)
b = Distributions.DiscreteUniform(0,10)

mydensity(a,b,x) = sum(pdf(a,x-k)*pdf(b,k) for k=0:10)

plot(y=mydensity.(0:20),Geom.line)

```

But that’s probably not ideal.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [September 25, 2018, 1:16am UTC](https://discourse.julialang.org/t/sum-or-convolution-of-discrete-uniform-random-variable/15426/8 "2018-09-25T01:16:47Z")

</div>

I don’t see how having it as a Distributions object will help you to do that calculation?

---

<div class="post-metadata">

**Author:** ![harven](https://avatars.discourse-cdn.com/v4/letter/h/3da27b/32.png) [@harven](https://discourse.julialang.org/u/harven)\
**Post date:** [April 28, 2021, 4:07pm UTC](https://discourse.julialang.org/t/sum-or-convolution-of-discrete-uniform-random-variable/15426/9 "2021-04-28T16:07:07Z")

</div>

I am reviving that topic, since I am wondering if there is now a package that implements basic operations on random variables such as sum, product, maximum etc. Preferably compatible with the current statistical libraries.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [April 29, 2021, 4:15pm UTC](https://discourse.julialang.org/t/sum-or-convolution-of-discrete-uniform-random-variable/15426/10 "2021-04-29T16:15:38Z")

</div>

@harven: Try this notebook: [18S191/random\_variables\_as\_types.jl at Spring21 · mitmath/18S191 · GitHub](https://github.com/mitmath/18S191/blob/Spring21/notebooks/week7/random_variables_as_types.jl)

from our course at MIT, viewable online here:  
[https://computationalthinking.mit.edu/Spring21/random\_variables\_as\_types/](https://computationalthinking.mit.edu/Spring21/random_variables_as_types/)

Also possibly the [MeasureTheory.jl](https://github.com/cscherrer/MeasureTheory.jl) package.

cc @cscherrer

---

<div class="post-metadata">

**Author:** ![acwatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/acwatt/32/35645_2.png) [@acwatt](https://discourse.julialang.org/u/acwatt)\
**Post date:** [March 3, 2023, 2:22pm UTC](https://discourse.julialang.org/t/sum-or-convolution-of-discrete-uniform-random-variable/15426/11 "2023-03-03T14:22:14Z")

</div>

In case anyone stumbles across this thread, a related convolutions thread (and some updated packages, code snippets, and solutions) is here:

> [@Distribution of sum of random variables](https://discourse.julialang.org/t/distribution-of-sum-of-random-variables/69023/5):
>
> As @mschauer said, it would be helpful to get more information. But in lieu of that, I’ll assume we’re talking about continuous univariate random variables being scaled by real scalars and added. First, Distributions.jl can handle the scaling with no problems, e.g. julia\> using Distributions julia\> Normal() \* 5 LocationScale{Float64, Continuous, Normal{Float64}}( μ: 0.0 σ: 5.0 ρ: Normal{Float64}(μ=0.0, σ=1.0) ) Addition of random variables is more complicated. The distribution of the sum of …

Note that Distributions.jl and it’s current `Distributions.convolve` function only support a subset of distributions (namely, not uniform random variables, discrete or continuous).
