Hi folks. Does anybody know how to parse/convert a String to a Distribution? For e

Hi folks. Does anybody know how to parse/convert a String to a Distribution? For example “Beta(2,5)” -> Beta{Float64}(α=2.0, β=5.0).
I am trying to store probability distribution values in JSON format and read them into Julia, but am unable to successfully parse/convert.

Note that the original poster on Slack cannot see your response here on Discourse. Consider transcribing the appropriate answer back to Slack, or pinging the poster here on Discourse so they can follow this thread.
(Original message :slack:) (More Info)

Terribly unsafe, but eval(Meta.parse("Beta(2,5)")). Safer, and far more work, you could split the string on punctuation, verify the substrings, and lookup the distribution based on the values. It depends on how much you trust the input. There’s some middle ground where you Meta.parse the string and validate the resultant expression before eval’ing it:

e=Meta.parse(s); if e.head==:call && e.args[1] in names(Distributions) && all(a->isa(a,Number),e.args[2:end]); eval(e); end
1 Like

Maybe use JSON3 and StructTypes. Define your own struct with a field distribution. That is what I did with FixedPointDecimals.
https://juliadata.github.io/StructTypes.jl/stable/#StructTypes.StringType