GiovineItalia / Gadfly.jl

Crafty statistical graphics for Julia.
http://gadflyjl.org/stable/
Other
1.9k stars 250 forks source link

Stat.density2d puts numbers bigger than 1 in z #1426

Closed NightMachinery closed 4 years ago

NightMachinery commented 4 years ago

I am trying to plot a 2d density graph, and I use this function to compute levels for Geom.density:

    levels = function (z)
        minz = minimum(z)
        maxz = maximum(z)
        stepz = min(0.1, ((maxz - minz) / 6))
        println("stepz: $stepz, minz: $minz, maxz: $maxz")
        return minz:stepz:maxz
    end

I get stepz: 0.1, minz: 0.0, maxz: 1.9412042100886135 after running the plot; Shouldn't z be between 0 and 1? It's supposed to be a density plot ...

Mattriks commented 4 years ago

In Geom.density2d, the contour levels are merely the heights of the density function. A density function can definitely have a height >1, see the mixture model example (third plot for Geom.density).

There is also Geom.ellipse, which gives the confidence ellipses for the MvNormal model.

NightMachinery commented 4 years ago

@Mattriks Hmm ... Can you point me on any docs on what the density function is? I thought it groups input by bins, and assigns each bin its fraction of the total.

Mattriks commented 4 years ago

Geom.density and Geom.density2d use the kde function in KernelDensity.jl. Here's another example:

N = Normal(0, 0.1) # try changing the sd

plot(x=rand(N, 100), Geom.density, color=["Estimated"],
    layer(x->pdf(N, x), -1, 1, color=["Parametric"])
)
Mattriks commented 4 years ago

Would adding to the doc strings for Geom.density and Geom.density2d, that those geoms use the kde function for density calculation, close this issue?

NightMachinery commented 4 years ago

@Mattriks yes, thank you.

NightMachinery commented 4 years ago

https://stats.stackexchange.com/questions/5819/kernel-density-estimate-takes-values-larger-than-1