# Loglikelihood vs logpdf

**URL:** <https://discourse.julialang.org/t/loglikelihood-vs-logpdf/102966>\
**Category:** Statistics\
**Created:** [August 18, 2023, 9:35pm UTC](https://discourse.julialang.org/t/loglikelihood-vs-logpdf/102966 "2023-08-18T21:35:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jar1](https://avatars.discourse-cdn.com/v4/letter/j/c0e974/32.png) [@jar1](https://discourse.julialang.org/u/jar1)\
**Post date:** [August 18, 2023, 9:35pm UTC](https://discourse.julialang.org/t/loglikelihood-vs-logpdf/102966/1 "2023-08-18T21:35:29Z")

</div>

What is the relationship between `StatsAPI.loglikelihood(d, x)` and `Distributions.logpdf(d, x)` where `d::Distribution` and `x` is a draw from it?

In the examples I’ve tried, they give the same results.

```julia
let d = Normal(0,1)
    x = .3
    logpdf(d, x) == loglikelihood(d,x) # true
end

```

Are they the same? When should I use each? For instance, should I call `loglikelihood(d,x)` inside of my own composite log-likelihood functions, and use `logpdf(d,x)` inside of prior-probability functions?

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [August 18, 2023, 10:44pm UTC](https://discourse.julialang.org/t/loglikelihood-vs-logpdf/102966/2 "2023-08-18T22:44:20Z")

</div>

The main difference is that `loglikelihood` computes the sum of the log likelihoods and `logpdf` computes the log likelihood of a single observation. Your example is a special case in which the outputs are the same.

```julia
using Distributions 

dist = Normal(0, 1)
x = rand(dist, 10)

```

`logpdf.(dist, x)`

**output**

```julia
10-element Vector{Float64}:
 -1.1116329495246389
 -0.9190086195213015
 -0.9432279155524945
 -1.0348876720528215
 -1.0466074563159673
 -1.7416591504427266
 -1.294146605795191
 -1.2890879707921559
 -2.0101113445525365
 -0.9189482910005295

```

`loglikelihood(dist, x)`  
**output**  
`-12.309317975550362`

The distinction would be clearer if the name `sumloglikelihood` was used instead.

---

<div class="post-metadata">

**Author:** ![mcreel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mcreel/32/30088_2.png) [@mcreel](https://discourse.julialang.org/u/mcreel)\
**Post date:** [August 19, 2023, 5:43am UTC](https://discourse.julialang.org/t/loglikelihood-vs-logpdf/102966/3 "2023-08-19T05:43:07Z")

</div>

If you call them the same way, broadcasting both, they give the same results.

```julia
julia> logpdf.(dist, x)
10-element Vector{Float64}:
 -1.0203926929373632
 -0.9234209417660356
 -0.9344046575034405
 -1.0199860243797683
 -3.1309569251593743
 -1.0147293928603724
 -1.1854557641508374
 -1.5117326368202066
 -1.2205551629592766
 -1.2778617723196464

julia> loglikelihood.(dist, x)
10-element Vector{Float64}:
 -1.0203926929373632
 -0.9234209417660356
 -0.9344046575034405
 -1.0199860243797683
 -3.1309569251593743
 -1.0147293928603724
 -1.1854557641508374
 -1.5117326368202066
 -1.2205551629592766
 -1.2778617723196464

```

In statistics, when estimating parameters, the term likelihood is used to speak of the distribution of the data, but evaluated at some point in the parameter space, which is the set of parameter values under consideration. The actual distribution is simply the likelihood, but evaluated at the point in the parameter space that corresponds to the true, often unknown, parameter vector. So, the distinction is one of the name only. When doing statistics, it is called the likelihood. When doing probability theory, it is called the pdf.
