Hi guys,
I want to create a (mutable) struct that lets me call functions from the Distributions
module in a very generic way, an example container for this looks like this:
using Distributions, Parameters
import DataStructures: OrderedDict
##################################################################
mutable struct ParameterInfo{E,F}
distr :: Distribution{E,F}
param :: #?? Question: Best type for this field?
end
#Would like to do this
function get_logpdf(info::ParameterInfo, x)
@unpack distr, param = info
return logpdf( distr(param ...), x )
end
- I need to update the
param
field over time. i) So I assume there is no way to make NamedTuples work with it? - Ideally,
distr(param ...)
ordistr(; param ...)
recognizes the keywords, i.e., I will get the same distribution no matter the ordering in param:
Normal(μ=1., σ=2.) or Normal(; μ=1., σ=2.)
Normal(σ=2., μ=1.) or Normal(; σ=2., μ=1.)
ii) Unfortunately, it seems like keywords are not supported in the Distributions
module? I read some articles about progress wrt to this questions, but I could not make it work with my examples.
In that case, I would need something that preservers the order of the input, even though it seems a lot safer to me if I could work with keywords. OrderedDicts let me change param
fields and preserve ordering, so a possible solution would be:
###################### Use a OrderedDict
param3 = OrderedDict(:μ => 12.3, :σ => 1., :α => 2.) #Does NOT order
param3[:μ] = 123456 #and is mutable
#But I cannot call Distributions easily with it
param3 = Dict(:μ => 12.3, :σ => 1. )
Normal(param3 ...) #not working
Normal(; param3 ...) #not working
Normal( collect(values(param3) ) ... ) #working
#But does not take into account keywords
param3 = OrderedDict( :σ => 1., :μ => 12.3 ) #Switch ordering
Normal( collect(values(param3) ) ... ) #Normal{Float64}(μ=1.0, σ=12.3) -> wrong
iii) However, ordering still seems to not work in that case. Does someone know a way to work with keywords in that case, and the container of these is mutable and (ideally) preserves ordering?
It would be amazing if anyone knows a workaround for this!
Best regards,