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: Distribution Fitting · Distributions.jl.

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

Hello,

I am dealing with a very similar issue to that discussed previously in this thread. Rather than fitting to one, I would like to generate a Maxwell-Boltzmann distribution so that I can sample from it.

I am familiar with the relationship between the Maxwell-Boltzmann and Chi distributions, but it’s unclear to me how to implement the scaling. I’m hoping that the solution will be as simple as:

d = Chi(3)
rand(s*d)
# where s is some scaling factor, which is a function of mass and temperature

I’m sure someone who actually understands statistics will tell me why that isn’t correct. @cscherr described the use of LocationScale, but I don’t fully understand how that method works, and the documentation is somewhat sparse.

Any help would be appreciated,
Thanks