# Distributions fit\_mle of distribution type vs distributions with parameters

**URL:** <https://discourse.julialang.org/t/distributions-fit-mle-of-distribution-type-vs-distributions-with-parameters/75228>\
**Category:** General Usage\
**Tags:** fit, distributions, mle\
**Created:** [January 26, 2022, 1:05pm UTC](https://discourse.julialang.org/t/distributions-fit-mle-of-distribution-type-vs-distributions-with-parameters/75228 "2022-01-26T13:05:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [January 26, 2022, 1:05pm UTC](https://discourse.julialang.org/t/distributions-fit-mle-of-distribution-type-vs-distributions-with-parameters/75228/1 "2022-01-26T13:05:33Z")

</div>

I try the following, but it does not work.

```julia
using Distributions
p = 0.2
x = rand(Bernoulli(0.2),50)
fit_mle(Bernoulli(p), x)
 MethodError: no method matching fit_mle(::Bernoulli{Float64}, ::Vector{Bool})

```

I know that

```julia
fit_mle(Bernoulli, x)

```

works.  
Adding the following seems to do what I want

```julia
function fit_mle(g::Distribution{Univariate,Discrete}, args...)
    fit_mle(typeof(g), args...)
end

function fit_mle(g::Distribution{Univariate,Continuous}, args...)
    fit_mle(typeof(g), args...)
end

```

However, I don’t understand why `fit_mle(Bernoulli(p), x)` is not authorized (or coded this way)?  
Sure, the parameter `p` does not matter for the usual function where the MLE is explicit.  
In my applications, I write extensions of `fit_mle` to other distributions where the actual parameter `p` is useful. It can be an initialization for an optimization algorithm.  
Or when the distributions are Products or Mixture, the type is not enough and the whole distribution structure is needed.

---

<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:** [January 26, 2022, 1:31pm UTC](https://discourse.julialang.org/t/distributions-fit-mle-of-distribution-type-vs-distributions-with-parameters/75228/2 "2022-01-26T13:31:03Z")

</div>

I’d be more surprised if that did work - the point of `fit_mle(Bernoulli, x)` is to find `p`.

This is the same with any type of optimization scheme I know. Say in Optim if I want to find the `x` that minimizes a function, I would do:

```julia
rosenbrock(x) = (1.0 - x[1])^2 + 100.0 * (x[2] - x[1]^2)^2
result = optimize(rosenbrock, zeros(2), BFGS())

```

and not `optimize(rosenbrock([1.5, 2.5]), zeros(2), BFGS())`.

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [January 26, 2022, 2:32pm UTC](https://discourse.julialang.org/t/distributions-fit-mle-of-distribution-type-vs-distributions-with-parameters/75228/3 "2022-01-26T14:32:22Z")

</div>

I get your point, the first `p = p_ini` is just an initial point here not what you look for.  
In optimization, it would not work because `rosenbrock([1.5, 2.5])` is a scalar and not a function.  
For distribution fitting `Bernoulli(p)` carries the same info as just the type `Bernoulli`.  
In fact, it carries more info because of the `p_ini`, in some situation it might be useless I agree (like for `Benoulli`).  
Imagine a `Product` distribution of an Exponential and Normal distribution.  
If one were to write a `fit_mle(::Type{<:Product}, x)` how do you extract the informations over the component of the `Product` if you just accept the type ? Whereas `fit_mle(g::Product, x)` has everything you need.  
Same thing for a Mixture distribution.

I am still uncomfortable with the types, so there might be a way to do that I do not understand.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [March 23, 2023, 4:56pm UTC](https://discourse.julialang.org/t/distributions-fit-mle-of-distribution-type-vs-distributions-with-parameters/75228/4 "2023-03-23T16:56:33Z")

</div>

Actually product types are parametrized:

```julia
julia> typeof(product_distribution(Gamma(),Normal()))
Distributions.ProductDistribution{1, 0, Tuple{Gamma{Float64}, Normal{Float64}}, Continuous, Float64}

julia> 

```

I agree with you for mixtures though.

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [April 3, 2023, 11:17am UTC](https://discourse.julialang.org/t/distributions-fit-mle-of-distribution-type-vs-distributions-with-parameters/75228/5 "2023-04-03T11:17:29Z")

</div>

Thanks for the heads-up, I did not notice it got updated with [`v0.25.65`](https://github.com/JuliaStats/Distributions.jl/releases/tag/v0.25.65) on July 22.

However, note that

```julia
# v0.25.64 and bellow
julia> typeof(product_distribution([Gamma(),Normal()]...))
ERROR: MethodError: no method matching product_distribution(::Gamma{Float64}, ::Normal{Float64})

julia> typeof(product_distribution([Gamma(),Normal()]))
Product{Continuous, Distribution{Univariate, Continuous}, Vector{Distribution{Univariate, Continuous}}}

```

```julia
# v0.25.65 and above
julia> typeof(product_distribution([Gamma(),Normal()]...))
Distributions.ProductDistribution{1, 0, Tuple{Gamma{Float64}, Normal{Float64}}, Continuous, Float64}

julia> typeof(product_distribution([Gamma(),Normal()]))
Product{Continuous, Distribution{Univariate, Continuous}, Vector{Distribution{Univariate, Continuous}}}
# -> still use the old (depreacated) Product interface

```

Indeed, having a similar update for `MixtureModels` would answer my concern and unify the interface.

---

<div class="post-metadata">

**Author:** ![lrnv](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lrnv/32/19373_2.png) [@lrnv](https://discourse.julialang.org/u/lrnv)\
**Post date:** [April 3, 2023, 12:36pm UTC](https://discourse.julialang.org/t/distributions-fit-mle-of-distribution-type-vs-distributions-with-parameters/75228/6 "2023-04-03T12:36:19Z")

</div>

```julia
# v0.25.65 and above
julia> typeof(product_distribution([Gamma(),Normal()]...))
Distributions.ProductDistribution{1, 0, Tuple{Gamma{Float64}, Normal{Float64}}, Continuous, Float64}

julia> typeof(product_distribution([Gamma(),Normal()]))
Product{Continuous, Distribution{Univariate, Continuous}, Vector{Distribution{Univariate, Continuous}}}
# -> still use the old (depreacated) Product interface

```

This looks like a bug and should be reported.

---

<div class="post-metadata">

**Author:** ![dmetivie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dmetivie/32/6926_2.png) [@dmetivie](https://discourse.julialang.org/u/dmetivie)\
**Post date:** [April 4, 2023, 8:53am UTC](https://discourse.julialang.org/t/distributions-fit-mle-of-distribution-type-vs-distributions-with-parameters/75228/7 "2023-04-04T08:53:24Z")

</div>

I opened an [issue](https://github.com/JuliaStats/Distributions.jl/issues/1706).
