【问题标题】:Plotting an exponential function given one parameter在给定一个参数的情况下绘制指数函数
【发布时间】:2021-08-23 22:08:02
【问题描述】:

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

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

【问题讨论】:

    标签: numpy matplotlib scipy estimation probability-distribution


    【解决方案1】:

    cumtrapz docs

    返回: ... 如果 initial 为 None,则形状使得积分轴的值比 y 少一个。如果给定initial,则形状等于y的形状。

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

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

    或丢弃 background_data 的第一个值:

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

    【讨论】:

      猜你喜欢
      • 2021-12-08
      • 2016-02-16
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-21
      • 2014-12-08
      • 2012-03-17
      相关资源
      最近更新 更多