How to sample array from gamma distribution with shape values given by different array in Julia?

269 Views Asked by At

In Julia, I have an array of shape values and I would like to sample an array who's values are gamma distributed according to the corresponding shape element of my shape array. What I want is:

    shapes = [1.1, 0.5, 10]  
    scale = 1  
    x = SampleGammaWithDifferentShapes(shapes,scale)

where x[1] is sampled from a gamma distribution with shape=shapes[1], and x[2] is sampled from a gamma distribution with shape=shape[2], and so on.

Is there a built in function that allows you to do this in one line or will I have to define my own function for this? This seems like it should be a built in function.

2

There are 2 best solutions below

2
On BEST ANSWER

The possibility to just broadcast any function over arrays makes it unnecessary to add special array-versions of functions. Can you do it for 1 value? Then just broadcast.

using Distributions

shapes = [1.1, 0.5, 10.]
scale = 1
x = rand.(Gamma.(shapes, scale))
0
On

The map function is useful for this.

using Random, Distributions

shapes = [1.1, 0.5, 10]  
scale = 1
n_samples = 1
x = map(x -> rand(Gamma(x,scale), n_samples)[1], shapes)