# What does \`density()\` do when applied to a \`Distribution\` directly?

**URL:** https://discourse.julialang.org/t/what-does-density-do-when-applied-to-a-distribution-directly/105621
**Category:** Visualization
**Tags:** plotting, plots, distributions, statsplots
**Created:** [October 31, 2023, 4:24pm UTC](https://discourse.julialang.org/t/what-does-density-do-when-applied-to-a-distribution-directly/105621 "2023-10-31T16:24:50Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Alseidon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alseidon/32/207775_2.png) [@Alseidon](https://discourse.julialang.org/u/Alseidon)
#### Post date: [October 31, 2023, 4:24pm UTC](https://discourse.julialang.org/t/what-does-density-do-when-applied-to-a-distribution-directly/105621/1 "2023-10-31T16:24:50Z")

</div>

I just started experimenting with `Distributions.jl` and `StatsPlots.jl`, and made a basic mistake, writing `density(Normal())` instead of `density(rand(Normal(), 100_000))` or `plot(Normal())`. When executing

```julia
julia> plot(Normal(), label="plot()")
julia> density!(rand(Normal(), 100_000), label="density(rand())")
julia> density!(Normal(), label="density()")

```

Here is the result:

![image](https://global.discourse-cdn.com/julialang/original/3X/3/9/39b0b5e54d5912d4482fcf3b193779ed4c92debd.png)

What is plotted with `density` ?

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [October 31, 2023, 8:36pm UTC](https://discourse.julialang.org/t/what-does-density-do-when-applied-to-a-distribution-directly/105621/2 "2023-10-31T20:36:00Z")

</div>

It’s strange that `density!(Normal(), label="density()")` doesn’t throw an error, because `StatsPlots.density!` is a generic function with 1 method.

```julia
?density # or density!

```

displays that

```julia
density(x)
density!(x)

```

make a line plot of a kernel density estimate of x, where x is an `AbstractVector` of samples for probability density estimation.  
Or you passed a distribution, not a sample from that distribution.

---

<div class="post-metadata">

### Author: ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)
#### Post date: [October 31, 2023, 10:00pm UTC](https://discourse.julialang.org/t/what-does-density-do-when-applied-to-a-distribution-directly/105621/3 "2023-10-31T22:00:31Z")

</div>

This does seem strange. I’m having a difficult time finding exactly where `density` is defined in the source code…?

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [November 1, 2023, 12:40am UTC](https://discourse.julialang.org/t/what-does-density-do-when-applied-to-a-distribution-directly/105621/4 "2023-11-01T00:40:47Z")

</div>

This is a use case for `@which`:

```julia
julia> using Distributions, StatsPlots

julia> plot(Normal(), label="plot()")

julia> density!(rand(Normal(), 100_000), label="density(rand())")

julia> density!(Normal(), label="density()")

julia> @which density!(Normal(), label="density()")
kwcall(::NamedTuple, ::typeof(density!), args...)
     @ Plots ~/.julia/packages/RecipesBase/BRe07/src/RecipesBase.jl:429

```

That line of code is in this block:

```julia
macro shorthands(funcname::Symbol)
    funcname2 = Symbol(funcname, "!")
    quote
        export $funcname, $funcname2
        Core.@ __doc__ $funcname(args...; kw...) =
            $RecipesBase.plot(args...; kw..., seriestype = $(Meta.quot(funcname)))
        Core.@ __doc__ $funcname2(args...; kw...) =
            $RecipesBase.plot!(args...; kw..., seriestype = $(Meta.quot(funcname)))
    end |> esc
end

```

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [November 1, 2023, 9:36am UTC](https://discourse.julialang.org/t/what-does-density-do-when-applied-to-a-distribution-directly/105621/5 "2023-11-01T09:36:24Z")

</div>

It’s a very old issue:

> <https://github.com/JuliaPlots/StatsPlots.jl/issues/245>
>
> \`\`\`
> using StatsPlots, Distributions
> density(Normal())
> \`\`\`
> currently gives a …weird bimodal distribution
> 
> !\[normal\_error\](https://user-images.githubusercontent.com/7264491/60345293-997e5d00-99b0-11e9-8d19-ba28c3e28926.PNG)
> 
> This is on Julia v1.1.1
> 
> \`\`\`
> Status \`C:\\Users\\~\\.julia\\environments\\v1.1\\Project.toml\`
> \[c52e3926\] Atom v0.8.7
> \[6e4b80f9\] BenchmarkTools v0.4.2
> \[336ed68f\] CSV v0.5.6
> \[a93c6f00\] DataFrames v0.18.3
> \[31c24e10\] Distributions v0.19.2
> \[38e38edf\] GLM v1.2.0
> \[7073ff75\] IJulia v1.18.1
> \[e5e0dc1b\] Juno v0.7.0
> \[ff71e718\] MixedModels v1.1.6
> \[c030b06c\] ODE v2.4.0
> \[91a5bcdd\] Plots v0.25.1
> \[6f49c342\] RCall v0.13.3
> \[d519eb52\] RegressionTables v0.2.0
> \[2913bbd2\] StatsBase v0.30.0
> \[f3b207a7\] StatsPlots v0.11.0
> \`\`\`

---

<div class="post-metadata">

### Author: ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)
#### Post date: [November 1, 2023, 10:32am UTC](https://discourse.julialang.org/t/what-does-density-do-when-applied-to-a-distribution-directly/105621/6 "2023-11-01T10:32:04Z")

</div>

It looks like the macro is auto-writing code and this is a special case where it should do something different?

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [November 1, 2023, 10:18pm UTC](https://discourse.julialang.org/t/what-does-density-do-when-applied-to-a-distribution-directly/105621/7 "2023-11-01T22:18:12Z")

</div>

That is my understanding, although it is bit tricky for me to follow: StatsPlots has a lot of explicit calls to that `@shorthand` macro, but I didn’t see the one for `density`.
