# Using MeasureTheory.jl with Distributions.jl

**URL:** https://discourse.julialang.org/t/using-measuretheory-jl-with-distributions-jl/60737
**Category:** Statistics
**Created:** [May 7, 2021, 6:06pm UTC](https://discourse.julialang.org/t/using-measuretheory-jl-with-distributions-jl/60737 "2021-05-07T18:06:05Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![cscherrer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cscherrer/32/7631_2.png) [@cscherrer](https://discourse.julialang.org/u/cscherrer)
#### Post date: [May 7, 2021, 6:06pm UTC](https://discourse.julialang.org/t/using-measuretheory-jl-with-distributions-jl/60737/1 "2021-05-07T18:06:05Z")

</div>

I’ve heard there may be some perception that to use MeasureTheory, you need to entirely drop Distributions.jl. That’s not true!

# Calling Distributions from MeasureTheory

MeasureTheory actually has Distributions as a dependency, and exports it as `Dists`. So this works just fine:

```julia
julia> using MeasureTheory

julia> m = Normal(2,3)
Normal(μ = 2, σ = 3)

julia> d = Dists.Normal(2,3)
Distributions.Normal{Float64}(μ=2.0, σ=3.0)

```

We generally prefer `logdensity` over `logpdf`, because probability densities are a very special case. And we generally leave off the normalization constant. Well, “leave off” isn’t quite right, it’s really that we change the base measure to make the computation more efficient.

Currently, `logdensity` of a `Distribution` just calls `logpdf`:

```julia
julia> logdensity(d, 0)
-2.2397730440950046

julia> Dists.logpdf(d, 0)
-2.2397730440950046

```

and vice versa:

```julia
julia> logdensity(m, 0)
-1.3208345108903319

julia> Dists.logpdf(m, 0)
-1.3208345108903319

```

This last one is really not correct, we should instead do

```julia
julia> logdensity(m, Lebesgue(ℝ), 0)
-2.2397730440950046

```

I mean, it’s correct as a log-density, but not as a logpdf a user might expect to be compatible with Distributions.jl.

Because the density of `m` with respect to `Lebesgue(ℝ)` integrates to one. But there’s not always a base measure that makes this true. And even if we do know this base measure, bringing the constant back in will always slow down the computations. Anyway, please let me know if you have ideas for a better way for the interface between these to be set up.

# Calling MeasureTheory from Distributions

Of course we can also go the other way:

```julia
julia> using Distributions

julia> import MeasureTheory

julia> m = MeasureTheory.Normal(2,3)
Normal(μ = 2, σ = 3)

julia> d = Normal(2,3)
Normal{Float64}(μ=2.0, σ=3.0)

julia> MeasureTheory.logdensity(m, 0)
-1.3208345108903319

julia> logpdf(m, 0)
-1.3208345108903319

julia> MeasureTheory.logdensity(d, 0)
-2.2397730440950046

julia> logpdf(d, 0)
-2.2397730440950046

julia> MeasureTheory.logdensity(m, MeasureTheory.Lebesgue(MeasureTheory.ℝ), 0)
-2.2397730440950046

```
