简体   繁体   English

Python:插入似然函数的值范围始终返回相同的值

[英]Python: Plugging in range of values for likelihood function always returns same value

I am writing a program to calculate a likelihood function.我正在编写一个程序来计算似然函数。 The parameter I am trying to fit is I. This is my function:我试图拟合的参数是 I。这是我的函数:

#loglikelihood
def like_xe(I):
    model=(0.1535*(Z/A)*((charge*c/v_array)**2))*(np.log((2*m_e*(gamma_array**2)*(v_array**2)*(w_array)/((I*1.6E-6)**2)))-(2*(v_array/c_speed)**2))
return np.sum(((-0.5*np.log(2*np.pi*xe_un))-((xe_sp-(model))/(2*xe_un**2))))

The "arrays" are all data arrays, all of length 50. Then, I want to calculate the likelihood function for 1000 values of I between 0.0001 and 0.001, and from this data extract the maximum value of the likelihood function. “数组”都是数据数组,长度均为 50。然后,我想计算 0.0001 和 0.001 之间的 1000 个 I 值的似然函数,并从该数据中提取似然函数的最大值。

I_list=np.arange(1E-4,0.001,1000)
like_val=[0 for like in range(len(I_list))]
for like in range(len(I_list)):
    like_val[like]=like_xe(I_list[like])

This only returns [-169.58003268336941].这只会返回 [-169.58003268336941]。 Obviously, this is incorrect.显然,这是不正确的。 What is the matter with how I am approaching this problem?我如何处理这个问题有什么问题? Is it possibly because it only reads the return statement once?可能是因为它只读取 return 语句一次?

EDIT: Changed the np.arange to np.linspace.编辑:将 np.arange 更改为 np.linspace。 This creates an array of the correct size, however, it is entirely full of the value -169.58003268336941.这将创建一个正确大小的数组,但是,它完全充满了值 -169.58003268336941。

np.arange takes start, stop and step. np.arange 需要启动、停止和步进。 A step of 1000 between 1e-4 and 0.0001 means 1 value. 1e-4 和 0.0001 之间的 1000 步长表示 1 个值。

>> np.arange(1E-4,0.001,1000)
array([ 0.0001])

replace this with np.linspace and you should get a better iteration:用 np.linspace 替换它,你应该得到更好的迭代:

>> np.linspace(1E-4,0.001,10)
array([ 0.0001,  0.0002,  0.0003,  0.0004,  0.0005,  0.0006,  0.0007,
    0.0008,  0.0009,  0.001 ])

(set to 10 rather than 1000 for brevity) (为简洁起见,设置为 10 而不是 1000)

https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html https://docs.scipy.org/doc/numpy/reference/generated/numpy.linspace.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM