# Maxwell-Boltzmann Distribution using Distributions.jl

**URL:** <https://discourse.julialang.org/t/maxwell-boltzmann-distribution-using-distributions-jl/131210>\
**Category:** Statistics\
**Created:** [July 30, 2025, 4:27pm UTC](https://discourse.julialang.org/t/maxwell-boltzmann-distribution-using-distributions-jl/131210 "2025-07-30T16:27:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![prconlin](https://avatars.discourse-cdn.com/v4/letter/p/b19c9b/32.png) [@prconlin](https://discourse.julialang.org/u/prconlin)\
**Post date:** [July 30, 2025, 4:27pm UTC](https://discourse.julialang.org/t/maxwell-boltzmann-distribution-using-distributions-jl/131210/1 "2025-07-30T16:27:45Z")

</div>

Hello,

Distributions.jl does not have an implementation of the [Maxwell-Boltzmann distribution](https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution), but it does have the [Chi distribution](https://en.wikipedia.org/wiki/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.

```julia-auto
"""
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-auto
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](https://discourse.julialang.org/t/maxwell-fit-julia/28835) discussed the relationship between Maxwell-Boltzmann and Chi distributions, and the use of LocationScale.

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [July 30, 2025, 5:13pm UTC](https://discourse.julialang.org/t/maxwell-boltzmann-distribution-using-distributions-jl/131210/2 "2025-07-30T17:13:01Z")

</div>

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-auto
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-auto
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)
)

```
