# Define a distribution from a given distribution

**URL:** <https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220>\
**Category:** Probabilistic Programming\
**Tags:** distributions\
**Created:** [October 12, 2020, 1:12pm UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220 "2020-10-12T13:12:31Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcql](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@mcql](https://discourse.julialang.org/u/mcql)\
**Post date:** [October 12, 2020, 1:12pm UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220/1 "2020-10-12T13:12:31Z")

</div>

Hello,  
I would like to define two distributions:

```julia
p = Uniform(0,1)
p2 = 1-p

```

give me typrerror.

> ERROR: MethodError: no method matching -(::Int64, ::Uniform{Float64})

I also cannot sum two distributions:

```julia
c = Normal(20,5) + Normal(10, 6)

```

I found this Issue: [Generic sum and product distributions · Issue #142 · JuliaStats/Distributions.jl · GitHub](https://github.com/JuliaStats/Distributions.jl/issues/142)  
Is it possible to build the new distribution in different ways?  
Has Turing a better support for operations among distributions?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 12, 2020, 2:20pm UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220/2 "2020-10-12T14:20:08Z")

</div>

AFAIK these kind of operations are not supported by Distributions.jl at the moment.

What do you want to do with the resulting new distribution? Eg generate random draws, calculate pdf/cdf/quantiles, etc?

---

<div class="post-metadata">

**Author:** ![mcql](https://avatars.discourse-cdn.com/v4/letter/m/59ef9b/32.png) [@mcql](https://discourse.julialang.org/u/mcql)\
**Post date:** [October 12, 2020, 2:34pm UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220/3 "2020-10-12T14:34:05Z")

</div>

> [@Tamas\_Papp](#):
>
> What do you want to do with the resulting new distribution?

I need them to use in Turing models, samples generation and posterior inference.

---

<div class="post-metadata">

**Author:** ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)\
**Post date:** [October 12, 2020, 2:58pm UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220/4 "2020-10-12T14:58:40Z")

</div>

The problem here is distinguishing between a _distribution_ and a _sample from a distribution_. Even if you could define something like

```julia
p2 = (1-x for x in p)

```

then there would _still_ be no way to distinguish `p` from `p2`.

There are two ways out:

1. Define a (degenerate) distribution over pairs. I don’t think Distributions will help you here, but you’ll be able to do this in MeasureTheory.jl.
2. Work in terms of samples

For the latter, it’s easy in MonteCarloMeasurements:

```julia
julia> using MonteCarloMeasurements

julia> p = Particles(Uniform())
Particles{Float64,2000}
 0.5 ± 0.289

julia> p2 = 1-p
Particles{Float64,2000}
 0.5 ± 0.289

julia> p+p2
Particles{Float64,2000}
 1.0

```

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [October 12, 2020, 3:06pm UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220/5 "2020-10-12T15:06:33Z")

</div>

Chances are that your best bet is to circumvent the problem. What are you trying to _do_, as Tamas asks?

---

<div class="post-metadata">

**Author:** ![cpfiffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cpfiffer/32/208747_2.png) [@cpfiffer](https://discourse.julialang.org/u/cpfiffer)\
**Post date:** [October 12, 2020, 3:51pm UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220/6 "2020-10-12T15:51:32Z")

</div>

For Turing, you should be using the tilde operator (`~`) rather than assignment (`=`).

Your first example would be

```julia
p ~ Uniform(0,1)
p2 = 1-p

```

which will work in a Turing `@model` block. For your second example, you would have to draw the two variables separately and assign them before you add them:

```julia
a1 ~ Normal(20,5)
a2 ~ Normal(10, 6)
c = a1 + a2

```

Check out [the documentation](https://turing.ml/dev/docs/using-turing/guide) for more.

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [October 12, 2020, 3:52pm UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220/7 "2020-10-12T15:52:35Z")

</div>

1. have you looked @ [Expectations](https://github.com/QuantEcon/Expectations.jl).jl?  
It allows you to compute expectations of transformations of random variables.

```julia
dist = Normal()
E = expectation(dist)
E(x -> x)
expectation(x->x^2, dist)
#
d = MixtureModel([Uniform(), Normal(), Gamma()]);
E = expectation(d);
E(x -> x) # 0.5000000000000016

```

1. I think it would be amazing if Distributions.jl supported transformations of random variables.  
[Mathematica](https://reference.wolfram.com/language/ref/TransformedDistribution.html) is the only software I know that does this well currently.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/0/8/08ee264183368059dde489af8be69e2766058749.png)  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/2/b/2b37ba7fad7ec32b4e5c2d239c24568d853cb701.png)  
I really hope this becomes available in Distributions.jl.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 13, 2020, 8:18am UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220/8 "2020-10-13T08:18:45Z")

</div>

> [@mcql](#):
>
> samples generation and posterior inference

Those are two distinct problems. Samples generation is super easy (you just define a `rand`), but inference will be tricker: for monotone transformations you just transform the likelihood and correct with the Jacobian, while for sums that don’t result in a “known” distribution you have to do something else, eg latent variables.

This is one of those problems that are easy to state but require a moderate amount of work to solve, specific to the problem. You should focus on the actual question you are trying to solve and describe that here, so that we can give more specific help.

---

<div class="post-metadata">

**Author:** ![Marco\_Antoniotti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_antoniotti/32/18697_2.png) [@Marco\_Antoniotti](https://discourse.julialang.org/u/Marco_Antoniotti)\
**Post date:** [October 15, 2020, 7:10pm UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220/9 "2020-10-15T19:10:46Z")

</div>

Hi Tamas

that may be the kind of problem that is intriguing enough to try to solve…

Just to have some guidance here, the place to start looking into is Distributions.jl? And look at the semantics provided by Mathematica as @Albert_Zevelev suggests?

All the best

Marco

---

<div class="post-metadata">

**Author:** ![Albert\_Zevelev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/albert_zevelev/32/11844_2.png) [@Albert\_Zevelev](https://discourse.julialang.org/u/Albert_Zevelev)\
**Post date:** [October 15, 2020, 7:42pm UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220/10 "2020-10-15T19:42:41Z")

</div>

@Marco_Antoniotti I have to make an important clarification.

Mathematica can handle transformations of a distribution when it can solve the problem symbolically.  
In Julia things are usually done numerically, not symbolically.  
Eg: Expectations.jl computes expectations numerically using quadrature.

For example consider a transformed Beta[4,3]. Mathematica can do all the following except it has no closed form for the entropy.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/4/94386d64d497361626c213c1d0c160ca181cbc9e.png)

Now consider a transformed Beta[\alpha, \beta]:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/3/5/354b0a9cdbe7c7b4db7449f300073f6af75c25ed.png)

I [wrote a cheat-sheet](https://github.com/azev77/QuantEcon.cheatsheet/blob/master/stats-cheatsheet.rst) which shows that Julia handles random variables much better than Matlab/R/STATA.  
If we want Julia to deal w/ general transformations of random variables, we are probably gonna have to do this numerically. (but this means we an handle more transformations than Mathematica)

---

<div class="post-metadata">

**Author:** ![Marco\_Antoniotti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/marco_antoniotti/32/18697_2.png) [@Marco\_Antoniotti](https://discourse.julialang.org/u/Marco_Antoniotti)\
**Post date:** [October 15, 2020, 7:58pm UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220/11 "2020-10-15T19:58:46Z")

</div>

Very nice summary @Albert_Zevelev . Thanks.  
Yes, I agree that things should be done numerically in Julia (unless there were  
any heavy C.A. lifters around; don’t look at me 🙂 )

All the best

Marco

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [October 16, 2020, 8:33am UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220/12 "2020-10-16T08:33:54Z")

</div>

> [@Marco\_Antoniotti](#):
>
> that may be the kind of problem that is intriguing enough to try to solve…

Generally the sum of two independent random variables only has a closed form in some special cases, so I am not sure what you are looking for here.

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [October 16, 2020, 8:55am UTC](https://discourse.julialang.org/t/define-a-distribution-from-a-given-distribution/48220/13 "2020-10-16T08:55:23Z")

</div>

You should look there! In some sense that is my research area. Assume you need to work with the conditional density (or posterior) `p` of `X` given `X + Y` which is not known in closed form. If you can find approximations `X̃`, `Ỹ` where you can compute the close form for the density `p̃` of `X̃` given `X̃ + Ỹ`, then you can use Monte Carlo methods to change samples from the conditional distribution of `X̃` into the conditional distribution `X` that you are actually interested in. Almost used in all of my papers on arxiv: [Moritz Schauer's articles on arXiv](https://arxiv.org/a/0000-0003-3310-7915.html)
