Python从正态分布生成随机的Maxwell分布

编程入门 行业动态 更新时间:2024-10-28 02:34:57
本文介绍了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分布

本文发布于:2023-11-30 06:27:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649018.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正态分布   Python   Maxwell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!