Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I write an arbitrary discrete distribution in Julia?

Tags:

julia

For example, a distribution that returns 1.0 with probability 0.3 and returns 1.1 with probability 0.7. Thank you.

like image 583
Ben S. Avatar asked Jan 11 '18 17:01

Ben S.


People also ask

How do you sample from a distribution in Julia?

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.

What is an arbitrary distribution?

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.

Which distribution is suitable for discrete variable?

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.


2 Answers

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).

like image 74
Bogumił Kamiński Avatar answered Sep 28 '22 01:09

Bogumił Kamiński


There isn't yet a built-in way to do it, but you could look at https://github.com/JuliaStats/Distributions.jl/pull/634).

like image 39
Simon Byrne Avatar answered Sep 28 '22 00:09

Simon Byrne