Maxwell-Boltzmann Distribution using Distributions.jl

Hello,

Distributions.jl does not have an implementation of the Maxwell-Boltzmann distribution, but it does have the Chi distribution, which is a more general form of the Maxwell-Boltzmann case. I’m hoping to simply apply a transformation to the Chi distribution, but that is not working as I had hoped.

"""
Returns a Maxwell-Boltzmann distribution of velocity (m/s)
for particles of given mass (Kg) at a given temperature (K)
"""
function maxwell(mass::AbstractFloat, temp=300::AbstractFloat)
    kb = 1.380649E-23 # J/K
    s = sqrt(kb * temp / mass)
    return s * Distributions.Chi(3)
end

This function does not seem to work. Compare what happens when I use my function vs scaling manually in the terminal:

julia> d1 = maxwell(4e-26)
Chi{Float64}(ν=3.0)

julia> d2 = maxwell(5e-26)
Chi{Float64}(ν=3.0)

julia> d3 = 3 * Distributions.Chi(3)
LocationScale{Int64, Continuous, Chi{Float64}}(
μ: 0
σ: 3
ρ: Chi{Float64}(ν=3.0)
)


julia> d4 = 4 * Distributions.Chi(3)
LocationScale{Int64, Continuous, Chi{Float64}}(
μ: 0
σ: 4
ρ: Chi{Float64}(ν=3.0)
)

If I plot the pdfs of d1 and d2, the curves are superimposed - they are identical. But if I plot the pdfs of d3 and d4, they are shifted as expected. I don’t understand where the discrepancy arises.

Any help with this issue would be appreciated,
Thanks

Note: A previous post discussed the relationship between Maxwell-Boltzmann and Chi distributions, and the use of LocationScale.

You have some additional method definitions of maxwell hanging around in addition to what you’ve posted. In fact, what you’ve posted simply doesn’t work (because 300::AbstractFloat is an error):

julia> function maxwell(mass::AbstractFloat, temp=300::AbstractFloat)
           kb = 1.380649E-23 # J/K
           s = sqrt(kb * temp / mass)
           return s * Distributions.Chi(3)
       end
maxwell (generic function with 2 methods)

julia> d1 = maxwell(4e-26)
ERROR: TypeError: in typeassert, expected AbstractFloat, got a value of type Int64

Remove the unnecessary annotations (and restart Julia or use Revise.jl to remove the old method definitions) and you get differently-scaled Chi distributions like you expect:

julia> function maxwell(mass, temp=300)
           kb = 1.380649E-23 # J/K
           s = sqrt(kb * temp / mass)
           return s * Distributions.Chi(3)
       end
maxwell (generic function with 2 methods)

julia> d1 = maxwell(4e-26)
LocationScale{Float64, Continuous, Chi{Float64}}(
μ: 0.0
σ: 321.7897994032751
ρ: Chi{Float64}(ν=3.0)
)


julia> d2 = maxwell(5e-26)
LocationScale{Float64, Continuous, Chi{Float64}}(
μ: 0.0
σ: 287.81754637269773
ρ: Chi{Float64}(ν=3.0)
)
7 Likes