Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to keyword constructors #768

Open
simonbyrne opened this issue Sep 7, 2018 · 14 comments · May be fixed by #823
Open

Switch to keyword constructors #768

simonbyrne opened this issue Sep 7, 2018 · 14 comments · May be fixed by #823
Milestone

Comments

@simonbyrne
Copy link
Member

Now that we have fast keywords (well, we will once JuliaLang/julia#29083 is fixed), I propose that we switch the constructors to use keywords. This has several advantages:

  1. It would match what we currently print.
  2. It makes clear what parameterisation is being used, e.g. Normal(μ=1.0, σ=2.0) makes clear that it is a standard deviation, not a variance.
  3. With a bit of trickery, we can dispatch on different keywords to allow different constructors, e.g. we could support all the following:
Normal(μ=1.0, σ=2.0) # the default
Normal(mu=1.0, sigma=2.0) # ASCII fallback
Normal(mean=1.0, std=2.0) # moment mathing
Normal(mean=1.0, var=4.0) # alternative parameterisations
  1. We could use the same thing for constrained fitting. e.g. fit(Normal, x, σ=2.0) could fit the MLE with σ constrained.
@simonbyrne
Copy link
Member Author

(I suggested something like this in #584).

@matbesancon
Copy link
Member

Looks nice, what do you mean by trickery for the dispatch?

@simonbyrne
Copy link
Member Author

I've actually now put together a package for the trickery part:
https://github.com/simonbyrne/KeywordDispatch.jl

@matbesancon
Copy link
Member

matbesancon commented Sep 19, 2018 via email

@kleinschmidt
Copy link
Member

Could do the R trick and just accept all the keywords and error/warn and use one if both std and var are specified

@matbesancon
Copy link
Member

matbesancon commented Sep 19, 2018 via email

@simonbyrne
Copy link
Member Author

Distributions already uses a lot of macros: we could put them in Distributions itself (which was my original plan), but it seemed easier to make it a separate package. I'm happy to make the errors more useful.

re: allowing all the keywords: I actually tied this a while ago, but I ended up with a lot of boilerplate if blocks, and the complexity increases exponentially as you add more arguments.

@kleinschmidt
Copy link
Member

you could use coalesce or something to simplify a lot of it though right?

@matbesancon
Copy link
Member

matbesancon commented Sep 19, 2018 via email

@simonbyrne
Copy link
Member Author

I've created a demonstration on the sb/keyword branch. An interesting example is the Gamma constructor:

@kwdispatch Gamma()
@kwmethod Gamma(;α,θ) = Gamma(α,θ)
@kwmethod Gamma(;alpha,theta) = Gamma(alpha,theta)
@kwmethod Gamma(;shape,scale) = Gamma(shape,scale)
@kwmethod Gamma(;α,β) = Gamma(α,1/β)
@kwmethod Gamma(;alpha,beta) = Gamma(alpha,1/beta)
@kwmethod Gamma(;shape,rate) = Gamma(shape,1/rate)
@kwmethod function Gamma(;mean, var)
θ=var/mean
Gamma(mean/θ, θ)
end
@kwmethod Gamma(;mean, std) = Gamma(;mean=mean,var=std^2)

@matbesancon
Copy link
Member

interesting just checked it. A good point is that we can use it only for some distributions and it's not a breaking change

@simonbyrne
Copy link
Member Author

I would like to take the opportunity to deprecate some of the more confusing "default value" ones, e.g. Normal(x).

@simonbyrne simonbyrne linked a pull request Jan 28, 2019 that will close this issue
@taqtiqa-mark
Copy link

I think JuliaLang/julia#9498 may be relevant, if not internally, then for users. Not sure if it is ablocking issue or just one to be aware of. HTH?

@matbesancon matbesancon added this to the 1.0 milestone Jun 5, 2019
@ghost
Copy link

ghost commented Apr 24, 2021

Now that we have fast keywords (well, we will once JuliaLang/julia#29083 is fixed), I propose that we switch the constructors to use keywords.

Are there still any plans to add this to Distributions.jl? I've definitely felt like this could be very useful before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants