# Random variables in Julia (working list)

**URL:** <https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762>\
**Category:** Statistics\
**Tags:** distributions\
**Created:** [October 21, 2020, 5:18pm UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762 "2020-10-21T17:18:20Z")\
**Posts on this page:** 17\
**Page:** 2

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [April 1, 2021, 9:43pm UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/21 "2021-04-01T21:43:52Z")

</div>

> [@Albert\_Zevelev](#):
>
> A few updates:  
> [Lognormals](https://github.com/bgctw/Lognormals.jl).jl  
> [MVN CDF](https://discourse.julialang.org/t/mvn-cdf-have-it-coded-need-help-getting-integrating-into-distributions-jl/38631/15): Distributions.jl doesn’t yet have multi-var CDFs  
> [ThorinDistributions](https://github.com/lrnv/ThorinDistributions.jl).jl

Thanks for noticing ThorinDistributions.jl ! However this is not yet usable and still very unstable project. Someday it might get better though 😉

---

<div class="post-metadata">

**Author:** ![Wei\_Yang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wei_yang/32/23951_2.png) [@Wei\_Yang](https://discourse.julialang.org/u/Wei_Yang)\
**Post date:** [April 27, 2021, 4:23am UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/22 "2021-04-27T04:23:17Z")

</div>

😥 I have started working on a un-published package on Random Matrix a few weeks ago.  
The goal is to gradually improve it in the next few months as I learned more about Julia while working on this project. And hopefully it will be something worth release at the end of the summer.

[https://github.com/weiyang2048/RandomMatrix.jl](https://github.com/weiyang2048/RandomMatrix.jl)

---

<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:** [May 12, 2021, 5:55pm UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/23 "2021-05-12T17:55:19Z")

</div>

[LRMoE](https://github.com/sparktseung/LRMoE.jl).jl has several Zero Inflated random variables along w/ `Burr` and `GammaCount`.  
He has a [PR](https://github.com/JuliaStats/Distributions.jl/pull/1182) to add Burr to Distributions.jl

---

<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:** [September 19, 2021, 1:11pm UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/24 "2021-09-19T13:11:02Z")

</div>

@tamasgal  
[https://github.com/JuliaHEP/LandauDistribution.jl](https://github.com/JuliaHEP/LandauDistribution.jl)

---

<div class="post-metadata">

**Author:** ![tamasgal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamasgal/32/27946_2.png) [@tamasgal](https://discourse.julialang.org/u/tamasgal)\
**Post date:** [September 19, 2021, 1:19pm UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/25 "2021-09-19T13:19:57Z")

</div>

Thanks @Albert_Zevelev! I am already using it and also contributed `:)` it will be released today as a Julia package.

---

<div class="post-metadata">

**Author:** ![sylvaticus](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sylvaticus/32/203883_2.png) [@sylvaticus](https://discourse.julialang.org/u/sylvaticus)\
**Post date:** [September 19, 2021, 4:15pm UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/26 "2021-09-19T16:15:36Z")

</div>

Another cheatset to compare basic distribution usage with R and Python:  
[https://github.com/sylvaticus/commonDistributionsInJuliaPythonR](https://github.com/sylvaticus/commonDistributionsInJuliaPythonR)

---

<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:** [September 19, 2021, 4:25pm UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/27 "2021-09-19T16:25:16Z")

</div>

See mine wip:

> **[Future Post Random Variables in Julia compared to...](https://azev77.github.io/posts/2021/04/blog-post-2/)**
>
> This post compares the way random variables are handled in Julia/MATLAB/R/STATA/Mathematica/Python.It was inspired by Bruce Hansen’s recent textbookwhich compares statistical commands in Matlab/R/STATA on page 114. This post will focus on the main...

---

<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:** [December 1, 2021, 3:43am UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/28 "2021-12-01T03:43:39Z")

</div>

@mlkrock added a repo w/ a 7-parameter distribution

> [@\[ANN\] BulkAndTails.jl](https://discourse.julialang.org/t/ann-bulkandtails-jl/72310):
>
> Hello. I’ve just registered [BulkAndTails.jl](https://github.com/mlkrock/BulkAndTails.jl), which offers an interface for the seven-parameter Bulk-And-Tails (BATs) distribution in the style of [Distributions.jl](https://github.com/JuliaStats/Distributions.jl). BATs was recently introduced in [Stein 2021](https://onlinelibrary.wiley.com/doi/abs/10.1002/env.2658) to model an entire univariate distribution when extremes (i.e., tails) are of interest. Traditional methods for extremes, such as Distributions.GeneralizedPareto or Distributions.GeneralizedExtremeValue, ignore the bulk (i.e., nonextremes) of the data, and separate analyses must be performed…

---

<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:** [November 8, 2022, 1:34am UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/29 "2022-11-08T01:34:13Z")

</div>

One of the nice features of [Distributions](https://github.com/JuliaStats/Distributions.jl).jl is the ability to create new transformed distributions from existing distributions.

- `MixtureModel([Normal(0,1),Cauchy(0,1)], [0.5,0.5])` returns a new random variable

- `Truncated(Cauchy(0,1), 0.25, 1.8)`

- `convolve(Cauchy(0,1), Cauchy(5,2))`

A recent PR proposes [folded distributions](https://github.com/JuliaStats/Distributions.jl/pull/1631).  
This is cool b/c it automatically allows the user to access a large number of distributions:  
folded-Cauchy/folded-normal/Half-Cauchy/half-logistic/half-normal etc

There has been discussion about a generic ZeroInflated distribution [here](https://github.com/JuliaStats/Distributions.jl/pull/1393), [here](https://discourse.julialang.org/t/truncated-poisson-distribution-probs-not-adding-up-to-one/61053), [here](https://github.com/JuliaStats/Distributions.jl/pull/390)

Are there other important transformations of random variables not considered yet?  
Maybe [CensoredDistribution](https://reference.wolfram.com/language/ref/CensoredDistribution.html), [Conditioned](https://reference.wolfram.com/language/ref/Conditioned.html) & [Derived Statistical Distributions](https://reference.wolfram.com/language/guide/DerivedDistributions.html) can provide some inspiration?

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [November 8, 2022, 1:53am UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/30 "2022-11-08T01:53:28Z")

</div>

The way Julia handles this stuff is just miles ahead of other languages thanks to first class structs etc. In R you would have to write all the rfoo,pfoo,dfoo functions even if they are trivially derived from something else. In Stan you have to write your own logpdf functions etc as well, the ability to just say stuff like convolve(A,B) is truly fabulous.

I should probably include distributions in my tutorial vignettes I’m working on.

---

<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:** [November 23, 2022, 5:56pm UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/31 "2022-11-23T17:56:59Z")

</div>

I think a good way to increase confidence in the correctness of our ecosystem is to implement more/better tests of systemically important packages such as Distributions.jl.

E.g.  
Popoviciu’s [inequality](https://en.wikipedia.org/wiki/Popoviciu%27s_inequality_on_variances): for any bounded univariate random variable X \in [m, M] we have \sigma^{2} \leq \frac{1}{4}(M-m)^2

Maybe some kind of loop over all uni distributions in the pkg, that checks if the RV is bounded & if various inequalities hold?

---

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [November 23, 2022, 7:56pm UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/32 "2022-11-23T19:56:24Z")

</div>

> [@Tamas\_Papp](#):
>
> O(1) IID sampling

What does this mean? That (for large `n`) `rand(dist, n)` and `rand(dist, 2n)` take the same amount of time?

---

<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:** [November 24, 2022, 9:09am UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/33 "2022-11-24T09:09:22Z")

</div>

Statistics! I often use `t-test` etc. to test deviations of a random variable from it’s known mean in unit testing, but it’s a bit difficult to get unit-testing working nicely with test which may fail, just not too often.  
Therefore I like your @Albert_Zevelev example with Popoviciu because it makes a deterministic test which has a bit of slack but on the other hand never should fail. Hoeffding inequality also comes to mind or other finite-sample properties.

---

<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:** [November 24, 2022, 9:42am UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/34 "2022-11-24T09:42:31Z")

</div>

No, of course not. O(1) is per IID sample.

The distinction is from situations where obtaining IID samples is practically impossible or very expensive, and you have to resort to MCMC. Practically, efficient IID sampling methods exist for all univariate distributions, but for multivariate distributions cheapo IID sampling is only possible in a few special cases.

---

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [November 25, 2022, 8:01pm UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/35 "2022-11-25T20:01:14Z")

</div>

I’m confused by the use of big-O notation here. In my understanding, big-O is used to express that the computation time depends on some parameter of the problem, most often its “size.” For example, if you were to generate a sample from the `binomial(p, n)` distribution by calling `sum(rand() < p for _ in 1:n)`, then this is an O(n) algorithm because its computation time grows linearly in n.

If your random variable is defined by a complicated stochastic process, for example X = f(Y, u) where Y is the RNG state and u is a vector of parameters, then it may take a long time to compute f, but this sampling time is typically constant in u, no?

I know it’s a little off topic, but if it’s not too much to ask, I’d appreciate an example.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [November 25, 2022, 10:08pm UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/36 "2022-11-25T22:08:26Z")

</div>

> [@Tamas\_Papp](#):
>
> Practically, efficient IID sampling methods exist for all univariate distributions, but for multivariate distributions cheapo IID sampling is only possible in a few special cases.

Many of these special cases corresponds to copula models, for which standard sampling tools (and more!) are now available in [`Copulas.jl`](https://www.github.com/lrnv/Copulas.jl). _Disclaimer : I’m the main author of the package_ 🙂

---

<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:** [November 27, 2022, 8:49am UTC](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762/37 "2022-11-27T08:49:29Z")

</div>

> [@maxkapur](#):
>
> I’m confused by the use of big-O notation here

It is not meant literally, just ignore it and focus on the practical part (which is, again: for _some_ distributions, you can draw IID samples cheaply, but generally you cannot).

[Previous page](https://discourse.julialang.org/t/random-variables-in-julia-working-list/48762.md?page=1)
