Sum or convolution of discrete uniform random variable

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 ?

1 Like

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

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

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> 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

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

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

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.

1 Like

Thereā€™s packages like Turing 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:

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.

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

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.

@harven: Try this notebook: 18S191/random_variables_as_types.jl at Spring21 Ā· mitmath/18S191 Ā· GitHub

from our course at MIT, viewable online here:
https://computationalthinking.mit.edu/Spring21/random_variables_as_types/

Also possibly the MeasureTheory.jl package.

cc @cscherrer

5 Likes

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

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).