简体   繁体   English

Python Scipy 中的麦克斯韦分布

[英]Maxwellian Distribution in Python Scipy

In the article that I am interested in, it states that the data is well represented with a Maxwellian distribution and it also provides a Mean speed (307 km/s) and 1 sigma uncertainty (47 km/s) for the distribution.在我感兴趣的文章中,它指出数据很好地表示为麦克斯韦分布,它还提供了平均速度 (307 km/s) 和 1 sigma 不确定性 (47 km/s) 的分布。

Using the provided values, I have attempted to re-generate the data and then fit it with the Maxwellian distribution using the python scipy.stats.使用提供的值,我尝试重新生成数据,然后使用 python scipy.stats 将其与麦克斯韦分布拟合。

As it described in here , maxwell function in scipy takes two input, 1) "loc" which shifts the x variable and 2) "a" parameter which corresponds to the parameter "a" in the maxwell-Boltzmann equation.如此处所述, scipy中的 maxwell function 采用两个输入,1)“loc”移动 x 变量和 2)“a”参数,其对应于 maxwell-Boltzmann 方程中的参数“a”。

In my case, I have neither of these parameters, so using the Mean and variance (sigma^2) description in wiki page , I have attempted to calculate the "a" and "loc" parameter.就我而言,我没有这两个参数,因此使用wiki 页面中的均值和方差 (sigma^2) 描述,我尝试计算“a”和“loc”参数。 Both mean and sigma parameters are only dependent on "a" parameter. mean 和 sigma 参数都只依赖于“a”参数。

The first problem I have encountered was the "a" parameter that I get from Mean (a = 192.4) and sigma (a = 69.8) are different from each other.我遇到的第一个问题是我从 Mean (a = 192.4) 和 sigma (a = 69.8) 得到的“a”参数彼此不同。 The second problem is that I don't know how can I obtain the exact loc (shift) value from Mean and sigma.第二个问题是我不知道如何从 Mean 和 sigma 中获得准确的 loc (shift) 值。

Based on the shape of the distribution (where mean speed values fall in the graph, check figure 2), I tried to guess the "loc" value and together with the "a" value obtained from sigma (a = 69.8), I have generated and fitted the data.根据分布的形状(图中平均速度值落在图中,检查图 2),我试图猜测“loc”值以及从 sigma 获得的“a”值(a = 69.8),我有生成并拟合数据。 Approximately it seems correct, but I don't know the answer to the questions I mentioned above and I need some expert's guidance on this.大约看起来是正确的,但我不知道我上面提到的问题的答案,我需要一些专家的指导。 I appreciate any help.我很感激任何帮助。

import matplotlib.pyplot as plt
import math
from scipy.stats import norm
import random
import numpy as np
import scipy.optimize
from scipy.stats import maxwell

samplesize = 100000

mean = 307
sigma = 47
loc = 175 #my guess
a_value = np.sqrt((sigma**2 * math.pi)/(3*math.pi - 8)) #calculated based on wiki description

fig, axs = plt.subplots(1)
v_2d = maxwell.rvs(loc, a_value, size=samplesize) #array corresponding to 2D proper motion obtained from Hubbs
mean, var, skew, kurt = maxwell.stats(moments='mvsk')

N, bins, patches = plt.hist(v_2d, bins=100, density=True, alpha=0.5, histtype='bar', ec='black')
maxx = np.linspace(min(v_2d), max(v_2d), samplesize)

axs.plot(maxx, maxwell.pdf(maxx, loc, a_value), color=colorset[6], lw=2, label= r'$\mathdefault{\mu}$ = '+'{:0.1f}'.format(mean)+r' , '+r'$\mathdefault{\sigma}$ = '+'{:0.1f}'.format(sigma))

axs.set(xlabel=r'2-D Maxwellian speed (km s$^{-1}$)')
axs.set(ylabel='Frequency')
plt.legend(loc='upper right')

分布基于我的猜测

麦克斯韦分布中的平均速度和中值速度

Well, mean value is affected by location, and sigma won't.好吧,平均值受位置影响,而 sigma 不会。 So compute a from sigma, compute mean as if loc=0, find the difference and assign it to location, sample 100K RVs to check if sampled mean/stddev are close enough.因此,从 sigma 计算a ,计算均值,就好像 loc=0,找到差异并将其分配给位置,对 100K RV 进行采样以检查采样均值/标准差是否足够接近。

Code, Python 3.8, Windows 10 x64代码,Python 3.8,Windows 10 x64

import numpy as np

from scipy.stats import maxwell

σ = 47
μ = 307

a = σ * np.sqrt(np.pi/(3.0*np.pi - 8.0))
print(a)

m = 2.0*a*np.sqrt(2.0/np.pi)
print(m) # as if loc=0

loc = μ - m
print(loc)

print("----------Now test--------------------")

# sampling
q = maxwell.rvs(loc=loc, scale=a, size=100000)

print(np.mean(q))
print(np.std(q))

as output I've got作为 output 我有

306.9022249667151
47.05319429681308

Good enough?够好了?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM