给定一个参数绘制指数函数

问题描述

我对 python 还很陌生,所以对我一无所知。我使用一些生成的数据绘制了一个直方图。这个数据有很多很多点。我已经用变量 vals 定义了它。然后我用这些值绘制了一个直方图,尽管我已经限制了它,以便只考虑 104 和 155 之间的值。这已完成如下:

bin_heights,bin_edges = np.histogram(vals,range=[104,155],bins=30)
bin_centres = (bin_edges[:-1] + bin_edges[1:])/2.
plt.errorbar(bin_centres,bin_heights,np.sqrt(bin_heights),fmt=',',capsize=2)

plt.xlabel("$m_{\gamma\gamma} (GeV)$")
plt.ylabel("Number of entries")
plt.show()

给出上面的情节:

enter image description here

我的下一步是考虑 vals 中小于 120 的值。我已按如下方式执行此操作:

background_data=[j for j in vals if j <= 120] #to avoid taking the signal bump,upper limit of 120 MeV set

我需要在与直方图相同的图上绘制一条曲线,其遵循形式 B(x) = Ae^(-x/λ) 然后我使用最大似然估计公式估计 λ 的值:

background_data=[j for j in vals if j <= 120] #to avoid taking the signal bump,upper limit of 120 MeV set
#print(background_data)
N_background=len(background_data)
print(N_background)
sigma_background_data=sum(background_data)
print(sigma_background_data)
lamb = (sigma_background_data)/(N_background) #maximum likelihood estimator for lambda
print('lambda estimate is',lamb)

其中羔羊 = λ。我得到的值大约为羊肉 = 27.75,我知道这是正确的。我现在需要得到 A 的估计值。

有人建议我这样做:

Given a value of λ,find A by scaling the PDF to the data such that the area beneath
the scaled PDF has equal area to the data

我不太确定这意味着什么,或者我将如何尝试做到这一点。 PDF 表示概率密度函数。我认为必须进行集成,因此为了获取数据 (vals) 下的区域,我已完成此操作:

data_area= integrate.cumtrapz(background_data,x=None,dx=1.0)
print(data_area)
plt.plot(background_data,data_area)

然而,这给了我一个错误

ValueError: x and y must have same first dimension,but have shapes (981555,) and (981554,)

我不知道如何解决它。最终结果应该是这样的:

enter image description here

解决方法

cumtrapz docs

返回:...如果初始值为无,则形状使得积分轴的值比 y 小一个。如果给定初始值,则形状等于 y 的形状。

所以你要么传递一个初始值

data_area = integrate.cumtrapz(background_data,x=None,dx=1.0,initial = 0.0)

或丢弃 background_data 的第一个值:

plt.plot(background_data[1:],data_area)