arraysrandomjuliagamma-distribution

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


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.


Solution

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