Where is the `pdf` function in Distribution.jl implemented?

Hello Julia community,

julia> pdf(Normal(), 0.3)
0.38138781546052414

I want to know how the pdf function is implemented, but the source code for Distribution.jl tells me nothing (in fact it looks like an infinite loop).

Am I missing something? Wouldn’t there at least have to have a 2\pi somewhere?

julia-1.1> d = Normal()
julia-1.1> @less pdf(d, 0.3) # shows this is implemented via a macro, so try something else
julia-1.1> @code_lowered pdf(d,0.3)
CodeInfo(
1 ─ %1 = (Base.getproperty)(d, :μ)
│   %2 = (Base.getproperty)(d, :σ)
│   %3 = (Distributions.normpdf)(%1, %2, x)
└──      return %3
)

julia-1.1> @less Distributions.normpdf(0.0,1.0,0.3) # this is what you want

Page up to see your beloved constant.

2 Likes

In case it is not clear: pdf and other relevant functions are implemented in StatsFuns.jl.

2 Likes

Technically, functions are not implemented, methods are. What you linked are some generic fallback methods for dealing with some general cases (and the first one doesn’t look like a function definition at all, maybe a bug?).

You can also use

@edit pdf(Normal(), 0.3)

to take you to the source code (if your editor is set up properly).

In this case it takes you to a macro defining functions using primitives from StatsFuns, which I agree is rather opaque.

How can we calculate PDF at each point (randomly generated) of a vector? I am trying to do this using the snippet below, but it is giving me PDF values > 1 which cannot be true for Normal distribution with combination of parameter

x = rand(Normal(3, 0.05), 10^3)
y = pdf.(Normal(3, 0.05), x)

There must be something wrong I am doing, please help me to get this.

It can be true :slight_smile:

This is what the pdf of Normal(3, 0.05) looks like

2 Likes

Oh, sorry, my bad, the property of density function is that (1) it must be greater than zero, (2) integrating it under the support (a.k.a total area under the curve) must be 0

Thanks a lot for correcting me. But what about evaluating density over the vector? Am I doing it correctly?