For example, a distribution that returns 1.0 with probability 0.3 and returns 1.1 with probability 0.7. Thank you.
Generating random numbers from a particular distribution using the Distributions package is a two-step process. First we create the distribution object and then we sample from it. The example below creates a normal distribution with a mean of 5 and a standard deviation of 3. Next, we draw 10 samples from it.
An arbitrary distribution, whose values and probabilities are user specified. A Gaussian (or Normal) distribution with a specified mean and standard deviation. A uniform (or flat) distribution, with events equally likely to occur anywhere within the interval from 0 to 1.
The most common discrete distributions used by statisticians or analysts include the binomial, Poisson, Bernoulli, and multinomial distributions. Others include the negative binomial, geometric, and hypergeometric distributions.
Maybe you do not need a full blown distribution type but just sampling from such a distribution is enough for you?
If this is the case, then the simplest way to do it is:
using StatsBase # corrected a typo here
values = [1.0, 1.1]
probabilities = [0.3, 0.7]
w = Weights(probabilities)
sample(values, w) # sampling
If you actually want to use a distribution the closest thing you can get now is:
using Distributions
values = [1.0, 1.1]
probabilities = [0.3, 0.7]
d = Categorical(probabilities)
values[rand(d)] # sampling
but it will be a bit slower.
If you want to define your own distribution following Distributions package type system, the simplest way is to take this code https://github.com/JuliaStats/Distributions.jl/blob/master/src/univariate/discrete/categorical.jl and modify it according to your needs (but this would be a significant effort I would say).
There isn't yet a built-in way to do it, but you could look at https://github.com/JuliaStats/Distributions.jl/pull/634).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With