JuliaMath / JuliaMath/MeasureTheory.jl

Design of `rand`

Open
#51 6 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
401
Forks
31
PR merge metrics
No merged PRs in 30d

Description

Last week I had some discussion in a PR to Distributions about the design of `rand` (https://github.com/JuliaStats/Distributions.jl/pull/1262#issuecomment-764995042), and I think it applies to MeasureTheory as well.

---

*TL;DR:* It is problematic to prescribe the type of samples of a distribution, both for users and internally. However, similar to `logdensity`, the type of the samples follows naturally if (1) the type of the basic samples from the RNG obtained with `rand`, `randn`, or `randexp`, and (2) the parameters of the distribution of interest are known. Thus I propose to not prescribe the type of the samples of the distribution but the type of the basic samples from the RNG with type `T` in `rand(::AbstractRNG, ::Type{T}, ::Distribution)`.

---

Partly copied from https://github.com/JuliaStats/Distributions.jl/pull/1262:

Basically every sampling procedure at some point starts sampling with `rand`, `randexp`, or `randn` in Random from a uniform distribution in `[0, 1)`, the exponential distribution with rate 1 on `R_{>0}`, or the standard normal distribution on `R`, respectively. The samples from the distribution of interest are obtained by transforming these most basic samples (ie. basically the distribution of interest is a push-forward of the distribution of these basic samples), and the type of the random variates follows automatically from the type of these basic samples and the function that transforms them and involves the parameters of the distribution.

My suggestion would be to allow to specify the type of exactly (and only) these base variates, i.e., to change such "basic calls" to `rand(rng, T)`, `randn(rng, T)` etc. where `T` can be specified by the user. The type of the random variates of the distribution of interest would then be determined both by `T`, the parameters of the distribution, and the transformations needed to turn the basic samples into a sample from the distribution.

More concretely, with a default of `T = Float64` one would obtain
```julia
julia> typeof(rand(Gamma(0.5f0))) # = typeof(rand(Float64, Gamma(0.5f0)))
Float64

julia> typeof(rand(Gamma(0.5))) # = typeof(rand(Float64, Gamma(0.5)))
Float64

julia> typeof(rand(Gamma(big(0.5)))) # = typeof(rand(Float64, Gamma(big(0.5))))
BigFloat

julia> typeof(rand(Float32, Gamma(0.5f0)))
Float32

julia> typeof(rand(Float32, Gamma(0.5)))
Float64

julia> typeof(rand(Float32, Gamma(big(0.5))))
BigFloat

julia> typeof(rand(BigFloat, Gamma(0.5f0))) # requires https://github.com/JuliaLang/julia/pull/35111
BigFloat

julia> typeof(rand(BigFloat, Gamma(0.5))) # requires https://github.com/JuliaLang/julia/pull/35111
BigFloat

julia> typeof(rand(BigFloat, Gamma(big(0.5)))) # requires https://github.com/JuliaLang/julia/pull/35111
BigFloat
```

An internal detail:
Probably one would want to promote `T` to the precision of the parameters to ensure that the precision of the underlying draws is sufficiently high, i.e., with parameters of `BigFloat` the samples should be based on draws of type `BigFloat` in all cases.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.