# Maxwell Fit, Julia

Hi,
I would love to make a Maxwell Fit to my data in Julia, unfortunately I’m lost here… I’ve seen a fit in Python with scipy, but I’ve no idea how to use PyCall…

Best,
Hannes

2 Likes

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

``````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,

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

And then use something like Optim.jl to find the top

3 Likes

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.

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: https://juliastats.github.io/Distributions.jl/stable/fit/.

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

``````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.

1 Like

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:

``````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)
``````
2 Likes