Python从正态分布生成随机的Maxwell分布 [英] Python Generate a random Maxwell distribution from a normal distribution

查看:583
本文介绍了Python从正态分布生成随机的Maxwell分布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组遵循正态分布的数据,在其中我可以拟合直方图并获得均值和西格玛.

I have a set of data that follows a normal distribution in which I can fit the histogram and obtain the mean and sigma.

为了举例说明,我将通过生成如下所示的随机正态分布来对其进行近似:

For the sake of example, I will approximate it by generating a random normal distribution as follows:

from scipy.stats import maxwell
import math
import random
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
from scipy.optimize import curve_fit
from IPython import embed # put embed() where you want to stop
import matplotlib.ticker as ticker

    data = random.gauss(307, 16)
    N, bins, patches = plt.hist(data, bins=40, density=True, alpha=0.5, histtype='bar', ec='black')
    mu, std = norm.fit(data)
    xmin, xmax = plt.xlim()
    x = np.linspace(xmin, xmax, 100)
    p = norm.pdf(x, mu, std)
    plt.plot(x, p, 'k', linewidth=2, label= r'$\mu$ = '+'{:0.1f}'.format(mu)+r' $\pm$ '+'{:0.1f}'.format(std))

接下来我要做的是从这个正态"分布生成一个麦克斯韦分布并能够拟合

What I would like to do next is to generate a Maxwell distribution from this "normal" distribution and be able to fit

我已阅读 scipy. stats.maxwell 网页和其他一些相关问题,但无法根据高斯分布"生成这样的分布并进行拟合.任何帮助将不胜感激.

I have read scipy.stats.maxwell webpage and several other related questions but was not able to generate such a distribution from "a gauss distribution" and fit it. Any help would much appreciate it.

推荐答案

好吧,知道每个Maxwell都是分子速度的绝对值的分布,其中每个分量都呈正态分布,因此可以像下面的代码那样进行采样

Well, knowing that each Maxwell is distribution of the absolute value of the molecule velocity, where each component is normally distributed, you could make sampling like code below

import numpy as np
import matplotlib.pyplot as plt

from scipy.stats import maxwell

def maxw(size = None):
    """Generates size samples of maxwell"""
    vx = np.random.normal(size=size)
    vy = np.random.normal(size=size)
    vz = np.random.normal(size=size)
    return np.sqrt(vx*vx + vy*vy + vz*vz)

mdata = maxw(100000)
h, bins = np.histogram(mdata, bins = 101, range=(0.0, 10.0))

x = np.linspace(0.0, 10.0, 100)
rv = maxwell()

fig, ax = plt.subplots(1, 1)

ax.hist(mdata, bins = bins, density=True)
ax.plot(x, rv.pdf(x), 'k-', lw=2, label='Maxwell pdf')
plt.title("Maxwell")
plt.show()

这是采样与Maxwell PDF重叠的图片

And here is the picture with sampling and Maxwell PDF overlapped

这篇关于Python从正态分布生成随机的Maxwell分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆