# Need to sample from two-parameter univariate exponential distribution

**URL:** <https://discourse.julialang.org/t/need-to-sample-from-two-parameter-univariate-exponential-distribution/39288>\
**Category:** New to Julia\
**Tags:** distributions\
**Created:** [May 11, 2020, 2:54pm UTC](https://discourse.julialang.org/t/need-to-sample-from-two-parameter-univariate-exponential-distribution/39288 "2020-05-11T14:54:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Manoj\_Gopalkrishnan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj_gopalkrishnan/32/14587_2.png) [@Manoj\_Gopalkrishnan](https://discourse.julialang.org/u/Manoj_Gopalkrishnan)\
**Post date:** [May 11, 2020, 2:54pm UTC](https://discourse.julialang.org/t/need-to-sample-from-two-parameter-univariate-exponential-distribution/39288/1 "2020-05-11T14:54:35Z")

</div>

Hello,

I need to sample from a two-parameter family of continuous univariate distributions supported on the interval [0,C] where C is around 10^{12}. The parameters are \alpha and \beta and the PDF is proportional to e^{-\alpha x - \beta x^2}, so this is a two-parameter exponential family.

What’s the easiest way to do this? Do I have to write my own distribution, or is there some support in Julia for exponential families?

In my use case, \alpha and \beta are obeying a differential equation, so I sample from different distributions as time changes, hence I need to sample from the entire family.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 11, 2020, 3:17pm UTC](https://discourse.julialang.org/t/need-to-sample-from-two-parameter-univariate-exponential-distribution/39288/2 "2020-05-11T15:17:55Z")

</div>

Can’t you complete the square? e^{-\alpha x - \beta x^2} = \mathrm{constant} \times e^{-\beta (x + \alpha/2\beta)^2}, so it’s equivalent to drawing from a Gaussian distribution with a shifted mean.

---

<div class="post-metadata">

**Author:** ![Manoj\_Gopalkrishnan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/manoj_gopalkrishnan/32/14587_2.png) [@Manoj\_Gopalkrishnan](https://discourse.julialang.org/u/Manoj_Gopalkrishnan)\
**Post date:** [May 11, 2020, 3:24pm UTC](https://discourse.julialang.org/t/need-to-sample-from-two-parameter-univariate-exponential-distribution/39288/3 "2020-05-11T15:24:19Z")

</div>

Wow that’s a lovely solution! Yes I will have to truncate my Gaussian both sides, but should work!

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [May 11, 2020, 4:50pm UTC](https://discourse.julialang.org/t/need-to-sample-from-two-parameter-univariate-exponential-distribution/39288/4 "2020-05-11T16:50:11Z")

</div>

> [@Manoj\_Gopalkrishnan](#):
>
> I will have to truncate my Gaussian both sides

Note that Distributions.jl [supports truncated normal distributions](https://juliastats.org/Distributions.jl/stable/truncate/).
