Python:两个正态分布

Python:两个正态分布,python,histogram,normal-distribution,Python,Histogram,Normal Distribution,我有两个数据集,其中测量了两个值。我对差值和差值的标准偏差之间的差值感兴趣。我做了一个直方图,我想拟合两个正态分布。计算最大值之间的差值。我还想评估一下,在数据集中,一个值上的数据要少得多。我已经看过这个链接,但它不是我真正需要的: 使用scipy可以轻松实现快速脏装: from scipy.optimize import curve_fit #non linear curve fitting tool from matplotlib import pyplot as plt def f

我有两个数据集,其中测量了两个值。我对差值和差值的标准偏差之间的差值感兴趣。我做了一个直方图,我想拟合两个正态分布。计算最大值之间的差值。我还想评估一下,在数据集中,一个值上的数据要少得多。我已经看过这个链接,但它不是我真正需要的:


使用scipy可以轻松实现快速脏装:

from scipy.optimize import curve_fit #non linear curve fitting tool
from matplotlib import pyplot as plt

def func2fit(x1,x2,m_1,m_2,std_1,std_2,height1, height2): #define a simple gauss curve
    return height1*exp(-(x1-m_1)**2/2/std_1**2)+height2*exp(-(x2-m_2)**2/2/std_2**2)

init_guess=(-.3,.3,.5,.5,3000,3000) 
#contains the initial guesses for the parameters (m_1, m_2, std_1, std_2, height1, height2) using your first figure

#do the fitting
fit_pars, pcov =curve_fit(func2fit,xdata,ydata,init_guess) 
#fit_pars contains the mean, the heights and the SD values, pcov contains the estimated covariance of these parameters 

plt.plot(xdata,func2fit(xdata,*fit_pars),label='fit') #plot the fit
有关更多参考,请参阅scipy手册页面:

假设两个样本是独立的,则无需使用曲线拟合来处理此问题。这是基本的统计数据。下面是一些执行所需计算的代码,在注释中对源代码进行了属性化

## adapted from http://onlinestatbook.com/2/estimation/difference_means.html

from random import gauss
from numpy import sqrt

sample_1 = [ gauss(0,1) for _ in range(10) ]
sample_2 = [ gauss(1,.5) for _ in range(20) ]

n_1 = len(sample_1)
n_2 = len(sample_2)

mean_1 = sum(sample_1)/n_1
mean_2 = sum(sample_2)/n_2

SSE = sum([(_-mean_1)**2 for _ in sample_1]) + sum([(_-mean_2)**2 for _ in sample_2])
df = (n_1-1) + (n_2-1)
MSE = SSE/df

n_h = 2 / ( 1/n_1 + 1/n_2 )
s_mean_diff = sqrt( 2* MSE / n_h )

print ( 'difference between means', abs(n_1-n_2))
print ( 'std dev of this difference', s_mean_diff )

什么是
plt
?这样的信息显然与您的问题相关,并且您的问题中缺少这些信息。这些信息来自
matplotlib
。如果您想使用高斯混合模型,这可能会变得很棘手。仔细看看。这两个样本在统计上是独立的吗?看起来不错。而且它正在处理我的大部分数据。但除了在你的例子中,两个峰值出现在一个数据文件中。所以我没有样本1和样本2。在很好的情况下,峰值之间的距离足够远,因此我可以轻松地分割数据并告诉我们您的方法。但是,在我发布的图中,峰值彼此如此接近的情况下,我们能做些什么呢?这是对单变量高斯混合模型进行估计的工作,我一点也不像专家。除此之外,它还意味着编程。我建议您先访问,获取最新的建议,甚至建议您使用什么软件。祝你好运!
## adapted from http://onlinestatbook.com/2/estimation/difference_means.html

from random import gauss
from numpy import sqrt

sample_1 = [ gauss(0,1) for _ in range(10) ]
sample_2 = [ gauss(1,.5) for _ in range(20) ]

n_1 = len(sample_1)
n_2 = len(sample_2)

mean_1 = sum(sample_1)/n_1
mean_2 = sum(sample_2)/n_2

SSE = sum([(_-mean_1)**2 for _ in sample_1]) + sum([(_-mean_2)**2 for _ in sample_2])
df = (n_1-1) + (n_2-1)
MSE = SSE/df

n_h = 2 / ( 1/n_1 + 1/n_2 )
s_mean_diff = sqrt( 2* MSE / n_h )

print ( 'difference between means', abs(n_1-n_2))
print ( 'std dev of this difference', s_mean_diff )