Take action based on Distribution type in a type stable way?


#1

I want to make a function that transforms some information given what kind of Distributions type a variable has. Something like:

tdist = typeof(prob.dist)
if tdist == Distributions.Gamma{Float64}
    par_transform[i] = exp(par)
elseif tdist == Distributions.Beta{Float64}
    par_transform[i] = logistic(par)
end

where prob.par is a float, and prob.dist is a Distributions type. The above works, but clearly is not type stable as tdist changes as I loop over an array of different distributions types. Is there a proper idiom for doing this kind of transformation? Or is this way the natural way?


#2

define an auxilliary function:

transform(prob::Gamma, par) = exp(par)
transform(prob::Beta, par) = logistic(par)

then in your code

par_transform[i] = transform(prob, par)

#3

Thank you so much … man my brain always forgets to actually use the key feature of Julia :wink: