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

Infer more defaults for univariate distributions #1489

Closed
rikhuijzer opened this issue Oct 25, 2020 · 3 comments
Closed

Infer more defaults for univariate distributions #1489

rikhuijzer opened this issue Oct 25, 2020 · 3 comments

Comments

@rikhuijzer
Copy link
Contributor

rikhuijzer commented Oct 25, 2020

I would like to quickly visualise univariate distributions. However, currently it requires quite a lot of code to do so. I have found two possible roads to plot this distribution.

One way is via Stat.func

using Distributions
using Gadfly

n = Normal(10, 1)
plot(xmin = [quantile(n, 0.01)], xmax = [quantile(n, 0.99)], y = [x -> pdf(n, x)], 
    Stat.func, Geom.line, Guide.xlabel("x"), Guide.ylabel("density")
)

image
which is quite nice; it also correctly handles multiple distributions when passing multiple values to xmin and xmax.

image

Another way could be via Stat.density. This would seem the most obvious candidate for me, but it appears to calculate the density of the density.

Attempt to create a pull request

I have tried to create a pull request for this feature. Unfortunately, I am now a little bit stuck on the solution. I have tried to catch the "poorly" defined distributions after

statistics = collect(statistics)

in src/Gadfly.jl and then quickly ensure that Stat.apply_statistic(statistics, scales, coord, plot_aes) jumps to apply_statistic(stat::FunctionStatistic, ...) with

if eltype(plot_aes.y) <: UnivariateDistribution && plot_aes.x == [1]
    statistics = [Stat.func() for y in plot_aes.y]
end

my plan was to prepare the plot for the apply_statistic method in this addition, but I get

MethodError: no method matching apply_statistics(::Gadfly.Stat.FunctionStatistic, ::Dict{Symbol,Gadfly.ScaleElement}, ::Gadfly.Coord.Cartesian, ::Gadfly.Aesthetics)

I have checked all the types and they match even though Julia report that they don't.

@bjarthur
Copy link
Member

would be easiest for us to help debug if you put your code into a pull request so we could see it

@Mattriks
Copy link
Member

Some features of Gadfly which support Distributions include Stat.qq and Stat.ellipse (used by Geom.ellipse). Those Stats show the general way that support for Distributions can be included in Gadfly. To plot UnivariateDistributions, you could make a Stat.distribution, which outputs to Geom.ribbon (effectively automating the first plot shown in Stat.density). When proposing a new PR, it's good to give a design outline!

Note that Gadfly also some special signatures for plots (and layers), so this is possible:

d1, d2 = Normal.([10, 0],1)
p = plot(layer([x->pdf(d1, x), x->pdf(d2, x)], -3, 13))

@rikhuijzer
Copy link
Contributor Author

I couldn’t figure this one out in a few hours and have some other priorities first. I hope to come up with a pull request in a few weeks. Thanks for the comments 🙂

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

No branches or pull requests

3 participants