Skip to content
Advertisement

Generating 1000 iterations

I am trying to generate 1000 sets of data points using fake_exp_y_values generator. All I am trying to do is to calculate the chi2 values for all 1000 iterations with different fitting models. But the way I code it right now only gives me back one chi2 value. I don’t understand what is wrong.

Here is my code:

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)

Yet the returned chi2 list is:

result

Advertisement

Answer

This code 1000 times creates an array with 1000 zeros and inserts one value in each of them, but only keeps the last one:

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

You only want to create one array and use it in every one of the 1000 iterations:

chi2 = np.zeros(1000)
for i in range(1000):
    # [...]
    chi2[i] = np.sum(pulls**2)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement