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

Plotting an exponential function given one parameter

我对 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()

给出上面的情节:

我的下一步是考虑 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)

其中羊肉 = λ。我得到的值大约为 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,)

我不确定如何修复它。最终结果应该是这样的:

参见 cumtrapz docs:

Returns: ... If initial is None, the shape is such that the axis of integration has one less value than y. If initial is given, the shape is equal to that of y.

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

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

或丢弃 background_data 的第一个值:

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