# Maxwell Fit, Julia

**URL:** https://discourse.julialang.org/t/maxwell-fit-julia/28835
**Category:** General Usage
**Tags:** question, package, plotting, statistics
**Created:** [September 17, 2019, 1:09am UTC](https://discourse.julialang.org/t/maxwell-fit-julia/28835 "2019-09-17T01:09:22Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Romero\_Azzalini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romero_azzalini/32/10325_2.png) [@Romero\_Azzalini](https://discourse.julialang.org/u/Romero_Azzalini)
#### Post date: [September 17, 2019, 1:09am UTC](https://discourse.julialang.org/t/maxwell-fit-julia/28835/1 "2019-09-17T01:09:22Z")

</div>

Hi,  
I would love to make a Maxwell Fit to my data in Julia, unfortunately I’m lost here… I’ve seen a [fit](https://stackoverflow.com/questions/18988855/fitting-maxwell-boltzman-distribution-in-python) in Python with scipy, but I’ve no idea how to use PyCall…

Best,  
Hannes

---

<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: [September 17, 2019, 3:19am UTC](https://discourse.julialang.org/t/maxwell-fit-julia/28835/2 "2019-09-17T03:19:06Z")

</div>

a Maxwell distribution is a scaled `Chi(3)`. Simplest thing you can do is moment-matching:

```julia
julia> d = LocationScale(0.0,1.4,Chi(3))
LocationScale{Float64,Chi{Float64}}(
μ: 0.0
σ: 1.4
ρ: Chi{Float64}(ν=3.0)
)

julia> data = rand(d,20);

julia> mean(data)/mean(Chi(3))
1.2660333885151915

```

Otherwise, you could do something like write the logpdf,

```julia
f(s) = sum(logpdf.(LocationScale(0.0,s,Chi(3)), data))

```

And then use something like [Optim.jl](https://github.com/JuliaNLSolvers/Optim.jl) to find the top

---

<div class="post-metadata">

### Author: ![Romero\_Azzalini](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/romero_azzalini/32/10325_2.png) [@Romero\_Azzalini](https://discourse.julialang.org/u/Romero_Azzalini)
#### Post date: [September 17, 2019, 5:17am UTC](https://discourse.julialang.org/t/maxwell-fit-julia/28835/3 "2019-09-17T05:17:10Z")

</div>

Hi, thanks for the answer! After thinking a bit I am going for a Lsq fit and type the function in by hand + a hist plot. I think that’s the easiest and best solution here.

---

<div class="post-metadata">

### Author: ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)
#### Post date: [September 17, 2019, 1:45pm UTC](https://discourse.julialang.org/t/maxwell-fit-julia/28835/4 "2019-09-17T13:45:17Z")

</div>

Hi Romero, @cscherrer had some great suggestions for you. Following on his suggestions, you might want to take a look at the relevant section in Distribution.jl’s docs for distribution-fitting: [Distribution Fitting · Distributions.jl](https://juliastats.github.io/Distributions.jl/stable/fit/).

---

<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: [September 17, 2019, 2:41pm UTC](https://discourse.julialang.org/t/maxwell-fit-julia/28835/5 "2019-09-17T14:41:36Z")

</div>

This would be nice, but I don’t think it works for this case. His distribution is

```julia
Maxwell(s) = LocationScale(0.0, s, Chi(3))

```

`LocationScale` is not supported by the API. And even if it were, there’s no way to freeze the location to 0.0.

---

<div class="post-metadata">

### Author: ![ianfiske](https://avatars.discourse-cdn.com/v4/letter/i/58f4c7/32.png) [@ianfiske](https://discourse.julialang.org/u/ianfiske)
#### Post date: [September 18, 2019, 4:00pm UTC](https://discourse.julialang.org/t/maxwell-fit-julia/28835/6 "2019-09-18T16:00:02Z")

</div>

Ah, good point. I missed that.

Just for completeness, so that the OP can see how straightforward it would be to use Optim to find the MLE here:

```julia
using Distributions, Optim

d = LocationScale(0.0,1.4,Chi(3));
data = rand(d, 5000);

function make_likelihood(data)
    function likelihood(s)
        -sum(logpdf.(LocationScale(0.0, s, Chi(3)), data))
    end
end

optimize(make_likelihood(data), eps(), 100)

```
