生成 1000 次迭代

时间:2021-02-06 19:56:39

标签: python

我正在尝试使用 fake_exp_y_values 生成器生成 1000 组数据点。我想要做的就是使用不同的拟合模型计算所有 1000 次迭代的 chi2 值。但是我现在的编码方式只给了我一个 chi2 值。我不明白出了什么问题。

这是我的代码:

true_mean = 5 #Assume that we know the perfect fitting model is gaussian 
#with mean value 5. And use try_mean with different mean values to see how 
#does the chi2 behave
true_amp = 100
true_wid = 2
true_offset =10
x_values =  np.array([i for i in np.arange(0,10,0.4)])
exact_y_values = np.array([true_offset + true_amp*
                               np.exp(-((i-true_mean)/true_wid)**2)
                               for i in x_values])
    
    
def func (x_values,offset,amp,mean,wid):
    return (offset + amp*np.exp(-((i-mean)/wid)**2))

try_mean=np.linspace(2,8,25)
y_values=func(x_values,true_offset,true_amp,try_mean,true_wid)

for i in range (1000):
    chi2=np.zeros(1000)
    fake_exp_y_values = np.array([np.random.poisson(y)
                                      for y in exact_y_values])
    residuals=fake_exp_y_values-y_values
    y_err=np.clip(np.sqrt(fake_exp_y_values),1,9999)
    pulls=residuals/y_err
    chi2[i]=np.sum(pulls**2)

然而返回的 chi2 列表是:

result

1 个答案:

答案 0 :(得分:1)

这段代码 1000 次创建了一个包含 1000 个零的数组,并在每个零中插入一个值,但只保留最后一个:

<块引用>
for i in range (1000):
    chi2=np.zeros(1000)
    # [...]
    chi2[i]=np.sum(pulls**2)

您只想创建一个数组并在 1000 次迭代中的每一次迭代中使用它:

chi2 = np.zeros(1000)
for i in range(1000):
    # [...]
    chi2[i] = np.sum(pulls**2)
相关问题